From 7f830c2f4eefb1129ae4acc7de4776b0fc18b7c8 Mon Sep 17 00:00:00 2001 From: dMetzger Date: Wed, 2 Nov 2011 10:47:01 +0000 Subject: [PATCH] // Retro compatibility for Discount --- classes/Cart.php | 6 +- classes/CartRule.php | 66 ++++- classes/Discount.php | 609 +++++++++----------------------------- classes/Group.php | 3 - classes/OrderDiscount.php | 10 +- classes/PaymentModule.php | 2 +- 6 files changed, 215 insertions(+), 481 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 13caae673..ce2d69f7e 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -286,8 +286,8 @@ class CartCore extends ObjectModel foreach ($result as &$row) { $cartRule = new CartRule($row['id_cart_rule'], (int)$this->id_lang); - $row['value_real'] = $cartRule->getValue(true); - $row['value_tax_exc'] = $cartRule->getValue(false); + $row['value_real'] = $cartRule->getContextualValue(true); + $row['value_tax_exc'] = $cartRule->getContextualValue(false); // Retro compatibility < 1.5.0.2 $row['id_discount'] = $row['id_cart_rule']; @@ -1032,7 +1032,7 @@ class CartCore extends ObjectModel { $result = $this->getCartRules(); foreach (ObjectModel::hydrateCollection('CartRule', $result, Configuration::get('PS_LANG_DEFAULT')) AS $cartRule) - $order_total_discount += Tools::ps_round($cartRule->getValue($withTaxes)); + $order_total_discount += Tools::ps_round($cartRule->getContextualValue($withTaxes)); $order_total_discount = min(Tools::ps_round($order_total_discount), $wrapping_fees + $order_total_products + $shipping_fees); $order_total -= $order_total_discount; } diff --git a/classes/CartRule.php b/classes/CartRule.php index cd194d3cd..17b8ef107 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -166,6 +166,70 @@ class CartRuleCore extends ObjectModel return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `id_cart_rule` FROM `'._DB_PREFIX_.'cart_rule` WHERE `code` = \''.pSQL($code).'\''); } + public static function getCustomerCartRules($id_lang, $id_customer, $active = false, $includeGeneric = true, $inStock = false, Cart $cart = null) + { + if (!CartRule::isFeatureActive()) + return array(); + + Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'cart_rule` cr + LEFT JOIN `'._DB_PREFIX_.'cart_rule_lang` crl ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = '.(int)$id_lang.') + WHERE ( + cr.`id_customer` = '.(int)$id_customer.' + '.($includeGeneric ? 'OR cr.`id_customer` = 0' : '').' + ) + '.($active ? 'AND cr.`active` = 1' : '').' + '.($inStock ? 'AND cr.`quantity` > 0' : '')); + + // Remove cart rule that does not match the customer groups + if ($includeGeneric) + { + $customerGroups = Customer::getGroupsStatic($id_customer); + foreach ($result as $key => $cart_rule) + if ($cart_rule['group_restriction']) + { + $cartRuleGroups = Db::getInstance()->getValue('SELECT id_group FROM '._DB_PREFIX_.'cart_rule_group WHERE id_cart_rule = '.(int)$cart_rule['id_cart_rule']); + foreach ($cartRuleGroups as $cartRuleGroup) + if (in_array($cartRuleGroups['id_group'], $customerGroups)) + continue 2; + unset($result[$key]); + } + } + return $result; + } + + public function usedByCustomer($id_customer) + { + return (bool)Db::getInstance()->getValue(' + SELECT id_cart_rule + FROM `'._DB_PREFIX_.'order_cart_rule` ocr + LEFT JOIN `'._DB_PREFIX_.'orders` o ON ocr.`id_order` = o.`id_order` + WHERE ocr.`id_cart_rule` = '.(int)$this->id.' + AND o.`id_customer` = '.(int)$id_customer); + } + + public static function cartRuleExists($name) + { + if (!CartRule::isFeatureActive()) + return false; + + return (bool)Db::getInstance()->getValue(' + SELECT `id_cart_rule` + FROM `'._DB_PREFIX_.'cart_rule` + WHERE `code` = \''.pSQL($name).'\''); + } + + public static function deleteByIdCustomer($id_customer) + { + $return = true; + $result = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'cart_rule` WHERE `id_customer` = '.(int)$id_customer); + $cartRules = ObjectModel::hydrateCollection('CartRule', $result); + foreach ($cartRules as $cartRule) + $return &= $cartRule->delete(); + return $return; + } + public function getProductRules() { if (!Validate::isLoadedObject($this) OR $this->product_restriction == 0) @@ -353,7 +417,7 @@ class CartRuleCore extends ObjectModel } // The reduction value is POSITIVE - public function getValue($useTax, Context $context = NULL) + public function getContextualValue($useTax, Context $context = NULL) { if (!CartRule::isFeatureActive()) return 0; diff --git a/classes/Discount.php b/classes/Discount.php index 158273e74..80207d792 100644 --- a/classes/Discount.php +++ b/classes/Discount.php @@ -25,288 +25,154 @@ * International Registered Trademark & Property of PrestaShop SA */ +/** + * @deprecated 1.5.0.1 + */ class DiscountCore extends CartRule { - public $id; - - /** @var integer Customer id only if discount is reserved */ - public $id_customer; - - /** @var integer Group id only if discount is reserved */ - public $id_group; - - /** @var integer Currency ID only if the discount type is 2 */ - public $id_currency; - - /** @var integer Discount type ID */ - public $id_discount_type; - - /** @var string Name (the one which must be entered) */ - public $name; - - /** @var string A short description for the discount */ - public $description; - - /** @var string Value in percent as well as in euros */ - public $value; - - /** @var integer Totale quantity available */ - public $quantity; - - /** @var integer User quantity available */ - public $quantity_per_user; - - /** @var boolean Indicate if discount is cumulable with others */ - public $cumulable; - - /** @var integer Indicate if discount is cumulable with already bargained products */ - public $cumulable_reduction; - - /** @var integer Date from wich discount become active */ - public $date_from; - - /** @var integer Date from wich discount is no more active */ - public $date_to; - - /** @var integer Minimum cart total amount required to use the discount */ - public $minimal; - - /** @var boolean include_tax selected for the choice of the calcul method in the cart*/ - public $include_tax; - - /** @var integer display the discount in the summary */ - public $cart_display; - - public $behavior_not_exhausted; - - /** @var boolean Status */ - public $active = true; - - /** @var string Object creation date */ - public $date_add; - - /** @var string Object last modification date */ - public $date_upd; - - protected $fieldsRequired = array('id_discount_type', 'name', 'value', 'quantity', 'quantity_per_user', 'date_from', 'date_to'); - protected $fieldsSize = array('name' => '32', 'date_from' => '32', 'date_to' => '32'); - protected $fieldsValidate = array('id_customer' => 'isUnsignedId', 'id_group' => 'isUnsignedId', 'id_discount_type' => 'isUnsignedId', 'id_currency' => 'isUnsignedId', - 'name' => 'isDiscountName', 'value' => 'isPrice', 'quantity' => 'isUnsignedInt', 'quantity_per_user' => 'isUnsignedInt', - 'cumulable' => 'isBool', 'cumulable_reduction' => 'isBool', 'date_from' => 'isDate', - 'date_to' => 'isDate', 'minimal' => 'isUnsignedFloat', 'active' => 'isBool'); - protected $fieldsRequiredLang = array('description'); - protected $fieldsSizeLang = array('description' => 128); - protected $fieldsValidateLang = array('description' => 'isVoucherDescription'); - - protected $table = 'discount'; - protected $identifier = 'id_discount'; - - protected $webserviceParameters = array( - 'fields' => array( - 'id_discount_type' => array('sqlId' => 'id_discount_type', 'xlink_resource' => 'discount_types'), - 'id_customer' => array('sqlId' => 'id_customer', 'xlink_resource' => 'customers'), - 'id_group' => array('sqlId' => 'id_group', 'xlink_resource' => 'groups'), - 'id_currency' => array('sqlId' => 'id_currency', 'xlink_resource' => 'currencies'), - 'name' => array('sqlId' => 'name'), - 'value' => array('sqlId' => 'value'), - 'quantity' => array('sqlId' => 'quantity'), - 'quantity_per_user' => array('sqlId' => 'quantity_per_user'), - 'cumulable' => array('sqlId' => 'cumulable'), - 'cumulable_reduction' => array('sqlId' => 'cumulable_reduction'), - 'behavior_not_exhausted' => array('sqlId' => 'behavior_not_exhausted'), - 'date_from' => array('sqlId' => 'date_from'), - 'date_to' => array('sqlId' => 'date_to'), - 'minimal' => array('sqlId' => 'minimal'), - 'include_tax' => array('sqlId' => 'include_tax'), - 'active' => array('sqlId' => 'active'), - 'cart_display' => array('sqlId' => 'cart_display'), - 'date_add' => array('sqlId' => 'date_add'), - 'date_upd' => array('sqlId' => 'date_upd') - ) - ); - const PERCENT = 1; const AMOUNT = 2; const FREE_SHIPPING = 3; - - public function getFields() + + public function __get($key) { - $this->validateFields(); + Tools::displayAsDeprecated(); + + if ($key == 'id_group') + return 0; + if ($key == 'id_discount_type') + { + if ($this->free_shipping) + return Discount::FREE_SHIPPING; + if ($this->reduction_percent > 0) + return Discount::PERCENT; + if ($this->reduction_amount > 0) + return Discount::AMOUNT; + } + if ($key == 'name') + return $this->code; + if ($key == 'value') + { + if ($this->reduction_percent > 0) + return $this->reduction_percent; + if ($this->reduction_amount > 0) + return $this->reduction_amount; + } + if ($key == 'cumulable') + return $this->cart_rule_restriction; + if ($key == 'cumulable_reduction') + return false; + if ($key == 'minimal') + return $this->minimum_amount; + if ($key == 'include_tax') + return $this->reduction_tax; + if ($key == 'behavior_not_exhausted') + return $this->partial_use; + if ($key == 'cart_display') + return true; - $fields['id_customer'] = (int)($this->id_customer); - $fields['id_group'] = (int)($this->id_group); - $fields['id_currency'] = (int)($this->id_currency); - $fields['id_discount_type'] = (int)($this->id_discount_type); - $fields['name'] = pSQL($this->name); - $fields['value'] = (float)($this->value); - $fields['quantity'] = (int)($this->quantity); - $fields['quantity_per_user'] = (int)($this->quantity_per_user); - $fields['cumulable'] = (int)($this->cumulable); - $fields['cumulable_reduction'] = (int)($this->cumulable_reduction); - $fields['date_from'] = pSQL($this->date_from); - $fields['date_to'] = pSQL($this->date_to); - $fields['minimal'] = (float)($this->minimal); - $fields['include_tax'] = (int)($this->include_tax); - $fields['behavior_not_exhausted'] = (int)$this->behavior_not_exhausted; - $fields['active'] = (int)($this->active); - $fields['cart_display'] = (int)($this->cart_display); - $fields['date_add'] = pSQL($this->date_add); - $fields['date_upd'] = pSQL($this->date_upd); - return $fields; + return $this->{$key}; + } + + public function __set($key, $value) + { + Tools::displayAsDeprecated(); + + if ($key == 'id_discount_type') + { + if ($value == Discount::FREE_SHIPPING) + { + $this->free_shipping = true; + $this->reduction_percent = false; + $this->reduction_amount = false; + } + if ($value == Discount::PERCENT) + { + $this->free_shipping = false; + $this->reduction_percent = true; + $this->reduction_amount = false; + } + if ($value == Discount::AMOUNT) + { + $this->free_shipping = false; + $this->reduction_percent = false; + $this->reduction_amount = true; + } + } + + if ($key == 'code') + $this->name[Configuration::get('PS_LANG_DEFAULT')] = $value; + + if ($key == 'value') + { + if ($this->reduction_percent) + $this->reduction_percent = $value; + if ($this->reduction_amount) + $this->reduction_amount = $value; + } + if ($key == 'cumulable') + $this->cart_rule_restriction = 1; + if ($key == 'minimal') + $this->minimum_amount = $value; + if ($key == 'include_tax') + $this->reduction_tax = $value; + if ($key == 'behavior_not_exhausted') + $this->partial_use = $value; + + $this->{$key} = $value; + } + + public function __call($method, $args) + { + Tools::displayAsDeprecated(); + $obj = $this->parent; + if (in_array($method, array('add', 'update', 'getIdByName', 'getCustomerDiscounts', 'getValue', 'discountExists', 'createOrderDiscount', 'getVouchersToCartDisplay'))) + $obj = $this; + return call_user_func_array(array(obj, $method), $args); } + /** + * @deprecated 1.5.0.1 + */ public function add($autodate = true, $nullValues = false, $categories = null) { - if (parent::add($autodate, $nullValues)) - { - $this->updateCategories($categories); - - // Set cache of feature detachable to true - Configuration::updateGlobalValue('PS_DISCOUNT_FEATURE_ACTIVE', '1'); - return true; - } - return false; + $r = parent::add($autodate, $nullValues); + // Todo : manage categories + return $r; } - - /* Categories initialization is different between add() and update() because the addition will set all categories if none are selected (compatibility with old modules) and update won't update categories if none are selected */ - public function update($autodate = true, $nullValues = false, $categories = false) - { - $ret = NULL; - if (parent::update($autodate, $nullValues)) - $ret = true; - - $this->updateCategories($categories); - return $ret; - } - - public function delete() - { - if (!parent::delete()) - return false; - - // Refresh cache of feature detachable - Configuration::updateGlobalValue('PS_DISCOUNT_FEATURE_ACTIVE', self::isCurrentlyUsed($this->table, true)); - - return (Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'cart_cart_rule WHERE id_discount = '.(int)($this->id)) && - Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'discount_category WHERE id_discount = '.(int)($this->id))); - } - - public function getTranslationsFieldsChild() - { - if (!$this->validateFieldsLang()) - return false; - return $this->getTranslationsFields(array('description')); - } - + /** - * Return discount types list - * - * @return array Discount types + * @deprecated 1.5.0.1 */ - public static function getDiscountTypes($id_lang) + public function update($autodate = true, $nullValues = false, $categories = null) { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT * - FROM '._DB_PREFIX_.'discount_type dt - LEFT JOIN `'._DB_PREFIX_.'discount_type_lang` dtl ON (dt.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = '.(int)($id_lang).')'); + $r = parent::update($autodate, $nullValues); + // Todo : manage categories + return $r; } /** - * Get discount ID from name - * - * @param string $discountName Discount name - * @return integer Discount ID + * @deprecated 1.5.0.1 */ - public static function getIdByName($discountName) + public static function getIdByName($code) { - if (!self::isFeatureActive()) - return 0; - - if (!Validate::isDiscountName($discountName)) - die(Tools::displayError()); - - return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' - SELECT `id_discount` - FROM `'._DB_PREFIX_.'discount` - WHERE `name` = \''.pSQL($discountName).'\''); + return parent::getIdByCode($code); } /** - * - * This method allow to get the customer discount - * @param int $id_lang - * @param int $id_customer - * @param bool $active - * @param bool $includeGenericOnes include the discount available for all customers - * @param bool $hasStock - * @param Cart $cart - */ + * @deprecated 1.5.0.1 + */ public static function getCustomerDiscounts($id_lang, $id_customer, $active = false, $includeGenericOnes = true, $hasStock = false, Cart $cart = null) { - if (!self::isFeatureActive()) - return array(); - - if (!$cart) - $cart = Context::getContext()->cart; - - $sql = ' - SELECT d.*, dtl.`name` AS `type`, dl.`description` - FROM `'._DB_PREFIX_.'discount` d - LEFT JOIN `'._DB_PREFIX_.'discount_lang` dl ON (d.`id_discount` = dl.`id_discount` AND dl.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'discount_type` dt ON dt.`id_discount_type` = d.`id_discount_type` - LEFT JOIN `'._DB_PREFIX_.'discount_type_lang` dtl ON (dt.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = '.(int)($id_lang).') - WHERE (d.`id_customer` = '.(int)$id_customer.' - '; - - // Group clause - if (Group::isFeatureActive()) - $sql .= 'OR d.`id_group` IN ( - SELECT `id_group` - FROM `'._DB_PREFIX_.'customer_group` cg - WHERE cg.`id_customer` = '.(int)$id_customer.' - )'; - else - $sql .= 'OR d.`id_group` = 1'; - - if ($includeGenericOnes) - $sql .= 'OR (d.`id_customer` = 0 AND d.`id_group` = 0)'; - - $sql .= ')'; // close parenthsis openned befor d.`id_customer` - - if ($active) - $sql .= ' AND d.`active` = 1'; - - if ($hasStock) - $sql .= ' AND d.`quantity` != 0'; - - $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); - - foreach ($res as &$discount) - if ($discount['quantity_per_user']) - { - $quantity_used = Order::getDiscountsCustomer($id_customer, $discount['id_discount']); - if (isset($cart) AND $cart->id) - $quantity_used += $cart->getDiscountsCustomer((int)($discount['id_discount'])); - $discount['quantity_for_user'] = $discount['quantity_per_user'] - $quantity_used; - } - else - $discount['quantity_for_user'] = 0; - return $res; + return parent::getCustomerCartRules($id_lang, $id_customer, $active, $includeGenericOnes, $hasStock, $cart); } - + /** - * - * @param int $id_customer - * @return bool - */ - public function usedByCustomer($id_customer) + * @deprecated 1.5.0.1 + */ + public static function getVouchersToCartDisplay($id_lang, $id_customer) { - return (bool)Db::getInstance()->getValue(' - SELECT COUNT(*) - FROM `'._DB_PREFIX_.'order_cart_rule` od - LEFT JOIN `'._DB_PREFIX_.'orders` o ON (od.`id_order` = o.`id_order`) - WHERE od.`id_discount` = '.(int)$this->id.' - AND o.`id_customer` = '.(int)$id_customer); + return array(); } /** @@ -314,133 +180,32 @@ class DiscountCore extends CartRule */ public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $id_cart = false, $useTax = true, Currency $currency = null, Shop $shop = null) { - Tools::displayAsDeprecated(); $context = Context::getContext(); - $context->cart = new Cart($id_cart); - return parent::getValue($useTax, $context); - if (!self::isFeatureActive()) - return 0; - - if (!$currency) - $currency = Context::getContext()->currency; - if (!$shop) - $shop = Context::getContext()->shop; - $totalAmount = 0; - $cart = new Cart($idCart); - if (!Validate::isLoadedObject($cart)) - return 0; - - if ((!$this->cumulable AND (int)$nb_discounts > 1) OR !$this->active OR (!$this->quantity AND !$cart->OrderExists())) - return 0; - - if ($this->usedByCustomer((int)$cart->id_customer) >= $this->quantity_per_user AND !$cart->OrderExists()) - return 0; - - $date_start = strtotime($this->date_from); - $date_end = strtotime($this->date_to); - if ((time() < $date_start OR time() > $date_end) AND !$cart->OrderExists()) return 0; - - if (!$this->isAssociatedToShop($shop->getID())) - return 0; - $products = $cart->getProducts(); - $categories = Discount::getCategories((int)$this->id); - - foreach ($products AS $product) - if (count($categories) AND Product::idIsOnCategoryId($product['id_product'], $categories)) - $totalAmount += $this->include_tax ? $product['total_wt'] : $product['total']; - if ($this->minimal > 0 AND $totalAmount < $this->minimal) - return 0; - switch ($this->id_discount_type) - { - /* Relative value (% of the order total) */ - case Discount::PERCENT: - $amount = 0; - $percentage = $this->value / 100; - foreach ($products AS $product) - if (Product::idIsOnCategoryId($product['id_product'], $categories)) - if ($this->cumulable_reduction OR (!$product['reduction_applies'] AND !$product['on_sale'])) - $amount += ($useTax? $product['total_wt'] : $product['total']) * $percentage; - return $amount; - - /* Absolute value */ - case Discount::AMOUNT: - // An "absolute" voucher is available in one currency only - $currency = ((int)$cart->id_currency ? Currency::getCurrencyInstance($cart->id_currency) : $currency); - if ($this->id_currency != $currency->id) - return 0; - - $taxDiscount = Cart::getTaxesAverageUsed((int)($cart->id)); - if (!$useTax AND isset($taxDiscount) AND $taxDiscount != 1) - $this->value = abs($this->value / (1 + $taxDiscount * 0.01)); - - // Main return - $value = 0; - foreach ($products AS $product) - if (Product::idIsOnCategoryId($product['id_product'], $categories)) - $value = $this->value; - // Return 0 if there are no applicable categories - return $value; - - /* Free shipping (does not return a value but a special code) */ - case Discount::FREE_SHIPPING: - return '!'; - } - return 0; + if ((int)$id_cart) + $context->cart = new Cart($id_cart); + if (Validate::isLoadedObject($currency)) + $context->currency = $currency; + if (Validate::isLoadedObject($shop)) + $context->shop = $shop; + return parent::getContextualValue($useTax, $context); } - public static function getCategories($id_discount) - { - return Db::getInstance()->executeS(' - SELECT `id_category` - FROM `'._DB_PREFIX_.'discount_category` - WHERE `id_discount` = '.(int)($id_discount)); - } - - public function updateCategories($categories) - { - /* false value will avoid category update and null value will force all category to be selected */ - if ($categories === false) - return ; - if ($categories === null) - { - // Compatibility for modules which create discount without setting categories (ex. fidelity, sponsorship) - $result = Db::getInstance()->executeS('SELECT id_category FROM '._DB_PREFIX_.'category'); - $categories = array(); - foreach ($result as $row) - $categories[] = $row['id_category']; - } - elseif (!is_array($categories) OR !sizeof($categories)) - return false; - Db::getInstance()->execute(' - DELETE FROM `'._DB_PREFIX_.'discount_category` - WHERE `id_discount`='.(int)$this->id); - foreach($categories AS $category) - { - Db::getInstance()->executeS(' - SELECT `id_discount` - FROM `'._DB_PREFIX_.'discount_category` - WHERE `id_discount`='.(int)($this->id).' AND `id_category`='.(int)($category)); - if (Db::getInstance()->NumRows() == 0) - Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'discount_category` (`id_discount`, `id_category`) - VALUES('.(int)($this->id).','.(int)($category).')'); - } - } - + /** + * @deprecated 1.5.0.1 + */ public static function discountExists($discountName, $id_discount = 0) { - if (!self::isFeatureActive()) - return false; - - return (bool)Db::getInstance()->getValue(' - SELECT `id_discount` - FROM `'._DB_PREFIX_.'discount` - WHERE `name` LIKE \''.pSQL($discountName).'\' - AND `id_discount` != '.(int)$id_discount); + return parent::cartRuleExists($discountName); } + /** + * @deprecated 1.5.0.1 + */ public static function createOrderDiscount($order, $productList, $qtyList, $name, $shipping_cost = false, $id_category = 0, $subcategory = 0) { + // Todo + die ('TODO'); + $languages = Language::getLanguages($order); $products = $order->getProducts(false, $productList, $qtyList); @@ -487,106 +252,6 @@ class DiscountCore extends CartRule return $voucher; } - - public static function display($discountValue, $discountType, $currency = false) - { - if ((float)($discountValue) AND (int)($discountType)) - { - if ($discountType == 1) - return $discountValue.chr(37); // ASCII #37 --> % (percent) - elseif ($discountType == 2) - return Tools::displayPrice($discountValue, $currency); - } - return ''; // return a string because it's a display method - } - - /** - * - * This method allows to get the vouchers can be shown in order page - * @param int $id_lang - * @param int $id_customer - * @return array contains discount - */ - public static function getVouchersToCartDisplay($id_lang, $id_customer = 0) - { - if (!CartRule::isFeatureActive()) - return array(); - - $sql = ' - SELECT d.`name`, dl.`description`, d.`id_discount` - FROM `'._DB_PREFIX_.'discount` d - LEFT JOIN `'._DB_PREFIX_.'discount_lang` dl ON (d.`id_discount` = dl.`id_discount`) - WHERE d.`active` = 1 - AND d.`date_from` <= \''.pSQL(date('Y-m-d H:i:s')).'\' AND d.`date_to` >= \''.pSQL(date('Y-m-d H:i:s')).'\' - AND dl.`id_lang` = '.(int)$id_lang.' - AND d.`cart_display` = 1 - AND d.`quantity` > 0 - AND ( - (d.`id_customer` = 0 AND d.`id_group` = 0)'; - - if ($id_customer) - { - $sql .= ' OR ('; - // adding id_customer clause - $sql .= 'd.`id_customer` = '.(int)$id_customer; - if (Group::isFeatureActive()) - $sql .= ' - OR d.`id_group` IN ( - SELECT cg.`id_group` - FROM `'._DB_PREFIX_.'customer_group` cg - WHERE cg.`id_customer` = '.(int)$id_customer.' - )'; - else - $sql .= ' OR d.`id_group` = 1'; - $sql .= ')'; - } - else - $sql .= ' OR d.`id_group` = 1'; - - $sql .= ')'; // close parenthesis openned above - - return Db::getInstance()->executeS($sql); - } - - public static function deleteByIdCustomer($id_customer) - { - $discounts = Db::getInstance()->executeS('SELECT `id_discount` FROM `'._DB_PREFIX_.'discount` WHERE `id_customer` = '.(int)($id_customer)); - foreach ($discounts as $discount) - { - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount` WHERE `id_discount` = '.(int)($discount['id_discount'])); - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount_category` WHERE `id_discount` = '.(int)($discount['id_discount'])); - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount_lang` WHERE `id_discount` = '.(int)($discount['id_discount'])); - } - return true; - } - - public static function deleteByIdGroup($id_group) - { - $discounts = Db::getInstance()->executeS('SELECT `id_discount` FROM `'._DB_PREFIX_.'discount` WHERE `id_group` = '.(int)($id_group)); - foreach ($discounts as $discount) - { - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount` WHERE `id_discount` = '.(int)($discount['id_discount'])); - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount_category` WHERE `id_discount` = '.(int)($discount['id_discount'])); - Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'discount_lang` WHERE `id_discount` = '.(int)($discount['id_discount'])); - } - return true; - } - - public static function getDiscount($id_discount) - { - return Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'discount` WHERE `id_discount` = '.(int)$id_discount); - } - - /** - * This metohd is allow to know if a feature is used or active - * @since 1.5.0.1 - * @return bool - */ - public static function isFeatureActive() - { - Tools::displayAsDeprecated(); - return CartRule::isFeatureActive(); - } public static function getVoucherByName($name, $id_lang) { diff --git a/classes/Group.php b/classes/Group.php index aa8e7207c..0da9148cc 100644 --- a/classes/Group.php +++ b/classes/Group.php @@ -172,7 +172,6 @@ class GroupCore extends ObjectModel Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'group_reduction` WHERE `id_group` = '.(int)$this->id); Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'product_group_reduction_cache` WHERE `id_group` = '.(int)$this->id); $this->truncateRestrictionsModules($this->id); - Discount::deleteByIdGroup((int)$this->id); // Refresh cache of feature detachable Configuration::updateGlobalValue('PS_GROUP_FEATURE_ACTIVE', self::isCurrentlyUsed()); @@ -194,8 +193,6 @@ class GroupCore extends ObjectModel 1) WHERE `id_default_group` = '.(int)$this->id); - Discount::deleteByIdGroup($this->id); - return true; } return false; diff --git a/classes/OrderDiscount.php b/classes/OrderDiscount.php index 37fd8fc42..2b493f9b2 100644 --- a/classes/OrderDiscount.php +++ b/classes/OrderDiscount.php @@ -29,6 +29,7 @@ class OrderDiscountCore extends OrderCartRule { public function __get($key) { + Tools::displayAsDeprecated(); if ($key == 'id_order_discount') return $this->id_order_cart_rule; if ($key == 'id_discount') @@ -38,10 +39,17 @@ class OrderDiscountCore extends OrderCartRule public function __set($key, $value) { + Tools::displayAsDeprecated(); if ($key == 'id_order_discount') $this->id_order_cart_rule = $value; if ($key == 'id_discount') $this->id_cart_rule = $value; $this->{$key} = $value; } -} + + public function __call($method, $args) + { + Tools::displayAsDeprecated(); + return call_user_func_array(array($this->parent, $method), $args); + } +} \ No newline at end of file diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index d0cdc9094..3e3f35f3c 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -250,7 +250,7 @@ abstract class PaymentModuleCore extends Module $cartRules = ObjectModel::hydrateCollection('CartRule', $result, (int)$order->id_lang); foreach ($cartRules AS $cartRule) { - $value = $cartRule->getValue(true); + $value = $cartRule->getContextualValue(true); // Todo: repair shrunk // if ($shrunk AND ($total_discount_value + $value) > ($order->total_products_wt + $order->total_shipping + $order->total_wrapping)) // {