From 45f322cce864e71342bb433e8bbacb56cb07acbc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 18 Jan 2020 20:17:21 +0100 Subject: [PATCH] Fix error management in API --- .../stock/class/api_stockmovements.class.php | 14 ++++++++++++-- .../product/stock/class/mouvementstock.class.php | 7 +++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/htdocs/product/stock/class/api_stockmovements.class.php b/htdocs/product/stock/class/api_stockmovements.class.php index 8e6621085b7..9951c05d323 100644 --- a/htdocs/product/stock/class/api_stockmovements.class.php +++ b/htdocs/product/stock/class/api_stockmovements.class.php @@ -156,6 +156,12 @@ class StockMovements extends DolibarrApi /** * Create stock movement object. + * You can use the following message to test this RES API: + * { "product_id": 1, "warehouse_id": 1, "qty": 1, "lot": "", "movementcode": "INV123", "movementlabel": "Inventory 123", "price": 0 } + * $price Can be set to update AWP (Average Weighted Price) when you make a stock increase + * $dlc Eat-by date. Will be used if lot does not exists yet and will be created. + * $dluo Sell-by date. Will be used if lot does not exists yet and will be created. + * * @param int $product_id Id product id {@min 1} {@from body} {@required true} * @param int $warehouse_id Id warehouse {@min 1} {@from body} {@required true} * @param float $qty Qty to add (Use negative value for a stock decrease) {@min 0} {@message qty must be higher than 0} {@from body} {@required true} @@ -163,8 +169,8 @@ class StockMovements extends DolibarrApi * @param string $movementcode Movement code {@example INV123} {@from body} * @param string $movementlabel Movement label {@example Inventory number 123} {@from body} * @param string $price To update AWP (Average Weighted Price) when you make a stock increase (qty must be higher then 0). {@from body} - * @param string $dlc {@from body} {@type date} - * @param string $dluo {@from body} {@type date} + * @param string $dlc Eat-by date. {@from body} {@type date} + * @param string $dluo Sell-by date. {@from body} {@type date} * * @return int ID of stock movement * @throws RestException @@ -175,6 +181,10 @@ class StockMovements extends DolibarrApi throw new RestException(401); } + if ($qty == 0) { + throw new RestException(503, "Making a stock movement with a quentity of 0 is not possible"); + } + // Type increase or decrease if ($qty >= 0) $type = 3; else $type = 2; diff --git a/htdocs/product/stock/class/mouvementstock.class.php b/htdocs/product/stock/class/mouvementstock.class.php index 790db5d7c5e..93c3d316be6 100644 --- a/htdocs/product/stock/class/mouvementstock.class.php +++ b/htdocs/product/stock/class/mouvementstock.class.php @@ -157,6 +157,8 @@ class MouvementStock extends CommonObject $result=$product->fetch($fk_product); if ($result < 0) { + $this->error = $product->error; + $this->errors = $product->errors; dol_print_error('', "Failed to fetch product"); return -1; } @@ -352,7 +354,7 @@ class MouvementStock extends CommonObject if(!empty($this->origin)) { // This is set by caller for tracking reason $origintype = $this->origin->element; $fk_origin = $this->origin->id; - if($origintype == 'project') $fk_project = $fk_origin; + if ($origintype == 'project') $fk_project = $fk_origin; else { $res = $this->origin->fetch($fk_origin); @@ -398,7 +400,8 @@ class MouvementStock extends CommonObject } else { - $this->errors[]=$this->db->lasterror(); + $this->error = $this->db->lasterror(); + $this->errors[] = $this->error; $error = -1; }