magento2: Invoice Grand Total wrong if discount applied

Preconditions (*)

  1. Magento 2.4.1
  2. tax/calculation/price_includes_tax => 1
  3. tax/calculation/apply_after_discount => 1
  4. tax/calculation/discount_tax => 1

Steps to reproduce (*)

  1. Create a product with price 20.50 (including tax as per setting above)
  2. Tax is set to 19%
  3. Create a cart rule for 100% discount
  4. Order the product
  5. Create invoice

Expected result (*)

  1. Order Grand Total is 0
  2. Order Tax is 0
  3. Invoice Tax is 0
  4. Invoice Grand Total excl. Tax is 0
  5. Invoice Grand Total incl. Tax is 0

Actual result (*)

  1. Order Grand Total is 0
  2. Order Tax is 0
  3. Invoice Tax is 0
  4. Invoice Grand Total excl. Tax is 3.27
  5. Invoice Grand Total incl. Tax is 3.27

Probable Reason

In vendor/magento/module-sales/Model/Order/Invoice/Total/Tax.php on line 121 the invoice grand total is set to:

$invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax + $totalDiscountTaxCompensation);

which resolves to:

$invoice->setGrandTotal(0 + 0 + 3.27);

The usage of $totalDiscountTaxCompensation is IMHO wrong when using these settings.

We now have the scenario that the invoices show a grand total > 0 which is not resolvable for accounting since these amounts were never received from the customer.

Basically the same issue occurs if the discount is not 100%, just the amounts differ of course.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (4 by maintainers)

Most upvoted comments

@willryanuk I don’t think these tickets are directly related. The tax is basically calculated correctly (against the assumptions in the linked tickets). As you see in your and my examples, the tax is 0 which is correct.

However, the invoice grand total contains the hidden tax (which is the tax that would be applied without the discount) that results in

  1. the grand total of invoice is greater than 0
  2. the total paid amount shown in order is greater than 0

whereas the customer actually (and correctly) paid nothing since the order grand total is 0.

So in my opinion the handling or application of the hidden tax is incorrect.

And this is a large issue for us at the moment.

This is probably fixed by: MC-41438: 100% Discount cart rule results in Tax invoiced, which I’m guessing is going to be included in Magento 2.4.3

Maybe somebody can confirm this?

Yeah, just had to apply the patch you mentioned and that has fixed the issue for me

The fix on develop and the one mentioned here doesn’t work if your discount does not apply to shipping costs. Screenshot 2021-07-08 at 13 42 06