From a17bf5b650fe8b525996a44f72c56849832a7552 Mon Sep 17 00:00:00 2001 From: rGaillard Date: Tue, 16 Oct 2012 17:48:30 +0000 Subject: [PATCH] [-] Core: fix cache product group reduction #PSCFV-4974 --- classes/Category.php | 2 +- classes/GroupReduction.php | 72 ++++++++++++++++++++++++-------------- classes/Product.php | 14 ++------ 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/classes/Category.php b/classes/Category.php index 7abc0eb507650..523ae1b4eacb6 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -332,7 +332,7 @@ public function delete() CartRule::cleanProductRuleIntegrity('categories', array($cat->id)); Category::cleanPositions($cat->id_parent); /* Delete Categories in GroupReduction */ - if (GroupReduction::getGroupReductionByCategoryId((int)$cat->id)) + if (GroupReduction::getGroupsReductionByCategoryId((int)$cat->id)) GroupReduction::deleteCategory($cat->id); } } diff --git a/classes/GroupReduction.php b/classes/GroupReduction.php index 57d136184f398..66fd73a1f30fa 100644 --- a/classes/GroupReduction.php +++ b/classes/GroupReduction.php @@ -149,41 +149,57 @@ public static function doesExist($id_group, $id_category) WHERE `id_group` = '.(int)$id_group.' AND `id_category` = '.(int)$id_category); } + public static function getGroupsByCategoryId($id_category) + { + return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT gr.`id_group` as id_group, gr.`reduction` as reduction, id_group_reduction + FROM `'._DB_PREFIX_.'group_reduction` gr + WHERE `id_category` = '.(int)$id_category + ); + } + public static function getGroupByCategoryId($id_category) { + Tools::displayAsDeprecated('Use GroupReduction::getGroupsByCategoryId($id_category)'); return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT gr.`id_group` as id_group, gr.`reduction` as reduction + SELECT gr.`id_group` as id_group, gr.`reduction` as reduction, id_group_reduction FROM `'._DB_PREFIX_.'group_reduction` gr - WHERE `id_category` = '.(int)$id_category, - false); + WHERE `id_category` = '.(int)$id_category + , false); } + public static function getGroupsReductionByCategoryId($id_category) + { + return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT gr.`id_group_reduction` as id_group_reduction, id_group + FROM `'._DB_PREFIX_.'group_reduction` gr + WHERE `id_category` = '.(int)$id_category + ); + } + public static function getGroupReductionByCategoryId($id_category) { + Tools::displayAsDeprecated('Use GroupReduction::getGroupsByCategoryId($id_category)'); return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT gr.`id_group_reduction` as id_group_reduction FROM `'._DB_PREFIX_.'group_reduction` gr - WHERE `id_category` = '.(int)$id_category, - false); + WHERE `id_category` = '.(int)$id_category + , false); } - public static function setProductReduction($id_product, $id_group, $id_category, $reduction) + public static function setProductReduction($id_product, $id_group = null, $id_category, $reduction = null) { - Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT pgr.`id_product`, pgr.`id_group`, pgr.`reduction` - FROM `'._DB_PREFIX_.'product_group_reduction_cache` pgr - WHERE pgr.`id_product` = '.(int)$id_product - ); - - if (Db::getInstance()->NumRows() == 0) - $query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) - VALUES ('.(int)$id_product.', '.(int)$id_group.', '.(float)$reduction.')'; - else - $query = 'UPDATE `'._DB_PREFIX_.'product_group_reduction_cache` - SET `reduction` = '.(float)$reduction.' - WHERE `id_product` = '.(int)$id_product.' AND `id_group` = '.(int)$id_group; + GroupReduction::deleteProductReduction((int)$id_product); + $reductions = GroupReduction::getGroupsByCategoryId((int)$id_category); + if ($reductions) + { + $res = true; + foreach ($reductions as $reduction) + $res &= Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) + VALUES ('.(int)$id_product.', '.(int)$reduction['id_group'].', '.(float)$reduction['reduction'].')'); + } - return (Db::getInstance()->execute($query)); + return $res; } public static function deleteProductReduction($id_product) @@ -196,23 +212,25 @@ public static function deleteProductReduction($id_product) public static function duplicateReduction($id_product_old, $id_product) { - $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executes(' SELECT pgr.`id_product`, pgr.`id_group`, pgr.`reduction` FROM `'._DB_PREFIX_.'product_group_reduction_cache` pgr WHERE pgr.`id_product` = '.(int)$id_product_old ); - if (!$row) + if (!$res) return true; - - $query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) VALUES '; - $query .= '('.(int)$id_product.', '.(int)$row['id_group'].', '.(float)$row['reduction'].')'; - return (Db::getInstance()->execute($query)); + foreach ($res as $row) + { + $query = 'INSERT INTO `'._DB_PREFIX_.'product_group_reduction_cache` (`id_product`, `id_group`, `reduction`) VALUES '; + $query .= '('.(int)$id_product.', '.(int)$row['id_group'].', '.(float)$row['reduction'].')'; + } + return Db::getInstance()->execute($query); } public static function deleteCategory($id_category) { $query = 'DELETE FROM `'._DB_PREFIX_.'group_reduction` WHERE `id_category` = '.(int)$id_category; - if (Db::getInstance()->execute($query) === false) + if (Db::getInstance()->Execute($query) === false) return false; return true; } diff --git a/classes/Product.php b/classes/Product.php index a05bf469529d8..8c7f2bc9272ca 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -467,6 +467,7 @@ public function add($autodate = true, $null_values = false) else StockAvailable::setProductOutOfStock((int)$this->id, 2); + $this->setGroupReduction(); Hook::exec('actionProductSave', array('id_product' => $this->id)); return true; } @@ -474,6 +475,7 @@ public function add($autodate = true, $null_values = false) public function update($null_values = false) { $return = parent::update($null_values); + $this->setGroupReduction(); Hook::exec('actionProductSave', array('id_product' => $this->id)); return $return; } @@ -803,8 +805,6 @@ public function updateCategories($categories, $keeping_current_pos = false) if (!$this->addToCategories($categories)) return false; - if (!$this->setGroupReduction()) - return false; SpecificPriceRule::applyAllRules(array((int)$this->id)); return true; } @@ -4522,15 +4522,7 @@ public static function resetEcoTax() */ public function setGroupReduction() { - $row = GroupReduction::getGroupByCategoryId($this->id_category_default); - if (!$row) // Remove - { - if (!GroupReduction::deleteProductReduction($this->id)) - return false; - } - else if (!GroupReduction::setProductReduction($this->id, $row['id_group'], $this->id_category_default, (float)$row['reduction'])) - return false; - return true; + return GroupReduction::setProductReduction($this->id, null, $this->id_category_default); } /**