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

OT modules and taxes calculations #6425

Open
piloujp opened this issue Apr 27, 2024 · 3 comments
Open

OT modules and taxes calculations #6425

piloujp opened this issue Apr 27, 2024 · 3 comments

Comments

@piloujp
Copy link
Contributor

piloujp commented Apr 27, 2024

In ZC last V2.0.0, after doing few PRs about Order Total modules, especially discount modules, it appears to me that with actual logic of these modules, it is impossible to apply all possible options and get correct results. There are different problems I identified:

1- Discount modules (discount coupons, GV and group pricing) are treated as 'charges' (fee) modules. They have their own tax class option like they were taxable items, which in my opinion is a non-sens. The items/fees they apply to have tax classes, but the discount in itself can't! There should not be any tax class to choose in discount modules.

2- When applying a discount, there are three options to re-calculate tax, 'None', 'Standard' and 'Credit Note'.
The first one, 'None' does not make any sens, especially with percentage discounts. How do you do to not re-calculate tax when applying a 10% discount, for example? When you apply it to total without tax, you get a result without tax. If you apply it to a total with tax included, your result includes tax, reduced from 10%. It is automatic ! This option should not exist in discount modules.
For other options, 'Standard' and 'Credit Note', I decided to interpret them as 'discount amount is price incl.' and 'discount amount is price excl.' when discount is a fixed amount. With percentage, it does not change a thing.

3- Option 'Include tax' was to be use only when no tax recalculation is done... See above. This one should not be there either.

4- When using multiple tax rates or even multiple tax classes with multiple tax rates, details for shipping tax rates are missing, making it impossible to use multiple tax rates as soon as shipping has two or more taxes applied. Shipping taxes for different tax rates should be saved.

I concentrated on all these points and did a rewrite of OT coupons. Discount ones have been heavily modified.
Now I get good results with multiples taxes, even compound, (tested with 4) and possibles options left in discount modules. Additional charge modules although perform well.
Files for now are available on branch Multiple-taxes-support-in-ot-_modules of my GitHub repo.
I am not sure of what to do with this: Make a new PR and deleting PR#6392 or updating this old PR with new files. Another problem is that this files won't pass some of ZC validation tests and these test conditions are kind of obscure which makes it hard to detect if error comes from new code or was in the test already.

@piloujp
Copy link
Contributor Author

piloujp commented Apr 29, 2024

Here are some test results I got:
First setting I used:
There are 2 tax classes with both 2 compound tax rates each.
Tax class 1: Tax rate 1 → 7%, tax rate 2 → 14%. Equivalent to one tax rate at 21.98%.
Tax class 2: Tax rate 1 → 5%, tax rate 2 → 20%. Equivalent to one tax rate at 26%.
Cart subtotal and C.o.d. charge use tax class 1.
Shipping and low order fee use tax class 2
At first, all modules have the option 'Include shipping' set to true.
C.o.d. fee is $9.99.
Low order fee is $5.
I added charge modules one by one then discount modules too.
Discount coupon fixed amount $10.
Group discount 10%.
Gift Certificate $20.
DieHard-4taxes
DieHard-cod
DieHard-cod-loworder
DieHard-cod-loworder-Disc Coupon10
DieHard-cod-loworder-Disc Coupon10-GroupDisc10
DieHard-cod-loworder-Disc Coupon10-GroupDisc10-GV20
Then I set Group discount option 'include shipping' to false.
DieHard-cod-loworder-Disc Coupon10-GroupDisc10noShip-GV20
And finally set Discount coupon tax calculation to 'Credit Note'.
DieHard-cod-loworder-Disc Coupon10CredNote-GroupDisc10noShip-GV20

I forgot to say that to obtain these results, PR#6411 must be applied too.
I recalculated all these numbers by hand and everything was checking out.

@piloujp
Copy link
Contributor Author

piloujp commented May 1, 2024

After correcting a logic error, the last two examples results have changed a little.
When shipping is not included in discount, I was using full shipping cost when subtracting from total. But like in these examples, if a discount already has been applied, the discounted shipping cost should be used.
Here are correct results:
Group discount option 'include shipping' to false.
DieHard-cod-loworder-Disc Coupon10-GroupDisc10noShip-GV20
Discount coupon tax calculation to 'Credit Note'.
DieHard-cod-loworder-Disc Coupon10CredNote-GroupDisc10noShip-GV20

@piloujp
Copy link
Contributor Author

piloujp commented May 5, 2024

For those who have courage to dig into this code, following information about order->info array might save you some time. This array is used by each order total module for their calculation and then updated with results before the next module use it. Numbers correspond to first example above (prices tax excluded).
["order_status"]=> string(1) "1"
["currency"]=> string(3) "USD"
["currency_value"]=> string(8) "1.000000"
["payment_method"]=> string(0) ""
["payment_module_code"]=> string(0) ""
["coupon_code"]=> string(0) ""
["shipping_method"]=> string(19) "Per Item (Best Way)"
["shipping_module_code"]=> string(9) "item_item"
["shipping_cost"]=> float(2.5) *****
["tax_subtotals"]=> array(4) {
["TAX 7%"]=> array(2) {
["tax_rate"]=> float(7.000000000000006) ["subtotal"]=> float(39.99) } Subtotal is the value on which this tax rate apply
["TAX 14%"]=> array(2) {
["tax_rate"]=> float(14.980000000000015) ["subtotal"]=> float(39.99) } Compound tax rate
["TAX 5%"]=> array(2) {
["tax_rate"]=> float(5.000000000000004) ["subtotal"]=> float(2.5) }
["TAX 20%"]=> array(2) {
["tax_rate"]=> float(20.999999999999996) ["subtotal"]=> float(2.5) } } Compound tax rate
["subtotal"]=> float(39.99) Cart content+charges-discounts *****
["shipping_tax"]=> float(0.65)
["shipping_tax_rate"]=> float(26) Compound tax rate, 5%->20%: (((1+5/100) * (1+20/100))-1)*100
["tax"]=> float(9.43980200000001)
["total"]=> float(51.92980200000001) Final total that will be displayed or used by next module
["tax_groups"]=> array(4) {
["TAX 7%"]=> float(2.7993000000000023) ["TAX 14%"]=> float(5.9905020000000055) ["TAX 5%"]=> float(0.1250000000000001) ["TAX 20%"]=> float(0.5249999999999999) }
["shipping_tax_groups"]=> array(2) {
["TAX 5%"]=> float(0.1250000000000001) ["TAX 20%"]=> float(0.5249999999999999) }
["comments"]=> string(0) ""
["ip_address"]=> string(31) "xxx.xxx.xxx.xxx - xxx.xxx.xxx.xxx"

***** Values that change depending on 'Display with tax' option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant