api_shipments.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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.'/expedition/class/expedition.class.php';
  20. /**
  21. * API class for shipments
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Shipments extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'socid',
  33. 'origin_id',
  34. 'origin_type',
  35. );
  36. /**
  37. * @var Expedition $shipment {@type Expedition}
  38. */
  39. public $shipment;
  40. /**
  41. * Constructor
  42. */
  43. public function __construct()
  44. {
  45. global $db, $conf;
  46. $this->db = $db;
  47. $this->shipment = new Expedition($this->db);
  48. }
  49. /**
  50. * Get properties of a shipment object
  51. *
  52. * Return an array with shipment informations
  53. *
  54. * @param int $id ID of shipment
  55. * @return Object Object with cleaned properties
  56. *
  57. * @throws RestException
  58. */
  59. public function get($id)
  60. {
  61. if (!DolibarrApiAccess::$user->rights->expedition->lire) {
  62. throw new RestException(401);
  63. }
  64. $result = $this->shipment->fetch($id);
  65. if (!$result) {
  66. throw new RestException(404, 'Shipment not found');
  67. }
  68. if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
  69. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  70. }
  71. $this->shipment->fetchObjectLinked();
  72. return $this->_cleanObjectDatas($this->shipment);
  73. }
  74. /**
  75. * List shipments
  76. *
  77. * Get a list of shipments
  78. *
  79. * @param string $sortfield Sort field
  80. * @param string $sortorder Sort order
  81. * @param int $limit Limit for list
  82. * @param int $page Page number
  83. * @param string $thirdparty_ids Thirdparty ids to filter shipments of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i}
  84. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  85. * @return array Array of shipment objects
  86. *
  87. * @throws RestException
  88. */
  89. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
  90. {
  91. global $db, $conf;
  92. if (!DolibarrApiAccess::$user->rights->expedition->lire) {
  93. throw new RestException(401);
  94. }
  95. $obj_ret = array();
  96. // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
  97. $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
  98. // If the internal user must only see his customers, force searching by him
  99. $search_sale = 0;
  100. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
  101. $search_sale = DolibarrApiAccess::$user->id;
  102. }
  103. $sql = "SELECT t.rowid";
  104. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  105. $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)
  106. }
  107. $sql .= " FROM ".MAIN_DB_PREFIX."expedition AS t LEFT JOIN ".MAIN_DB_PREFIX."expedition_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  108. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  109. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  110. }
  111. $sql .= ' WHERE t.entity IN ('.getEntity('expedition').')';
  112. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
  113. $sql .= " AND t.fk_soc = sc.fk_soc";
  114. }
  115. if ($socids) {
  116. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
  117. }
  118. if ($search_sale > 0) {
  119. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  120. }
  121. // Insert sale filter
  122. if ($search_sale > 0) {
  123. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  124. }
  125. // Add sql filters
  126. if ($sqlfilters) {
  127. $errormessage = '';
  128. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  129. if ($errormessage) {
  130. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  131. }
  132. }
  133. $sql .= $this->db->order($sortfield, $sortorder);
  134. if ($limit) {
  135. if ($page < 0) {
  136. $page = 0;
  137. }
  138. $offset = $limit * $page;
  139. $sql .= $this->db->plimit($limit + 1, $offset);
  140. }
  141. dol_syslog("API Rest request");
  142. $result = $this->db->query($sql);
  143. if ($result) {
  144. $num = $this->db->num_rows($result);
  145. $min = min($num, ($limit <= 0 ? $num : $limit));
  146. $i = 0;
  147. while ($i < $min) {
  148. $obj = $this->db->fetch_object($result);
  149. $shipment_static = new Expedition($this->db);
  150. if ($shipment_static->fetch($obj->rowid)) {
  151. $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
  152. }
  153. $i++;
  154. }
  155. } else {
  156. throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
  157. }
  158. if (!count($obj_ret)) {
  159. throw new RestException(404, 'No shipment found');
  160. }
  161. return $obj_ret;
  162. }
  163. /**
  164. * Create shipment object
  165. *
  166. * @param array $request_data Request data
  167. * @return int ID of shipment
  168. */
  169. public function post($request_data = null)
  170. {
  171. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  172. throw new RestException(401, "Insuffisant rights");
  173. }
  174. // Check mandatory fields
  175. $result = $this->_validate($request_data);
  176. foreach ($request_data as $field => $value) {
  177. $this->shipment->$field = $value;
  178. }
  179. if (isset($request_data["lines"])) {
  180. $lines = array();
  181. foreach ($request_data["lines"] as $line) {
  182. array_push($lines, (object) $line);
  183. }
  184. $this->shipment->lines = $lines;
  185. }
  186. if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
  187. throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
  188. }
  189. return $this->shipment->id;
  190. }
  191. // /**
  192. // * Get lines of an shipment
  193. // *
  194. // * @param int $id Id of shipment
  195. // *
  196. // * @url GET {id}/lines
  197. // *
  198. // * @return int
  199. // */
  200. /*
  201. public function getLines($id)
  202. {
  203. if(! DolibarrApiAccess::$user->rights->expedition->lire) {
  204. throw new RestException(401);
  205. }
  206. $result = $this->shipment->fetch($id);
  207. if( ! $result ) {
  208. throw new RestException(404, 'Shipment not found');
  209. }
  210. if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
  211. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  212. }
  213. $this->shipment->getLinesArray();
  214. $result = array();
  215. foreach ($this->shipment->lines as $line) {
  216. array_push($result,$this->_cleanObjectDatas($line));
  217. }
  218. return $result;
  219. }
  220. */
  221. // /**
  222. // * Add a line to given shipment
  223. // *
  224. // * @param int $id Id of shipment to update
  225. // * @param array $request_data ShipmentLine data
  226. // *
  227. // * @url POST {id}/lines
  228. // *
  229. // * @return int
  230. // */
  231. /*
  232. public function postLine($id, $request_data = null)
  233. {
  234. if(! DolibarrApiAccess::$user->rights->expedition->creer) {
  235. throw new RestException(401);
  236. }
  237. $result = $this->shipment->fetch($id);
  238. if ( ! $result ) {
  239. throw new RestException(404, 'Shipment not found');
  240. }
  241. if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
  242. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  243. }
  244. $request_data = (object) $request_data;
  245. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  246. $request_data->label = sanitizeVal($request_data->label);
  247. $updateRes = $this->shipment->addline(
  248. $request_data->desc,
  249. $request_data->subprice,
  250. $request_data->qty,
  251. $request_data->tva_tx,
  252. $request_data->localtax1_tx,
  253. $request_data->localtax2_tx,
  254. $request_data->fk_product,
  255. $request_data->remise_percent,
  256. $request_data->info_bits,
  257. $request_data->fk_remise_except,
  258. 'HT',
  259. 0,
  260. $request_data->date_start,
  261. $request_data->date_end,
  262. $request_data->product_type,
  263. $request_data->rang,
  264. $request_data->special_code,
  265. $fk_parent_line,
  266. $request_data->fk_fournprice,
  267. $request_data->pa_ht,
  268. $request_data->label,
  269. $request_data->array_options,
  270. $request_data->fk_unit,
  271. $request_data->origin,
  272. $request_data->origin_id,
  273. $request_data->multicurrency_subprice
  274. );
  275. if ($updateRes > 0) {
  276. return $updateRes;
  277. }
  278. return false;
  279. }*/
  280. // /**
  281. // * Update a line to given shipment
  282. // *
  283. // * @param int $id Id of shipment to update
  284. // * @param int $lineid Id of line to update
  285. // * @param array $request_data ShipmentLine data
  286. // *
  287. // * @url PUT {id}/lines/{lineid}
  288. // *
  289. // * @return object
  290. // */
  291. /*
  292. public function putLine($id, $lineid, $request_data = null)
  293. {
  294. if (! DolibarrApiAccess::$user->rights->expedition->creer) {
  295. throw new RestException(401);
  296. }
  297. $result = $this->shipment->fetch($id);
  298. if ( ! $result ) {
  299. throw new RestException(404, 'Shipment not found');
  300. }
  301. if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
  302. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  303. }
  304. $request_data = (object) $request_data;
  305. $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
  306. $request_data->label = sanitizeVal($request_data->label);
  307. $updateRes = $this->shipment->updateline(
  308. $lineid,
  309. $request_data->desc,
  310. $request_data->subprice,
  311. $request_data->qty,
  312. $request_data->remise_percent,
  313. $request_data->tva_tx,
  314. $request_data->localtax1_tx,
  315. $request_data->localtax2_tx,
  316. 'HT',
  317. $request_data->info_bits,
  318. $request_data->date_start,
  319. $request_data->date_end,
  320. $request_data->product_type,
  321. $request_data->fk_parent_line,
  322. 0,
  323. $request_data->fk_fournprice,
  324. $request_data->pa_ht,
  325. $request_data->label,
  326. $request_data->special_code,
  327. $request_data->array_options,
  328. $request_data->fk_unit,
  329. $request_data->multicurrency_subprice
  330. );
  331. if ($updateRes > 0) {
  332. $result = $this->get($id);
  333. unset($result->line);
  334. return $this->_cleanObjectDatas($result);
  335. }
  336. return false;
  337. }*/
  338. /**
  339. * Delete a line to given shipment
  340. *
  341. *
  342. * @param int $id Id of shipment to update
  343. * @param int $lineid Id of line to delete
  344. *
  345. * @url DELETE {id}/lines/{lineid}
  346. *
  347. * @return int
  348. *
  349. * @throws RestException 401
  350. * @throws RestException 404
  351. */
  352. public function deleteLine($id, $lineid)
  353. {
  354. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  355. throw new RestException(401);
  356. }
  357. $result = $this->shipment->fetch($id);
  358. if (!$result) {
  359. throw new RestException(404, 'Shipment not found');
  360. }
  361. if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
  362. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  363. }
  364. // TODO Check the lineid $lineid is a line of ojbect
  365. $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
  366. if ($updateRes > 0) {
  367. return $this->get($id);
  368. } else {
  369. throw new RestException(405, $this->shipment->error);
  370. }
  371. }
  372. /**
  373. * Update shipment general fields (won't touch lines of shipment)
  374. *
  375. * @param int $id Id of shipment to update
  376. * @param array $request_data Datas
  377. *
  378. * @return int
  379. */
  380. public function put($id, $request_data = null)
  381. {
  382. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  383. throw new RestException(401);
  384. }
  385. $result = $this->shipment->fetch($id);
  386. if (!$result) {
  387. throw new RestException(404, 'Shipment not found');
  388. }
  389. if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
  390. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  391. }
  392. foreach ($request_data as $field => $value) {
  393. if ($field == 'id') {
  394. continue;
  395. }
  396. $this->shipment->$field = $value;
  397. }
  398. if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
  399. return $this->get($id);
  400. } else {
  401. throw new RestException(500, $this->shipment->error);
  402. }
  403. }
  404. /**
  405. * Delete shipment
  406. *
  407. * @param int $id Shipment ID
  408. *
  409. * @return array
  410. */
  411. public function delete($id)
  412. {
  413. if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
  414. throw new RestException(401);
  415. }
  416. $result = $this->shipment->fetch($id);
  417. if (!$result) {
  418. throw new RestException(404, 'Shipment not found');
  419. }
  420. if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
  421. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  422. }
  423. if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
  424. throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
  425. }
  426. return array(
  427. 'success' => array(
  428. 'code' => 200,
  429. 'message' => 'Shipment deleted'
  430. )
  431. );
  432. }
  433. /**
  434. * Validate a shipment
  435. *
  436. * This may record stock movements if module stock is enabled and option to
  437. * decrease stock on shipment is on.
  438. *
  439. * @param int $id Shipment ID
  440. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  441. *
  442. * @url POST {id}/validate
  443. *
  444. * @return object
  445. * \todo An error 403 is returned if the request has an empty body.
  446. * Error message: "Forbidden: Content type `text/plain` is not supported."
  447. * Workaround: send this in the body
  448. * {
  449. * "notrigger": 0
  450. * }
  451. */
  452. public function validate($id, $notrigger = 0)
  453. {
  454. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  455. throw new RestException(401);
  456. }
  457. $result = $this->shipment->fetch($id);
  458. if (!$result) {
  459. throw new RestException(404, 'Shipment not found');
  460. }
  461. if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
  462. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  463. }
  464. $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
  465. if ($result == 0) {
  466. throw new RestException(304, 'Error nothing done. May be object is already validated');
  467. }
  468. if ($result < 0) {
  469. throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
  470. }
  471. // Reload shipment
  472. $result = $this->shipment->fetch($id);
  473. $this->shipment->fetchObjectLinked();
  474. return $this->_cleanObjectDatas($this->shipment);
  475. }
  476. // /**
  477. // * Classify the shipment as invoiced
  478. // *
  479. // * @param int $id Id of the shipment
  480. // *
  481. // * @url POST {id}/setinvoiced
  482. // *
  483. // * @return int
  484. // *
  485. // * @throws RestException 400
  486. // * @throws RestException 401
  487. // * @throws RestException 404
  488. // * @throws RestException 405
  489. // */
  490. /*
  491. public function setinvoiced($id)
  492. {
  493. if(! DolibarrApiAccess::$user->rights->expedition->creer) {
  494. throw new RestException(401);
  495. }
  496. if(empty($id)) {
  497. throw new RestException(400, 'Shipment ID is mandatory');
  498. }
  499. $result = $this->shipment->fetch($id);
  500. if( ! $result ) {
  501. throw new RestException(404, 'Shipment not found');
  502. }
  503. $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
  504. if( $result < 0) {
  505. throw new RestException(400, $this->shipment->error);
  506. }
  507. return $result;
  508. }
  509. */
  510. // /**
  511. // * Create a shipment using an existing order.
  512. // *
  513. // * @param int $orderid Id of the order
  514. // *
  515. // * @url POST /createfromorder/{orderid}
  516. // *
  517. // * @return int
  518. // * @throws RestException 400
  519. // * @throws RestException 401
  520. // * @throws RestException 404
  521. // * @throws RestException 405
  522. // */
  523. /*
  524. public function createShipmentFromOrder($orderid)
  525. {
  526. require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
  527. if(! DolibarrApiAccess::$user->rights->expedition->lire) {
  528. throw new RestException(401);
  529. }
  530. if(! DolibarrApiAccess::$user->rights->expedition->creer) {
  531. throw new RestException(401);
  532. }
  533. if(empty($proposalid)) {
  534. throw new RestException(400, 'Order ID is mandatory');
  535. }
  536. $order = new Commande($this->db);
  537. $result = $order->fetch($proposalid);
  538. if( ! $result ) {
  539. throw new RestException(404, 'Order not found');
  540. }
  541. $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
  542. if( $result < 0) {
  543. throw new RestException(405, $this->shipment->error);
  544. }
  545. $this->shipment->fetchObjectLinked();
  546. return $this->_cleanObjectDatas($this->shipment);
  547. }
  548. */
  549. /**
  550. * Close a shipment (Classify it as "Delivered")
  551. *
  552. * @param int $id Expedition ID
  553. * @param int $notrigger Disabled triggers
  554. *
  555. * @url POST {id}/close
  556. *
  557. * @return object
  558. */
  559. public function close($id, $notrigger = 0)
  560. {
  561. if (!DolibarrApiAccess::$user->rights->expedition->creer) {
  562. throw new RestException(401);
  563. }
  564. $result = $this->shipment->fetch($id);
  565. if (!$result) {
  566. throw new RestException(404, 'Shipment not found');
  567. }
  568. if (!DolibarrApi::_checkAccessToResource('expedition', $this->commande->id)) {
  569. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  570. }
  571. $result = $this->shipment->setClosed();
  572. if ($result == 0) {
  573. throw new RestException(304, 'Error nothing done. May be object is already closed');
  574. }
  575. if ($result < 0) {
  576. throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
  577. }
  578. // Reload shipment
  579. $result = $this->shipment->fetch($id);
  580. $this->shipment->fetchObjectLinked();
  581. return $this->_cleanObjectDatas($this->shipment);
  582. }
  583. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  584. /**
  585. * Clean sensible object datas
  586. *
  587. * @param Object $object Object to clean
  588. * @return Object Object with cleaned properties
  589. */
  590. protected function _cleanObjectDatas($object)
  591. {
  592. // phpcs:enable
  593. $object = parent::_cleanObjectDatas($object);
  594. unset($object->thirdparty); // id already returned
  595. unset($object->note);
  596. unset($object->address);
  597. unset($object->barcode_type);
  598. unset($object->barcode_type_code);
  599. unset($object->barcode_type_label);
  600. unset($object->barcode_type_coder);
  601. if (!empty($object->lines) && is_array($object->lines)) {
  602. foreach ($object->lines as $line) {
  603. if (is_array($line->detail_batch)) {
  604. foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
  605. unset($line->detail_batch[$keytmp2]->db);
  606. }
  607. }
  608. unset($line->tva_tx);
  609. unset($line->vat_src_code);
  610. unset($line->total_ht);
  611. unset($line->total_ttc);
  612. unset($line->total_tva);
  613. unset($line->total_localtax1);
  614. unset($line->total_localtax2);
  615. unset($line->remise_percent);
  616. }
  617. }
  618. return $object;
  619. }
  620. /**
  621. * Validate fields before create or update object
  622. *
  623. * @param array $data Array with data to verify
  624. * @return array
  625. * @throws RestException
  626. */
  627. private function _validate($data)
  628. {
  629. $shipment = array();
  630. foreach (Shipments::$FIELDS as $field) {
  631. if (!isset($data[$field])) {
  632. throw new RestException(400, "$field field missing");
  633. }
  634. $shipment[$field] = $data[$field];
  635. }
  636. return $shipment;
  637. }
  638. }