Forums / Bug Reports / [Done 1.3.6] 1.3.5 coupon tax calculation and fix

[Done 1.3.6] 1.3.5 coupon tax calculation and fix

Locked

Started by swguy on 4 Sep 2006, 1:59 PM · Views: 12,404

Results 1 to 20 of 33
This thread is locked. New replies are disabled.
4 Sep 2006, 1:59 PM
#1
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

[Done 1.3.6] 1.3.5 coupon tax calculation and fix

Note: Tax calculation for discount coupons was broken prior to 1.3.5, but the line numbers
specified below are for the 1.3.5 version of ot_coupon.php.

To reproduce:
Enable discount coupon under modules/order total. Set recalculate tax to standard.
Add two items to your cart and check out. (If you simply add one item, the tax is correct.)

Fix: In ot_coupon.php, delete the following lines: l 279-280.
Then delete lines 214-218.

Now the tax will always show as the tax on the subtotal, but if you "include tax"
in your coupon settings, the amount shown for the discount will compensate
for the price deduction. I believe that this is the correct approach, and the way
that group discounts does it (which modifies the tax) is misleading - but in any event,
the calculations are correct.

Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

4 Sep 2006, 9:37 PM
#2
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

I've done some more testing with different settings and this fix seems good.
If you want to just grab it and not have to make the file edits I've described, please
download

<moderator edit: file link removed>

Good luck,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

5 Sep 2006, 7:25 PM
#3
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,940
Plugin Contributions:
3

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

I hate to be blunt here, but I would not advise anyione to use the fixes you mentuion here,

They basically remove all of the product restriction code, which for some people is one of the major components of the coupon system.

However, if you do have a bug related to the coupon tax, I would really like to help you resolve that in a way that does not mean removing features.

I reaise tis may be difficut if you have now changed your codebase, but what would have been nice would have been a posted example from checkout showing how the tax calculation was wrong. such an example should also include details of ALL of the settings you used for the coupon module.

One thing that should be noted is that the default settings are not always the best for specific purposes, and unfortunately I have to admit that docs in this area are lacking. That will improve,

5 Sep 2006, 7:33 PM
#4
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

I posted instructions on reproducing the problem in my first post. Did you not see the problem when you followed these instructions? I can provide some more testcases tonight, but I used the default settings, the problem seemed to occur in any number of cases where I added multiple items to my cart.

Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

5 Sep 2006, 7:58 PM
#5
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,940
Plugin Contributions:
3

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Hi,

yes I did read your first post.

My point in this, With the greatest will in the world, its impossible to reproduce some one elses setup. k, so you said you were using the default settings of the coupon module, but you did not give any details of the coupon, was it a percentage or fixed coupon, I assumed it was not a restricted coupon, but that was an assumption.

All I was asking for was you to actually show some figures. If there is a bug, having the figures can actually help in debugging the problem

5 Sep 2006, 9:37 PM
#6
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

I added three items to my cart. The store is in FL, and I live in FL.
State sales tax is 7%. Products are taxable goods. Here are the numbers:

Without a coupon:
Sub-Total: $360.
Flat Rate (Best Way): $10.00
FL TAX 7.0%: $25.20
Total: $395.20
-- these numbers are correct.

Now add a 10% off coupon. Nothing special, just 10% off,
uses per customer is blank.

Coupon:
Sub-Total: $360
Flat Rate (Best Way): $10.00
Discount Coupon: SAVETEN : -$36.00
FL TAX 7.0%: $17.64
Total: $351.64
The state tax here is wrong.

with my fix:
Sub-Total: $360
Flat Rate (Best Way): $10.00
Discount Coupon: SAVETEN : -$36.00
FL TAX 7.0%: $25.20
Total: $359.20
The state tax here is correct.

Discount Coupon Settings:

Sort Order
280

Include Shipping
false

Include Tax
false

Re-calculate Tax
Standard

Tax Class
--none--

The only difference between this and the default is that include shipping is set to false.
However, sales tax is still wrong if include shipping is set to true.

The coupon itself was just a 10% coupon - nothing special.

I don't use the product restriction code, but I do need the sales tax to be correct.

Can you post a testcase in which the numbers work correctly? Or can you post
a fix that maintains the product restriction code but fixes the sales tax?

Thanks,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

5 Sep 2006, 10:05 PM
#7
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,940
Plugin Contributions:
3

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Scott,

Sorry for being so anal, but having the figures does make bugfixing so much easier,

Thanks for the detail

5 Sep 2006, 10:09 PM
#8
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

My fault; I spend all day long complaining about irreproduceable phantom bug reports and then I commit the same sin! :)

Hopefully you can dup this easily. One thing: is it possible that you've been testing with Re-Calculate Tax = None? Because this will bypass the code that I believe is causing the problem.

Thanks,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

5 Sep 2006, 10:38 PM
#9
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,940
Plugin Contributions:
3

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Actually once I had your figures and I tested against a couple of different selections of cart contents I found the bug fairly quickly.

I am a bit shamefaced that this made its way thru testing, as I was sure that I had tested this fairly comprehensivly

As mentioned the bug only shows if there is more than 1 item in the cart, the bug however has nothing to do with the restriction code.

heres my updated function. Note I would like to test this a bit more before saying that this fix is authorative, especially as I have not tested this with restrictions in place.

  function calculate_deductions($order_total) {
    global $db, $order, $messageStack;
    $tax_address = zen_get_tax_locations();
    $od_amount = array();
    if ($_SESSION['cc_id']) {
      $coupon = $db->Execute("select * from " . TABLE_COUPONS . " where coupon_id = '" . (int)$_SESSION['cc_id'] . "'");
      if (($coupon->RecordCount() > 0 && $order_total !=0) || ($coupon->RecordCount() > 0 && $coupon->fields['coupon_type']=='S') ) {
        if ($coupon->fields['coupon_minimum_order'] <= $order_total) {
          if ($coupon->fields['coupon_type']=='S') {
            $od_amount['total'] = $order->info['shipping_cost'];
            $od_amount['type'] = 'S';
          } else {
            if ($coupon->fields['coupon_type'] == 'P') {
              $od_amount['total'] = zen_round($order_total*($coupon->fields['coupon_amount']/100), 2);
           } else {
              $od_amount['total'] = $coupon->fields['coupon_amount'] * ($order_total>0);
            }
            if ($od_amount['total']>$order_total) $od_amount['total'] = $order_total;
            $products = $_SESSION['cart']->get_products();
            for ($i=0; $i<sizeof($products); $i++) {
              // speed up process and store value
              $is_valid_results = is_product_valid($products[$i]['id'], $_SESSION['cc_id']);
              if ($is_valid_results) {
                if ($coupon->fields['coupon_type'] == 'P') {
                  switch ($this->calculate_tax) {
                    case 'Credit Note':
                      $tax_rate = zen_get_tax_rate($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                      $tax_desc = zen_get_tax_description($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                      $od_amount[$tax_desc] = $od_amount['total'] / 100 * $tax_rate;
                      $od_amount['tax'] += $od_amount[$tax_desc];
                    break;
                    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 . "'");

                        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] += round(((($products[$i]['final_price'] * $products[$i]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2);
                            $od_amount['tax'] += $od_amount[$tax_desc];
                          }
                        }
                    break;
                    default:
                  }
                }
                if ($coupon->fields['coupon_type'] == 'F') {
                  switch ($this->calculate_tax) {
                    case 'Credit Note':
                      $tax_rate = zen_get_tax_rate($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                      $tax_desc = zen_get_tax_description($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                      $od_amount[$tax_desc] = $od_amount['total'] / 100 * $tax_rate;
                      $od_amount['tax'] += $od_amount[$tax_desc];
                    break;
                    case 'Standard':
                      $ratio = $od_amount['total']/$this->get_order_total();
                      $products = $_SESSION['cart']->get_products();
                        $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 . "'");

                        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] += round(((($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2);
                            $od_amount['tax'] += $od_amount[$tax_desc];
                          }
                        }
                    break;
                    default:
                  }
                }
              }
            }
          }
        }
      }
    }
    return $od_amount;
  }
5 Sep 2006, 10:52 PM
#10
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

This is good news! Please post a new copy of ot_coupon.php when you're satisfied.

Other readers of this thread: I have deleted coupon_1.3.5_fix.zip off my site; please sit tight until Wilt's comprehensive fix is ready.

Thanks,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

9 Sep 2006, 2:37 PM
#11
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Oh, by the way, I mean to ask:

Are the loops to get products_tax_class_id (l. 232, 262 in 1.3.5 ot_coupon.php) actually required?
This field is in $_SESSION['cart']->get_products(); (retrieved by shopping_cart.php l. 1182).

Similarly, the query on l. 310 of coupon.php does not appear to be required.

Thanks,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

24 Sep 2006, 9:26 AM
#12
ellivir avatar

ellivir

Zen Follower

Join Date:
Sep 2005
Posts:
444
Plugin Contributions:
0

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

wilt:

Actually once I had your figures and I tested against a couple of different selections of cart contents I found the bug fairly quickly.

I am a bit shamefaced that this made its way thru testing, as I was sure that I had tested this fairly comprehensivly

As mentioned the bug only shows if there is more than 1 item in the cart, the bug however has nothing to do with the restriction code.

heres my updated function. Note I would like to test this a bit more before saying that this fix is authorative, especially as I have not tested this with restrictions in place.

function calculate_deductions($order_total) {
global $db, $order, $messageStack;
$tax_address = zen_get_tax_locations();



Anything that would solve the same thing in the previous version, 13.0.2? I can't just upgrade on this exact moment but we're selling minus by giving too much discount. Coupon is anyway an important thing for us!

Elli

I may be blond but at least I found Zen.

24 Sep 2006, 10:10 AM
#13
ellivir avatar

ellivir

Zen Follower

Join Date:
Sep 2005
Posts:
444
Plugin Contributions:
0

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

If I put the code that Wilt put on this thread for the 1.3.0.2, it says that your discount coupon was successfully used but it doesn't make any discount at all, neither in admin side is visible any discount coupon used. :(

I may be blond but at least I found Zen.

24 Sep 2006, 11:04 AM
#14
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

A lot of logic changed in this area between 1.3.0.2 and 1.3.5, ao I'm not surprised that this fix can't be back-ported. This is a good incentive to apply the upgrade.

Good luck,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

24 Sep 2006, 1:59 PM
#15
ellivir avatar

ellivir

Zen Follower

Join Date:
Sep 2005
Posts:
444
Plugin Contributions:
0

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

swguy:

A lot of logic changed in this area between 1.3.0.2 and 1.3.5, ao I'm not surprised that this fix can't be back-ported. This is a good incentive to apply the upgrade.

Good luck,
Scott

Yeah, the shop with 1.3.0.2 was opened exactly when 1.3.5 came out, there was no time to apply that there. Now the upgrade process with huge number of products can't be made that fast and people are getting too high discounts... can't also close the shop or put under maintenance since lot of buyers are online buying...

My sums come like this:
Product 1: 320,00 EUR
Product 2: 121,00 EUR

Sub-total: 441,00EUR

Post package (1 x 9.00kg) (Post office):10,60EUR

Discount Coupon: Test456 : -20,00EUR

VAT 22%: 72,63 EUR

Total: 422,80 EUR

So, if my Sub-Total is 441,00 euro and the products have already VAT tax displayed, and then it is added Post fee of 10,60 euro, the total before discount is 551,60 euro. Then it is applied 20 euro discount, it means the total after should be 431,60 euro! But as we can see the total is 422,80 euro.

I have tried different settings in ot-coupon and I get also different totals since the tax amounts changes, but never they are correct.
The above calculation came with these settings:

This module is installed: true
Sort Order: 280
Include Shipping: false
Include Tax: false
Re-calculate Tax: Standard
Tax Class: Taxable Goods

I may be blond but at least I found Zen.

24 Sep 2006, 2:10 PM
#16
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Hmmm.... as a temporary work around, can you cancel the coupons and just use group discounts for all groups?

Good luck,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

26 Sep 2006, 8:30 PM
#17
ellivir avatar

ellivir

Zen Follower

Join Date:
Sep 2005
Posts:
444
Plugin Contributions:
0

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

swguy:

Hmmm.... as a temporary work around, can you cancel the coupons and just use group discounts for all groups?

Good luck,
Scott

Hi, I do not have groups but I got this thing working, or looks like so, with putting "Re-calculate tax" to "None". So many different combinations to try to get the right thing, but looks like this works for now...

I may be blond but at least I found Zen.

26 Sep 2006, 9:47 PM
#18
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Recalculate tax=none gives you the original tax figure, which is the most accurate figure available. Order total modules typically make assumptions about tax calculations (to simplify their logic) which presently make them less accurate.

Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

28 Sep 2006, 5:35 AM
#19
ttalk avatar

ttalk

New Zenner

Join Date:
Sep 2006
Posts:
27
Plugin Contributions:
0

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

Is Wilt's suggested fix on the previous page good enough? If not, when can we expect an update?

28 Sep 2006, 9:41 AM
#20
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: [Done 1.3.6] 1.3.5 coupon tax calculation and fix

I have not heard any complaints about Wilt's fix.

Good luck,
Scott

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.