Zen Cart Logo
Forums / Bug Reports / tax calculation problem with coupons and multiple products

tax calculation problem with coupons and multiple products

Locked

Views: 16,028

Results 21 to 40 of 54
This thread is locked. New replies are disabled.
5 Jan 2007, 6:59 PM
#21
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

tax calculation problem with coupons and multiple products

Hi Jeff,

I don't have much time now, but I will compare more carefully and do some more testing later.

I do have recalculate tax set to true and we do have two tax classes (only one of which (19%) I use currently for products and shipping). Other than that the settings seem the same here.

Here is an example of an order using the freeshipping code:

Sub-Totaal: € 53,50

Basis (300gr) (Met tracking, zonder handtekening.): € 6,50

Kortingbonnen: FREESHIPPER2007 : -€ 6,50

Inc. 19% BTW: € 8,54

Totaal: € 53,50
(the sort order appears to be different than yours)

Really have to go now...

5 Jan 2007, 7:08 PM
#22
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

One last additional note:

I have another small difference in the code:

//        $od_amount['total'] += zen_calculate_tax($od_amount['total'], $tax);   // JTD - BikeMike post
        //$od_amount['total'] += zen_calculate_tax($od_amount['total'], $tax_rate);
          $od_amount['total'] += $tax; // paulm
```I don't think it matters in this case though.
8 Jan 2007, 10:40 AM
#23
daveky avatar

daveky

New Zenner

Join Date:
Aug 2006
Location:
Manchester, UK
Posts:
36
Plugin Contributions:
0

Re: tax calculation problem with coupons and multiple products

Hi All!

I have been reading this post with interest (with respect to http://www.zen-cart.com/forum/showthread.php?t=54176%29.

Could someone please tell me where you are up to with this bug chasing? The code (as you have it so far) and where to put it would be great. Then I can do some testing and put my tuppence in the hat.

Cheers, D.

10 Jan 2007, 9:52 AM
#24
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

Hi Dave,

Jeff posted a link to an updated ot_coupon.php file a couple of posts back in this thread. And my version you can download here: http://www.beterelektro.nl/zen-cart/index.php?page=my_contributions

Current: ot_coupon-pm1.zip

It works for me on a 1.3.6 shop (note: ot_coupon v 1.3.7 is the same as the 1.3.6 version) including tax setting for admin and catalog and shipping includes tax as well. Let us know if it works for you too (Jeff's version or mine).

10 Jan 2007, 10:44 AM
#25
daveky avatar

daveky

New Zenner

Join Date:
Aug 2006
Location:
Manchester, UK
Posts:
36
Plugin Contributions:
0

Re: tax calculation problem with coupons and multiple products

Hello there!

I have placed your file in my shop and it does seem to be be better on initial testing. I had to set the order Total > Discount Coupon > Recalculate Tax to 'Standard' to get it to work out. (I'm not sure what that setting actually means though, so maybe I have been misleading myself all along!)

There is still a slight rounding issue which I'm prepared to live with:

Sub-Total:£211.70
Flat Rate (Best Way):£9.95
Discount Coupon: LL0107 :-£21.18
Total:£200.47
VAT:£29.85

(10% of 211.7 is 21.18? er, OK!)

For all I know at this point there could still be issue with other configurations but it is working for the settings we wish to work with so I'm happy for now!

Thanks a lot for this. I'll let you know if anything ugly raises it's head.

Cheers, D.

10 Jan 2007, 11:02 AM
#26
daveky avatar

daveky

New Zenner

Join Date:
Aug 2006
Location:
Manchester, UK
Posts:
36
Plugin Contributions:
0

Re: tax calculation problem with coupons and multiple products

For example: If I change the OT settings to include tax it seems to give too much discount. I think that this means it is adding tax to the total (even though it already includes tax) before it works out the discount (but that's with recalculate tax = true, should it be false?). It'a all quite misleading and hard to work out but it's obviously not perfect (or at least the settings are not intuitive enough). I guess recalculate tax and include tax will affect each other. I just don't know whether the effects I'm seeing are what I should expect! But, as I said: I'm getting what I expect with the settings I'm using.

18 Jan 2007, 9:43 PM
#27
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

Revisiting this... it seems the error has nothing to do with restricted coupons as I originally thought... its just a problem with multiple products in general.

I just did another test

Product 1 - $100
Product 2 - $100
Discount 10% - $20
Tax (13%) should be $23.40 but is incorrectly calculated as $20.80

However, when I remove one product I get this:

Product 1 - $100
Discount 10% - $10
Tax (13%) is correctly calculated as $11.70

Hmm... I think I figured out what is happening. The tax calculation is assuming the FULL discount was applied to EACH product. $20.80 / 0.13 = $160, which is what you would get if you took $20 off twice instead of once.

  • Steven
19 Jan 2007, 8:51 AM
#28
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

@daveky: it looks like you are right. I have "Include Tax" set to false (whatever that means in this case) but all the results are including tax after my modifications. I had already wasted so much time on trying all different settings without succes, that I totally forgot about the settings in the end. I don't feel like correcting this at the moment since I rather have it working with wrong settings than not working at all. Maybe I will take another look at it later.

@s_mack: I don't have this issue with multiple products. Just did another test to be sure:

Sub-Totaal: € 296,40

Basis (1,403.00gr) (Met tracking, zonder handtekening.): € 6,50

Kortingbonnen: 10procent : -€ 29,64

Inc. 19% BTW: € 43,63

Totaal: € 273,26The including 19% tax calculated is correct here. Having 5 different products (different quantities of each). Using one tax class for all.

19 Jan 2007, 5:36 PM
#29
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

Paul... you're using your version though, right? I mean the stock file.

This is a bug (as far as I can tell) and we should have the zen folks fixing it rather than patches and hacks.

The only problem I have with your version and Jeff's is that they appear to be based on older versions, correct? There are so many differences beween your files and the stock one that can't possibly be there for correcting this problem. And as you just said, you have yours working with the incorrect settings... that means your file is doing what you want it to do regardless of what the settings are.

I need a fix that encompasses all the possible tax settings.

  • Steven
19 Jan 2007, 6:30 PM
#30
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

Hi Steven,

I am using the file I uploaded to this thread. It's not based on an old version (as far as I know, some of the added changes might be based on an older version?).

If I compare I only find 9 differences to the original 1.3.6/1.3.7, most of which are really small changes. So if it it helps to fix your issue it should not be very difficult to find the diff that does the trick I suppose.

Strange thing is that I thought the issue with multiple products you mention was a bug in an older version that was solved. But I could be completely wrong there, just thought I read about it some time ago. I never experienced this bug myself (never used coupons before, so I could not experience it :smartass: ).

that means your file is doing what you want it to do regardless of what the settings areIt won't work regardless I think, only with the wrong tax setting.

19 Jan 2007, 6:36 PM
#31
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

Actually "my" version is mainly Jeff's version with two small diffs (if I remember correctly).

19 Jan 2007, 8:18 PM
#32
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

I tried taking a look at it - spend a couple hours trying to figure out the logic. I'm not one to just look for diffs and apply them and hope for the best. I want to understand what is going on so that I can spot potential problems.

But how this works baffled me. First off, why are we looking at the coupon file at all when its the taxes that are getting screwed up? :) The ot_tax file is so simple it hardly seems to be doing anything.

I'll take another look when I get some time.

  • Steven
19 Jan 2007, 8:56 PM
#33
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

s_mack:

But how this works baffled me. First off, why are we looking at the coupon file at all when its the taxes that are getting screwed up?I am glad to hear you are baffled too, after looking at ot_coupon ;-)

And I think you are right, the taxes are "all over the place" but should probably calculated by a couple of tax functions and/or class(es). So now we only need to wait for someone to rewrite the Zen tax code... :smartass:

19 Jan 2007, 9:04 PM
#34
kim avatar

kim

Obaa-san

Join Date:
Jun 2003
Posts:
26,592
Plugin Contributions:
0

Re: tax calculation problem with coupons and multiple products

...and we can all say a great big "thank you" to Harald for such a wonderfully crappy concoction of calculations to start with.

19 Jan 2007, 10:45 PM
#35
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

well, at least I've isolated the section of code that fixes the bug.

find this section of code:```
case 'Standard':
$ratio = $od_amount['total']/$this->get_order_total();
$products = $_SESSION['cart']->get_products();
for ($j=0; $j<sizeof($products); $j++) {
$t_prid = zen_get_prid($products[$j]['id']);
$cc_result = $db->Execute("select products_tax_class_id
from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");

                    if ($is_valid_results) {
                      $tax_rate = zen_get_tax_rate($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                      $tax_desc = zen_get_tax_description($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                      if ($tax_rate > 0) {

// $od_amount[$tax_desc] += (($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate)/100 * $ratio;
$od_amount[$tax_desc] += round(((($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2);
$od_amount['tax'] += $od_amount[$tax_desc];
}
}
}
break;
and replace with
case 'Standard':
$ratio = $od_amount['total']/$this->get_order_total();
$t_prid = zen_get_prid($products[$i]['id']);
$cc_result = $db->Execute("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
$tax_rate = zen_get_tax_rate($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
$tax_desc = zen_get_tax_description($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
if ($tax_rate > 0) {
$od_amount[$tax_desc] += round(((($products[$i]['final_price'] * $products[$i]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2); // JTD:08/30/05 - Tax Calc bug fix
$od_amount['tax'] += $od_amount[$tax_desc];
}
break;


And it now calculates correctly when coupons are applied to multiple product purchases.  You are right that it appears it was fixed in a previous version - why it came back with 1.3.6 (7?) i'm not sure.

 - Steven
19 Jan 2007, 10:56 PM
#36
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

Hmmm....

well, now I just tried it with a dollar-off coupon instead of percent-off... and its all gone to crap again.

With or without the above "fix", the taxes are incorrect. Here's an example:

Sub total: $705
Shipping: $24.16
Discount: -$10
Tax (13%): $90.92 (should be $93.49)

  • Steven

Edit: with Jeff's complete file they do work

19 Jan 2007, 11:03 PM
#37
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

Ahh.. oops. Ignore the above...
in addition to the above... find ```
case 'Standard':
$ratio = $od_amount['total']/$this->get_order_total();
$products = $_SESSION['cart']->get_products();
for ($j=0; $j<sizeof($products); $j++) {
$t_prid = zen_get_prid($products[$j]['id']);
$cc_result = $db->Execute("select products_tax_class_id
from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");

                    if ($is_valid_results) {
                      $tax_rate = zen_get_tax_rate($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                      $tax_desc = zen_get_tax_description($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                      if ($tax_rate > 0) {

// $od_amount[$tax_desc] += (($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate)/100 * $ratio;
$od_amount[$tax_desc] += round(((($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2);
$od_amount['tax'] += $od_amount[$tax_desc];
}
}
}
break;
and replace with
case 'Standard':
$ratio = $od_amount['total']/$this->get_order_total();
$t_prid = zen_get_prid($products[$i]['id']);
$cc_result = $db->Execute("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
$tax_rate = zen_get_tax_rate($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
$tax_desc = zen_get_tax_description($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
if ($tax_rate > 0) {
$od_amount[$tax_desc] += round(((($products[$i]['final_price'] * $products[$i]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2); // JTD:08/30/05 - Tax Calc bug fix
$od_amount['tax'] += $od_amount[$tax_desc];
}
break;


 - Steven
19 Jan 2007, 11:18 PM
#38
paulm avatar

paulm

Totally Zenned

Join Date:
Nov 2003
Posts:
1,878
Plugin Contributions:
5

Re: tax calculation problem with coupons and multiple products

/me is confused

@Steven: did you apply the fix to an original ot_coupon.php file? And if so, what Zen version? Or to one of the files posted here?

And I am not sure who we should "thank" for this, if anyone (although I am quite sure we can't blame Harald anymore). It's a very complicated issue. Probably not very difficult to solve in one case, or another, but very difficult to solve for all in the same script. Even getting information from the local tax office, about how to apply taxes in cases like this can be a pain in the ....

19 Jan 2007, 11:35 PM
#39
kim avatar

kim

Obaa-san

Join Date:
Jun 2003
Posts:
26,592
Plugin Contributions:
0

Re: tax calculation problem with coupons and multiple products

paulm:

And I am not sure who we should "thank" for this, if anyone (although I am quite sure we can't blame Harald anymore). It's a very complicated issue. Probably not very difficult to solve in one case, or another, but very difficult to solve for all in the same script. Even getting information from the local tax office, about how to apply taxes in cases like this can be a pain in the ....

I agree Paul, it is a very complicated issue and I was venting my frustration with having to add bandaids to the code to fix "small" parts and pieces while we are working on the rewrite of the checkout code and all of the associated parts and pieces.

The problem is there is not a one-size-fits all solution. There are as many different ways of applying taxes as there are tax rates and every country/state/region has its own specifications.

Thanks guys for working on this. :)

22 Jan 2007, 3:22 AM
#40
s_mack avatar

s_mack

Totally Zenned

Join Date:
Jun 2005
Location:
Kelowna, BC Canada
Posts:
1,033
Plugin Contributions:
4

Re: tax calculation problem with coupons and multiple products

Yeah, but its all math :) It is complicated, I agree... but i do believe there IS a one-size-fits-all solution if we look at it hard enough. The first step may be to get an understanding of how the taxes are applied in different jurisdictions. I think ultimately you'll find there aren't THAT many variations, in general anyway. Once that is determined, coding it will be easier.

It'd be pretty cool if the store owner could just select their country/province and have the tax settings pre-set for them! I volunteer for Canada/BC if it ever gets to that :)

Paul - My suggestions were to be applied to the original file for 1.3.7 of course, they weren't my suggestions... I pulled them right out of the Jeff's version. My point was that I was trying to address ONLY that issue (taxes not calculated correctly with multiple items and a coupon)... not touching the whole taxes in/excluded thing.

  • Steven