From bb16564b982636ae5e54ddcf579702441661804b 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:08:46 +0200 Subject: [PATCH 1/7] NEW triggers create, modify, delete Also modified deleteByFkAttribute() to use the function delete() --- .../class/ProductAttributeValue.class.php | 99 ++++++++++++++----- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/htdocs/variants/class/ProductAttributeValue.class.php b/htdocs/variants/class/ProductAttributeValue.class.php index 0a26b3eace2..2c48a6ce7ce 100644 --- a/htdocs/variants/class/ProductAttributeValue.class.php +++ b/htdocs/variants/class/ProductAttributeValue.class.php @@ -16,17 +16,18 @@ * along with this program. If not, see . */ +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; /** * Class ProductAttributeValue * Used to represent a product attribute value */ -class ProductAttributeValue +class ProductAttributeValue extends CommonObject { /** * Database handler * @var DoliDB */ - private $db; + public $db; /** * Attribute value id @@ -144,14 +145,24 @@ class ProductAttributeValue /** * Creates a value for a product attribute * - * @param User $user Object user - * @return int <0 KO >0 OK + * @param User $user Object user + * @param int $notrigger Do not execute trigger + * @return int <0 KO >0 OK */ - public function create(User $user) + public function create(User $user, $notrigger = 0) { if (!$this->fk_product_attribute) { return -1; } + + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } // Ref must be uppercase $this->ref = strtoupper($this->ref); @@ -173,11 +184,21 @@ class ProductAttributeValue /** * Updates a product attribute value * - * @param User $user Object user - * @return int <0 if KO, >0 if OK + * @param User $user Object user + * @param int $notrigger Do not execute trigger + * @return int <0 if KO, >0 if OK */ - public function update(User $user) + public function update(User $user, $notrigger = 0) { + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_MODIFY', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + //Ref must be uppercase $this->ref = trim(strtoupper($this->ref)); $this->value = trim($this->value); @@ -196,33 +217,63 @@ class ProductAttributeValue /** * Deletes a product attribute value * + * @param User $user Object user + * @param int $notrigger Do not execute trigger * @return int <0 KO, >0 OK */ - public function delete() + public function delete(User $user, $notrigger = 0) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $this->id; - - if ($this->db->query($sql)) { - return 1; - } - - return -1; + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $this->id; + + if ($this->db->query($sql)) { + return 1; + } + + return -1; } /** * Deletes all product attribute values by a product attribute id * * @param int $fk_attribute Product attribute id + * @param int $notrigger Do not execute trigger * @return int <0 KO, >0 OK */ - public function deleteByFkAttribute($fk_attribute) + public function deleteByFkAttribute($fk_attribute, User $user) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $fk_attribute; - - if ($this->db->query($sql)) { - return 1; - } - - return -1; + $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $fk_attribute; + + $query = $this->db->query($sql); + + if (!$query) { + return -1; + } + + if (!$this->db->num_rows($query)) { + return 1; + } + + while ($obj = $this->db->fetch_object($query)) { + $tmp = new ProductAttributeValue($this->db); + if ($tmp->fetch($obj->rowid) > 0) { + $result = $tmp->delete($user); + if ($result < 0) { + return -1; + } + } else { + return -1; + } + } + + return 1; } } From e73a7c78f768c64ec0d46bfcae86349e96ab07f5 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:10:50 +0200 Subject: [PATCH 2/7] NEW triggers create, modify, delete --- .../variants/class/ProductAttribute.class.php | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/htdocs/variants/class/ProductAttribute.class.php b/htdocs/variants/class/ProductAttribute.class.php index 36b4823b51b..6d9fadf0101 100644 --- a/htdocs/variants/class/ProductAttribute.class.php +++ b/htdocs/variants/class/ProductAttribute.class.php @@ -16,17 +16,18 @@ * along with this program. If not, see . */ +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; /** * Class ProductAttribute * Used to represent a product attribute */ -class ProductAttribute +class ProductAttribute extends CommonObject { /** * Database handler * @var DoliDB */ - private $db; + public $db; /** * Id of the product attribute @@ -119,7 +120,8 @@ class ProductAttribute $return[] = $tmp; } - } else dol_print_error($this->db); + } + else dol_print_error($this->db); return $return; } @@ -127,11 +129,21 @@ class ProductAttribute /** * Creates a product attribute * - * @param User $user Object user that create + * @param User $user Object user + * @param int $notrigger Do not execute trigger * @return int <0 KO, Id of new variant if OK */ - public function create(User $user) + public function create(User $user, $notrigger = 0) { + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_CREATE', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + //Ref must be uppercase $this->ref = strtoupper($this->ref); @@ -152,11 +164,21 @@ class ProductAttribute /** * Updates a product attribute * - * @param User $user Object user + * @param User $user Object user + * @param int $notrigger Do not execute trigger * @return int <0 KO, >0 OK */ - public function update(User $user) + public function update(User $user, $notrigger = 0) { + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_MODIFY', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + //Ref must be uppercase $this->ref = trim(strtoupper($this->ref)); $this->label = trim($this->label); @@ -173,11 +195,21 @@ class ProductAttribute /** * Deletes a product attribute * - * @param User $user Object user - * @return int <0 KO, >0 OK + * @param User $user Object user + * @param int $notrigger Do not execute trigger + * @return int <0 KO, >0 OK */ - public function delete($user = null) + public function delete(User $user, $notrigger = 0) { + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_DELETE', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $this->id; if ($this->db->query($sql)) { From ef57bb7e842d804c66052eca42388f9498d8a25c 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:14:53 +0200 Subject: [PATCH 3/7] pass $user to delete functions --- htdocs/variants/card.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/variants/card.php b/htdocs/variants/card.php index 0669964ba57..6d077e85ad1 100644 --- a/htdocs/variants/card.php +++ b/htdocs/variants/card.php @@ -90,9 +90,9 @@ if ($confirm == 'yes') { if ($action == 'confirm_delete') { $db->begin(); - $res = $objectval->deleteByFkAttribute($object->id); + $res = $objectval->deleteByFkAttribute($object->id, $user); - if ($res < 1 || ($object->delete() < 1)) { + if ($res < 1 || ($object->delete($user) < 1)) { $db->rollback(); setEventMessages($langs->trans('CoreErrorMessage'), $object->errors, 'errors'); header('Location: '.dol_buildpath('/variants/card.php?id='.$object->id, 2)); @@ -105,7 +105,7 @@ if ($confirm == 'yes') { } elseif ($action == 'confirm_deletevalue') { if ($objectval->fetch($valueid) > 0) { - if ($objectval->delete() < 1) { + if ($objectval->delete($user) < 1) { setEventMessages($langs->trans('CoreErrorMessage'), $objectval->errors, 'errors'); } else { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); From ed1a2cd917e8c9722889fabd7f3c0483344d7031 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:45:51 +0200 Subject: [PATCH 4/7] Update 1 --- .../class/ProductAttributeValue.class.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/htdocs/variants/class/ProductAttributeValue.class.php b/htdocs/variants/class/ProductAttributeValue.class.php index 2c48a6ce7ce..c655e188c5d 100644 --- a/htdocs/variants/class/ProductAttributeValue.class.php +++ b/htdocs/variants/class/ProductAttributeValue.class.php @@ -154,27 +154,28 @@ class ProductAttributeValue extends CommonObject if (!$this->fk_product_attribute) { return -1; } - - if (empty($notrigger)) { - // Call trigger - $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user); - if ($result < 0) { - return -1; - } - // End call triggers - } // Ref must be uppercase $this->ref = strtoupper($this->ref); + $this->value = $this->db->escape($this->value); $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_value (fk_product_attribute, ref, value, entity) VALUES ('".(int) $this->fk_product_attribute."', '".$this->db->escape($this->ref)."', - '".$this->db->escape($this->value)."', ".(int) $this->entity.")"; + '".$this->value."', ".(int) $this->entity.")"; $query = $this->db->query($sql); if ($query) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_value'); + if (empty($notrigger)) { + // Call trigger + $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_CREATE', $user); + if ($result < 0) { + return -1; + } + // End call triggers + } + return 1; } @@ -223,6 +224,7 @@ class ProductAttributeValue extends CommonObject */ public function delete(User $user, $notrigger = 0) { + if (empty($notrigger)) { // Call trigger $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user); @@ -231,9 +233,7 @@ class ProductAttributeValue extends CommonObject } // End call triggers } - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $this->id; - if ($this->db->query($sql)) { return 1; } 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 5/7] 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); } } From d7a65a540e47a88517d7d342a9f5823ac3878599 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sat, 8 Aug 2020 20:58:51 +0000 Subject: [PATCH 6/7] Fixing style errors. --- htdocs/product/class/api_products.class.php | 426 +++++++++--------- .../variants/class/ProductAttribute.class.php | 6 +- .../class/ProductAttributeValue.class.php | 18 +- 3 files changed, 225 insertions(+), 225 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index a63709b5c4b..f1b3e91617b 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,22 +268,22 @@ 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; } @@ -292,14 +292,14 @@ class Products extends DolibarrApi } $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,20 +563,20 @@ 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); @@ -585,14 +585,14 @@ class Products extends DolibarrApi 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] ); } - + /** * Add/Update purchase prices for a product. * @@ -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 * @@ -758,9 +758,9 @@ class Products extends DolibarrApi $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); } @@ -801,16 +801,16 @@ class Products extends DolibarrApi 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 { @@ -821,7 +821,7 @@ class Products extends DolibarrApi } 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"); } - + 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,30 +1160,30 @@ class Products extends DolibarrApi if (!DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - + $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. * @@ -1200,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. * @@ -1220,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. * @@ -1260,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. * @@ -1293,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) { @@ -1321,7 +1321,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error updating attribute"); } - + /** * Delete attribute value by id. * @@ -1338,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(DolibarrApiAccess::$user) > 0) { return 1; } throw new RestException(500, "Error deleting attribute value"); } - + /** * Get product variants. * @@ -1364,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. * @@ -1392,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. * @@ -1433,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) { @@ -1451,14 +1451,14 @@ 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) { @@ -1467,7 +1467,7 @@ class Products extends DolibarrApi throw new RestException(500, "Error creating new product variant"); } } - + /** * Add variant by product ref. * @@ -1491,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) { @@ -1509,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)) { @@ -1529,7 +1529,7 @@ class Products extends DolibarrApi return $prodcomb->id; } } - + /** * Put product variants. * @@ -1547,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) { @@ -1564,7 +1564,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error editing variant"); } - + /** * Delete product variants. * @@ -1581,7 +1581,7 @@ 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); @@ -1591,7 +1591,7 @@ class Products extends DolibarrApi } return $result; } - + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas @@ -1603,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 * @@ -1639,7 +1639,7 @@ class Products extends DolibarrApi } return $product; } - + /** * Get properties of a product object * @@ -1662,25 +1662,25 @@ 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)) { @@ -1691,19 +1691,19 @@ class Products extends DolibarrApi } } } - + 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); } } diff --git a/htdocs/variants/class/ProductAttribute.class.php b/htdocs/variants/class/ProductAttribute.class.php index 6d9fadf0101..6e2335c2f99 100644 --- a/htdocs/variants/class/ProductAttribute.class.php +++ b/htdocs/variants/class/ProductAttribute.class.php @@ -143,7 +143,7 @@ class ProductAttribute extends CommonObject } // End call triggers } - + //Ref must be uppercase $this->ref = strtoupper($this->ref); @@ -178,7 +178,7 @@ class ProductAttribute extends CommonObject } // End call triggers } - + //Ref must be uppercase $this->ref = trim(strtoupper($this->ref)); $this->label = trim($this->label); @@ -209,7 +209,7 @@ class ProductAttribute extends CommonObject } // End call triggers } - + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $this->id; if ($this->db->query($sql)) { diff --git a/htdocs/variants/class/ProductAttributeValue.class.php b/htdocs/variants/class/ProductAttributeValue.class.php index c655e188c5d..b93f8b04c0b 100644 --- a/htdocs/variants/class/ProductAttributeValue.class.php +++ b/htdocs/variants/class/ProductAttributeValue.class.php @@ -175,7 +175,7 @@ class ProductAttributeValue extends CommonObject } // End call triggers } - + return 1; } @@ -199,7 +199,7 @@ class ProductAttributeValue extends CommonObject } // End call triggers } - + //Ref must be uppercase $this->ref = trim(strtoupper($this->ref)); $this->value = trim($this->value); @@ -224,7 +224,7 @@ class ProductAttributeValue extends CommonObject */ public function delete(User $user, $notrigger = 0) { - + if (empty($notrigger)) { // Call trigger $result = $this->call_trigger('PRODUCT_ATTRIBUTE_VALUE_DELETE', $user); @@ -237,7 +237,7 @@ class ProductAttributeValue extends CommonObject if ($this->db->query($sql)) { return 1; } - + return -1; } @@ -251,17 +251,17 @@ class ProductAttributeValue extends CommonObject public function deleteByFkAttribute($fk_attribute, User $user) { $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $fk_attribute; - + $query = $this->db->query($sql); - + if (!$query) { return -1; } - + if (!$this->db->num_rows($query)) { return 1; } - + while ($obj = $this->db->fetch_object($query)) { $tmp = new ProductAttributeValue($this->db); if ($tmp->fetch($obj->rowid) > 0) { @@ -273,7 +273,7 @@ class ProductAttributeValue extends CommonObject return -1; } } - + return 1; } } From 3f233a3200f93990ef8cf1136c883831f681c969 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 23:01:41 +0200 Subject: [PATCH 7/7] Fix comment params --- htdocs/variants/class/ProductAttributeValue.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/variants/class/ProductAttributeValue.class.php b/htdocs/variants/class/ProductAttributeValue.class.php index b93f8b04c0b..ab998c6752b 100644 --- a/htdocs/variants/class/ProductAttributeValue.class.php +++ b/htdocs/variants/class/ProductAttributeValue.class.php @@ -244,8 +244,8 @@ class ProductAttributeValue extends CommonObject /** * Deletes all product attribute values by a product attribute id * - * @param int $fk_attribute Product attribute id - * @param int $notrigger Do not execute trigger + * @param int $fk_attribute Product attribute id + * @param User $user Object user * @return int <0 KO, >0 OK */ public function deleteByFkAttribute($fk_attribute, User $user)