api_orders.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  20. /**
  21. * API class for orders
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Orders extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. static $FIELDS = array(
  32. 'socid',
  33. 'date'
  34. );
  35. /**
  36. * @var Commande $commande {@type Commande}
  37. */
  38. public $commande;
  39. /**
  40. * Constructor
  41. */
  42. public function __construct()
  43. {
  44. global $db, $conf;
  45. $this->db = $db;
  46. $this->commande = new Commande($this->db);
  47. }
  48. /**
  49. * Get properties of an order object by id
  50. *
  51. * Return an array with order informations
  52. *
  53. * @param int $id ID of order
  54. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  55. * @return array|mixed data without useless information
  56. *
  57. * @throws RestException
  58. */
  59. public function get($id, $contact_list = 1)
  60. {
  61. return $this->_fetch($id, '', '', $contact_list);
  62. }
  63. /**
  64. * Get properties of an order object by ref
  65. *
  66. * Return an array with order informations
  67. *
  68. * @param string $ref Ref of object
  69. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  70. * @return array|mixed data without useless information
  71. *
  72. * @url GET ref/{ref}
  73. *
  74. * @throws RestException
  75. */
  76. public function getByRef($ref, $contact_list = 1)
  77. {
  78. return $this->_fetch('', $ref, '', $contact_list);
  79. }
  80. /**
  81. * Get properties of an order object by ref_ext
  82. *
  83. * Return an array with order informations
  84. *
  85. * @param string $ref_ext External reference of object
  86. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  87. * @return array|mixed data without useless information
  88. *
  89. * @url GET ref_ext/{ref_ext}
  90. *
  91. * @throws RestException
  92. */
  93. public function getByRefExt($ref_ext, $contact_list = 1)
  94. {
  95. return $this->_fetch('', '', $ref_ext, $contact_list);
  96. }
  97. /**
  98. * Get properties of an order object
  99. *
  100. * Return an array with order informations
  101. *
  102. * @param int $id ID of order
  103. * @param string $ref Ref of object
  104. * @param string $ref_ext External reference of object
  105. * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id
  106. * @return array|mixed data without useless information
  107. *
  108. * @throws RestException
  109. */
  110. private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
  111. {
  112. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  113. throw new RestException(401);
  114. }
  115. $result = $this->commande->fetch($id, $ref, $ref_ext);
  116. if (!$result) {
  117. throw new RestException(404, 'Order not found');
  118. }
  119. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  120. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  121. }
  122. // Add external contacts ids
  123. $tmparray = $this->commande->liste_contact(-1, 'external', $contact_list);
  124. if (is_array($tmparray)) {
  125. $this->commande->contacts_ids = $tmparray;
  126. }
  127. $this->commande->fetchObjectLinked();
  128. // Add online_payment_url, cf #20477
  129. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  130. $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
  131. return $this->_cleanObjectDatas($this->commande);
  132. }
  133. /**
  134. * List orders
  135. *
  136. * Get a list of orders
  137. *
  138. * @param string $sortfield Sort field
  139. * @param string $sortorder Sort order
  140. * @param int $limit Limit for list
  141. * @param int $page Page number
  142. * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  143. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  144. * @return array Array of order objects
  145. *
  146. * @throws RestException 404 Not found
  147. * @throws RestException 503 Error
  148. */
  149. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  150. {
  151. global $db, $conf;
  152. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  153. throw new RestException(401);
  154. }
  155. $obj_ret = array();
  156. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  157. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  158. // If the internal user must only see his customers, force searching by him
  159. $search_sale = 0;
  160. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  161. $search_sale = DolibarrApiAccess::$user->id;
  162. }
  163. $sql = "SELECT t.rowid";
  164. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  165. $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  166. }
  167. $sql .= " FROM ".MAIN_DB_PREFIX."commande as t";
  168. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  169. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  170. }
  171. $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
  172. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  173. $sql .= " AND t.fk_soc = sc.fk_soc";
  174. }
  175. if ($socids) {
  176. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  177. }
  178. if ($search_sale > 0) {
  179. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  180. }
  181. // Insert sale filter
  182. if ($search_sale > 0) {
  183. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  184. }
  185. // Add sql filters
  186. if ($sqlfilters) {
  187. $errormessage = '';
  188. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  189. if ($errormessage) {
  190. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  191. }
  192. }
  193. $sql .= $this->db->order($sortfield, $sortorder);
  194. if ($limit) {
  195. if ($page < 0) {
  196. $page = 0;
  197. }
  198. $offset = $limit * $page;
  199. $sql .= $this->db->plimit($limit + 1, $offset);
  200. }
  201. dol_syslog("API Rest request");
  202. $result = $this->db->query($sql);
  203. if ($result) {
  204. $num = $this->db->num_rows($result);
  205. $min = min($num, ($limit <= 0 ? $num : $limit));
  206. $i = 0;
  207. while ($i < $min) {
  208. $obj = $this->db->fetch_object($result);
  209. $commande_static = new Commande($this->db);
  210. if ($commande_static->fetch($obj->rowid)) {
  211. // Add external contacts ids
  212. $tmparray = $commande_static->liste_contact(-1, 'external', 1);
  213. if (is_array($tmparray)) {
  214. $commande_static->contacts_ids = $tmparray;
  215. }
  216. // Add online_payment_url, cf #20477
  217. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  218. $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
  219. $obj_ret[] = $this->_cleanObjectDatas($commande_static);
  220. }
  221. $i++;
  222. }
  223. } else {
  224. throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
  225. }
  226. if (!count($obj_ret)) {
  227. throw new RestException(404, 'No order found');
  228. }
  229. return $obj_ret;
  230. }
  231. /**
  232. * Create a sale order
  233. *
  234. * Exemple: { "socid": 2, "date": 1595196000, "type": 0, "lines": [{ "fk_product": 2, "qty": 1 }] }
  235. *
  236. * @param array $request_data Request data
  237. * @return int ID of order
  238. */
  239. public function post($request_data = null)
  240. {
  241. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  242. throw new RestException(401, "Insuffisant rights");
  243. }
  244. // Check mandatory fields
  245. $result = $this->_validate($request_data);
  246. foreach ($request_data as $field => $value) {
  247. $this->commande->$field = $value;
  248. }
  249. /*if (isset($request_data["lines"])) {
  250. $lines = array();
  251. foreach ($request_data["lines"] as $line) {
  252. array_push($lines, (object) $line);
  253. }
  254. $this->commande->lines = $lines;
  255. }*/
  256. if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
  257. throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
  258. }
  259. return $this->commande->id;
  260. }
  261. /**
  262. * Get lines of an order
  263. *
  264. * @param int $id Id of order
  265. *
  266. * @url GET {id}/lines
  267. *
  268. * @return int
  269. */
  270. public function getLines($id)
  271. {
  272. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  273. throw new RestException(401);
  274. }
  275. $result = $this->commande->fetch($id);
  276. if (!$result) {
  277. throw new RestException(404, 'Order not found');
  278. }
  279. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  280. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  281. }
  282. $this->commande->getLinesArray();
  283. $result = array();
  284. foreach ($this->commande->lines as $line) {
  285. array_push($result, $this->_cleanObjectDatas($line));
  286. }
  287. return $result;
  288. }
  289. /**
  290. * Add a line to given order
  291. *
  292. * @param int $id Id of order to update
  293. * @param array $request_data OrderLine data
  294. *
  295. * @url POST {id}/lines
  296. *
  297. * @return int
  298. */
  299. public function postLine($id, $request_data = null)
  300. {
  301. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  302. throw new RestException(401);
  303. }
  304. $result = $this->commande->fetch($id);
  305. if (!$result) {
  306. throw new RestException(404, 'Order not found');
  307. }
  308. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  309. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  310. }
  311. $request_data = (object) $request_data;
  312. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  313. $request_data->label = sanitizeVal($request_data->label);
  314. $updateRes = $this->commande->addline(
  315. $request_data->desc,
  316. $request_data->subprice,
  317. $request_data->qty,
  318. $request_data->tva_tx,
  319. $request_data->localtax1_tx,
  320. $request_data->localtax2_tx,
  321. $request_data->fk_product,
  322. $request_data->remise_percent,
  323. $request_data->info_bits,
  324. $request_data->fk_remise_except,
  325. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  326. $request_data->subprice,
  327. $request_data->date_start,
  328. $request_data->date_end,
  329. $request_data->product_type,
  330. $request_data->rang,
  331. $request_data->special_code,
  332. $request_data->fk_parent_line,
  333. $request_data->fk_fournprice,
  334. $request_data->pa_ht,
  335. $request_data->label,
  336. $request_data->array_options,
  337. $request_data->fk_unit,
  338. $request_data->origin,
  339. $request_data->origin_id,
  340. $request_data->multicurrency_subprice,
  341. $request_data->ref_ext
  342. );
  343. if ($updateRes > 0) {
  344. return $updateRes;
  345. } else {
  346. throw new RestException(400, $this->commande->error);
  347. }
  348. }
  349. /**
  350. * Update a line to given order
  351. *
  352. * @param int $id Id of order to update
  353. * @param int $lineid Id of line to update
  354. * @param array $request_data OrderLine data
  355. *
  356. * @url PUT {id}/lines/{lineid}
  357. *
  358. * @return array|bool
  359. */
  360. public function putLine($id, $lineid, $request_data = null)
  361. {
  362. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  363. throw new RestException(401);
  364. }
  365. $result = $this->commande->fetch($id);
  366. if (!$result) {
  367. throw new RestException(404, 'Order not found');
  368. }
  369. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  370. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  371. }
  372. $request_data = (object) $request_data;
  373. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  374. $request_data->label = sanitizeVal($request_data->label);
  375. $updateRes = $this->commande->updateline(
  376. $lineid,
  377. $request_data->desc,
  378. $request_data->subprice,
  379. $request_data->qty,
  380. $request_data->remise_percent,
  381. $request_data->tva_tx,
  382. $request_data->localtax1_tx,
  383. $request_data->localtax2_tx,
  384. $request_data->price_base_type ? $request_data->price_base_type : 'HT',
  385. $request_data->info_bits,
  386. $request_data->date_start,
  387. $request_data->date_end,
  388. $request_data->product_type,
  389. $request_data->fk_parent_line,
  390. 0,
  391. $request_data->fk_fournprice,
  392. $request_data->pa_ht,
  393. $request_data->label,
  394. $request_data->special_code,
  395. $request_data->array_options,
  396. $request_data->fk_unit,
  397. $request_data->multicurrency_subprice,
  398. 0,
  399. $request_data->ref_ext,
  400. $request_data->rang
  401. );
  402. if ($updateRes > 0) {
  403. $result = $this->get($id);
  404. unset($result->line);
  405. return $this->_cleanObjectDatas($result);
  406. }
  407. return false;
  408. }
  409. /**
  410. * Delete a line to given order
  411. *
  412. *
  413. * @param int $id Id of order to update
  414. * @param int $lineid Id of line to delete
  415. *
  416. * @url DELETE {id}/lines/{lineid}
  417. *
  418. * @return int
  419. *
  420. * @throws RestException 401
  421. * @throws RestException 404
  422. */
  423. public function deleteLine($id, $lineid)
  424. {
  425. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  426. throw new RestException(401);
  427. }
  428. $result = $this->commande->fetch($id);
  429. if (!$result) {
  430. throw new RestException(404, 'Order not found');
  431. }
  432. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  433. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  434. }
  435. // TODO Check the lineid $lineid is a line of ojbect
  436. $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
  437. if ($updateRes > 0) {
  438. return $this->get($id);
  439. } else {
  440. throw new RestException(405, $this->commande->error);
  441. }
  442. }
  443. /**
  444. * Get contacts of given order
  445. *
  446. * Return an array with contact informations
  447. *
  448. * @param int $id ID of order
  449. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
  450. *
  451. * @url GET {id}/contacts
  452. *
  453. * @return array data without useless information
  454. *
  455. * @throws RestException
  456. */
  457. public function getContacts($id, $type = '')
  458. {
  459. if (!DolibarrApiAccess::$user->rights->commande->lire) {
  460. throw new RestException(401);
  461. }
  462. $result = $this->commande->fetch($id);
  463. if (!$result) {
  464. throw new RestException(404, 'Order not found');
  465. }
  466. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  467. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  468. }
  469. $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
  470. return $this->_cleanObjectDatas($contacts);
  471. }
  472. /**
  473. * Add a contact type of given order
  474. *
  475. * @param int $id Id of order to update
  476. * @param int $contactid Id of contact to add
  477. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER)
  478. *
  479. * @url POST {id}/contact/{contactid}/{type}
  480. *
  481. * @return int
  482. *
  483. * @throws RestException 401
  484. * @throws RestException 404
  485. */
  486. public function postContact($id, $contactid, $type)
  487. {
  488. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  489. throw new RestException(401);
  490. }
  491. $result = $this->commande->fetch($id);
  492. if (!$result) {
  493. throw new RestException(404, 'Order not found');
  494. }
  495. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  496. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  497. }
  498. $result = $this->commande->add_contact($contactid, $type, 'external');
  499. if ($result < 0) {
  500. throw new RestException(500, 'Error when added the contact');
  501. }
  502. if ($result == 0) {
  503. throw new RestException(304, 'contact already added');
  504. }
  505. return array(
  506. 'success' => array(
  507. 'code' => 200,
  508. 'message' => 'Contact linked to the order'
  509. )
  510. );
  511. }
  512. /**
  513. * Unlink a contact type of given order
  514. *
  515. * @param int $id Id of order to update
  516. * @param int $contactid Id of contact
  517. * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER).
  518. *
  519. * @url DELETE {id}/contact/{contactid}/{type}
  520. *
  521. * @return int
  522. *
  523. * @throws RestException 401
  524. * @throws RestException 404
  525. * @throws RestException 500 System error
  526. */
  527. public function deleteContact($id, $contactid, $type)
  528. {
  529. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  530. throw new RestException(401);
  531. }
  532. $result = $this->commande->fetch($id);
  533. if (!$result) {
  534. throw new RestException(404, 'Order not found');
  535. }
  536. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  537. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  538. }
  539. $contacts = $this->commande->liste_contact();
  540. foreach ($contacts as $contact) {
  541. if ($contact['id'] == $contactid && $contact['code'] == $type) {
  542. $result = $this->commande->delete_contact($contact['rowid']);
  543. if (!$result) {
  544. throw new RestException(500, 'Error when deleted the contact');
  545. }
  546. }
  547. }
  548. return array(
  549. 'success' => array(
  550. 'code' => 200,
  551. 'message' => 'Contact unlinked from order'
  552. )
  553. );
  554. }
  555. /**
  556. * Update order general fields (won't touch lines of order)
  557. *
  558. * @param int $id Id of order to update
  559. * @param array $request_data Datas
  560. *
  561. * @return int
  562. */
  563. public function put($id, $request_data = null)
  564. {
  565. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  566. throw new RestException(401);
  567. }
  568. $result = $this->commande->fetch($id);
  569. if (!$result) {
  570. throw new RestException(404, 'Order not found');
  571. }
  572. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  573. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  574. }
  575. foreach ($request_data as $field => $value) {
  576. if ($field == 'id') {
  577. continue;
  578. }
  579. $this->commande->$field = $value;
  580. }
  581. // Update availability
  582. if (!empty($this->commande->availability_id)) {
  583. if ($this->commande->availability($this->commande->availability_id) < 0) {
  584. throw new RestException(400, 'Error while updating availability');
  585. }
  586. }
  587. if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
  588. return $this->get($id);
  589. } else {
  590. throw new RestException(500, $this->commande->error);
  591. }
  592. }
  593. /**
  594. * Delete order
  595. *
  596. * @param int $id Order ID
  597. * @return array
  598. */
  599. public function delete($id)
  600. {
  601. if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
  602. throw new RestException(401);
  603. }
  604. $result = $this->commande->fetch($id);
  605. if (!$result) {
  606. throw new RestException(404, 'Order not found');
  607. }
  608. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  609. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  610. }
  611. if (!$this->commande->delete(DolibarrApiAccess::$user)) {
  612. throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
  613. }
  614. return array(
  615. 'success' => array(
  616. 'code' => 200,
  617. 'message' => 'Order deleted'
  618. )
  619. );
  620. }
  621. /**
  622. * Validate an order
  623. *
  624. * If you get a bad value for param notrigger check, provide this in body
  625. * {
  626. * "idwarehouse": 0,
  627. * "notrigger": 0
  628. * }
  629. *
  630. * @param int $id Order ID
  631. * @param int $idwarehouse Warehouse ID
  632. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  633. *
  634. * @url POST {id}/validate
  635. *
  636. * @throws RestException 304
  637. * @throws RestException 401
  638. * @throws RestException 404
  639. * @throws RestException 500 System error
  640. *
  641. * @return array
  642. */
  643. public function validate($id, $idwarehouse = 0, $notrigger = 0)
  644. {
  645. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  646. throw new RestException(401);
  647. }
  648. $result = $this->commande->fetch($id);
  649. if (!$result) {
  650. throw new RestException(404, 'Order not found');
  651. }
  652. $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
  653. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  654. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  655. }
  656. $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
  657. if ($result == 0) {
  658. throw new RestException(304, 'Error nothing done. May be object is already validated');
  659. }
  660. if ($result < 0) {
  661. throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
  662. }
  663. $result = $this->commande->fetch($id);
  664. $this->commande->fetchObjectLinked();
  665. //fix #20477 : add online_payment_url
  666. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  667. $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
  668. return $this->_cleanObjectDatas($this->commande);
  669. }
  670. /**
  671. * Tag the order as validated (opened)
  672. *
  673. * Function used when order is reopend after being closed.
  674. *
  675. * @param int $id Id of the order
  676. *
  677. * @url POST {id}/reopen
  678. *
  679. * @return int
  680. *
  681. * @throws RestException 304
  682. * @throws RestException 400
  683. * @throws RestException 401
  684. * @throws RestException 404
  685. * @throws RestException 405
  686. */
  687. public function reopen($id)
  688. {
  689. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  690. throw new RestException(401);
  691. }
  692. if (empty($id)) {
  693. throw new RestException(400, 'Order ID is mandatory');
  694. }
  695. $result = $this->commande->fetch($id);
  696. if (!$result) {
  697. throw new RestException(404, 'Order not found');
  698. }
  699. $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
  700. if ($result < 0) {
  701. throw new RestException(405, $this->commande->error);
  702. } elseif ($result == 0) {
  703. throw new RestException(304);
  704. }
  705. return $result;
  706. }
  707. /**
  708. * Classify the order as invoiced. Could be also called setbilled
  709. *
  710. * @param int $id Id of the order
  711. *
  712. * @url POST {id}/setinvoiced
  713. *
  714. * @return int
  715. *
  716. * @throws RestException 400
  717. * @throws RestException 401
  718. * @throws RestException 404
  719. * @throws RestException 405
  720. */
  721. public function setinvoiced($id)
  722. {
  723. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  724. throw new RestException(401);
  725. }
  726. if (empty($id)) {
  727. throw new RestException(400, 'Order ID is mandatory');
  728. }
  729. $result = $this->commande->fetch($id);
  730. if (!$result) {
  731. throw new RestException(404, 'Order not found');
  732. }
  733. $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
  734. if ($result < 0) {
  735. throw new RestException(400, $this->commande->error);
  736. }
  737. $result = $this->commande->fetch($id);
  738. if (!$result) {
  739. throw new RestException(404, 'Order not found');
  740. }
  741. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  742. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  743. }
  744. $this->commande->fetchObjectLinked();
  745. return $this->_cleanObjectDatas($this->commande);
  746. }
  747. /**
  748. * Close an order (Classify it as "Delivered")
  749. *
  750. * @param int $id Order ID
  751. * @param int $notrigger Disabled triggers
  752. *
  753. * @url POST {id}/close
  754. *
  755. * @return int
  756. */
  757. public function close($id, $notrigger = 0)
  758. {
  759. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  760. throw new RestException(401);
  761. }
  762. $result = $this->commande->fetch($id);
  763. if (!$result) {
  764. throw new RestException(404, 'Order not found');
  765. }
  766. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  767. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  768. }
  769. $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
  770. if ($result == 0) {
  771. throw new RestException(304, 'Error nothing done. May be object is already closed');
  772. }
  773. if ($result < 0) {
  774. throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
  775. }
  776. $result = $this->commande->fetch($id);
  777. if (!$result) {
  778. throw new RestException(404, 'Order not found');
  779. }
  780. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  781. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  782. }
  783. $this->commande->fetchObjectLinked();
  784. return $this->_cleanObjectDatas($this->commande);
  785. }
  786. /**
  787. * Set an order to draft
  788. *
  789. * @param int $id Order ID
  790. * @param int $idwarehouse Warehouse ID to use for stock change (Used only if option STOCK_CALCULATE_ON_VALIDATE_ORDER is on)
  791. *
  792. * @url POST {id}/settodraft
  793. *
  794. * @return array
  795. */
  796. public function settodraft($id, $idwarehouse = -1)
  797. {
  798. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  799. throw new RestException(401);
  800. }
  801. $result = $this->commande->fetch($id);
  802. if (!$result) {
  803. throw new RestException(404, 'Order not found');
  804. }
  805. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  806. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  807. }
  808. $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
  809. if ($result == 0) {
  810. throw new RestException(304, 'Nothing done. May be object is already closed');
  811. }
  812. if ($result < 0) {
  813. throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
  814. }
  815. $result = $this->commande->fetch($id);
  816. if (!$result) {
  817. throw new RestException(404, 'Order not found');
  818. }
  819. if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
  820. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  821. }
  822. $this->commande->fetchObjectLinked();
  823. return $this->_cleanObjectDatas($this->commande);
  824. }
  825. /**
  826. * Create an order using an existing proposal.
  827. *
  828. *
  829. * @param int $proposalid Id of the proposal
  830. *
  831. * @url POST /createfromproposal/{proposalid}
  832. *
  833. * @return int
  834. * @throws RestException 400
  835. * @throws RestException 401
  836. * @throws RestException 404
  837. * @throws RestException 405
  838. */
  839. public function createOrderFromProposal($proposalid)
  840. {
  841. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  842. if (!DolibarrApiAccess::$user->rights->propal->lire) {
  843. throw new RestException(401);
  844. }
  845. if (!DolibarrApiAccess::$user->rights->commande->creer) {
  846. throw new RestException(401);
  847. }
  848. if (empty($proposalid)) {
  849. throw new RestException(400, 'Proposal ID is mandatory');
  850. }
  851. $propal = new Propal($this->db);
  852. $result = $propal->fetch($proposalid);
  853. if (!$result) {
  854. throw new RestException(404, 'Proposal not found');
  855. }
  856. $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
  857. if ($result < 0) {
  858. throw new RestException(405, $this->commande->error);
  859. }
  860. $this->commande->fetchObjectLinked();
  861. return $this->_cleanObjectDatas($this->commande);
  862. }
  863. /**
  864. * Get the shipments of an order
  865. *
  866. * @param int $id Id of the order
  867. *
  868. * @url GET {id}/shipment
  869. *
  870. * @return array
  871. *
  872. * @throws RestException 401
  873. * @throws RestException 404
  874. * @throws RestException 500 System error
  875. */
  876. public function getOrderShipments($id)
  877. {
  878. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  879. if (!DolibarrApiAccess::$user->rights->expedition->lire) {
  880. throw new RestException(401);
  881. }
  882. $obj_ret = array();
  883. $sql = "SELECT e.rowid";
  884. $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
  885. $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
  886. $sql .= " ON e.rowid = edet.fk_expedition";
  887. $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
  888. $sql .= " ON edet.fk_origin_line = cdet.rowid";
  889. $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
  890. $sql .= " ON cdet.fk_commande = c.rowid";
  891. $sql .= " WHERE c.rowid = ".((int) $id);
  892. $sql .= " GROUP BY e.rowid";
  893. $sql .= $this->db->order("e.rowid", "ASC");
  894. dol_syslog("API Rest request");
  895. $result = $this->db->query($sql);
  896. if ($result) {
  897. $num = $this->db->num_rows($result);
  898. if ($num <= 0) {
  899. throw new RestException(404, 'Shipments not found ');
  900. }
  901. $i = 0;
  902. while ($i < $num) {
  903. $obj = $this->db->fetch_object($result);
  904. $shipment_static = new Expedition($this->db);
  905. if ($shipment_static->fetch($obj->rowid)) {
  906. $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
  907. }
  908. $i++;
  909. }
  910. } else {
  911. throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
  912. }
  913. return $obj_ret;
  914. }
  915. /**
  916. * Create the shipment of an order
  917. *
  918. * @param int $id Id of the order
  919. * @param int $warehouse_id Id of a warehouse
  920. *
  921. * @url POST {id}/shipment/{warehouse_id}
  922. *
  923. * @return int
  924. *
  925. * @throws RestException 401
  926. * @throws RestException 404
  927. * @throws RestException 500 System error
  928. */
  929. public function createOrderShipment($id, $warehouse_id)
  930. {
  931. require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
  932. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  933. throw new RestException(401);
  934. }
  935. if ($warehouse_id <= 0) {
  936. throw new RestException(404, 'Warehouse not found');
  937. }
  938. $result = $this->commande->fetch($id);
  939. if (!$result) {
  940. throw new RestException(404, 'Order not found');
  941. }
  942. $shipment = new Expedition($this->db);
  943. $shipment->socid = $this->commande->socid;
  944. $result = $shipment->create(DolibarrApiAccess::$user);
  945. if ($result <= 0) {
  946. throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
  947. }
  948. foreach ($this->commande->lines as $line) {
  949. $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
  950. if ($result <= 0) {
  951. throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
  952. }
  953. }
  954. return $shipment->id;
  955. }
  956. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  957. /**
  958. * Clean sensible object datas
  959. *
  960. * @param Object $object Object to clean
  961. * @return Object Object with cleaned properties
  962. */
  963. protected function _cleanObjectDatas($object)
  964. {
  965. // phpcs:enable
  966. $object = parent::_cleanObjectDatas($object);
  967. unset($object->note);
  968. unset($object->address);
  969. unset($object->barcode_type);
  970. unset($object->barcode_type_code);
  971. unset($object->barcode_type_label);
  972. unset($object->barcode_type_coder);
  973. return $object;
  974. }
  975. /**
  976. * Validate fields before create or update object
  977. *
  978. * @param array $data Array with data to verify
  979. * @return array
  980. * @throws RestException
  981. */
  982. private function _validate($data)
  983. {
  984. $commande = array();
  985. foreach (Orders::$FIELDS as $field) {
  986. if (!isset($data[$field])) {
  987. throw new RestException(400, $field." field missing");
  988. }
  989. $commande[$field] = $data[$field];
  990. }
  991. return $commande;
  992. }
  993. }