magento2: Wrong tax calculation after applying discount coupon in Magento Open Source v2.3.4

Preconditions (*)

  1. Fresh Magento 2.3.4, 2.4-develop (Open Source) with sample data

Steps to reproduce (*)

  1. Stores->Configuration-> Sales->Tax : Set all values including tax and Apply Customer Tax After Discount (http://prnt.sc/tyizqn)
  2. Stores->Tax Zones and Rates: Set VAT of 15% (http://prnt.sc/tyizuc)
  3. Stores->Tax Rules: Set VAT of 15% (http://prnt.sc/tyizwl)
  4. Marketing-> Cart Price Rule: Set 10% off on “Percent of product price discount” (http://prnt.sc/tyizzo)

Expected result (*)

If any taxable item’s (https://prnt.sc/tyjm6f) selling price 100 and tax 15% (13.04) included, then:

  1. Base price without tax should be (100 - 13.04) = 86.96
  2. As 10% Discount from that Base price should be (86.96 * 10%) = 8.70
  3. After Discount Base price should be (86.96 - 8.70) = 78.26
  4. 15% Tax on New discounted base price (78.26 * 15%) = 11.74
  5. So Total price after discount with tax (78.26 + 11.74) = 90

Actual result (*)

But on Magento after discount, it calculates wrong Tax and total

  1. it shows Base price without tax 86.96 which is ok
  2. it shows As 10% Discount from that Base price 8.70 which is also ok
  3. but the Tax it shows 11.91 which is wrong
  4. and it shows total price after discount with tax 91.30 which is also wrong http://prnt.sc/tyj01h Can anybody explain why it’s working this way and what is the logic behind it?

Temporary solution

If we follow this fix https://github.com/magento/magento2/issues/21456#issuecomment-467475342

Fix on file vendor/magento/module-tax/Model/Calculation/AbstractAggregateCalculator.php:49

Replace:

//TODO: handle originalDiscountAmount
$taxableAmount = max($rowTotalInclTax - $discountAmount, 0);
$rowTaxAfterDiscount = $this->calculationTool->calcTaxAmount(
    $taxableAmount,
    $rate,
    true,
    false
);
$rowTaxAfterDiscount = $this->roundAmount(
    $rowTaxAfterDiscount,
    $rate,
    true,
    self::KEY_REGULAR_DELTA_ROUNDING,
    $round,
    $item
);
// Set discount tax compensation
$discountTaxCompensationAmount = $rowTax - $rowTaxAfterDiscount;
$rowTax = $rowTaxAfterDiscount;

With the following:

$taxableAmount = max($rowTotal - $discountAmount, 0);
$discountTaxCompensationAmount = 0;
$rowTax = $taxableAmount * ($rate / 100);

In our case, it works as expected (http://prnt.sc/tyj04g). Thanks to @pierzakp Is it reliable or safe enough to apply on the production site? Is there any alternative way or settings to fix that tax issue?

Note: After applying this fix it wasn’t working as expected, then find out its need clear cache properly both for Magento + Browser + 3rd party (if any).

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (5 by maintainers)

Most upvoted comments

Do we have any patch for this?