From f0268b4aafa979d9502cb72666f039602765d46d Mon Sep 17 00:00:00 2001 From: John Botella Date: Wed, 12 Aug 2020 10:14:29 +0200 Subject: [PATCH 1/4] Add declinaison price level --- .../install/mysql/migration/12.0.0-13.0.0.sql | 10 +- ...duct_attribute_combination_price_level.sql | 28 ++ htdocs/langs/en_US/products.lang | 3 + htdocs/product/class/product.class.php | 2 +- .../class/ProductCombination.class.php | 440 +++++++++++++++++- htdocs/variants/combinations.php | 95 +++- 6 files changed, 562 insertions(+), 16 deletions(-) create mode 100644 htdocs/install/mysql/tables/llx_product_attribute_combination_price_level.sql diff --git a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql index 24b6fd687e1..cf4339f56b9 100644 --- a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql +++ b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql @@ -123,5 +123,13 @@ create table llx_recruitment_recruitmentjobposition_extrafields ALTER TABLE llx_recruitment_recruitmentjobposition_extrafields ADD INDEX idx_fk_object(fk_object); +CREATE TABLE llx_product_attribute_combination_price_level +( + rowid INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, + fk_product_attribute_combination INTEGER DEFAULT 1 NOT NULL, + fk_price_level INTEGER DEFAULT 1 NOT NULL, + variation_price DOUBLE(24,8) NOT NULL, + variation_price_percentage INTEGER NULL +)ENGINE=innodb; - +ALTER TABLE llx_product_attribute_combination_price_level ADD UNIQUE( fk_product_attribute_combination, fk_price_level); diff --git a/htdocs/install/mysql/tables/llx_product_attribute_combination_price_level.sql b/htdocs/install/mysql/tables/llx_product_attribute_combination_price_level.sql new file mode 100644 index 00000000000..ae068b71ed0 --- /dev/null +++ b/htdocs/install/mysql/tables/llx_product_attribute_combination_price_level.sql @@ -0,0 +1,28 @@ +-- ============================================================================ +-- Copyright (C) 2020 John BOTELLA +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- +-- ============================================================================ + +CREATE TABLE llx_product_attribute_combination_price_level +( + rowid INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT, + fk_product_attribute_combination INTEGER DEFAULT 1 NOT NULL, + fk_price_level INTEGER DEFAULT 1 NOT NULL, + variation_price DOUBLE(24,8) NOT NULL, + variation_price_percentage INTEGER NULL +)ENGINE=innodb; + +ALTER TABLE llx_product_attribute_combination_price_level ADD UNIQUE( fk_product_attribute_combination, fk_price_level); diff --git a/htdocs/langs/en_US/products.lang b/htdocs/langs/en_US/products.lang index 7f82db63178..be1afc5b7e7 100644 --- a/htdocs/langs/en_US/products.lang +++ b/htdocs/langs/en_US/products.lang @@ -361,6 +361,9 @@ SelectCombination=Select combination ProductCombinationGenerator=Variants generator Features=Features PriceImpact=Price impact +ImpactOnPriceLevel=Impact on price level %s +ApplyToAllPriceImpactLevel= Apply to all levels +ApplyToAllPriceImpactLevelHelp=By clicking here you set the same price impact on all levels WeightImpact=Weight impact NewProductAttribute=New attribute NewProductAttributeValue=New attribute value diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 07450eef805..39634c11302 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -1077,7 +1077,7 @@ class Product extends CommonObject $comb = new ProductCombination($this->db); foreach ($comb->fetchAllByFkProductParent($this->id) as $currcomb) { - $currcomb->updateProperties($this, $user); + $currcomb->updateProperties($this, $user); } } diff --git a/htdocs/variants/class/ProductCombination.class.php b/htdocs/variants/class/ProductCombination.class.php index 3bb4f7312ee..777cb136b05 100644 --- a/htdocs/variants/class/ProductCombination.class.php +++ b/htdocs/variants/class/ProductCombination.class.php @@ -71,6 +71,12 @@ class ProductCombination */ public $entity; + /** + * Combination price level + * @var ProductCombinationLevel[] + */ + public $combination_price_levels; + /** * Constructor * @@ -92,6 +98,8 @@ class ProductCombination */ public function fetch($rowid) { + global $conf; + $sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $rowid." AND entity IN (".getEntity('product').")"; $query = $this->db->query($sql); @@ -113,9 +121,95 @@ class ProductCombination $this->variation_price_percentage = $obj->variation_price_percentage; $this->variation_weight = $obj->variation_weight; + if (!empty($conf->global->PRODUIT_MULTIPRICES)) { + $this->fetchCombinationPriceLevels(); + } + return 1; } + + /** + * Retrieves combination price levels + * + * @param int $fk_price_level the price level to fetch, use 0 for all + * @param bool $useCache to use cache or not + * @return int <0 KO, >0 OK + */ + public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true) + { + // Check cache + if(!empty($this->combination_price_levels) && $useCache){ + if((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ + return 1; + } + } + + if(!is_array($this->combination_price_levels) + || empty($fk_price_level) // if fetch an unique level dont erase all already fetched + ){ + $this->combination_price_levels = array(); + } + + $staticProductCombinationLevel = new ProductCombinationLevel($this->db); + $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->id, $fk_price_level); + + if (!$combination_price_levels || !is_array($combination_price_levels)) { + return -1; + } + + $this->combination_price_levels = $combination_price_levels; + + return 1; + } + + /** + * Retrieves combination price levels + * + * @param int $clean levels off PRODUIT_MULTIPRICES_LIMIT + * @return int <0 KO, >0 OK + */ + public function saveCombinationPriceLevels($clean = 1) + { + global $conf; + + $errors = 0; + + $staticProductCombinationLevel = new ProductCombinationLevel($this->db); + + // Delete all + if(empty($this->combination_price_levels)){ + return $staticProductCombinationLevel->deleteAllForCombination($this->id); + } + + // Clean not needed price levels + if($clean){ + $res = $staticProductCombinationLevel->clean($this->id); + + if($res<0){ + $this->errors[] = 'Fail to clean not needed price levels'; + return -1; + } + } + + foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level){ + $res = $combination_price_level->save(); + if($res<1){ + $this->error = 'save combination price level '.$fk_price_level . ' '.$combination_price_level->error; + $this->errors[] = $this->error; + $errors ++; + } + } + + if($errors > 0){ + return $errors*-1; + } + else{ + return 1; + } + } + + /** * Retrieves a product combination by a child product row id * @@ -124,6 +218,8 @@ class ProductCombination */ public function fetchByFkProductChild($fk_child) { + global $conf; + $sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".(int) $fk_child." AND entity IN (".getEntity('product').")"; $query = $this->db->query($sql); @@ -145,6 +241,10 @@ class ProductCombination $this->variation_price_percentage = $result->variation_price_percentage; $this->variation_weight = $result->variation_weight; + if (!empty($conf->global->PRODUIT_MULTIPRICES)) { + $this->fetchCombinationPriceLevels(); + } + return 1; } @@ -156,6 +256,8 @@ class ProductCombination */ public function fetchAllByFkProductParent($fk_product_parent) { + global $conf; + $sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product').")"; $query = $this->db->query($sql); @@ -175,6 +277,10 @@ class ProductCombination $tmp->variation_price_percentage = $result->variation_price_percentage; $tmp->variation_weight = $result->variation_weight; + if (!empty($conf->global->PRODUIT_MULTIPRICES)) { + $tmp->fetchCombinationPriceLevels(); + } + $return[] = $tmp; } @@ -209,6 +315,8 @@ class ProductCombination */ public function create($user) { + global $conf; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_attribute_combination (fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight, entity) VALUES (".(int) $this->fk_product_parent.", ".(int) $this->fk_product_child.", @@ -221,6 +329,13 @@ class ProductCombination return -1; } + if (!empty($conf->global->PRODUIT_MULTIPRICES)) { + $res = $this->saveCombinationPriceLevels(); + if($res<0){ + return -2; + } + } + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'product_attribute_combination'); return 1; @@ -234,6 +349,8 @@ class ProductCombination */ public function update(User $user) { + global $conf; + $sql = "UPDATE ".MAIN_DB_PREFIX."product_attribute_combination SET fk_product_parent = ".(int) $this->fk_product_parent.", fk_product_child = ".(int) $this->fk_product_child.", variation_price = ".(float) $this->variation_price.", variation_price_percentage = ".(int) $this->variation_price_percentage.", @@ -245,6 +362,14 @@ class ProductCombination return -1; } + + if (!empty($conf->global->PRODUIT_MULTIPRICES)) { + $res = $this->saveCombinationPriceLevels(); + if($res<0){ + return -2; + } + } + $parent = new Product($this->db); $parent->fetch($this->fk_product_parent); @@ -266,6 +391,12 @@ class ProductCombination $comb2val = new ProductCombination2ValuePair($this->db); $comb2val->deleteByFkCombination($this->id); + // remove combination price levels + if(!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) { + $this->db->rollback(); + return -1; + } + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $this->id; if ($this->db->query($sql)) { @@ -341,6 +472,7 @@ class ProductCombination $child->label = $parent->label.$varlabel;; } + if ($child->update($child->id, $user) > 0) { $new_vat = $parent->tva_tx; $new_npr = $parent->tva_npr; @@ -349,9 +481,12 @@ class ProductCombination if (!empty($conf->global->PRODUIT_MULTIPRICES)) { for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) { - if ($parent->multiprices[$i] != '') { + if ($parent->multiprices[$i] != '' || isset($this->combination_price_levels[$i]->variation_price)) { $new_type = $parent->multiprices_base_type[$i]; $new_min_price = $parent->multiprices_min[$i]; + $variation_price = doubleval(!isset($this->combination_price_levels[$i]->variation_price) ? $this->variation_price : $this->combination_price_levels[$i]->variation_price); + $variation_price_percentage = doubleval(!isset($this->combination_price_levels[$i]->variation_price_percentage) ? $this->variation_price_percentage : $this->combination_price_levels[$i]->variation_price_percentage); + if ($parent->prices_by_qty_list[$i]) { $new_psq = 1; } else { @@ -364,12 +499,12 @@ class ProductCombination $new_price = $parent->multiprices[$i]; } - if ($this->variation_price_percentage) { + if ($variation_price_percentage) { if ($new_price != 0) { - $new_price *= 1 + ($this->variation_price / 100); + $new_price *= 1 + ($variation_price / 100); } } else { - $new_price += $this->variation_price; + $new_price += $variation_price; } $child->updatePrice($new_price, $new_type, $user, $new_vat, $new_min_price, $i, $new_npr, $new_psq); @@ -508,7 +643,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; * @param Product $product Parent product * @param array $combinations Attribute and value combinations. * @param array $variations Price and weight variations - * @param bool $price_var_percent Is the price variation a relative variation? + * @param bool|array $price_var_percent Is the price variation a relative variation? * @param bool|float $forced_pricevar If the price variation is forced * @param bool|float $forced_weightvar If the weight variation is forced * @param bool|string $forced_refvar If the reference is forced @@ -523,6 +658,8 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $db->begin(); + $price_impact = array(1=>0); // init level price impact + $forced_refvar = trim($forced_refvar); if (!empty($forced_refvar) && $forced_refvar != $product->ref) { @@ -545,7 +682,12 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $weight_impact = (float) $forced_weightvar; // If false, return 0 //Final price impact - $price_impact = (float) $forced_pricevar; // If false, return 0 + if(!is_array($forced_pricevar)){ + $price_impact[1] = (float) $forced_pricevar; // If false, return 0 + } + else{ + $price_impact = $forced_pricevar; + } $newcomb = new ProductCombination($db); $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations); @@ -587,7 +729,15 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $weight_impact += (float) price2num($variations[$currcombattr][$currcombval]['weight']); } if ($forced_pricevar === false) { - $price_impact += (float) price2num($variations[$currcombattr][$currcombval]['price']); + $price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']); + + // Manage Price levels + if($conf->global->PRODUIT_MULTIPRICES){ + for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) + { + $price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']); + } + } } if ($forced_refvar === false) { @@ -606,9 +756,27 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; } $newcomb->variation_price_percentage = $price_var_percent; - $newcomb->variation_price = $price_impact; + $newcomb->variation_price = $price_impact[1]; $newcomb->variation_weight = $weight_impact; + // Init price level + if($conf->global->PRODUIT_MULTIPRICES){ + for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ + $productCombinationLevel = new ProductCombinationLevel($this->db); + $productCombinationLevel->fk_product_attribute_combination = 0; + $productCombinationLevel->fk_price_level = $i; + $productCombinationLevel->variation_price = $price_impact[$i]; + + if(is_array($price_var_percent)){ + $productCombinationLevel->variation_price_percentage = !empty($price_var_percent[$i]) ? $price_var_percent[$i] : 0; + }else{ + $productCombinationLevel->variation_price_percentage = $price_var_percent; + } + + $newcomb->combination_price_levels[$i] = $productCombinationLevel; + } + } + $newproduct->weight += $weight_impact; // Now create the product @@ -764,3 +932,259 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; return $label; } } + + + +/** + * Class ProductCombinationLevel + * Used to represent a product combination Level + */ +class ProductCombinationLevel +{ + /** + * Database handler + * @var DoliDB + */ + private $db; + + /** + * @var string Name of table without prefix where object is stored + */ + public $table_element = 'product_attribute_combination_price_level'; + + /** + * Rowid of combination + * @var int + */ + public $id; + + /** + * Rowid of parent product combination + * @var int + */ + public $fk_product_attribute_combination; + + /** + * Combination price level + * @var int + */ + public $fk_price_level; + + /** + * Price variation + * @var float + */ + public $variation_price; + + /** + * Is the price variation a relative variation? + * @var bool + */ + public $variation_price_percentage = false; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct(DoliDB $db) + { + $this->db = $db; + } + + /** + * Retrieves a combination level by its rowid + * + * @param int $rowid Row id + * @return int <0 KO, >0 OK + */ + public function fetch($rowid) + { + $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int)$rowid; + + $obj = $this->db->getRow($sql); + + if($obj){ + return $this->fetchFormObj($obj); + } + + return -1; + } + + + /** + * Retrieves combination price levels + * + * @param int $fk_product_attribute_combination + * @param int $fk_price_level the price level to fetch, use 0 for all + * @return self[] | -1 on KO + */ + public function fetchAll($fk_product_attribute_combination, $fk_price_level = 0) + { + + $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage" + ." FROM ".MAIN_DB_PREFIX.$this->table_element + ." WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination); + + if(!empty($fk_price_level)){ + $sql.= ' AND fk_price_level = '.intval($fk_price_level); + } + + $combination_price_levels = $this->db->getRows($sql); + + if (!$combination_price_levels || !is_array($combination_price_levels)) { + return -1; + } + + $result = array(); + + // For more simple usage set level as array key + foreach($combination_price_levels as $k => $row){ + $productCombinationLevel = new ProductCombinationLevel($this->db); + $productCombinationLevel->fetchFormObj($row); + $result[$row->fk_price_level] = $productCombinationLevel; + } + + return $result; + } + + /** + * assign vars form an stdclass like sql obj + * + * @param int $rowid Row id + * @return int <0 KO, >0 OK + */ + public function fetchFormObj($obj) + { + if (!$obj) { + return -1; + } + + $this->id = $obj->rowid; + $this->fk_product_attribute_combination = doubleval($obj->fk_product_attribute_combination); + $this->fk_price_level = intval($obj->fk_price_level); + $this->variation_price = doubleval($obj->variation_price); + $this->variation_price_percentage = (bool)$obj->variation_price_percentage; + + return 1; + } + + + /** + * save + * + * @return int <0 KO, >0 OK + */ + public function save() + { + $errors = 0; + + + if(empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){ + return -1; + } + + // check if level exist in DB before add + if(empty($this->id)){ + $sql = "SELECT rowid id" + ." FROM ".MAIN_DB_PREFIX . $this->table_element + ." WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination + .' AND fk_price_level = '.intval($this->fk_price_level); + + $existObj = $this->db->getRow($sql); + if($existObj){ + $this->id = $existObj->id; + } + } + + // Update + if(!empty($this->id)) { + $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET ' + . ' variation_price = '.doubleval($this->variation_price) + . ' , variation_price_percentage = '.intval($this->variation_price_percentage) + . ' WHERE rowid = '.intval($this->id); + + $res = $this->db->query($sql); + if($res>0){ + return $this->id; + } + else{ + $this->error = $this->db->error(); + $this->errors[] = $this->error; + return -1; + } + } + else{ + // ADD + $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (" + . " fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage" + . " ) VALUES ( " + . intval($this->fk_product_attribute_combination) + . ' , '.intval($this->fk_price_level) + . ' , '.doubleval($this->variation_price) + . ' , '.intval($this->variation_price_percentage) + . " )"; + + $res = $this->db->query($sql); + if($res){ + $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); + } + else{ + $this->error = $this->db->error(); + $this->errors[] = $this->error; + return -1; + } + } + + return $this->id; + } + + + /** + * delete + * + * @return int <0 KO, >0 OK + */ + public function delete() + { + $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element + ." WHERE rowid = ".(int) $this->id); + + return $res ? 1 : -1; + } + + + /** + * delete all for a combination + * + * @param $fk_product_attribute_combination + * @return int <0 KO, >0 OK + */ + public function deleteAllForCombination($fk_product_attribute_combination) + { + $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element + ." WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination); + + return $res ? 1 : -1; + } + + + /** + * Clean not needed price levels for a combination + * + * @param $fk_product_attribute_combination + * @return int <0 KO, >0 OK + */ + public function clean($fk_product_attribute_combination) + { + global $conf; + + $res = $this->db->query("DELETE FROM ".MAIN_DB_PREFIX.$this->table_element + . " WHERE fk_product_attribute_combination = ".(int) $fk_product_attribute_combination + . " AND fk_price_level > ".intval($conf->global->PRODUIT_MULTIPRICES_LIMIT) ); + + + return $res ? 1 : -1; + } + +} diff --git a/htdocs/variants/combinations.php b/htdocs/variants/combinations.php index 3886a8fcdce..5ad7b38af2e 100644 --- a/htdocs/variants/combinations.php +++ b/htdocs/variants/combinations.php @@ -33,6 +33,10 @@ $ref = GETPOST('ref', 'alpha'); $weight_impact = GETPOST('weight_impact', 'alpha'); $price_impact = GETPOST('price_impact', 'alpha'); $price_impact_percent = (bool) GETPOST('price_impact_percent'); + +$level_price_impact = GETPOST('level_price_impact', 'array'); +$level_price_impact_percent = GETPOST('level_price_impact_percent', 'array'); + $reference = GETPOST('reference', 'alpha'); $form = new Form($db); @@ -112,6 +116,18 @@ if ($_POST) { } $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); + + // for conf PRODUIT_MULTIPRICES + if($conf->global->PRODUIT_MULTIPRICES) { + $level_price_impact = array_map('price2num', $level_price_impact); + $level_price_impact_percent = array_map('price2num', $level_price_impact_percent); + } + else{ + $level_price_impact = array(1 => $weight_impact); + $level_price_impact_percent = array(1 => $price_impact_percent); + } + + $sanit_features = array(); //First, sanitize @@ -141,11 +157,10 @@ if ($_POST) { // sanit_feature is an array with 1 (and only 1) value per attribute. // For example: Color->blue, Size->Small, Option->2 //var_dump($sanit_features); - //var_dump($productCombination2ValuePairs1); exit; if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) { - $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact, $reference); + $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); @@ -227,6 +242,31 @@ if ($_POST) { $prodcomb->variation_price = $price_impact; $prodcomb->variation_weight = $weight_impact; + // for conf PRODUIT_MULTIPRICES + if($conf->global->PRODUIT_MULTIPRICES) { + $level_price_impact = array_map('price2num', $level_price_impact); + $level_price_impact_percent = array_map(function($a){ return !empty($a);}, $level_price_impact_percent); + + $prodcomb->variation_price = $level_price_impact[1]; + $prodcomb->variation_price_percentage = (bool)$level_price_impact_percent[1]; + } + else{ + $level_price_impact = array(1 => $weight_impact); + $level_price_impact_percent = array(1 => $price_impact_percent); + } + + if($conf->global->PRODUIT_MULTIPRICES){ + $prodcomb->combination_price_levels = array(); + for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ + $productCombinationLevel = new ProductCombinationLevel($db); + $productCombinationLevel->fk_product_attribute_combination = $prodcomb->id; + $productCombinationLevel->fk_price_level = $i; + $productCombinationLevel->variation_price = $level_price_impact[$i]; + $productCombinationLevel->variation_price_percentage = $level_price_impact_percent[$i]; + $prodcomb->combination_price_levels[$i] = $productCombinationLevel; + } + } + if ($prodcomb->update($user) > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2)); @@ -594,12 +634,34 @@ if (!empty($id) || !empty($ref)) +global->PRODUIT_MULTIPRICES)){ ?> - > + > + - fetchCombinationPriceLevels(); + + for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) + { + + print ''; + print ''; + if($i===1){ + print ' ('.$langs->trans('ApplyToAllPriceImpactLevel').')'; + } + print ''; + print ''; + print 'combination_price_levels[$i]->variation_price_percentage) ? ' checked' : '' ).'> '; + + print ''; + print ''; + } + } + if ($object->isProduct()) { print ''; print ''; @@ -609,9 +671,30 @@ if (!empty($id) || !empty($ref)) print ''; } - dol_fiche_end(); - ?> + if (!empty($conf->global->PRODUIT_MULTIPRICES)){ +?> + +
value="trans('Create') : $langs->trans('Save') ?>" class="button">   From 1cc673a9c61722dd9c628f3dde9d386c374bc7bb Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 12 Aug 2020 08:53:24 +0000 Subject: [PATCH 2/4] Fixing style errors. --- .../class/ProductCombination.class.php | 65 +++++++++---------- htdocs/variants/combinations.php | 32 ++++----- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/htdocs/variants/class/ProductCombination.class.php b/htdocs/variants/class/ProductCombination.class.php index 777cb136b05..17d1688d268 100644 --- a/htdocs/variants/class/ProductCombination.class.php +++ b/htdocs/variants/class/ProductCombination.class.php @@ -139,13 +139,13 @@ class ProductCombination public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true) { // Check cache - if(!empty($this->combination_price_levels) && $useCache){ - if((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ + if (!empty($this->combination_price_levels) && $useCache){ + if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ return 1; } } - if(!is_array($this->combination_price_levels) + if (!is_array($this->combination_price_levels) || empty($fk_price_level) // if fetch an unique level dont erase all already fetched ){ $this->combination_price_levels = array(); @@ -178,15 +178,15 @@ class ProductCombination $staticProductCombinationLevel = new ProductCombinationLevel($this->db); // Delete all - if(empty($this->combination_price_levels)){ + if (empty($this->combination_price_levels)){ return $staticProductCombinationLevel->deleteAllForCombination($this->id); } // Clean not needed price levels - if($clean){ + if ($clean){ $res = $staticProductCombinationLevel->clean($this->id); - if($res<0){ + if ($res<0){ $this->errors[] = 'Fail to clean not needed price levels'; return -1; } @@ -194,17 +194,17 @@ class ProductCombination foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level){ $res = $combination_price_level->save(); - if($res<1){ + if ($res<1){ $this->error = 'save combination price level '.$fk_price_level . ' '.$combination_price_level->error; $this->errors[] = $this->error; $errors ++; } } - if($errors > 0){ + if ($errors > 0){ return $errors*-1; } - else{ + else { return 1; } } @@ -331,7 +331,7 @@ class ProductCombination if (!empty($conf->global->PRODUIT_MULTIPRICES)) { $res = $this->saveCombinationPriceLevels(); - if($res<0){ + if ($res<0){ return -2; } } @@ -365,7 +365,7 @@ class ProductCombination if (!empty($conf->global->PRODUIT_MULTIPRICES)) { $res = $this->saveCombinationPriceLevels(); - if($res<0){ + if ($res<0){ return -2; } } @@ -392,7 +392,7 @@ class ProductCombination $comb2val->deleteByFkCombination($this->id); // remove combination price levels - if(!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) { + if (!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) { $this->db->rollback(); return -1; } @@ -682,10 +682,10 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $weight_impact = (float) $forced_weightvar; // If false, return 0 //Final price impact - if(!is_array($forced_pricevar)){ + if (!is_array($forced_pricevar)){ $price_impact[1] = (float) $forced_pricevar; // If false, return 0 } - else{ + else { $price_impact = $forced_pricevar; } @@ -732,7 +732,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']); // Manage Price levels - if($conf->global->PRODUIT_MULTIPRICES){ + if ($conf->global->PRODUIT_MULTIPRICES){ for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) { $price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']); @@ -760,16 +760,16 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $newcomb->variation_weight = $weight_impact; // Init price level - if($conf->global->PRODUIT_MULTIPRICES){ + if ($conf->global->PRODUIT_MULTIPRICES){ for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ $productCombinationLevel = new ProductCombinationLevel($this->db); $productCombinationLevel->fk_product_attribute_combination = 0; $productCombinationLevel->fk_price_level = $i; $productCombinationLevel->variation_price = $price_impact[$i]; - if(is_array($price_var_percent)){ + if (is_array($price_var_percent)){ $productCombinationLevel->variation_price_percentage = !empty($price_var_percent[$i]) ? $price_var_percent[$i] : 0; - }else{ + }else { $productCombinationLevel->variation_price_percentage = $price_var_percent; } @@ -1000,11 +1000,11 @@ class ProductCombinationLevel */ public function fetch($rowid) { - $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int)$rowid; + $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int) $rowid; $obj = $this->db->getRow($sql); - if($obj){ + if ($obj){ return $this->fetchFormObj($obj); } @@ -1026,7 +1026,7 @@ class ProductCombinationLevel ." FROM ".MAIN_DB_PREFIX.$this->table_element ." WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination); - if(!empty($fk_price_level)){ + if (!empty($fk_price_level)){ $sql.= ' AND fk_price_level = '.intval($fk_price_level); } @@ -1039,7 +1039,7 @@ class ProductCombinationLevel $result = array(); // For more simple usage set level as array key - foreach($combination_price_levels as $k => $row){ + foreach ($combination_price_levels as $k => $row){ $productCombinationLevel = new ProductCombinationLevel($this->db); $productCombinationLevel->fetchFormObj($row); $result[$row->fk_price_level] = $productCombinationLevel; @@ -1064,7 +1064,7 @@ class ProductCombinationLevel $this->fk_product_attribute_combination = doubleval($obj->fk_product_attribute_combination); $this->fk_price_level = intval($obj->fk_price_level); $this->variation_price = doubleval($obj->variation_price); - $this->variation_price_percentage = (bool)$obj->variation_price_percentage; + $this->variation_price_percentage = (bool) $obj->variation_price_percentage; return 1; } @@ -1080,41 +1080,41 @@ class ProductCombinationLevel $errors = 0; - if(empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){ + if (empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){ return -1; } // check if level exist in DB before add - if(empty($this->id)){ + if (empty($this->id)){ $sql = "SELECT rowid id" ." FROM ".MAIN_DB_PREFIX . $this->table_element ." WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination .' AND fk_price_level = '.intval($this->fk_price_level); $existObj = $this->db->getRow($sql); - if($existObj){ + if ($existObj){ $this->id = $existObj->id; } } // Update - if(!empty($this->id)) { + if (!empty($this->id)) { $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET ' . ' variation_price = '.doubleval($this->variation_price) . ' , variation_price_percentage = '.intval($this->variation_price_percentage) . ' WHERE rowid = '.intval($this->id); $res = $this->db->query($sql); - if($res>0){ + if ($res>0){ return $this->id; } - else{ + else { $this->error = $this->db->error(); $this->errors[] = $this->error; return -1; } } - else{ + else { // ADD $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (" . " fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage" @@ -1126,10 +1126,10 @@ class ProductCombinationLevel . " )"; $res = $this->db->query($sql); - if($res){ + if ($res){ $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); } - else{ + else { $this->error = $this->db->error(); $this->errors[] = $this->error; return -1; @@ -1186,5 +1186,4 @@ class ProductCombinationLevel return $res ? 1 : -1; } - } diff --git a/htdocs/variants/combinations.php b/htdocs/variants/combinations.php index 5ad7b38af2e..b9f62bdcf73 100644 --- a/htdocs/variants/combinations.php +++ b/htdocs/variants/combinations.php @@ -118,11 +118,11 @@ if ($_POST) { $price_impact = price2num($price_impact); // for conf PRODUIT_MULTIPRICES - if($conf->global->PRODUIT_MULTIPRICES) { + if ($conf->global->PRODUIT_MULTIPRICES) { $level_price_impact = array_map('price2num', $level_price_impact); $level_price_impact_percent = array_map('price2num', $level_price_impact_percent); } - else{ + else { $level_price_impact = array(1 => $weight_impact); $level_price_impact_percent = array(1 => $price_impact_percent); } @@ -243,19 +243,20 @@ if ($_POST) { $prodcomb->variation_weight = $weight_impact; // for conf PRODUIT_MULTIPRICES - if($conf->global->PRODUIT_MULTIPRICES) { + if ($conf->global->PRODUIT_MULTIPRICES) { $level_price_impact = array_map('price2num', $level_price_impact); - $level_price_impact_percent = array_map(function($a){ return !empty($a);}, $level_price_impact_percent); + $level_price_impact_percent = array_map(function ($a) { + return !empty($a);}, $level_price_impact_percent); $prodcomb->variation_price = $level_price_impact[1]; - $prodcomb->variation_price_percentage = (bool)$level_price_impact_percent[1]; + $prodcomb->variation_price_percentage = (bool) $level_price_impact_percent[1]; } - else{ + else { $level_price_impact = array(1 => $weight_impact); $level_price_impact_percent = array(1 => $price_impact_percent); } - if($conf->global->PRODUIT_MULTIPRICES){ + if ($conf->global->PRODUIT_MULTIPRICES){ $prodcomb->combination_price_levels = array(); for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ $productCombinationLevel = new ProductCombinationLevel($db); @@ -634,23 +635,22 @@ if (!empty($id) || !empty($ref)) -global->PRODUIT_MULTIPRICES)){ ?> + global->PRODUIT_MULTIPRICES)){ ?> > -fetchCombinationPriceLevels(); for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) { - print ''; - print ''; - if($i===1){ + print ''; + if ($i===1){ print ' ('.$langs->trans('ApplyToAllPriceImpactLevel').')'; } print ''; @@ -672,7 +672,7 @@ if (!empty($id) || !empty($ref)) } if (!empty($conf->global->PRODUIT_MULTIPRICES)){ -?> + ?> - + ?>
value="trans('Create') : $langs->trans('Save') ?>" class="button">   From 4e1f554c15b0368a43fa2eb56fca821801d9cea5 Mon Sep 17 00:00:00 2001 From: John Botella Date: Thu, 13 Aug 2020 11:43:33 +0200 Subject: [PATCH 3/4] Fix - add variation price fallback behavior --- .../class/ProductCombination.class.php | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/htdocs/variants/class/ProductCombination.class.php b/htdocs/variants/class/ProductCombination.class.php index 777cb136b05..0b3b3843867 100644 --- a/htdocs/variants/class/ProductCombination.class.php +++ b/htdocs/variants/class/ProductCombination.class.php @@ -138,6 +138,8 @@ class ProductCombination */ public function fetchCombinationPriceLevels($fk_price_level = 0, $useCache = true) { + global $conf; + // Check cache if(!empty($this->combination_price_levels) && $useCache){ if((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ @@ -154,10 +156,31 @@ class ProductCombination $staticProductCombinationLevel = new ProductCombinationLevel($this->db); $combination_price_levels = $staticProductCombinationLevel->fetchAll($this->id, $fk_price_level); - if (!$combination_price_levels || !is_array($combination_price_levels)) { + if (!is_array($combination_price_levels)) { return -1; } + if(empty($combination_price_levels)){ + + /** + * for auto retrocompatibility with last behavior + */ + $productCombinationLevel = new ProductCombinationLevel($this->db); + $productCombinationLevel->fk_price_level = intval($fk_price_level); + $productCombinationLevel->fk_product_attribute_combination = $this->id; + $productCombinationLevel->variation_price = $this->variation_price; + $productCombinationLevel->variation_price_percentage = $this->variation_price_percentage; + + if($fk_price_level>0){ + $combination_price_levels[$fk_price_level] = $productCombinationLevel; + } + else{ + for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ + $combination_price_levels[$i] = $productCombinationLevel; + } + } + } + $this->combination_price_levels = $combination_price_levels; return 1; @@ -1032,17 +1055,19 @@ class ProductCombinationLevel $combination_price_levels = $this->db->getRows($sql); - if (!$combination_price_levels || !is_array($combination_price_levels)) { + if (!is_array($combination_price_levels)) { return -1; } $result = array(); - // For more simple usage set level as array key - foreach($combination_price_levels as $k => $row){ - $productCombinationLevel = new ProductCombinationLevel($this->db); - $productCombinationLevel->fetchFormObj($row); - $result[$row->fk_price_level] = $productCombinationLevel; + if(!empty($combination_price_levels)) { + // For more simple usage set level as array key + foreach($combination_price_levels as $k => $row){ + $productCombinationLevel = new ProductCombinationLevel($this->db); + $productCombinationLevel->fetchFormObj($row); + $result[$row->fk_price_level] = $productCombinationLevel; + } } return $result; From 959ccce7770fe1174a07b31991f5a15e478c34c5 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 13 Aug 2020 09:48:28 +0000 Subject: [PATCH 4/4] Fixing style errors. --- .../class/ProductCombination.class.php | 73 +++++++++---------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/htdocs/variants/class/ProductCombination.class.php b/htdocs/variants/class/ProductCombination.class.php index 0b3b3843867..7d6a07d736d 100644 --- a/htdocs/variants/class/ProductCombination.class.php +++ b/htdocs/variants/class/ProductCombination.class.php @@ -141,13 +141,13 @@ class ProductCombination global $conf; // Check cache - if(!empty($this->combination_price_levels) && $useCache){ - if((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ + if (!empty($this->combination_price_levels) && $useCache){ + if ((!empty($fk_price_level) && isset($this->combination_price_levels[$fk_price_level])) || empty($fk_price_level)){ return 1; } } - if(!is_array($this->combination_price_levels) + if (!is_array($this->combination_price_levels) || empty($fk_price_level) // if fetch an unique level dont erase all already fetched ){ $this->combination_price_levels = array(); @@ -160,7 +160,7 @@ class ProductCombination return -1; } - if(empty($combination_price_levels)){ + if (empty($combination_price_levels)){ /** * for auto retrocompatibility with last behavior @@ -171,10 +171,10 @@ class ProductCombination $productCombinationLevel->variation_price = $this->variation_price; $productCombinationLevel->variation_price_percentage = $this->variation_price_percentage; - if($fk_price_level>0){ + if ($fk_price_level>0){ $combination_price_levels[$fk_price_level] = $productCombinationLevel; } - else{ + else { for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ $combination_price_levels[$i] = $productCombinationLevel; } @@ -201,15 +201,15 @@ class ProductCombination $staticProductCombinationLevel = new ProductCombinationLevel($this->db); // Delete all - if(empty($this->combination_price_levels)){ + if (empty($this->combination_price_levels)){ return $staticProductCombinationLevel->deleteAllForCombination($this->id); } // Clean not needed price levels - if($clean){ + if ($clean){ $res = $staticProductCombinationLevel->clean($this->id); - if($res<0){ + if ($res<0){ $this->errors[] = 'Fail to clean not needed price levels'; return -1; } @@ -217,17 +217,17 @@ class ProductCombination foreach ($this->combination_price_levels as $fk_price_level => $combination_price_level){ $res = $combination_price_level->save(); - if($res<1){ + if ($res<1){ $this->error = 'save combination price level '.$fk_price_level . ' '.$combination_price_level->error; $this->errors[] = $this->error; $errors ++; } } - if($errors > 0){ + if ($errors > 0){ return $errors*-1; } - else{ + else { return 1; } } @@ -354,7 +354,7 @@ class ProductCombination if (!empty($conf->global->PRODUIT_MULTIPRICES)) { $res = $this->saveCombinationPriceLevels(); - if($res<0){ + if ($res<0){ return -2; } } @@ -388,7 +388,7 @@ class ProductCombination if (!empty($conf->global->PRODUIT_MULTIPRICES)) { $res = $this->saveCombinationPriceLevels(); - if($res<0){ + if ($res<0){ return -2; } } @@ -415,7 +415,7 @@ class ProductCombination $comb2val->deleteByFkCombination($this->id); // remove combination price levels - if(!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) { + if (!$this->db->query("DELETE FROM ".MAIN_DB_PREFIX."product_attribute_combination_price_level WHERE fk_product_attribute_combination = ".(int) $this->id)) { $this->db->rollback(); return -1; } @@ -705,10 +705,10 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $weight_impact = (float) $forced_weightvar; // If false, return 0 //Final price impact - if(!is_array($forced_pricevar)){ + if (!is_array($forced_pricevar)){ $price_impact[1] = (float) $forced_pricevar; // If false, return 0 } - else{ + else { $price_impact = $forced_pricevar; } @@ -755,7 +755,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $price_impact[1] += (float) price2num($variations[$currcombattr][$currcombval]['price']); // Manage Price levels - if($conf->global->PRODUIT_MULTIPRICES){ + if ($conf->global->PRODUIT_MULTIPRICES){ for ($i = 2; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) { $price_impact[$i] += (float) price2num($variations[$currcombattr][$currcombval]['price']); @@ -783,16 +783,16 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; $newcomb->variation_weight = $weight_impact; // Init price level - if($conf->global->PRODUIT_MULTIPRICES){ + if ($conf->global->PRODUIT_MULTIPRICES){ for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++){ $productCombinationLevel = new ProductCombinationLevel($this->db); $productCombinationLevel->fk_product_attribute_combination = 0; $productCombinationLevel->fk_price_level = $i; $productCombinationLevel->variation_price = $price_impact[$i]; - if(is_array($price_var_percent)){ + if (is_array($price_var_percent)){ $productCombinationLevel->variation_price_percentage = !empty($price_var_percent[$i]) ? $price_var_percent[$i] : 0; - }else{ + }else { $productCombinationLevel->variation_price_percentage = $price_var_percent; } @@ -1023,11 +1023,11 @@ class ProductCombinationLevel */ public function fetch($rowid) { - $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int)$rowid; + $sql = "SELECT rowid, fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage FROM " . MAIN_DB_PREFIX . $this->table_element." WHERE rowid = " . (int) $rowid; $obj = $this->db->getRow($sql); - if($obj){ + if ($obj){ return $this->fetchFormObj($obj); } @@ -1049,7 +1049,7 @@ class ProductCombinationLevel ." FROM ".MAIN_DB_PREFIX.$this->table_element ." WHERE fk_product_attribute_combination = ".intval($fk_product_attribute_combination); - if(!empty($fk_price_level)){ + if (!empty($fk_price_level)){ $sql.= ' AND fk_price_level = '.intval($fk_price_level); } @@ -1061,9 +1061,9 @@ class ProductCombinationLevel $result = array(); - if(!empty($combination_price_levels)) { + if (!empty($combination_price_levels)) { // For more simple usage set level as array key - foreach($combination_price_levels as $k => $row){ + foreach ($combination_price_levels as $k => $row){ $productCombinationLevel = new ProductCombinationLevel($this->db); $productCombinationLevel->fetchFormObj($row); $result[$row->fk_price_level] = $productCombinationLevel; @@ -1089,7 +1089,7 @@ class ProductCombinationLevel $this->fk_product_attribute_combination = doubleval($obj->fk_product_attribute_combination); $this->fk_price_level = intval($obj->fk_price_level); $this->variation_price = doubleval($obj->variation_price); - $this->variation_price_percentage = (bool)$obj->variation_price_percentage; + $this->variation_price_percentage = (bool) $obj->variation_price_percentage; return 1; } @@ -1105,41 +1105,41 @@ class ProductCombinationLevel $errors = 0; - if(empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){ + if (empty($this->fk_product_attribute_combination) || empty($this->fk_price_level)){ return -1; } // check if level exist in DB before add - if(empty($this->id)){ + if (empty($this->id)){ $sql = "SELECT rowid id" ." FROM ".MAIN_DB_PREFIX . $this->table_element ." WHERE fk_product_attribute_combination = ".(int) $this->fk_product_attribute_combination .' AND fk_price_level = '.intval($this->fk_price_level); $existObj = $this->db->getRow($sql); - if($existObj){ + if ($existObj){ $this->id = $existObj->id; } } // Update - if(!empty($this->id)) { + if (!empty($this->id)) { $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET ' . ' variation_price = '.doubleval($this->variation_price) . ' , variation_price_percentage = '.intval($this->variation_price_percentage) . ' WHERE rowid = '.intval($this->id); $res = $this->db->query($sql); - if($res>0){ + if ($res>0){ return $this->id; } - else{ + else { $this->error = $this->db->error(); $this->errors[] = $this->error; return -1; } } - else{ + else { // ADD $sql = "INSERT INTO " . MAIN_DB_PREFIX . $this->table_element . " (" . " fk_product_attribute_combination, fk_price_level, variation_price, variation_price_percentage" @@ -1151,10 +1151,10 @@ class ProductCombinationLevel . " )"; $res = $this->db->query($sql); - if($res){ + if ($res){ $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element); } - else{ + else { $this->error = $this->db->error(); $this->errors[] = $this->error; return -1; @@ -1211,5 +1211,4 @@ class ProductCombinationLevel return $res ? 1 : -1; } - }