2
0
forked from Wavyzz/dolibarr

[Core] Add product search from barcode via REST api

This commit is contained in:
Laurent Bouquet
2018-11-05 23:06:19 +01:00
parent 0f43521274
commit cb10da4d41
2 changed files with 18 additions and 7 deletions

View File

@@ -52,12 +52,15 @@ class Products extends DolibarrApi
}
/**
* Get properties of a product object
* Get properties of a product object (from its ID, Ref, Ref_ext or Barcode)
*
* Return an array with product information.
* TODO implement getting a product by ref or by $ref_ext
*
* @param int $id ID of product
* @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
*
@@ -65,17 +68,23 @@ class Products extends DolibarrApi
* @throws 403
* @throws 404
*/
function get($id, $includestockdata=0)
function get($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);
$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);
}

View File

@@ -1885,10 +1885,11 @@ class Product extends CommonObject
* @param int $id Id of product/service to load
* @param string $ref Ref of product/service to load
* @param string $ref_ext Ref ext of product/service to load
* @param string $barcode Barcode of product/service to load
* @param int $ignore_expression Ignores the math expression for calculating price and uses the db value instead
* @return int <0 if KO, 0 if not found, >0 if OK
*/
function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
function fetch($id='', $ref='', $ref_ext='', $barcode='', $ignore_expression=0)
{
include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
@@ -1897,7 +1898,7 @@ class Product extends CommonObject
dol_syslog(get_class($this)."::fetch id=".$id." ref=".$ref." ref_ext=".$ref_ext);
// Check parameters
if (! $id && ! $ref && ! $ref_ext)
if (! $id && ! $ref && ! $ref_ext && ! $barcode)
{
$this->error='ErrorWrongParameters';
dol_syslog(get_class($this)."::fetch ".$this->error);
@@ -1919,6 +1920,7 @@ class Product extends CommonObject
$sql.= " WHERE entity IN (".getEntity($this->element).")";
if ($ref) $sql.= " AND ref = '".$this->db->escape($ref)."'";
else if ($ref_ext) $sql.= " AND ref_ext = '".$this->db->escape($ref_ext)."'";
else if ($barcode) $sql.= " AND barcode = '".$this->db->escape($barcode)."'";
}
$resql = $this->db->query($sql);