Close/CLOSE #13739 Product API route added to get product stock

Using GET /products/{id}/stock user can get product stock from all warehouses. Using warehouse id will get stock of warehouse given.
This commit is contained in:
kastoras 2021-04-28 00:32:37 +03:00
parent 42938ffe31
commit 2e108e1e2f

View File

@ -1793,6 +1793,50 @@ class Products extends DolibarrApi
return $result;
}
/**
* Get stock data for the product id given.
* Optionaly with $selected_warehouse_id parameter user can get stock of specific warehouse
*
* @param int $id ID of Product
* @param int $selected_warehouse_id ID of warehouse
* @return int
*
* @throws RestException 500
* @throws RestException 401
* @throws RestException 404
*
* @url GET {id}/stock
*/
public function getStock($id,$selected_warehouse_id=null){
if (!DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(401);
}
if (!DolibarrApi::_checkAccessToResource('product', $id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$product_model = new Product($this->db);
$product_model->fetch($id);
$product_model->load_stock();
$stockData = $this->_cleanObjectDatas($product_model)->stock_warehouse;
if($selected_warehouse_id){
foreach($stockData as $warehouse_id => $warehouse){
if($warehouse_id != $selected_warehouse_id){
unset($stockData[$warehouse_id]);
}
}
}
if(empty($stockData)){
throw new RestException(404, 'No stock found');
}
return ['stock_warehouses'=>$stockData];
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas