magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

Price including Tax and Row Total Including Tax are incorrect after the Discount appliance #35243

Open riversy opened 2 years ago

riversy commented 2 years ago

Preconditions (*)

  1. Vanilla Magento Community 2.4.3-p1 with Sample Data
  2. The following settings are applied:
    tax/cart_display/subtotal =>3
    tax/cart_display/price =>3
    tax/calculation/apply_after_discount => 1

    image

image

Steps to reproduce (*)

  1. Set Default Tax applied to products with Tax Class “Taxable Goods“ to 20%
  2. Create a test product with a Price of 100$ and Tax Class “Taxable Goods”
  3. Create Cart Price Rule with Coupon “Fix5” which provides a discount of 5%
  4. Go to the storefront and add the Test product to the shopping cart
  5. Proceed to the Payment Method Step
  6. Verify the totals

Expected result (*)

image

Totals Cart Subtotal Excl. Tax: $100.00 Cart Subtotal Incl. Tax: $119.00 Discount (fix5): -$5.00 Shipping Flat Rate - Fixed: $5.00 Tax: $19.00 Order Total: $119.00

Quote Item Data

select sku, name, qty, base_price, tax_amount, base_price_incl_tax, base_row_total, base_row_total_incl_tax
from quote_item
where quote_id = 3;

+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+
|sku                      |name               |qty   |base_price|tax_amount|base_price_incl_tax|base_row_total|base_row_total_incl_tax|
+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+
|Sample_100_dollar_product|Sample 100$ product|1.0000|100.0000  |19.0000   |119.0000           |100.0000      |119.0000               |
+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+

Actual result (*)

image

Totals Cart Subtotal Excl. Tax: $100.00 Cart Subtotal Incl. Tax: $120.00 Discount (fix5): -$5.00 Shipping Flat Rate - Fixed: $5.00 Tax: $19.00 Order Total: $119.00

Quote Item Data

select sku, name, qty, base_price, tax_amount, base_price_incl_tax, base_row_total, base_row_total_incl_tax
from quote_item
where quote_id = 3;

+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+
|sku                      |name               |qty   |base_price|tax_amount|base_price_incl_tax|base_row_total|base_row_total_incl_tax|
+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+
|Sample_100_dollar_product|Sample 100$ product|1.0000|100.0000  |19.0000   |120.0000           |100.0000      |120.0000               |
+-------------------------+-------------------+------+----------+----------+-------------------+--------------+-----------------------+

Cart Subtotal Incl. Tax is not correlating to the subtotal and final tax.

Reference

According to the following documentation, the actual tax amount is expected to be added to the subtotal instead of the original: https://docs.magento.com/user-guide/configuration/sales/tax.html

image

The place to fix the issue in the source code: https://github.com/magento/magento2/blob/0763e060d1e491385280c82ac9f951b1a8b1fcfe/app/code/Magento/Tax/Model/Calculation/AbstractAggregateCalculator.php#L157

The following code:

$rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount;

Should be:

$rowTotalInclTax = $rowTotal + $rowTax;

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

m2-assistant[bot] commented 2 years ago

Hi @riversy. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

riversy commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @riversy. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @riversy, here is your Magento Instance: https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering Admin access: https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/admin_bc93 Login: aca737d3 Password: 8835845b11f1

m2-assistant[bot] commented 2 years ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

riversy commented 2 years ago

Trying to replicate the issue on the https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/ instance.

Preconditions

(1) Applied the following settings to the instance: image

(2) Applied tax rule image image

(3) Created necessary Cart Price Rule image

(4) Created a test product image

Steps to reproduce

  1. Place the following product in the shopping cart and proceed to checkout - https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/sample-100-taxable-product.html
  2. Pass the Shipping Checkout step with a sample US address
  3. Verify totals on the Payment Checkout step

image

Expected Result

Cart Subtotal Incl. Tax to be $119.00 because Cart Subtotal Excl. Tax ($100.00) + Tax ($19.00) = $119.00

Actual Result

Cart Subtotal Incl. Tax is $120.00 It can be treated as an incorrect value.

engcom-November commented 2 years ago

Hi @riversy , Thank you for reporting and collaboration. Verified this on Magento 2.4-develop instance however this behavior is there due to incorrect Tax Configuration. As per Magento docs for tax configuration - Catalog Price: Excluding tax, Display settings: "Including and Excluding tax" & Tax calculation method based on "Total" , can result in rounding errors because the tax calculation is rounded, the amount that appears in the cart might differ from the amount that a customer expects to pay and warning message is displayed to the user in Admin. Please refer the document for valid combinations when configuring tax calculation settings. Thank you.

riversy commented 2 years ago

Hi @engcom-November, Thank you for your clarification of this problem and the provided documentation reference.

According to the document (Magento Docs), the following combination of the settings shouldn't lead to any warnings and confusion, as I understand:

Trying to retest the issue on the https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/ instance using a combination of settings above:

image

Steps to reproduce

  1. Place the following product in the shopping cart and proceed to checkout - https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/sample-100-taxable-product.html
  2. Pass the Shipping Checkout step with a sample US address
  3. Apply coupon code "Fix5" on the Payment Checkout step
  4. Verify checkout totals

image

Expected Result

Cart Subtotal Incl. Tax to be $119.00 because Cart Subtotal Excl. Tax ($100.00) + Tax ($19.00) = $119.00

Actual Result

Cart Subtotal Incl. Tax is $120.00 It can be treated as an incorrect value.

Thougths

The problem here is not only related to the displayed subtotal on the Checkout page, which is also confusing.

The incorrect values row_total_incl_tax, base_row_total_incl_tax, price_incl_tax, base_price_incl_tax are stored in the table quote_item as a result of mentioned code. Some payment modules later can use this information to initiate a payment on the gateway, leading to issues with whose payment methods.

There's one only example: vendor/klarna/module-core/Model/Checkout/Orderline/Items.php:161 image

As for rounding errors, there's a special values discount_tax_compensation_amount and base_discount_tax_compensation_amount are presented in the table quote_item. It isn't used in the \Magento\Tax\Model\Calculation\AbstractAggregateCalculator::calculateWithTaxNotInPrice method, but as I understand the logic it can be.

So my main question here is an incorrect value of Cart Subtotal Incl. Tax is a part of a plan and shouldn't be fixed, or is this an issue worth fixing to make a whole solution more stable?

If Magento is going to fix it, I will be happy to help with a resolution. Thank you!

engcom-November commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-November. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-November, here is your Magento Instance: https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering Admin access: https://9202dc350ac6ddc7308a4c44cefb2813.instances.magento-community.engineering/admin_7b6a Login: 42f35b80 Password: e50027f32f0b

engcom-November commented 2 years ago

Verified the issue again on Magento 2.4-develop branch with above mentioned configuration and the issue is reproducible. Cart Subtotal Incl. Tax to be $119.00 but displayed $120 image Also as per Magento docs, for tax combination: Catalog prices: excluding tax, Tax calculation method: "Row Total" & Display settings set to - "Including and Excluding Tax", there should not be any warning message on admin but Warning message is getting displayed on admin UI image

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-2735 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

ilnytskyi commented 2 years ago

@riversy I faced similar issue, noticed that the column row_total_incl_tax has different value. Correct value would be row_total - discount_amount + tax_amount

Did you manage to find a workaround?

riversy commented 2 years ago

@riversy I faced similar issue, noticed that the column row_total_incl_tax has different value. Correct value would be row_total - discount_amount + tax_amount

Did you manage to find a workaround?

Hi, @ilnytskyi

The following patch fixed the problem on my project:

--- a/vendor/magento/module-tax/Model/Calculation/AbstractAggregateCalculator.php
+++ b/vendor/magento/module-tax/Model/Calculation/AbstractAggregateCalculator.php   (date 1648194054598)
@@ -154,7 +154,7 @@
         }
         $rowTax = array_sum($rowTaxes);
         $rowTaxBeforeDiscount = array_sum($rowTaxesBeforeDiscount);
-        $rowTotalInclTax = $rowTotal + $rowTaxBeforeDiscount;
+        $rowTotalInclTax = $rowTotal + $rowTax;
         $priceInclTax = $rowTotalInclTax / $quantity;

         if ($round) {

I hope it will help you as well.

ilnytskyi commented 2 years ago

@engcom-November since the issue is reproducible and confirmed. Could you check falling tests in https://github.com/magento/magento2/pull/36141 this run https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/36141/5ec82fdcae5038efdcf4f5a02e903286/Integration/console-error-logs.html

We have config like this: Selection_015

The fix suggested by @riversy resolves the problem in our case as well. But now I have doubts if values for row_total_incl_tax in core tests are correct.

cc @maghamed @sidolov

Maybe you could help up to figure out what is the reason that the column row_total_incl_tax is always calculated as if tax applied before discount? So for order with discount and tax appliance after the discount row_total_incl_tax != (row_total + tax_amount - discount_amount)

can be clearly seen with this SQL

select name,
       qty,
       price,
       discount_percent,
       tax_percent,
       round(((price * qty) * (discount_percent / 100)), 4) as calculated_discount_amount,
       discount_amount,
       round((row_total - discount_amount) * (tax_percent / 100), 2) as calculated_tax_amount_after_discount,
       round((row_total) * (tax_percent / 100), 2) as calculated_tax_amount_before_discount,
       tax_amount,
       row_total,
       round((price * qty), 2) as calculated_row_total,
       price_incl_tax,
       round((row_total + tax_amount), 2) as calculated_row_total_incl_tax_after_discount,
       round((row_total + round((row_total) * (tax_percent / 100), 2)), 2) as calculated_row_total_incl_tax_before_discount,
       row_total_incl_tax,
       (row_total + tax_amount)                   as row_with_tax,
       (row_total_incl_tax - discount_amount)     as row_without_discount, #current
       (row_total - discount_amount)              as final_no_tax,
       (row_total + tax_amount - discount_amount) as final_with_tax #correct
from quote_item
where quote_id = {your quote id};