From 50491e7f128f6f44cfcdd1029f08fbb407a7fa61 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 31 May 2022 18:18:49 +0530 Subject: [PATCH] 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;