From 12d67c5f855de196a56823d2c844ec3c589ad5d2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 15 Sep 2019 17:18:10 +0200 Subject: [PATCH] Rename api to get all purchase prices --- htdocs/product/class/api_products.class.php | 177 ++++++++++---------- 1 file changed, 89 insertions(+), 88 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 524e36fda6e..812c2df7459 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -178,93 +178,6 @@ class Products extends DolibarrApi } return $obj_ret; } - - /** - * List supplier's products - * - * Get a list of supplier's products - * - * @param string $sortfield Sort field - * @param string $sortorder Sort order - * @param int $limit Limit for list - * @param int $page Page number - * @param int $mode Use this param to filter list (0 for all, 1 for only product, 2 for only service) - * @param int $category Use this param to filter list by category - * @param int $supplier Use this param to filter list by supplier - * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.tobuy:=:0) and (t.tosell:=:1)" - * @return array Array of product objects - * - * @url GET supplier_prices - */ - public function getSupplierProducts($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters = '') - { - global $db, $conf; - $obj_ret = array(); - $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; - $sql = "SELECT t.rowid, t.ref, t.ref_ext"; - $sql.= " FROM ".MAIN_DB_PREFIX."product as t"; - if ($category > 0) { - $sql.= ", ".MAIN_DB_PREFIX."categorie_product as c"; - } - $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur_price as s"; - - $sql.= ' WHERE t.entity IN ('.getEntity('product').')'; - - if ($supplier > 0) { - $sql.= " AND s.fk_soc = ".$db->escape($supplier); - } - $sql.= " AND s.fk_product = t.rowid "; - // Select products of given category - if ($category > 0) { - $sql.= " AND c.fk_categorie = ".$db->escape($category); - $sql.= " AND c.fk_product = t.rowid "; - } - if ($mode == 1) { - // Show only products - $sql.= " AND t.fk_product_type = 0"; - } elseif ($mode == 2) { - // Show only services - $sql.= " AND t.fk_product_type = 1"; - } - // Add sql filters - if ($sqlfilters) { - if (! DolibarrApi::_checkFilters($sqlfilters)) { - throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); - } - $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; - $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; - } - $sql.= $db->order($sortfield, $sortorder); - if ($limit) { - if ($page < 0) { - $page = 0; - } - $offset = $limit * $page; - $sql.= $db->plimit($limit + 1, $offset); - } - $result = $db->query($sql); - if ($result) { - $num = $db->num_rows($result); - $min = min($num, ($limit <= 0 ? $num : $limit)); - $i = 0; - while ($i < $min) - { - $obj = $db->fetch_object($result); - $product_static = new Product($db); - if($product_static->fetch($obj->rowid)) { - $obj_ret[] = $this->_cleanObjectDatas($product_static); - } - $i++; - } - } - else { - throw new RestException(503, 'Error when retrieve product list : '.$db->lasterror()); - } - if(! count($obj_ret)) { - throw new RestException(404, 'No product found'); - } - return $obj_ret; - } /** * Create product object @@ -554,6 +467,94 @@ class Products extends DolibarrApi ); } + + /** + * List purchase prices + * + * Get a list of all purchase prices of products + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit for list + * @param int $page Page number + * @param int $mode Use this param to filter list (0 for all, 1 for only product, 2 for only service) + * @param int $category Use this param to filter list by category of product + * @param int $supplier Use this param to filter list by supplier + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.tobuy:=:0) and (t.tosell:=:1)" + * @return array Array of product objects + * + * @url GET purchase_prices + */ + public function getSupplierProducts($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $supplier = 0, $sqlfilters = '') + { + global $db, $conf; + $obj_ret = array(); + $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; + $sql = "SELECT t.rowid, t.ref, t.ref_ext"; + $sql.= " FROM ".MAIN_DB_PREFIX."product as t"; + if ($category > 0) { + $sql.= ", ".MAIN_DB_PREFIX."categorie_product as c"; + } + $sql.= ", ".MAIN_DB_PREFIX."product_fournisseur_price as s"; + + $sql.= ' WHERE t.entity IN ('.getEntity('product').')'; + + if ($supplier > 0) { + $sql.= " AND s.fk_soc = ".$db->escape($supplier); + } + $sql.= " AND s.fk_product = t.rowid "; + // Select products of given category + if ($category > 0) { + $sql.= " AND c.fk_categorie = ".$db->escape($category); + $sql.= " AND c.fk_product = t.rowid "; + } + if ($mode == 1) { + // Show only products + $sql.= " AND t.fk_product_type = 0"; + } elseif ($mode == 2) { + // Show only services + $sql.= " AND t.fk_product_type = 1"; + } + // Add sql filters + if ($sqlfilters) { + if (! DolibarrApi::_checkFilters($sqlfilters)) { + throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); + } + $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; + $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + $sql.= $db->order($sortfield, $sortorder); + if ($limit) { + if ($page < 0) { + $page = 0; + } + $offset = $limit * $page; + $sql.= $db->plimit($limit + 1, $offset); + } + $result = $db->query($sql); + if ($result) { + $num = $db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + $i = 0; + while ($i < $min) + { + $obj = $db->fetch_object($result); + $product_static = new Product($db); + if($product_static->fetch($obj->rowid)) { + $obj_ret[] = $this->_cleanObjectDatas($product_static); + } + $i++; + } + } + else { + throw new RestException(503, 'Error when retrieve product list : '.$db->lasterror()); + } + if(! count($obj_ret)) { + throw new RestException(404, 'No product found'); + } + return $obj_ret; + } + /** * Get purchase prices for a product * @@ -602,7 +603,7 @@ class Products extends DolibarrApi if($result) { $this->product = new ProductFournisseur($this->db); $this->product->fetch($id, $ref); - $this->product->list_product_fournisseur_price($id, $sortfield, $sortorder, 0, 0); + $this->product->list_product_fournisseur_price($id, '', '', 0, 0); } return $this->_cleanObjectDatas($this->product);