From 9b00a1f61a873438eab5e91c3587fdc85e6c6e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sat, 8 Aug 2020 22:48:21 +0200 Subject: [PATCH] Some updates for attributes triggers --- htdocs/product/class/api_products.class.php | 630 ++++++++++---------- 1 file changed, 322 insertions(+), 308 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 89c9a78ddaa..a63709b5c4b 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -41,17 +41,17 @@ class Products extends DolibarrApi 'ref', 'label' ); - + /** * @var Product $product {@type Product} */ public $product; - + /** * @var ProductFournisseur $productsupplier {@type ProductFournisseur} */ public $productsupplier; - + /** * Constructor */ @@ -62,7 +62,7 @@ class Products extends DolibarrApi $this->product = new Product($this->db); $this->productsupplier = new ProductFournisseur($this->db); } - + /** * Get properties of a product object by id * @@ -81,7 +81,7 @@ class Products extends DolibarrApi { return $this->_fetch($id, '', '', '', $includestockdata, $includesubproducts); } - + /** * Get properties of a product object by ref * @@ -103,7 +103,7 @@ class Products extends DolibarrApi { return $this->_fetch('', $ref, '', '', $includestockdata, $includesubproducts); } - + /** * Get properties of a product object by ref_ext * @@ -125,7 +125,7 @@ class Products extends DolibarrApi { return $this->_fetch('', '', $ref_ext, '', $includestockdata, $includesubproducts); } - + /** * Get properties of a product object by barcode * @@ -147,7 +147,7 @@ class Products extends DolibarrApi { return $this->_fetch('', '', '', $barcode, $includestockdata, $includesubproducts); } - + /** * List products * @@ -165,11 +165,11 @@ class Products extends DolibarrApi public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '') { global $db, $conf; - + $obj_ret = array(); - + $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : ''; - + $sql = "SELECT t.rowid, t.ref, t.ref_ext"; $sql .= " FROM ".MAIN_DB_PREFIX."product as t"; if ($category > 0) { @@ -196,17 +196,17 @@ class Products extends DolibarrApi $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); @@ -229,7 +229,7 @@ class Products extends DolibarrApi } return $obj_ret; } - + /** * Create product object * @@ -243,17 +243,17 @@ class Products extends DolibarrApi } // Check mandatory fields $result = $this->_validate($request_data); - + foreach ($request_data as $field => $value) { $this->product->$field = $value; } if ($this->product->create(DolibarrApiAccess::$user) < 0) { throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors)); } - + return $this->product->id; } - + /** * Update product. * Price will be updated by this API only if option is set on "One price per product". See other APIs for other price modes. @@ -268,38 +268,38 @@ class Products extends DolibarrApi public function put($id, $request_data = null) { global $conf; - + if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $result = $this->product->fetch($id); 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); } - + $oldproduct = dol_clone($this->product, 0); - + foreach ($request_data as $field => $value) { if ($field == 'id') { continue; } - if ($field == 'stock_reel') { - throw new RestException(400, 'Stock reel cannot be updated here. Use the /stockmovements endpoint instead'); + if ($field == 'stock_reel') { + throw new RestException(400, 'Stock reel cannot be updated here. Use the /stockmovements endpoint instead'); } $this->product->$field = $value; } - + $updatetype = false; if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) { $updatetype = true; } - + $result = $this->product->update($id, DolibarrApiAccess::$user, 1, 'update', $updatetype); - + // If price mode is 1 price per product if ($result > 0 && !empty($conf->global->PRODUCT_PRICE_UNIQ)) { // We update price only if it was changed @@ -312,7 +312,7 @@ class Products extends DolibarrApi } if ($this->product->default_vat_code != $oldproduct->default_vat_code) { $pricemodified = true; } - + if ($this->product->price_base_type == 'TTC') { if ($this->product->price_ttc != $oldproduct->price_ttc) { $pricemodified = true; } @@ -325,30 +325,30 @@ class Products extends DolibarrApi } } } - + if ($pricemodified) { $newvat = $this->product->tva_tx; $newnpr = $this->product->tva_npr; $newvatsrccode = $this->product->default_vat_code; - + $newprice = $this->product->price; $newpricemin = $this->product->price_min; if ($this->product->price_base_type == 'TTC') { $newprice = $this->product->price_ttc; $newpricemin = $this->product->price_min_ttc; } - + $result = $this->product->updatePrice($newprice, $this->product->price_base_type, DolibarrApiAccess::$user, $newvat, $newpricemin, 0, $newnpr, 0, 0, array(), $newvatsrccode); } } - + if ($result <= 0) { throw new RestException(500, "Error updating product", array_merge(array($this->product->error), $this->product->errors)); } - + return $this->get($id); } - + /** * Delete product * @@ -364,18 +364,18 @@ class Products extends DolibarrApi 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); } - + // The Product::delete() method uses the global variable $user. global $user; $user = DolibarrApiAccess::$user; - + return $this->product->delete(DolibarrApiAccess::$user); } - + /** * Get the list of subproducts of the product. * @@ -393,22 +393,22 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + if (!DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $childsArbo = $this->product->getChildsArbo($id, 1); - + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; $childs = []; foreach ($childsArbo as $values) { $childs[] = array_combine($keys, $values); } - + return $childs; } - + /** * Add subproduct. * @@ -431,18 +431,18 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if (!DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec); if ($result <= 0) { throw new RestException(500, "Error adding product child"); } return $result; } - + /** * Remove subproduct. * @@ -462,19 +462,19 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if (!DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $result = $this->product->del_sousproduit($id, $subproduct_id); if ($result <= 0) { throw new RestException(500, "Error while removing product child"); } return $result; } - - + + /** * Get categories for a product * @@ -493,22 +493,22 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->categorie->lire) { throw new RestException(401); } - + $categories = new Categorie($this->db); - + $result = $categories->getListForItem($id, 'product', $sortfield, $sortorder, $limit, $page); - + if (empty($result)) { throw new RestException(404, 'No category found'); } - + if ($result < 0) { throw new RestException(503, 'Error when retrieve category list : '.array_merge(array($categories->error), $categories->errors)); } - + return $result; } - + /** * Get prices per segment for a product * @@ -521,24 +521,24 @@ class Products extends DolibarrApi public function getCustomerPricesPerSegment($id) { global $conf; - + if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + if (empty($conf->global->PRODUIT_MULTIPRICES)) { throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup'); } - + $result = $this->product->fetch($id); if (!$result) { throw new RestException(404, 'Product not found'); } - + if ($result < 0) { throw new RestException(503, 'Error when retrieve prices list : '.array_merge(array($this->product->error), $this->product->errors)); } - + return array( 'multiprices'=>$this->product->multiprices, 'multiprices_inc_tax'=>$this->product->multiprices_ttc, @@ -549,7 +549,7 @@ class Products extends DolibarrApi //'multiprices_default_vat_code'=>$this->product->multiprices_default_vat_code ); } - + /** * Get prices per customer for a product * @@ -563,36 +563,36 @@ class Products extends DolibarrApi public function getCustomerPricesPerCustomer($id, $thirdparty_id = '') { global $conf; - + if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + if (empty($conf->global->PRODUIT_CUSTOMER_PRICES)) { throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup'); } - + $result = $this->product->fetch($id); if (!$result) { throw new RestException(404, 'Product not found'); } - + if ($result > 0) { - require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php'; - $prodcustprice = new Productcustomerprice($this->db); - $filter = array(); - $filter['t.fk_product'] .= $id; - if ($thirdparty_id) $filter['t.fk_soc'] .= $thirdparty_id; - $result = $prodcustprice->fetch_all('', '', 0, 0, $filter); + require_once DOL_DOCUMENT_ROOT.'/product/class/productcustomerprice.class.php'; + $prodcustprice = new Productcustomerprice($this->db); + $filter = array(); + $filter['t.fk_product'] .= $id; + if ($thirdparty_id) $filter['t.fk_soc'] .= $thirdparty_id; + $result = $prodcustprice->fetch_all('', '', 0, 0, $filter); } - + if (empty($prodcustprice->lines)) { throw new RestException(404, 'Prices not found'); } - + return $prodcustprice->lines; } - + /** * Get prices per quantity for a product * @@ -605,30 +605,30 @@ class Products extends DolibarrApi public function getCustomerPricesPerQuantity($id) { global $conf; - + if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + if (empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) { throw new RestException(400, 'API not available: this mode of pricing is not enabled by setup'); } - + $result = $this->product->fetch($id); if (!$result) { throw new RestException(404, 'Product not found'); } - + if ($result < 0) { throw new RestException(503, 'Error when retrieve prices list : '.array_merge(array($this->product->error), $this->product->errors)); } - + return array( - 'prices_by_qty'=>$this->product->prices_by_qty[0], // 1 if price by quantity was activated for the product - 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0] + 'prices_by_qty'=>$this->product->prices_by_qty[0], // 1 if price by quantity was activated for the product + 'prices_by_qty_list'=>$this->product->prices_by_qty_list[0] ); } - + /** * Add/Update purchase prices for a product. * @@ -641,11 +641,11 @@ class Products extends DolibarrApi * @param string $ref_fourn Supplier ref * @param float $tva_tx New VAT Rate (For example 8.5. Should not be a string) * @param string $charges costs affering to product - * @param float $remise_percent Discount regarding qty (percent) - * @param float $remise Discount regarding qty (amount) - * @param int $newnpr Set NPR or not - * @param int $delivery_time_days Delay in days for delivery (max). May be '' if not defined. - * @param string $supplier_reputation Reputation with this product to the defined supplier (empty, FAVORITE, DONOTORDER) + * @param float $remise_percent Discount regarding qty (percent) + * @param float $remise Discount regarding qty (amount) + * @param int $newnpr Set NPR or not + * @param int $delivery_time_days Delay in days for delivery (max). May be '' if not defined. + * @param string $supplier_reputation Reputation with this product to the defined supplier (empty, FAVORITE, DONOTORDER) * @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function). * @param string $newdefaultvatcode Default vat code * @param float $multicurrency_buyprice Purchase price for the quantity min in currency @@ -667,35 +667,35 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $result = $this->productsupplier->fetch($id); if (!$result) { throw new RestException(404, 'Product not found'); } - + if (!DolibarrApi::_checkAccessToResource('product', $this->productsupplier->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $result = $this->productsupplier->add_fournisseur(DolibarrApiAccess::$user, $fourn_id, $ref_fourn, $qty); if ($result < 0) { throw new RestException(500, "Error adding supplier to product : ".$this->db->lasterror()); } - + $fourn = new Fournisseur($this->db); $result = $fourn->fetch($fourn_id); if ($result <= 0) { throw new RestException(404, 'Supplier not found'); } - + $result = $this->productsupplier->update_buyprice($qty, $buyprice, DolibarrApiAccess::$user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges, $remise_percent, $remise, $newnpr, $delivery_time_days, $supplier_reputation, $localtaxes_array, $newdefaultvatcode, $multicurrency_buyprice, $multicurrency_price_base_type, $multicurrency_tx, $multicurrency_code, $desc_fourn, $barcode, $fk_barcode_type); - + if ($result <= 0) { throw new RestException(500, "Error updating buy price : ".$this->db->lasterror()); } return (int) $this->productsupplier->product_fourn_price_id; } - + /** * Delete purchase price for a product * @@ -719,19 +719,19 @@ class Products extends DolibarrApi if (!$result) { throw new RestException(404, 'Product not found'); } - + if (!DolibarrApi::_checkAccessToResource('product', $this->productsupplier->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $resultsupplier = 0; if ($result > 0) { $resultsupplier = $this->productsupplier->remove_product_fournisseur_price($priceid); } - + return $resultsupplier; } - + /** * Get a list of all purchase prices of products * @@ -749,79 +749,79 @@ class Products extends DolibarrApi */ 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->socid ? DolibarrApiAccess::$user->socid : ''; - $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_fourn = new ProductFournisseur($this->db); - $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid, '', '', 0, 0); - foreach ($product_fourn_list as $tmpobj) { - $this->_cleanObjectDatas($tmpobj); - } - - //var_dump($product_fourn_list->db);exit; - $obj_ret[$obj->rowid] = $product_fourn_list; - - $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; + global $db, $conf; + $obj_ret = array(); + $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : ''; + $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_fourn = new ProductFournisseur($this->db); + $product_fourn_list = $product_fourn->list_product_fournisseur_price($obj->rowid, '', '', 0, 0); + foreach ($product_fourn_list as $tmpobj) { + $this->_cleanObjectDatas($tmpobj); + } + + //var_dump($product_fourn_list->db);exit; + $obj_ret[$obj->rowid] = $product_fourn_list; + + $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 * @@ -846,36 +846,36 @@ class Products extends DolibarrApi 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, $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); } - + $product_fourn_list = array(); - + if ($result) { $product_fourn = new ProductFournisseur($this->db); $product_fourn_list = $product_fourn->list_product_fournisseur_price($this->product->id, '', '', 0, 0); } - + foreach ($product_fourn_list as $tmpobj) { $this->_cleanObjectDatas($tmpobj); } - + return $this->_cleanObjectDatas($product_fourn_list); } - + /** * Get attributes. * @@ -890,11 +890,11 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); return $prodattr->fetchAll(); } - + /** * Get attribute by ID. * @@ -912,17 +912,17 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $result = $prodattr->fetch((int) $id); - + if ($result < 0) { throw new RestException(404, "Attribute not found"); } - + return $prodattr; } - + /** * Get attributes by ref. * @@ -939,26 +939,26 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, ref, label, rang FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '".trim($ref)."' AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$this->db->num_rows($query)) { throw new RestException(404); } - + $result = $this->db->fetch_object($query); - + $attr = []; $attr['id'] = $result->rowid; $attr['ref'] = $result->ref; $attr['label'] = $result->label; $attr['rang'] = $result->rang; - + return $attr; } - + /** * Add attributes. * @@ -976,18 +976,18 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr->label = $label; $prodattr->ref = $ref; - + $resid = $prodattr->create(DolibarrApiAccess::$user); if ($resid <= 0) { throw new RestException(500, "Error creating new attribute"); } return $resid; } - + /** * Update attributes by id. * @@ -1006,22 +1006,22 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); - + $result = $prodattr->fetch((int) $id); if ($result == 0) { throw new RestException(404, 'Attribute not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute"); } - + foreach ($request_data as $field => $value) { if ($field == 'rowid') { continue; } $prodattr->$field = $value; } - + if ($prodattr->update(DolibarrApiAccess::$user) > 0) { $result = $prodattr->fetch((int) $id); if ($result == 0) { @@ -1034,7 +1034,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error updating attribute"); } - + /** * Delete attributes by id. * @@ -1051,18 +1051,18 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr->id = (int) $id; $result = $prodattr->delete(DolibarrApiAccess::$user); - + if ($result <= 0) { - throw new RestException(500, "Error deleting attribute"); + throw new RestException(500, "Error deleting attribute"); } - + return $result; } - + /** * Get attribute value by id. * @@ -1079,30 +1079,30 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $id." AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$query) { throw new RestException(401); } - + if (!$this->db->num_rows($query)) { throw new RestException(404, 'Attribute value not found'); } - + $result = $this->db->fetch_object($query); - + $attrval = []; $attrval['id'] = $result->rowid; $attrval['fk_product_attribute'] = $result->fk_product_attribute; $attrval['ref'] = $result->ref; $attrval['value'] = $result->value; - + return $attrval; } - + /** * Get attribute value by ref. * @@ -1120,30 +1120,30 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '".trim($ref)."' AND fk_product_attribute = ".(int) $id." AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$query) { throw new RestException(401); } - + if (!$this->db->num_rows($query)) { throw new RestException(404, 'Attribute value not found'); } - + $result = $this->db->fetch_object($query); - + $attrval = []; $attrval['id'] = $result->rowid; $attrval['fk_product_attribute'] = $result->fk_product_attribute; $attrval['ref'] = $result->ref; $attrval['value'] = $result->value; - + return $attrval; } - + /** * Delete attribute value by ref. * @@ -1160,16 +1160,30 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '".trim($ref)."' AND fk_product_attribute = ".(int) $id; - - if ($this->db->query($sql)) { + + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '".trim($ref)."' AND fk_product_attribute = ".(int) $id." AND entity IN (".getEntity('product').")"; + $query = $this->db->query($sql); + + if (!$query) { + throw new RestException(401); + } + + if (!$this->db->num_rows($query)) { + throw new RestException(404, 'Attribute value not found'); + } + + $result = $this->db->fetch_object($query); + + $attrval = new ProductAttributeValue($this->db); + $attrval->id = $result->rowid; + $result = $attrval->delete(DolibarrApiAccess::$user); + if ($result > 0) { return 1; } - + throw new RestException(500, "Error deleting attribute value"); } - + /** * Get all values for an attribute id. * @@ -1186,11 +1200,11 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); return $objectval->fetchAllByProductAttribute((int) $id); } - + /** * Get all values for an attribute ref. * @@ -1206,28 +1220,28 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $return = array(); - + $sql = 'SELECT '; $sql .= 'v.fk_product_attribute, v.rowid, v.ref, v.value FROM '.MAIN_DB_PREFIX.'product_attribute_value v '; $sql .= "WHERE v.fk_product_attribute = ( SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '".strtoupper(trim($ref))."' LIMIT 1)"; - + $query = $this->db->query($sql); - + while ($result = $this->db->fetch_object($query)) { $tmp = new ProductAttributeValue($this->db); $tmp->fk_product_attribute = $result->fk_product_attribute; $tmp->id = $result->rowid; $tmp->ref = $result->ref; $tmp->value = $result->value; - + $return[] = $tmp; } - + return $return; } - + /** * Add attribute value. * @@ -1246,22 +1260,22 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if (empty($ref) || empty($value)) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $objectval->fk_product_attribute = $id; $objectval->ref = $ref; $objectval->value = $value; - + if ($objectval->create(DolibarrApiAccess::$user) > 0) { return $objectval->id; } throw new RestException(500, "Error creating new attribute value"); } - + /** * Update attribute value. * @@ -1279,22 +1293,22 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $result = $objectval->fetch((int) $id); - + if ($result == 0) { throw new RestException(404, 'Attribute value not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute value"); } - + foreach ($request_data as $field => $value) { if ($field == 'rowid') { continue; } $objectval->$field = $value; } - + if ($objectval->update(DolibarrApiAccess::$user) > 0) { $result = $objectval->fetch((int) $id); if ($result == 0) { @@ -1307,7 +1321,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error updating attribute"); } - + /** * Delete attribute value by id. * @@ -1324,16 +1338,16 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $objectval->id = (int) $id; - - if ($objectval->delete() > 0) { + + if ($objectval->delete(DolibarrApiAccess::$user) > 0) { return 1; } throw new RestException(500, "Error deleting attribute value"); } - + /** * Get product variants. * @@ -1350,18 +1364,18 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); $combinations = $prodcomb->fetchAllByFkProductParent((int) $id); - + foreach ($combinations as $key => $combination) { $prodc2vp = new ProductCombination2ValuePair($this->db); $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); } - + return $combinations; } - + /** * Get product variants by Product ref. * @@ -1378,23 +1392,23 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $result = $this->product->fetch('', $ref); if (!$result) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); $combinations = $prodcomb->fetchAllByFkProductParent((int) $this->product->id); - + foreach ($combinations as $key => $combination) { $prodc2vp = new ProductCombination2ValuePair($this->db); $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); } - + return $combinations; } - + /** * Add variant. * @@ -1419,14 +1433,14 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if (empty($id) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); - + $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); foreach ($features as $id_attr => $id_value) { @@ -1437,23 +1451,23 @@ class Products extends DolibarrApi throw new RestException(401); } } - + $result = $this->product->fetch((int) $id); if (!$result) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); - + $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact, $reference); if ($result > 0) { - return $result; + return $result; } else { - throw new RestException(500, "Error creating new product variant"); + throw new RestException(500, "Error creating new product variant"); } } - + /** * Add variant by product ref. * @@ -1477,14 +1491,14 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if (empty($ref) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); - + $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); foreach ($features as $id_attr => $id_value) { @@ -1495,12 +1509,12 @@ class Products extends DolibarrApi throw new RestException(404); } } - + $result = $this->product->fetch('', trim($ref)); if (!$result) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); if (!$prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) { @@ -1515,7 +1529,7 @@ class Products extends DolibarrApi return $prodcomb->id; } } - + /** * Put product variants. * @@ -1533,16 +1547,16 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); $prodcomb->fetch((int) $id); - + foreach ($request_data as $field => $value) { if ($field == 'rowid') { continue; } $prodcomb->$field = $value; } - + $result = $prodcomb->update(DolibarrApiAccess::$user); if ($result > 0) { @@ -1550,7 +1564,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error editing variant"); } - + /** * Delete product variants. * @@ -1567,17 +1581,17 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); $prodcomb->id = (int) $id; $result = $prodcomb->delete(DolibarrApiAccess::$user); if ($result <= 0) { - throw new RestException(500, "Error deleting variant"); + throw new RestException(500, "Error deleting variant"); } return $result; } - + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas @@ -1589,24 +1603,24 @@ class Products extends DolibarrApi { // phpcs:enable $object = parent::_cleanObjectDatas($object); - + unset($object->regeximgext); unset($object->price_by_qty); unset($object->prices_by_qty_id); unset($object->libelle); unset($object->product_id_already_linked); unset($object->reputations); - + unset($object->name); unset($object->firstname); unset($object->lastname); unset($object->civility_id); - + unset($object->recuperableonly); - + return $object; } - + /** * Validate fields before create or update object * @@ -1625,7 +1639,7 @@ class Products extends DolibarrApi } return $product; } - + /** * Get properties of a product object * @@ -1648,48 +1662,48 @@ class Products extends DolibarrApi 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, $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); } - + if ($includestockdata) { - $this->product->load_stock(); - - if (is_array($this->product->stock_warehouse)) { - foreach ($this->product->stock_warehouse as $keytmp => $valtmp) { - if (is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) { - foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) { - unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db); - } - } - } - } + $this->product->load_stock(); + + if (is_array($this->product->stock_warehouse)) { + foreach ($this->product->stock_warehouse as $keytmp => $valtmp) { + if (is_array($this->product->stock_warehouse[$keytmp]->detail_batch)) { + foreach ($this->product->stock_warehouse[$keytmp]->detail_batch as $keytmp2 => $valtmp2) { + unset($this->product->stock_warehouse[$keytmp]->detail_batch[$keytmp2]->db); + } + } + } + } } - + if ($includesubproducts) { $childsArbo = $this->product->getChildsArbo($id, 1); - + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; $childs = []; foreach ($childsArbo as $values) { $childs[] = array_combine($keys, $values); } - + $this->product->sousprods = $childs; } - + return $this->_cleanObjectDatas($this->product); } }