From cb10da4d4113c82c40ed7adc44827ffe07c43376 Mon Sep 17 00:00:00 2001 From: Laurent Bouquet Date: Mon, 5 Nov 2018 23:06:19 +0100 Subject: [PATCH] [Core] Add product search from barcode via REST api --- htdocs/product/class/api_products.class.php | 19 ++++++++++++++----- htdocs/product/class/product.class.php | 6 ++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index ce6928c7331..9767d152770 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -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); } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 805d5d214a2..7cbad44cd31 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -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);