diff --git a/classes/Cart.php b/classes/Cart.php index 18b2c15cf..5a8934980 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1731,6 +1731,7 @@ class CartCore extends ObjectModel $order_total_discount = 0; $order_shipping_discount = 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) { @@ -1787,10 +1788,28 @@ 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) { + $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; - $order_total -= $order_total_discount; + if ($type == Cart::ADVANCE_PAYMENT) { + // get order total without 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; + // 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 - $advance_payment_products_discount; + } 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..9086135ad 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -875,9 +875,10 @@ 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) + 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; @@ -890,15 +891,34 @@ 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)$only_advance_payment_products; + foreach ($package['products'] as $key => $product) { + if ($only_advance_payment_products) { + if ($advancePaymentInfo = $objHotelAdvancePayment->getIdAdvPaymentByIdProduct($product['id_product'])) { + if (!$advancePaymentInfo['active']) { + unset($package['products'][$key]); + continue; + } + } else { + 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); } @@ -1001,7 +1021,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)) { @@ -1011,7 +1031,31 @@ class CartRuleCore extends ObjectModel } } + $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 + 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; + } + } + } + + 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); @@ -1058,7 +1102,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 4b4cf141d..f24c8818e 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -583,9 +583,11 @@ 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; + } + $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), @@ -598,13 +600,25 @@ 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 // 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 ($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); @@ -616,16 +630,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; @@ -667,12 +678,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);