From f482be84bff2102fca52ded5415f69f8331f33ef Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Thu, 26 Aug 2021 12:35:31 +0200 Subject: [PATCH 1/7] New : rest api GET orders shipements --- htdocs/commande/class/api_orders.class.php | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 745fea5ab76..9d2dd3c8317 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -966,7 +966,55 @@ class Orders extends DolibarrApi return $this->_cleanObjectDatas($this->commande); } + /** + * Get the shipments of an order + * + * + * @param int $id Id of the order + * + * @url GET {id}/shipment + * + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 500 + * + * @return array + */ + public function getOrderShipements($id) + { + require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; + if (!DolibarrApiAccess::$user->rights->expedition->lire) { + throw new RestException(401); + } + $sql = "SELECT t.rowid"; + $sql .= " FROM ".MAIN_DB_PREFIX."expedition as t"; + $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as tdet"; + $sql .= " ON t.rowid = tdet.rowid"; + $sql .= " WHERE tdet.fk_origin_line = ".$id; + $sql .= $this->db->order("t.rowid", "ASC"); + dol_syslog("API Rest request"); + $result = $this->db->query($sql); + + if ($result) { + $num = $this->db->num_rows($result); + if ($num <= 0) { + throw new RestException(404, 'Shipments not found '); + } + $i = 0; + while ($i < $num) { + $obj = $this->db->fetch_object($result); + $shipment_static = new Expedition($this->db); + if ($shipment_static->fetch($obj->rowid)) { + $obj_ret[] = $this->_cleanObjectDatas($shipment_static); + } + $i++; + } + } else { + throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror()); + } + return $obj_ret; + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** From c70813d718f37b74c8d29694880ced0e5681434f Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Thu, 26 Aug 2021 17:03:00 +0200 Subject: [PATCH 2/7] 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 From eb48566798f68bc90e330d6ae34301e09bd3e80e Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 27 Aug 2021 10:40:33 +0200 Subject: [PATCH 3/7] Close #18074 : New /order/{id}/shipment get & post --- htdocs/commande/class/api_orders.class.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index ed0cc3859d5..ca49b426a87 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -987,13 +987,18 @@ class Orders extends DolibarrApi if (!DolibarrApiAccess::$user->rights->expedition->lire) { throw new RestException(401); } + $sql = "SELECT e.rowid"; + $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e"; + $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet"; + $sql .= " ON e.rowid = edet.fk_expedition"; + $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet"; + $sql .= " ON edet.fk_origin_line = cdet.rowid"; + $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c"; + $sql .= " ON cdet.fk_commande = c.rowid"; + $sql .= " WHERE c.rowid = ".$this->db->escape($id); + $sql .= " GROUP BY e.rowid"; + $sql .= $this->db->order("e.rowid", "ASC"); - $sql = "SELECT t.rowid"; - $sql .= " FROM ".MAIN_DB_PREFIX."expedition as t"; - $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as tdet"; - $sql .= " ON t.rowid = tdet.rowid"; - $sql .= " WHERE tdet.fk_origin_line = ".$id; - $sql .= $this->db->order("t.rowid", "ASC"); dol_syslog("API Rest request"); $result = $this->db->query($sql); From b9039eea143f5004fc9145554923b45e5570fa87 Mon Sep 17 00:00:00 2001 From: lmarcouiller Date: Fri, 27 Aug 2021 10:45:04 +0200 Subject: [PATCH 4/7] FIx warehouse limit testing --- htdocs/commande/class/api_orders.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index ca49b426a87..2510e793018 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -1043,6 +1043,9 @@ class Orders extends DolibarrApi if (!DolibarrApiAccess::$user->rights->expedition->creer) { throw new RestException(401); } + if ($warehouse_id <= 0) { + throw new RestException(404, 'Warehouse not found'); + } $result = $this->commande->fetch($id); if (!$result) { throw new RestException(404, 'Order not found'); From d98390e6757de87331fc51db270303bc492a8b2c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 28 Aug 2021 14:42:31 +0200 Subject: [PATCH 5/7] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 2510e793018..88fed4bce63 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -970,18 +970,17 @@ class Orders extends DolibarrApi /** * Get the shipments of an order * - * * @param int $id Id of the order * * @url GET {id}/shipment * + * @return array + * * @throws RestException 401 * @throws RestException 404 * @throws RestException 500 - * - * @return array */ - public function getOrderShipements($id) + public function getOrderShipments($id) { require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; if (!DolibarrApiAccess::$user->rights->expedition->lire) { From 3585259b555ea6b11bb60e8f97dfa408074d03d5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 28 Aug 2021 14:43:17 +0200 Subject: [PATCH 6/7] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 88fed4bce63..743c0633ffa 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -1036,7 +1036,7 @@ class Orders extends DolibarrApi * * @return int */ - public function createOrderShipement($id, $warehouse_id) + public function createOrderShipment($id, $warehouse_id) { require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; if (!DolibarrApiAccess::$user->rights->expedition->creer) { From 049992aed47900fe5354416ed1949d908c5767ea Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 28 Aug 2021 14:44:13 +0200 Subject: [PATCH 7/7] Update api_orders.class.php --- htdocs/commande/class/api_orders.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index 743c0633ffa..d625f2acbf1 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -1030,11 +1030,11 @@ class Orders extends DolibarrApi * * @url POST {id}/shipment/{warehouse_id} * + * @return int + * * @throws RestException 401 * @throws RestException 404 * @throws RestException 500 - * - * @return int */ public function createOrderShipment($id, $warehouse_id) {