lat9 / edit_orders

Edit Orders: Updates for continued operation on Zen Cart v1.5.8 and later
GNU General Public License v2.0
5 stars 9 forks source link

ot_group_pricing core override tax calculation problem #236

Closed daphilli224 closed 3 months ago

daphilli224 commented 4 months ago

Using the ot_group_pricing core override in edit order 4.7.0 leads to incorrect tax calculations. Even though "Include tax value in amount before discount calculation" is set to false, the override includes the discount amount in the calculation of tax in the storefront. Changing "Include tax value in amount before discount calculation" to true provides the correct calculation. The original 1.5.8a ot_group_pricing.php file gives the correct tax when "Include tax value in amount before discount calculation" is set to false. See screenshots attached. I set the undefined language constants to '' for the override file. The "Include shipping value in amount before discount calculation" is set to false. Shipping charges are taxable for my store.

Hope this helps you find the bug.

All the best, Dave

Original file with include tax set false Override file with include tax set true Override file with include tax set false
daphilli224 commented 4 months ago

I see the file names on the screen shots are lost. The first two screen shots show correct amounts. The last one is the core override file with "Include tax value in amount before discount calculation" set to false with the incorrect tax.

lat9 commented 3 months ago

Capturing images of the various combinations, for follow-on review:

With no pricing group: image

With a 15% pricing group: image

image

image

image

lat9 commented 3 months ago

Restructuring (a) the output and (b) the processing. Language file will be changed so that the description of the discount identifies which elements 'participate' in the reductions:

image

image

image

image

daphilli224 commented 3 months ago

Taking each case below: (1) sub-total included: discount is ok (4.25.15=.6375). Tax is incorrect ((4.25-.64)+5.00).06 = 0.5166 or 0.52, not 0.56. I'm interpreting sub-total included as shipping and tax are not discounted, and only the sub-total minus discount plus shipping is taxed since shipping is taxable in the store state. (2) sub-total and shipping included: discount is ok (4.25+5.00).15=1.3875. Tax is incorrect (4.25+5.00-1.39).06=0.4716 or 0.47, not 0.56. (3) sub-total, shipping and tax included: discount is incorrect (4.25+5.00+(4.25+5.00).06)).15 = (9.25+.56).15=1.4715 or 1.47, not 1.39. Tax is correct (4.25+5.00).06=0.5550.85=0.47175 or 0.47. This is the tax remaining after the discount on tax is subtracted. (4) sub-total and tax included: discount is incorrect (4.25 + (4.25+5.00).06).15=(4.25+.56).15=0.7215 or 0.72, not 0.64. Tax is close ((4.25-4.250.15)+5.00-0.560.15).06=(4.25-0.6375+5.00-0.08325).06=8.52925*.06=0.511755 or 0.51, not 0.52. Possible round-off in calculations???

This stuff hurts my head, but I think I've got the calculations correct according to my understanding of including tax and shipping in discount calculations. But I might not have the right understanding. For my purposes if the calculations for discount and tax are correct when including only the subtotal in the discount calculation, I'll be happy.

daphilli224 commented 3 months ago

Question: Is tax always calculated on the undiscounted total in ot_tax rather than calculating the tax on the discounted total? Maybe that has been my problem all along?

lat9 commented 3 months ago
  1. When only the subtotal (no tax, no shipping) is included, the tax is calculated on the full sub-total (4.25) and shipping, so (4.25+5)*.06 = .555 (rounded = .56).
  2. When subtotal and shipping (no tax) is included, the discount includes the sub-total+shipping, but the tax is still calculated on the full sub-total and shipping (as above).
  3. When subtotal, shipping and tax is included, the discount is calculated on the full sub-total and shipping (9.25.15 = 1.39) and the tax is reduced by the tax on the subtotal/shipping discount (0.555-(1.39.06)) = 0.47.
  4. When subtotal (no shipping, but tax included), the discount is calculated only on the sub-total (4.25.15 = 0.64) and the tax is reduced by the tax on the subtotal's discount (0.555-(0.64.06)) = 0.52.

Hopefully, that's clearer than mud!