From 373bc1138e0da688b6a61a81f1cbb86333c6f28b Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Wed, 9 Nov 2022 15:41:58 +0530 Subject: [PATCH 1/7] apply dicount coupon on due amount in advance payment --- classes/Cart.php | 18 +++++++++++++++++- classes/CartRule.php | 25 ++++++++++++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 9d08d6834..2d7da1a43 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1717,6 +1717,7 @@ class CartCore extends ObjectModel $order_total_discount = 0; $order_shipping_discount = 0; + $order_total_discount_advance_product = 0; if (!in_array($type, array(Cart::ONLY_SHIPPING, Cart::ONLY_PRODUCTS, Cart::ADVANCE_PAYMENT_ONLY_PRODUCTS)) && CartRule::isFeatureActive()) { // First, retrieve the cart rules associated to this "getOrderTotal" if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) { @@ -1773,10 +1774,25 @@ class CartCore extends ObjectModel // If the cart rule offers a reduction, the amount is prorated (with the products in the package) if ($cart_rule['obj']->reduction_percent > 0 || $cart_rule['obj']->reduction_amount > 0) { $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), $compute_precision); + + if ($type == Cart::ADVANCE_PAYMENT) { + $order_total_discount_advance_product += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache, true), $compute_precision); + } } } $order_total_discount = min(Tools::ps_round($order_total_discount, 2), (float)$order_total_products) + (float)$order_shipping_discount; - $order_total -= $order_total_discount; + if ($type == Cart::ADVANCE_PAYMENT) { + // if discount is greater than due amount then only decrease the price of advance payment amount + // otherwise discount will be applied on due amount not advance payment amount + $total_without_discount = self::getOrderTotal() + $order_total_discount; + $due_amount = $total_without_discount - $order_total; + if ($order_total_discount_advance_product > $due_amount) { + $order_total -= ($order_total_discount_advance_product - $due_amount); + } + $order_total -= $order_total_discount - $order_total_discount_advance_product; + } else { + $order_total -= $order_total_discount; + } } if ($type == Cart::BOTH || $type == Cart::ADVANCE_PAYMENT) { diff --git a/classes/CartRule.php b/classes/CartRule.php index 88670b0de..b4059af27 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -877,7 +877,7 @@ class CartRuleCore extends ObjectModel * @param bool $use_cache Allow using cache to avoid multiple free gift using multishipping * @return float|int|string */ - public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true) + public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true, $forAdvancePayment = false) { if (!CartRule::isFeatureActive()) { return 0; @@ -890,15 +890,30 @@ class CartRuleCore extends ObjectModel } $all_products = $context->cart->getProducts(); - $package_products = (is_null($package) ? $all_products : $package['products']); + if (is_null($package)) { + $package = array( + 'products' => $all_products, + 'id_carrier' => null, + 'id_address' => 0 + ); + } $reduction_value = 0; - - $cache_id = 'getContextualValue_'.(int)$this->id.'_'.(int)$use_tax.'_'.(int)$context->cart->id.'_'.(int)$filter; - foreach ($package_products as $product) { + $objHotelAdvancePayment = new HotelAdvancedPayment(); + $cache_id = 'getContextualValue_'.(int)$this->id.'_'.(int)$use_tax.'_'.(int)$context->cart->id.'_'.(int)$filter.'_'.(int)$forAdvancePayment; + foreach ($package['products'] as $key => $product) { + if ($forAdvancePayment) { + $paymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product']); + if (!$paymentInfo['active']) { + unset($package['products'][$key]); + continue; + } + } $cache_id .= '_'.(int)$product['id_product'].'_'.(int)$product['id_product_attribute'].(isset($product['in_stock']) ? '_'.(int)$product['in_stock'] : ''); } + $package_products = $package['products']; + if (Cache::isStored($cache_id)) { return Cache::retrieve($cache_id); } From 50491e7f128f6f44cfcdd1029f08fbb407a7fa61 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 31 May 2022 18:18:49 +0530 Subject: [PATCH 2/7] Fix applied voucher amount exceeds restricted room type price --- classes/CartRule.php | 28 ++++++++++++++++++++++++++-- classes/PaymentModule.php | 24 +++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index b4059af27..22726708e 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -1016,7 +1016,7 @@ class CartRuleCore extends ObjectModel $reduction_value += $selected_products_reduction * $this->reduction_percent / 100; } - // Discount (¤) + // Discount (by amount) if ((float)$this->reduction_amount > 0) { $prorata = 1; if (!is_null($package) && count($all_products)) { @@ -1026,7 +1026,31 @@ class CartRuleCore extends ObjectModel } } + $restricted_product = null; + // If the cart rule is restricted to one room type it can't exceed this room type price + if ($this->reduction_product > 0) { + foreach ($package_products as $product) { + if ($product['id_product'] == $this->reduction_product) { + $restricted_product = $product; + } + } + } + $reduction_amount = $this->reduction_amount; + if ($this->reduction_product) { + if ($restricted_product) { + $prorata = 1; // do not split this cart rule + if ($this->reduction_tax) { + $max_reduction_amount = (int)$restricted_product['cart_quantity'] * (float)$restricted_product['price_wt']; + } else { + $max_reduction_amount = (int)$restricted_product['cart_quantity'] * (float)$restricted_product['price']; + } + $reduction_amount = min($reduction_amount, $max_reduction_amount); + } else { + $reduction_amount = 0; + } + } + // If we need to convert the voucher value to the cart currency if (isset($context->currency) && $this->reduction_currency != $context->currency->id) { $voucherCurrency = new Currency($this->reduction_currency); @@ -1073,7 +1097,7 @@ class CartRuleCore extends ObjectModel } } } - // Discount (¤) on the whole order + // Discount (by amount) on the whole order elseif ($this->reduction_product == 0) { $cart_amount_te = null; $cart_amount_ti = null; diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index c73fbc854..24c15fcb8 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -547,6 +547,10 @@ abstract class PaymentModuleCore extends Module $total_reduction_value_ti = 0; $total_reduction_value_tex = 0; foreach ($cart_rules as $cart_rule) { + if ($cart_rule['obj']->reduction_product > 0 && !$order->orderContainProduct($cart_rule['obj']->reduction_product)) { + continue; + } + $package = array('id_carrier' => $order->id_carrier, 'id_address' => $order->id_address_delivery, 'products' => $order->product_list); $values = array( 'tax_incl' => $cart_rule['obj']->getContextualValue(true, $this->context, CartRule::FILTER_ACTION_ALL_NOCAP, $package), @@ -565,7 +569,20 @@ abstract class PaymentModuleCore extends Module // This is an "amount" reduction, not a reduction in % or a gift // THEN // The voucher is cloned with a new value corresponding to the remainder - if (count($order_list) == 1 && $values['tax_incl'] > ($order->total_products_wt - $total_reduction_value_ti) && $cart_rule['obj']->partial_use == 1 && $cart_rule['obj']->reduction_amount > 0) { + $reduction_amount_converted = $cart_rule['obj']->reduction_amount; + if ((int) $cart_rule['obj']->reduction_currency !== (int) $cart->id_currency) { + $reduction_amount_converted = Tools::convertPriceFull( + $cart_rule['obj']->reduction_amount, + new Currency($cart_rule['obj']->reduction_currency), + new Currency($cart->id_currency) + ); + } + if ($cart_rule['obj']->reduction_tax) { + $remaining_amount = $reduction_amount_converted - $values['tax_incl']; + } else { + $remaining_amount = $reduction_amount_converted - $values['tax_excl']; + } + if (count($order_list) == 1 && $remaining_amount > 0 && $cart_rule['obj']->partial_use == 1 && $reduction_amount_converted > 0) { // Create a new voucher from the original $voucher = new CartRule((int)$cart_rule['obj']->id); // We need to instantiate the CartRule without lang parameter to allow saving it unset($voucher->id); @@ -577,16 +594,13 @@ abstract class PaymentModuleCore extends Module } // Set the new voucher value + $voucher->reduction_amount = $remaining_amount; if ($voucher->reduction_tax) { - $voucher->reduction_amount = ($total_reduction_value_ti + $values['tax_incl']) - $order->total_products_wt; - // Add total shipping amout only if reduction amount > total shipping if ($voucher->free_shipping == 1 && $voucher->reduction_amount >= $order->total_shipping_tax_incl) { $voucher->reduction_amount -= $order->total_shipping_tax_incl; } } else { - $voucher->reduction_amount = ($total_reduction_value_tex + $values['tax_excl']) - $order->total_products; - // Add total shipping amout only if reduction amount > total shipping if ($voucher->free_shipping == 1 && $voucher->reduction_amount >= $order->total_shipping_tax_excl) { $voucher->reduction_amount -= $order->total_shipping_tax_excl; From 224e8087e79191d0b8eaf86ee61c0f0a62467d77 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Thu, 29 Dec 2022 12:42:20 +0530 Subject: [PATCH 3/7] update advance payment discount calculation block --- classes/CartRule.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 22726708e..e2c5baf48 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -877,7 +877,7 @@ class CartRuleCore extends ObjectModel * @param bool $use_cache Allow using cache to avoid multiple free gift using multishipping * @return float|int|string */ - public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true, $forAdvancePayment = false) + public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true, $only_advance_payment_products = false) { if (!CartRule::isFeatureActive()) { return 0; @@ -900,11 +900,15 @@ class CartRuleCore extends ObjectModel $reduction_value = 0; $objHotelAdvancePayment = new HotelAdvancedPayment(); - $cache_id = 'getContextualValue_'.(int)$this->id.'_'.(int)$use_tax.'_'.(int)$context->cart->id.'_'.(int)$filter.'_'.(int)$forAdvancePayment; + $cache_id = 'getContextualValue_'.(int)$this->id.'_'.(int)$use_tax.'_'.(int)$context->cart->id.'_'.(int)$filter.'_'.(int)$only_advance_payment_products; foreach ($package['products'] as $key => $product) { - if ($forAdvancePayment) { - $paymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product']); - if (!$paymentInfo['active']) { + if ($only_advance_payment_products) { + if ($paymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product'])) { + if (!$paymentInfo['active']) { + unset($package['products'][$key]); + continue; + } + } else { unset($package['products'][$key]); continue; } From ea4c1a68877a35cbad3e6f0d7b3b643ef160e529 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Tue, 3 Jan 2023 15:28:47 +0530 Subject: [PATCH 4/7] updated comments and variable declaration --- classes/Cart.php | 17 ++++++++++------- classes/CartRule.php | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/classes/Cart.php b/classes/Cart.php index 2d7da1a43..409f33fd9 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1717,7 +1717,7 @@ class CartCore extends ObjectModel $order_total_discount = 0; $order_shipping_discount = 0; - $order_total_discount_advance_product = 0; + $advance_payment_products_discount = 0; if (!in_array($type, array(Cart::ONLY_SHIPPING, Cart::ONLY_PRODUCTS, Cart::ADVANCE_PAYMENT_ONLY_PRODUCTS)) && CartRule::isFeatureActive()) { // First, retrieve the cart rules associated to this "getOrderTotal" if ($with_shipping || $type == Cart::ONLY_DISCOUNTS) { @@ -1776,20 +1776,23 @@ class CartCore extends ObjectModel $order_total_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache), $compute_precision); if ($type == Cart::ADVANCE_PAYMENT) { - $order_total_discount_advance_product += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache, true), $compute_precision); + $advance_payment_products_discount += Tools::ps_round($cart_rule['obj']->getContextualValue($with_taxes, $virtual_context, CartRule::FILTER_ACTION_REDUCTION, $package, $use_cache, true), $compute_precision); } } } $order_total_discount = min(Tools::ps_round($order_total_discount, 2), (float)$order_total_products) + (float)$order_shipping_discount; if ($type == Cart::ADVANCE_PAYMENT) { - // if discount is greater than due amount then only decrease the price of advance payment amount - // otherwise discount will be applied on due amount not advance payment amount + // get order total without discount $total_without_discount = self::getOrderTotal() + $order_total_discount; + // to find due amount substract advance payment without discuount from order total without discount + // Due Amount = order total - advance payment (without discount) $due_amount = $total_without_discount - $order_total; - if ($order_total_discount_advance_product > $due_amount) { - $order_total -= ($order_total_discount_advance_product - $due_amount); + // if discount for advance payment products is greater than due amount then only decrease the price of advance payment amount + // otherwise discount will be applied on due amount not advance payment amount + if ($advance_payment_products_discount > $due_amount) { + $order_total -= ($advance_payment_products_discount - $due_amount); } - $order_total -= $order_total_discount - $order_total_discount_advance_product; + $order_total -= $order_total_discount - $advance_payment_products_discount; } else { $order_total -= $order_total_discount; } diff --git a/classes/CartRule.php b/classes/CartRule.php index e2c5baf48..a21f6b9eb 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -875,6 +875,7 @@ class CartRuleCore extends ObjectModel * @param bool $use_tax * @param Context $context * @param bool $use_cache Allow using cache to avoid multiple free gift using multishipping + * @param bool $only_advance_payment_products to calculate discount applied only on products that have advance payment * @return float|int|string */ public function getContextualValue($use_tax, Context $context = null, $filter = null, $package = null, $use_cache = true, $only_advance_payment_products = false) @@ -903,8 +904,8 @@ class CartRuleCore extends ObjectModel $cache_id = 'getContextualValue_'.(int)$this->id.'_'.(int)$use_tax.'_'.(int)$context->cart->id.'_'.(int)$filter.'_'.(int)$only_advance_payment_products; foreach ($package['products'] as $key => $product) { if ($only_advance_payment_products) { - if ($paymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product'])) { - if (!$paymentInfo['active']) { + if ($advancePaymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product'])) { + if (!$advancePaymentInfo['active']) { unset($package['products'][$key]); continue; } From 5ceca9c9e1fa67379f4999fad257564c6e24ef8b Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 10 Jan 2023 16:25:31 +0530 Subject: [PATCH 5/7] Fix voucher creation issue in case of multiple orders --- classes/CartRule.php | 30 ++++++++++++++++++++---------- classes/PaymentModule.php | 10 +--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index a21f6b9eb..30cdcb53d 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -1041,21 +1041,31 @@ class CartRuleCore extends ObjectModel } } + $restricted_product = null; $reduction_amount = $this->reduction_amount; - if ($this->reduction_product) { - if ($restricted_product) { - $prorata = 1; // do not split this cart rule - if ($this->reduction_tax) { - $max_reduction_amount = (int)$restricted_product['cart_quantity'] * (float)$restricted_product['price_wt']; - } else { - $max_reduction_amount = (int)$restricted_product['cart_quantity'] * (float)$restricted_product['price']; + // If the cart rule is restricted to one room type it can't exceed this room type price + if ($this->reduction_product > 0) { + foreach ($package_products as $product) { + if ($product['id_product'] == $this->reduction_product) { + $restricted_product = $product; + + $prorata = 1; // do not split this cart rule + if ($this->reduction_tax) { + $max_reduction_amount = (int) $restricted_product['cart_quantity'] * (float) $restricted_product['price_wt']; + } else { + $max_reduction_amount = (int) $restricted_product['cart_quantity'] * (float) $restricted_product['price']; + } + $reduction_amount = min($reduction_amount, $max_reduction_amount); + + break; } - $reduction_amount = min($reduction_amount, $max_reduction_amount); - } else { - $reduction_amount = 0; } } + if (($this->reduction_product > 0) && !$restricted_product) { + $reduction_amount = 0; + } + // If we need to convert the voucher value to the cart currency if (isset($context->currency) && $this->reduction_currency != $context->currency->id) { $voucherCurrency = new Currency($this->reduction_currency); diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 24c15fcb8..9d953da1a 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -544,8 +544,6 @@ abstract class PaymentModuleCore extends Module } $cart_rules_list = array(); - $total_reduction_value_ti = 0; - $total_reduction_value_tex = 0; foreach ($cart_rules as $cart_rule) { if ($cart_rule['obj']->reduction_product > 0 && !$order->orderContainProduct($cart_rule['obj']->reduction_product)) { continue; @@ -563,7 +561,6 @@ abstract class PaymentModuleCore extends Module } // IF - // This is not multi-shipping // The value of the voucher is greater than the total of the order // Partial use is allowed // This is an "amount" reduction, not a reduction in % or a gift @@ -582,7 +579,7 @@ abstract class PaymentModuleCore extends Module } else { $remaining_amount = $reduction_amount_converted - $values['tax_excl']; } - if (count($order_list) == 1 && $remaining_amount > 0 && $cart_rule['obj']->partial_use == 1 && $reduction_amount_converted > 0) { + if ($remaining_amount > 0 && $cart_rule['obj']->partial_use == 1 && $reduction_amount_converted > 0) { // Create a new voucher from the original $voucher = new CartRule((int)$cart_rule['obj']->id); // We need to instantiate the CartRule without lang parameter to allow saving it unset($voucher->id); @@ -642,12 +639,7 @@ abstract class PaymentModuleCore extends Module null, null, null, null, _PS_MAIL_DIR_, false, (int)$order->id_shop ); } - - $values['tax_incl'] = $order->total_products_wt - $total_reduction_value_ti; - $values['tax_excl'] = $order->total_products - $total_reduction_value_tex; } - $total_reduction_value_ti += $values['tax_incl']; - $total_reduction_value_tex += $values['tax_excl']; $order->addCartRule($cart_rule['obj']->id, $cart_rule['obj']->name, $values, 0, $cart_rule['obj']->free_shipping); From a75d3deee262b5bfd8a05bcafa8eeb84e9d03992 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 10 Jan 2023 16:31:27 +0530 Subject: [PATCH 6/7] Remove unwanted code from last commit --- classes/CartRule.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index 30cdcb53d..9086135ad 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -1031,16 +1031,6 @@ class CartRuleCore extends ObjectModel } } - $restricted_product = null; - // If the cart rule is restricted to one room type it can't exceed this room type price - if ($this->reduction_product > 0) { - foreach ($package_products as $product) { - if ($product['id_product'] == $this->reduction_product) { - $restricted_product = $product; - } - } - } - $restricted_product = null; $reduction_amount = $this->reduction_amount; // If the cart rule is restricted to one room type it can't exceed this room type price From 4869a20c00fce5dba2d129ffddefbae35218ab36 Mon Sep 17 00:00:00 2001 From: Shreesh Arora Date: Tue, 17 Jan 2023 19:25:23 +0530 Subject: [PATCH 7/7] update getOrderTotal call in advance payment --- classes/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Cart.php b/classes/Cart.php index 409f33fd9..38cba9904 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1783,7 +1783,7 @@ class CartCore extends ObjectModel $order_total_discount = min(Tools::ps_round($order_total_discount, 2), (float)$order_total_products) + (float)$order_shipping_discount; if ($type == Cart::ADVANCE_PAYMENT) { // get order total without discount - $total_without_discount = self::getOrderTotal() + $order_total_discount; + $total_without_discount = $this->getOrderTotal() + $order_total_discount; // to find due amount substract advance payment without discuount from order total without discount // Due Amount = order total - advance payment (without discount) $due_amount = $total_without_discount - $order_total;