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 /**