From d1f9762ca9d9c3db6378b1eb4e2daef04ddb1d38 Mon Sep 17 00:00:00 2001 From: lainwir3d Date: Sat, 29 Aug 2020 13:15:55 +0200 Subject: [PATCH 1/2] CLOSE #18319 REST API - Shipment: Add 'close' action / endpoint / POST method. Allow "closing" a shipment through the REST API. --- .../expedition/class/api_shipments.class.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/htdocs/expedition/class/api_shipments.class.php b/htdocs/expedition/class/api_shipments.class.php index 2a4b6765b4e..3b4070b138b 100644 --- a/htdocs/expedition/class/api_shipments.class.php +++ b/htdocs/expedition/class/api_shipments.class.php @@ -626,6 +626,53 @@ class Shipments extends DolibarrApi } */ + /** + * Close a shipment (Classify it as "Delivered") + * + * @param int $id Expedition ID + * @param int $notrigger Disabled triggers + * + * @url POST {id}/close + * + * @return int + */ + public function close($id, $notrigger = 0) + { + if (!DolibarrApiAccess::$user->rights->expedition->creer) { + throw new RestException(401); + } + + $result = $this->shipment->fetch($id); + if (!$result) { + throw new RestException(404, 'Shipment not found'); + } + + if (!DolibarrApi::_checkAccessToResource('expedition', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->shipment->setClosed(); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already closed'); + } + if ($result < 0) { + throw new RestException(500, 'Error when closing Order: '.$this->commande->error); + } + + $result = $this->shipment->fetch($id); + if (!$result) { + throw new RestException(404, 'Shipment not found'); + } + + if (!DolibarrApi::_checkAccessToResource('expedition', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $this->shipment->fetchObjectLinked(); + + return $this->_cleanObjectDatas($this->shipment); + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas From 115b0e683129e363cd00f6506809331858958dc0 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 3 Aug 2021 15:07:16 +0200 Subject: [PATCH 2/2] Update api_shipments.class.php --- htdocs/expedition/class/api_shipments.class.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/htdocs/expedition/class/api_shipments.class.php b/htdocs/expedition/class/api_shipments.class.php index 3b4070b138b..a92fd4f436a 100644 --- a/htdocs/expedition/class/api_shipments.class.php +++ b/htdocs/expedition/class/api_shipments.class.php @@ -530,14 +530,9 @@ class Shipments extends DolibarrApi if ($result < 0) { throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error); } + + // Reload shipment $result = $this->shipment->fetch($id); - if (!$result) { - throw new RestException(404, 'Shipment not found'); - } - - if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } $this->shipment->fetchObjectLinked(); return $this->_cleanObjectDatas($this->shipment); @@ -659,14 +654,8 @@ class Shipments extends DolibarrApi throw new RestException(500, 'Error when closing Order: '.$this->commande->error); } + // Reload shipment $result = $this->shipment->fetch($id); - if (!$result) { - throw new RestException(404, 'Shipment not found'); - } - - if (!DolibarrApi::_checkAccessToResource('expedition', $this->commande->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } $this->shipment->fetchObjectLinked();