Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tax calculation process does not follow "Apply Tax On" setting #14469

Closed
vbuck opened this issue Mar 30, 2018 · 7 comments
Closed

Tax calculation process does not follow "Apply Tax On" setting #14469

vbuck opened this issue Mar 30, 2018 · 7 comments
Labels
Component: Tax Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@vbuck
Copy link

vbuck commented Mar 30, 2018

Tax is not correctly calculated when "Apply Tax On" option is set to "Original price only." Tax is always based on item calculation price (custom price).

Preconditions

  • Magento Open Source 2.2.3
  • PHP 7.1.14
  • Apache 2.2.15
  • Magento sample data installed
  • Shipping methods Flat Rate, Table Rates, and UPS enabled
  • Payment methods Check/Money Order, Credit Card (Braintree) enabled
  • Setting "Base Currency" set to US Dollar
  • Setting "Default Display Currency" set to US Dollar
  • Setting "Tax Calculation Method Based On" set to Total
  • Setting "Tax Calculation Based On" set to Shipping Address
  • Setting "Shipping Tax Class" set to Taxable Goods
  • Setting "Apply Tax On" set to Original price only
  • New tax rate configured as: any zip code match (*), region of PA/US, rate of 6.0000
  • Existing tax rule "Rule1" expanded to accept new tax rate

Steps to reproduce

  1. From the admin panel, go to Sales > Orders
  2. Select Create New Order
  3. Choose existing customer Veronica Costello
  4. Click Add Products
  5. Select simple product Overnight Duffle ($45.00)
  6. Click Add Selected Product(s) to Order
  7. In the Shipping Address section, uncheck option Same As Billing Address
  8. Change the Street Address to 123 Main Street
  9. Change the City to Mechanicsburg
  10. Change the State to Pennsylvania
  11. Change the Zip Code to 17055
  12. Observe Tax line-item reads $2.70 (0.06 * 45 = 2.6999)
  13. For quote item Overnight Duffle, select option Custom Price
  14. Enter value 10.00
  15. Click Update Items and Quantities
  16. Observe Tax line-item reads $2.70 (0.06 * 45 = 2.6999)

Expected result

  • Final tax line-item reads $2.70

Actual result

  • Final tax line-item reads $0.60

Additional Information

Class Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector is the underlying processor for the total and subtotal tax collectors. This class declares method mapItem, and this method is responsible for converting quote items into a more specific instance of Magento\Tax\Api\Data\QuoteDetailsItemInterface. Lines 216-228 determine the unit price of the given item:

    if ($useBaseCurrency) {
        if (!$item->getBaseTaxCalculationPrice()) {
            $item->setBaseTaxCalculationPrice($item->getBaseCalculationPriceOriginal());
        }
        $itemDataObject->setUnitPrice($item->getBaseTaxCalculationPrice())
            ->setDiscountAmount($item->getBaseDiscountAmount());
    } else {
        if (!$item->getTaxCalculationPrice()) {
            $item->setTaxCalculationPrice($item->getCalculationPriceOriginal());
        }
        $itemDataObject->setUnitPrice($item->getTaxCalculationPrice())
            ->setDiscountAmount($item->getDiscountAmount());
    }

My understanding is as follows:

  • Look for presence of tax_calculation_price via magic getter (note: no implementation found)
  • If not set, use calculation_price via method AbstractItem::getCalculationPriceOriginal

Given the name "tax calculation price," it can be assumed that any modifications to the calculation price should be made here for the sake of tax-specific constraints. Yet, there are none made. Instead the original calculation price is used as-is.

This becomes problematic under the following conditions:

  • Method AbstractItem::setCustomPrice is used; and,
  • Tax configuration is set to "Apply Tax On" the original price

These conditions cause the mapItem method to incorrectly refer to a price that is different from the originating product's base price. That is, a custom price. Furthermore, the "Apply Tax On" setting itself appears to be unused in the core altogether. There are even helper methods in Magento\Tax\Helper\Data, applyTaxOnCustomPrice and applyTaxOnOriginalPrice, which appear to be useful to this mapping process.

I believe the solution is to conditionally fall back to AbstractItem::getCalculationPriceOriginal only when the setting "Apply Tax On" is set to "Custom price if available." Otherwise, if set to "Original price only," then the tax calculation price must be derived from AbstractItem::getBaseOriginalPrice.

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Mar 30, 2018
@iindranil
Copy link

Yes, That's correct. Apply Tax On setting not working. Any solution for this issue ?

Thanks in advance,

@vbuck
Copy link
Author

vbuck commented Jun 8, 2018

@iindranil I haven't submitted a PR for this issue, but I believe one solution is given in my closing statement of the issue description:

conditionally fall back to AbstractItem::getCalculationPriceOriginal only when the setting "Apply Tax On" is set to "Custom price if available." Otherwise, if set to "Original price only," then the tax calculation price must be derived from AbstractItem::getBaseOriginalPrice

I happen to be working on this very issue today in my day job, so I may follow up with a diff or PR to reference when I come out of it.

@iindranil
Copy link

For example, I have added one product in my cart where product original price is $20 and I have set custom price $15.
then I want to add tax on original price $20 which is not working as I reported.

If I do the changes as per your solution on CommonTaxCollector.php then cart item row update with $20 which is incorrect.

I want to sale item at $15 but want to add tax on $20 which is not working with your solution.

Any idea ?

Thank you,

@vbuck
Copy link
Author

vbuck commented Jun 14, 2018

@iindranil I am continuing to research this issue in pursuit of a fix.

As an update to the issue, I have one additional architectural comment. By design this method, being called mapItem and based on its DocBlock description, should not be modifying any properties on the given AbstractItem. It should be treating that item as read-only as it transfers properties to the resulting QuoteDetailsItemInterface object. Therefore it seems this bit of code was an afterthought, or maybe a "quick fix" to a problem encountered during development.

I am not sure it's worth fixing the method to adhere to the principle I outlined, but it's worth noting in case "the dog has to return to its vomit."

@ghost ghost self-assigned this Jul 16, 2018
@ghost ghost added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Progress: needs update and removed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Jul 16, 2018
@ghost
Copy link

ghost commented Jul 20, 2018

Hi @vbuck , thank you for your report.
We've acknowledged the issue and added to our backlog.

@ghost ghost added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Component: Tax Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release and removed Progress: needs update labels Jul 20, 2018
@ghost ghost removed their assignment Jul 20, 2018
@vbuck
Copy link
Author

vbuck commented Jul 20, 2018

@engcom-backlog-nazar I am not able to get to a PR at the moment, but in response to your update I wanted to share a diff I've had sitting around until I could come back to this issue:

I noticed that you made a comment about not being able to reproduce, which I received via email. But by the time I came back to this issue the comment had changed. I'm guessing you were finally able to reproduce.

Let me know if that diff above will solve your problem. If so, I'd be glad to open a PR over the weekend so that you can approve it.

magento-engcom-team pushed a commit that referenced this issue Feb 12, 2019
* [MAGETWO-91631](https://jira.corp.magento.com/browse/MAGETWO-91631) [Magento Cloud]Prefix in checkout not starting with blank value
* [MAGETWO-95836](https://jira.corp.magento.com/browse/MAGETWO-95836) [Magento Cloud] If a product has a custom attributes which requires a Unique Value, it cannot be saved when there's an active Scheduled Update
* [MAGETWO-96852](https://jira.corp.magento.com/browse/MAGETWO-96852) [2.3.x] Impossible to sort Root Categories via drag'n'drop
* [MAGETWO-96421](https://jira.corp.magento.com/browse/MAGETWO-96421) [2.3.x] Header Minicart ,Shopping cart page and Checkout page show incorrect product name
* [MAGETWO-95812](https://jira.corp.magento.com/browse/MAGETWO-95812) Required RMA Attributes are not validated while Customer submits a return
* [MAGETWO-95186](https://jira.corp.magento.com/browse/MAGETWO-95186) [2.3] Incorrect table rates shipping charge at check out with cart price rule
* [MC-4316](https://jira.corp.magento.com/browse/MC-4316) Tax calculation process does not follow "Apply Tax On" setting - GitHub #14469
* [MAGETWO-96545](https://jira.corp.magento.com/browse/MAGETWO-96545) Wrong calculation of invoiced items in shipment document in order with bundle product after partial invoice
* [MC-10963](https://jira.corp.magento.com/browse/MC-10963) Can only export default store view items when upgrade to 2.3.0
* [MAGETWO-64324](https://jira.corp.magento.com/browse/MAGETWO-64324) WebAPI: product "has_options" flag not updated when options added via API
* [MC-10938](https://jira.corp.magento.com/browse/MC-10938) 2.3.0 - clearing pub/static makes me login 3 times
* [MC-10944](https://jira.corp.magento.com/browse/MC-10944) [B2B] My Quote disappears from Customer account when FPC is on
* [MC-5542](https://jira.corp.magento.com/browse/MC-5542) Fix testOutputErrorsWithMessageCategoryAndTrace on PHP 7.2
* [MAGETWO-58841](https://jira.corp.magento.com/browse/MAGETWO-58841) API call with pageSize and currentPage > items should return error
@sdzhepa
Copy link
Contributor

sdzhepa commented Apr 30, 2019

Hello @vbuck

Thank you for contribution and collaboration!

The corresponding internal ticket MC-4316 (MAGETWO-93593), MAGETWO-93592 were fixed and closed by Magento team

Delivered to 2.3-develop branch and should be available with 2.3.1 release
Please see details in the next commits:

Delivered to 2.2-develop branch and should be available with 2.2.9 release
Please see details in the next commits:

@sdzhepa sdzhepa closed this as completed Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Tax Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

4 participants