From a322f113bfda316ee5442e0174016ae9910db39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cserveny=2C=20Tam=C3=A1s?= Date: Thu, 20 Jan 2022 22:18:36 +0100 Subject: [PATCH 1/3] missing supplierorder api --- .../fourn/class/api_supplier_orders.class.php | 183 ++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/htdocs/fourn/class/api_supplier_orders.class.php b/htdocs/fourn/class/api_supplier_orders.class.php index 01c0079171e..4dc3799ec41 100644 --- a/htdocs/fourn/class/api_supplier_orders.class.php +++ b/htdocs/fourn/class/api_supplier_orders.class.php @@ -368,6 +368,189 @@ class SupplierOrders extends DolibarrApi ) ); } + + /** + * Approve an order + * + * @param int $id Order ID + * @param int $idwarehouse Warehouse ID + * @param int $secondlevel 1=Does not execute triggers, 0= execute triggers + * + * @url POST {id}/approve + * + * @return array + * FIXME An error 403 is returned if the request has an empty body. + * Error message: "Forbidden: Content type `text/plain` is not supported." + * Workaround: send this in the body + * { + * "idwarehouse": 0, + * "secondlevel": 0 + * } + */ + public function approve($id, $idwarehouse = 0, $secondlevel = 0) + { + if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) { + throw new RestException(401); + } + $result = $this->order->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already approved'); + } + if ($result < 0) { + throw new RestException(500, 'Error when approve Order: '.$this->order->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Order approved (Ref='.$this->order->ref.')' + ) + ); + } + + + /** + * Sends an order to the vendor + * + * @param int $id Order ID + * @param integer $date Date (unix timestamp in sec) + * @param int $method Method + * @param string $comment Comment + * + * @url POST {id}/makeorder + * + * @return array + * FIXME An error 403 is returned if the request has an empty body. + * Error message: "Forbidden: Content type `text/plain` is not supported." + * Workaround: send this in the body + * { + * "date": 0, + * "method": 0, + * "comment": "" + * } + */ + public function makeOrder($id, $date, $method, $comment='') + { + if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) { + throw new RestException(401); + } + $result = $this->order->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment); + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already sent'); + } + if ($result < 0) { + throw new RestException(500, 'Error when sending Order: '.$this->order->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Order sent (Ref='.$this->order->ref.')' + ) + ); + } + + /** + * Receives the order, dispatches products. + * + * Example: + * { + * "closeopenorder": 1, + * "comment": "", + * "lines": [{ + * "id": 14, + * "fk_product": 112, + * "qty": 18, + * "warehouse": 1, + * "price": 114, + * "comment": "", + * "eatby": 0, + * "sellby": 0, + * "batch": 0, + * "notrigger": 0 + * }] + * } + * + * @param int $id Order ID + * @param integer $closeopenorder Close order if everything is received + * @param string $comment Comment {@required false} + * @param array $lines Array of product dispatches + * + * @url POST {id}/receive + * + * @return array + * FIXME An error 403 is returned if the request has an empty body. + * Error message: "Forbidden: Content type `text/plain` is not supported." + * + */ + public function receiveOrder($id, $closeopenorder = 1, $comment = null, $lines) + { + if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) { + throw new RestException(401); + } + $result = $this->order->fetch($id); + if (!$result) { + throw new RestException(404, 'Order not found'); + } + + if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + foreach ($lines as $line) { + $lineObj =(object) $line; + + $result=$this->order->dispatchProduct(DolibarrApiAccess::$user, + $lineObj->fk_product, + $lineObj->qty, + $lineObj->warehouse, + $lineObj->price, + $lineObj->comment, + $lineObj->eatby, + $lineObj->sellby, + $lineObj->batch, + $lineObj->id, + $lineObj->notrigger); + + if ($result < 0) { + throw new RestException(500, 'Error dispatch order line '.$line->id.': '.$this->order->error); + } + } + + $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment); + + if ($result == 0) { + throw new RestException(304, 'Error nothing done. May be object is already dispatched'); + } + if ($result < 0) { + throw new RestException(500, 'Error when receivce order: '.$this->order->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Order received (Ref='.$this->order->ref.')' + ) + ); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** From c5136240b49c90057ebebf260028a280cbb6ad53 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 20 Jan 2022 21:25:35 +0000 Subject: [PATCH 2/3] Fixing style errors. --- .../fourn/class/api_supplier_orders.class.php | 88 +++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/htdocs/fourn/class/api_supplier_orders.class.php b/htdocs/fourn/class/api_supplier_orders.class.php index 4dc3799ec41..e7087dcd22e 100644 --- a/htdocs/fourn/class/api_supplier_orders.class.php +++ b/htdocs/fourn/class/api_supplier_orders.class.php @@ -368,7 +368,7 @@ class SupplierOrders extends DolibarrApi ) ); } - + /** * Approve an order * @@ -416,8 +416,8 @@ class SupplierOrders extends DolibarrApi ) ); } - - + + /** * Sends an order to the vendor * @@ -438,7 +438,7 @@ class SupplierOrders extends DolibarrApi * "comment": "" * } */ - public function makeOrder($id, $date, $method, $comment='') + public function makeOrder($id, $date, $method, $comment = '') { if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) { throw new RestException(401); @@ -467,28 +467,28 @@ class SupplierOrders extends DolibarrApi ) ); } - - /** + + /** * Receives the order, dispatches products. - * - * Example: + * + * Example: * { * "closeopenorder": 1, * "comment": "", - * "lines": [{ - * "id": 14, - * "fk_product": 112, - * "qty": 18, - * "warehouse": 1, - * "price": 114, - * "comment": "", - * "eatby": 0, - * "sellby": 0, - * "batch": 0, - * "notrigger": 0 - * }] + * "lines": [{ + * "id": 14, + * "fk_product": 112, + * "qty": 18, + * "warehouse": 1, + * "price": 114, + * "comment": "", + * "eatby": 0, + * "sellby": 0, + * "batch": 0, + * "notrigger": 0 + * }] * } - * + * * @param int $id Order ID * @param integer $closeopenorder Close order if everything is received * @param string $comment Comment {@required false} @@ -499,7 +499,7 @@ class SupplierOrders extends DolibarrApi * @return array * FIXME An error 403 is returned if the request has an empty body. * Error message: "Forbidden: Content type `text/plain` is not supported." - * + * */ public function receiveOrder($id, $closeopenorder = 1, $comment = null, $lines) { @@ -515,29 +515,29 @@ class SupplierOrders extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - foreach ($lines as $line) { - $lineObj =(object) $line; - - $result=$this->order->dispatchProduct(DolibarrApiAccess::$user, - $lineObj->fk_product, - $lineObj->qty, - $lineObj->warehouse, - $lineObj->price, - $lineObj->comment, - $lineObj->eatby, - $lineObj->sellby, - $lineObj->batch, - $lineObj->id, - $lineObj->notrigger); - - if ($result < 0) { - throw new RestException(500, 'Error dispatch order line '.$line->id.': '.$this->order->error); - } - } - + foreach ($lines as $line) { + $lineObj =(object) $line; + + $result=$this->order->dispatchProduct(DolibarrApiAccess::$user, + $lineObj->fk_product, + $lineObj->qty, + $lineObj->warehouse, + $lineObj->price, + $lineObj->comment, + $lineObj->eatby, + $lineObj->sellby, + $lineObj->batch, + $lineObj->id, + $lineObj->notrigger); + + if ($result < 0) { + throw new RestException(500, 'Error dispatch order line '.$line->id.': '.$this->order->error); + } + } + $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment); - - if ($result == 0) { + + if ($result == 0) { throw new RestException(304, 'Error nothing done. May be object is already dispatched'); } if ($result < 0) { From c6ac4d63a54084ef79b16ee9be86b6457a8b7c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cserveny=2C=20Tam=C3=A1s?= Date: Thu, 20 Jan 2022 22:28:02 +0100 Subject: [PATCH 3/3] fix code review errors --- htdocs/fourn/class/api_supplier_orders.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/fourn/class/api_supplier_orders.class.php b/htdocs/fourn/class/api_supplier_orders.class.php index e7087dcd22e..e21eb3436dc 100644 --- a/htdocs/fourn/class/api_supplier_orders.class.php +++ b/htdocs/fourn/class/api_supplier_orders.class.php @@ -490,7 +490,7 @@ class SupplierOrders extends DolibarrApi * } * * @param int $id Order ID - * @param integer $closeopenorder Close order if everything is received + * @param integer $closeopenorder Close order if everything is received {@required false} * @param string $comment Comment {@required false} * @param array $lines Array of product dispatches * @@ -501,7 +501,7 @@ class SupplierOrders extends DolibarrApi * Error message: "Forbidden: Content type `text/plain` is not supported." * */ - public function receiveOrder($id, $closeopenorder = 1, $comment = null, $lines) + public function receiveOrder($id, $closeopenorder, $comment, $lines) { if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) { throw new RestException(401);