Merge pull request #17413 from kastoras/product_api_get_product_stock
Close/CLOSE #13739 Product API route added to get product stock
This commit is contained in:
commit
0b0d1ee832
@ -1793,6 +1793,51 @@ 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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user