diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 446b4fa3cfe..428ebe79c4a 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -178,6 +178,93 @@ 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 + */ + 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