From c70813d718f37b74c8d29694880ced0e5681434f Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Thu, 26 Aug 2021 17:03:00 +0200 Subject: [PATCH] New : rest api POST orders shipment --- htdocs/commande/class/api_orders.class.php | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 9d2dd3c8317..ed0cc3859d5 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -966,6 +966,7 @@ class Orders extends DolibarrApi return $this->_cleanObjectDatas($this->commande); } + /** * Get the shipments of an order * @@ -1016,6 +1017,47 @@ class Orders extends DolibarrApi return $obj_ret; } + /** + * Create the shipments of an order + * + * + * @param int $id Id of the order + * @param int $warehouse_id Id of a warehouse + * + * @url POST {id}/shipment/{warehouse_id} + * + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 500 + * + * @return int + */ + public function createOrderShipement($id, $warehouse_id) + { + require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; + if (!DolibarrApiAccess::$user->rights->expedition->creer) { + throw new RestException(401); + } + $result = $this->commande->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + $shipment = new Expedition($this->db); + $shipment->socid = $this->commande->socid; + $result = $shipment->create(DolibarrApiAccess::$user); + if ($result <= 0) { + throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror()); + } + foreach ($this->commande->lines as $line) { + $result = $shipment->create_line($warehouse_id, $line->id, $line->qty); + if ($result <= 0) { + throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror()); + } + $i++; + } + return $shipment->id; + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas