Skip to content

Commit

Permalink
Can get product by ref, ref_ext, barcode through API Rest
Browse files Browse the repository at this point in the history
  • Loading branch information
c3do committed Oct 19, 2019
1 parent 263bd9c commit 6742700
Showing 1 changed file with 108 additions and 28 deletions.
136 changes: 108 additions & 28 deletions htdocs/product/class/api_products.class.php
Expand Up @@ -59,48 +59,84 @@ public function __construct()
}

/**
* Get properties of a product object (from its ID, Ref, Ref_ext or Barcode)
* Get properties of a product object by id
*
* Return an array with product information.
* TODO implement getting a product by ref or by $ref_ext
*
* @param int $id ID of product
* @param string $ref Ref of element
* @param string $ref_ext Ref ext of element
* @param string $barcode Barcode of element
* @param int $includestockdata Load also information about stock (slower)
* @return array|mixed Data without useless information
*
* @throws 401
* @throws 403
* @throws 404
*/
public function get($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0)
public function get($id, $includestockdata = 0)
{
if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode');
}

$id = (empty($id)?0:$id);

if(! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(403);
}

$result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
if(! $result ) {
throw new RestException(404, 'Product not found');
}
return $this->_fetch($id, '', '', '', $includestockdata);
}

if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
/**
* Get properties of a product object by ref
*
* Return an array with product information.
*
* @param string $ref Ref of element
* @param int $includestockdata Load also information about stock (slower)
*
* @return array|mixed Data without useless information
*
* @url GET byRef/{ref}
*
* @throws 401
* @throws 403
* @throws 404
*/
public function getByRef($ref, $includestockdata = 0)
{
return $this->_fetch('', $ref, '', '', $includestockdata);
}

if ($includestockdata) {
$this->product->load_stock();
}
/**
* Get properties of a product object by ref_ext
*
* Return an array with product information.
*
* @param string $ref_ext Ref_ext of element
* @param int $includestockdata Load also information about stock (slower)
*
* @return array|mixed Data without useless information
*
* @url GET byRefExt/{ref_ext}
*
* @throws 401
* @throws 403
* @throws 404
*/
public function getByRefExt($ref_ext, $includestockdata = 0)
{
return $this->_fetch('', '', $ref_ext, '', $includestockdata);
}

return $this->_cleanObjectDatas($this->product);
/**
* Get properties of a product object by barcode
*
* Return an array with product information.
*
* @param string $barcode Barcode of element
* @param int $includestockdata Load also information about stock (slower)
*
* @return array|mixed Data without useless information
*
* @url GET byBarcode/{barcode}
*
* @throws 401
* @throws 403
* @throws 404
*/
public function getByBarcode($barcode, $includestockdata = 0)
{
return $this->_fetch('', '', '', $barcode, $includestockdata);
}

/**
Expand Down Expand Up @@ -246,7 +282,7 @@ public function put($id, $request_data = null)
}
$this->product->$field = $value;
}

$updatetype = false;
if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) {
$updatetype = true;
Expand Down Expand Up @@ -696,4 +732,48 @@ private function _validate($data)
}
return $product;
}

/**
* Get properties of a product object
*
* Return an array with product information.
*
* @param int $id ID of product
* @param string $ref Ref of element
* @param string $ref_ext Ref ext of element
* @param string $barcode Barcode of element
* @param int $includestockdata Load also information about stock (slower)
* @return array|mixed Data without useless information
*
* @throws 401
* @throws 403
* @throws 404
*/
private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0)
{
if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) {
throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode');
}

$id = (empty($id)?0:$id);

if(! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(403);
}

$result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
if(! $result ) {
throw new RestException(404, 'Product not found');
}

if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

if ($includestockdata) {
$this->product->load_stock();
}

return $this->_cleanObjectDatas($this->product);
}
}

0 comments on commit 6742700

Please sign in to comment.