Skip to content

Commit

Permalink
Fix error management in API
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jan 18, 2020
1 parent a1b03e0 commit 45f322c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 12 additions & 2 deletions htdocs/product/stock/class/api_stockmovements.class.php
Expand Up @@ -156,15 +156,21 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100,

/**
* 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}
* @param string $lot Lot {@from body}
* @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
Expand All @@ -175,6 +181,10 @@ public function post($product_id, $warehouse_id, $qty, $lot = '', $movementcode
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;
Expand Down
7 changes: 5 additions & 2 deletions htdocs/product/stock/class/mouvementstock.class.php
Expand Up @@ -157,6 +157,8 @@ public function _create($user, $fk_product, $entrepot_id, $qty, $type, $price =
$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;
}
Expand Down Expand Up @@ -352,7 +354,7 @@ public function _create($user, $fk_product, $entrepot_id, $qty, $type, $price =
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);
Expand Down Expand Up @@ -398,7 +400,8 @@ public function _create($user, $fk_product, $entrepot_id, $qty, $type, $price =
}
else
{
$this->errors[]=$this->db->lasterror();
$this->error = $this->db->lasterror();
$this->errors[] = $this->error;
$error = -1;
}

Expand Down

0 comments on commit 45f322c

Please sign in to comment.