[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. * Return an array with product information.
* TODO implement getting a product by ref or by $ref_ext * 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) * @param int $includestockdata Load also information about stock (slower)
* @return array|mixed Data without useless information * @return array|mixed Data without useless information
* *
@ -65,13 +68,19 @@ class Products extends DolibarrApi
* @throws 403 * @throws 403
* @throws 404 * @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) { if(! DolibarrApiAccess::$user->rights->produit->lire) {
throw new RestException(403); throw new RestException(403);
} }
$result = $this->product->fetch($id); $result = $this->product->fetch($id, $ref, $ref_ext, $barcode);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Product not found'); throw new RestException(404, 'Product not found');
} }

View File

@ -1885,10 +1885,11 @@ class Product extends CommonObject
* @param int $id Id of product/service to load * @param int $id Id of product/service to load
* @param string $ref Ref 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 $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 * @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 * @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'; 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); dol_syslog(get_class($this)."::fetch id=".$id." ref=".$ref." ref_ext=".$ref_ext);
// Check parameters // Check parameters
if (! $id && ! $ref && ! $ref_ext) if (! $id && ! $ref && ! $ref_ext && ! $barcode)
{ {
$this->error='ErrorWrongParameters'; $this->error='ErrorWrongParameters';
dol_syslog(get_class($this)."::fetch ".$this->error); dol_syslog(get_class($this)."::fetch ".$this->error);
@ -1919,6 +1920,7 @@ class Product extends CommonObject
$sql.= " WHERE entity IN (".getEntity($this->element).")"; $sql.= " WHERE entity IN (".getEntity($this->element).")";
if ($ref) $sql.= " AND ref = '".$this->db->escape($ref)."'"; if ($ref) $sql.= " AND ref = '".$this->db->escape($ref)."'";
else if ($ref_ext) $sql.= " AND ref_ext = '".$this->db->escape($ref_ext)."'"; 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); $resql = $this->db->query($sql);