Skip to content

Commit

Permalink
Merge pull request PrestaShop#29715 from mflasquin/bugfix/29331
Browse files Browse the repository at this point in the history
Fix discount detail in cart when a product has en ecotax with applied tax
  • Loading branch information
jolelievre authored Oct 28, 2022
2 parents 8e6b400 + 6d0eeac commit 832b1aa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
7 changes: 1 addition & 6 deletions classes/CartRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,7 @@ public function getContextualValue($use_tax, Context $context = null, $filter =
&& (($this->reduction_exclude_special && !$product['reduction_applies']) || !$this->reduction_exclude_special)) {
$price = $product['price'];
if ($use_tax) {
$infos = Product::getTaxesInformations($product, $context);
$tax_rate = $infos['rate'] / 100;
// As the price is tax excluded but ecotax included, we need to substract the ecotax before getting the price tax included
$price -= $product['ecotax'];
$price *= (1 + $tax_rate);
$price += $product['ecotax'];
$price = $product['price_without_reduction'];
}

$selected_products_reduction += $price * $product['cart_quantity'];
Expand Down
11 changes: 7 additions & 4 deletions classes/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ class ProductCore extends ObjectModel
*/
protected static $cacheStock = [];

/** @var int|null */
protected static $psEcotaxTaxRulesGroupId = null;

/**
* Product can be temporary saved in database
*/
Expand Down Expand Up @@ -1175,6 +1178,7 @@ public static function resetStaticCache()
static::$_pricesLevel2 = [];
static::$_incat = [];
static::$_combinations = [];
static::$psEcotaxTaxRulesGroupId = null;
}

/**
Expand Down Expand Up @@ -3906,14 +3910,13 @@ public static function priceCalculation(
$ecotax = Tools::convertPrice($ecotax, $id_currency);
}
if ($use_tax) {
static $psEcotaxTaxRulesGroupId = null;
if ($psEcotaxTaxRulesGroupId === null) {
$psEcotaxTaxRulesGroupId = (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID');
if (self::$psEcotaxTaxRulesGroupId === null) {
self::$psEcotaxTaxRulesGroupId = (int) Configuration::get('PS_ECOTAX_TAX_RULES_GROUP_ID');
}
// reinit the tax manager for ecotax handling
$tax_manager = TaxManagerFactory::getManager(
$address,
$psEcotaxTaxRulesGroupId
self::$psEcotaxTaxRulesGroupId
);
$ecotax_tax_calculator = $tax_manager->getTaxCalculator();
$price += $ecotax_tax_calculator->addTaxes($ecotax);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Cart;
use Context;
use LegacyTests\Unit\Core\Cart\Calculation\CartOld;
use PHPUnit\Framework\Assert;

class CartFeatureContext extends AbstractPrestaShopFeatureContext
{
Expand Down Expand Up @@ -197,4 +198,14 @@ public function calculateCartShippingFees($expectedShippingFees, $taxes = null)
throw new \RuntimeException(sprintf('Expects %s, got %s instead', $expectedTotal, $shippingFees));
}
}

/**
* @Then /^I should have a voucher named "(.+)" with (\d+\.\d+) of discount$/
*/
public function cartVoucher($voucherName, $discountAmount)
{
$cartRules = $this->getCurrentCart()->getCartRules();
Assert::assertEquals($cartRules[0]['code'], $voucherName);
Assert::assertEquals((float) $cartRules[0]['value_real'], (float) $discountAmount);
}
}
10 changes: 10 additions & 0 deletions tests/Integration/Behaviour/Features/Context/TaxFeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ public function setCarrierTaxRuleGroup($carrierName, $taxName)
$carrier = $this->carrierFeatureContext->getCarrierWithName($carrierName);
$carrier->setTaxRulesGroup($this->taxRuleGroups[$taxName]->id);
}

/**
* @Given /^Ecotax belongs to tax group "(.+)"$/
*/
public function setEcotaxTaxRuleGroup($taxName)
{
$this->checkTaxRuleWithNameExists($taxName);
$configuration = CommonFeatureContext::getContainer()->get('prestashop.adapter.legacy.configuration');
$configuration->set('PS_ECOTAX_TAX_RULES_GROUP_ID', $this->taxRuleGroups[$taxName]->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Feature: Cart rule (percent) calculation with one cart rule
And there is a country named "country1" and iso code "FR" in zone "zone1"
And there is a state named "state1" with iso code "TEST-1" in country "country1" and zone "zone1"
And there is an address named "address1" with postcode "1" in state "state1"
And shop configuration for "PS_USE_ECOTAX" is set to 1

Scenario:
Given shop configuration for "PS_CART_RULE_FEATURE_ACTIVE" is set to 1
Expand All @@ -27,8 +28,8 @@ Feature: Cart rule (percent) calculation with one cart rule
And cart rule "cartRuleFiftyPercent" is restricted on the selection of products
And cart rule "cartRuleFiftyPercent" has a discount code "cartRuleFiftyPercent"
## Add product
When I add 1 item of product "product_without_ecotax" in my cart
And I select address "address1" in my cart
When I select address "address1" in my cart
And I add 1 item of product "product_without_ecotax" in my cart
Then my cart total should be 10.400 tax included
And my cart total should be 10.000 tax excluded
And I use the discount "cartRuleFiftyPercent"
Expand All @@ -50,10 +51,36 @@ Feature: Cart rule (percent) calculation with one cart rule
And cart rule "cartRuleFiftyPercent" is restricted on the selection of products
And cart rule "cartRuleFiftyPercent" has a discount code "cartRuleFiftyPercent"
## Add product
When I add 1 item of product "product_with_ecotax" in my cart
And I select address "address1" in my cart
Then my cart total should be 10.400 tax included
And my cart total should be 10.000 tax excluded
When I select address "address1" in my cart
And I add 1 item of product "product_with_ecotax" in my cart
Then my cart total should be 12.400 tax included
And my cart total should be 12.000 tax excluded
And I use the discount "cartRuleFiftyPercent"
Then my cart total should be 5.200 tax included
And my cart total should be 5.000 tax excluded
Then my cart total should be 6.200 tax included
And my cart total should be 6.000 tax excluded
Then I should have a voucher named "cartRuleFiftyPercent" with 6.2 of discount

Scenario:
Given shop configuration for "PS_CART_RULE_FEATURE_ACTIVE" is set to 1
And there is a tax named "tax4Percent" and rate 4.0%
And there is a tax rule named "taxRule4Percent" in country "country1" where tax "tax4Percent" is applied
And Ecotax belongs to tax group "taxRule4Percent"
And I have an empty default cart
## Set Product
And there is a product in the catalog named "product_with_ecotax" with a price of 10.000 and 1000 items in stock
And the product "product_with_ecotax" ecotax is 2.00
And product "product_with_ecotax" belongs to tax group "taxRule4Percent"
## Set Cart Rule
When there is a cart rule named "cartRuleFiftyPercent" that applies a percent discount of 50.0% with priority 2, quantity of 1000 and quantity per user 1000
And cart rule "cartRuleFiftyPercent" is restricted to product "product_with_ecotax"
And cart rule "cartRuleFiftyPercent" is restricted on the selection of products
And cart rule "cartRuleFiftyPercent" has a discount code "cartRuleFiftyPercent"
## Add product
When I select address "address1" in my cart
And I add 1 item of product "product_with_ecotax" in my cart
Then my cart total should be precisely 12.48 tax included
And my cart total should be 12.000 tax excluded
And I use the discount "cartRuleFiftyPercent"
Then my cart total should be precisely 6.24 tax included
And my cart total should be 6.000 tax excluded
Then I should have a voucher named "cartRuleFiftyPercent" with 6.24 of discount

0 comments on commit 832b1aa

Please sign in to comment.