If this is your first visit, check our documentation. To post, register or login.
Forums / Code Matters / Bug Reports / reopen thread [Done v1.3.9] Discount groups and tax calc problem with tax-included pr

reopen thread [Done v1.3.9] Discount groups and tax calc problem with tax-included pr

Locked

Started by antonios on 30 Jun 2010, 7:22 PM · Views: 3,569

Results 1 to 18 of 18
This thread is locked. New replies are disabled.
30 Jun 2010, 7:22 PM
#1
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #1 by antonios

I'm running 1.3.9d and still have the same tax problems as in 1.3.8 involving tax calculation and group discounts
The issue is said to be solved in 1.3.9 but it isn't

Here's what's happening:

These results are all correct when buying a product with free shipping:

Sub-Total:€16.95
Free Shipping Options (Free Shipping):€0.00
Group Discount:-€5.09
TAX 6%:€0.67
Total:€11.86

I was very happy about this, since this wasn't working correct in 1.3.8. But now there's the problem when adding shipping costs/tax

Once I choose flat rate shipping (TAX 19%) it messes up everything:

Sub-Total:€16.95
Flat Rate (Best Way):€5.65
Group Discount:-€4.82 -- incorrect
TAX 6%:€0.81 --incorrect
TAX 19%:€0.76 --incorrect
Total:€17.78 --incorrect

I've tried every sort order in ordertotal, every config in shipping and in grouppricing, nothing seems to help.

Please reopen this issue, or please help me with this.

Greetings Antonios

30 Jun 2010, 9:33 PM
#2
pasi avatar

pasi

Zen Follower

Join Date:
Mar 2004
Location:
Finland
Posts:
435
Plugin Contributions:
1

Post #2 by pasi

This does not only affect the group discount, but basicly every discount module... They are flawed.

I had to rewrite ALL of them to get a rest for my soul, because they were always calculating things wrong.

And no, it's not about settings, when you strip the code line by line and think it logically, you will realize that it calculates things wrong in it's logic... mostly regarding to shipping costs.

Working with Zen Cart since 2003 :: www.prr.fi
Author of the original Finnish language pack for Zen Cart since 2004

30 Jun 2010, 9:48 PM
#3
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #3 by antonios

Hello Pasi,

Thanks for replying.
Glad it wasn't just me, thought I was missing something.

Can you tell me which lines you rewrote or is that too much to write down here.

Greetings Antonios

1 Jul 2010, 5:12 PM
#4
wilt avatar

wilt

Oji-san

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

Post #4 by wilt

If you are running v139, then try this

In includes/modules/order_totals/ot_group_pricing.php at around line 67 you should see a commented out line

//    if ($this->include_tax != "true" && $this->include_shipping != 'true') $order_total += $order->info['shipping_tax'];

and remove the // so the line is no longer commented, and test again.

Note. while I have tested this fix with some other variations of group discount and tax with no deleterious results, I have not tested enough combinations yet, to say whether its the correct fix for all use cases.

1 Jul 2010, 5:17 PM
#5
wilt avatar

wilt

Oji-san

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

Post #5 by wilt

moved to bug reports

1 Jul 2010, 7:12 PM
#6
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #6 by antonios

Hello Wilt,

I've tried your fix, but it doesn't make a difference.
I've tried it in combination with every possible setting but unfortunately it didn't help.

I'm getting really pissed off by this bug. Group discount is quite an essential feature for webshops, at least it is for ours.. It does work, if you don't charge shipping costs with tax which is obligatory in the netherlands.

Well, if anyone has a hint, please feel free to write it down.

Greetings Antonios

1 Jul 2010, 7:55 PM
#7
wilt avatar

wilt

Oji-san

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

Post #7 by wilt

k, can you post what you expect the output to be.

I'm setting up something that uses all the settings you have, and will post back here shortly with what current code does output.

2 Jul 2010, 9:44 AM
#8
waddtown avatar

waddtown

New Zenner

Join Date:
Feb 2005
Location:
UK
Posts:
77
Plugin Contributions:
0

Post #8 by waddtown

I am also having a similar problem with the discount not calculation properly, I posted a thread here
http://www.zen-cart.com/forum/showthread.php?t=154755
some time ago but I never had a reply
Not sure if its related to this problem or not.
Any input appreciated

2 Jul 2010, 4:53 PM
#9
wilt avatar

wilt

Oji-san

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

Post #9 by wilt

k,

The fix I gave definitely works, and produces correct totals, EXCEPT if you have split tax lines. e.g. where each tax group is shown as a separate line in checkout confirmation.

While the total of the taxes is correct, the individual lines are not.

I have a fix for that as well, and i'm just testing at the moment

2 Jul 2010, 9:00 PM
#10
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #10 by antonios

Hello Wilt,

Here's what I'm trying to achieve.

First I should say I use two tax rates: 6% and 19%
Second I use discount groups, for loyal customers etc.
And third the shop displays prices incl tax.

When a customer in my discount group orders for €100.00 incl 6% tax this is what the calculation must look like:

Sub-Total: €100.00
Group Discount 30%: -€30.00
Shipping cost (incl 19%): €5.65
TAX 6%: €3.96
TAX 19%: €0.90
Total: €75,65

The group discount is subtracted from the subtotal with tax included, so the tax mentioned should be calculated after the discount is subtracted

When there are no shipping costs charged, the calculation is correct.
Even when a customer orders products with 6% tax AND products with 19% tax the calculation is correct (as long as there are no shipping costs).

Things only get messed up when shipping cost are charged.

Greetings Antonios

2 Jul 2010, 9:11 PM
#11
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #11 by antonios

One thing I'd like to add:
When a customer orders products (any tax) and is NOT in a discount group the calculation is also correct, even when shipping costs are charged.

So it's clear that the problem is where there's a combination of shipping costs/tax and discount groups.

2 Jul 2010, 9:21 PM
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,512
Plugin Contributions:
176

Post #12 by drbyte

And have you tried setting your Tax settings to NOT display the tax details split into multiple lines?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Jul 2010, 9:31 PM
#13
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #13 by antonios

Yes I've tried that, also incorrect:

Sub-Total: €16.95
Flat Rate (Best Way): €5.65
Group Discount: -€4.82 (should be -€5.09)
tax 6% + tax 19%: €1.57 (total of tax seems ok, doesn't mean that the calculation is)
Total: €17.78 (should be €17.51)

Like I said, the problem isn't the difference in tax rates, but the combination of group discounts and shipping tax.

Greetings Antonios

8 Jul 2010, 8:39 AM
#14
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #14 by antonios

I've been coding and puzzling , still no result.
If nobody knows how to fix this, I think I have to leave zen-cart for what it is and find webshop software that can calculate taxes etc.

What a mess and what a loss .. :cry:

8 Jul 2010, 11:27 AM
#15
wilt avatar

wilt

Oji-san

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

Post #15 by wilt

Hi

Here's the patch/diff that I applied to Zen Cart v139d to fix the problem.

--- includes/modules/order_total/ot_group_pricing.php
+++ iincludes/modules/order_total/ot_group_pricing.php
@@ -61,14 +61,26 @@
 //    echo "order total = $order_total<br>";
 //    echo "order total shipping = {$order->info['shipping_cost']}<br>";
 //    echo "order total tax = {$order->info['tax']}<br>";
     if ($this->include_shipping != 'true') $order_total -= $order->info['shipping_cost'];
     if ($this->include_shipping != 'true') $order_total_tax -= $order->info['shipping_tax'];
+    $taxGroups = array();
+    foreach ($order->info['tax_groups'] as $key=>$value) {
+      if (isset($_SESSION['shipping_tax_description']) && $key == $_SESSION['shipping_tax_description'])
+      {
+        if ($this->include_shipping != 'true')
+        {
+          $value -= $order->info['shipping_tax'];
+        }
+      }
+      $taxGroups[$key] = $value;
+    }
     if ($this->include_tax != 'true') $order_total -= $order->info['tax'];
- //     if ($this->include_tax != "true" && $this->include_shipping != 'true') $order_total += $order->info['shipping_tax'];
+     if ($this->include_tax != "true" && $this->include_shipping != 'true') $order_total += $order->info['shipping_tax'];

     $orderTotalFull = $order_total;
 //    if (DISPLAY_PRICE_WITH_TAX != 'true') $order_total += $order_total_tax;
 //    echo "order total* = $order_total<br>";
-    $order_total = array('totalFull'=>$orderTotalFull, 'total'=>$order_total, 'tax'=>$order_total_tax);
+    $order_total = array('totalFull'=>$orderTotalFull, 'total'=>$order_total, 'tax'=>$order_total_tax, 'taxGroups'=>$taxGroups);
     return $order_total;
   }
   function calculate_deductions($order_total) {
@@ -77,6 +89,7 @@
     if ($order_total == 0) return $od_amount;
     $orderTotal = $this->get_order_total();
     $orderTotalTax = $orderTotal['tax'];
+    $taxGroups = $orderTotal['taxGroups'];
     $group_query = $db->Execute("select customers_group_pricing from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$_SESSION['customer_id'] . "'");
     if ($group_query->fields['customers_group_pricing'] != '0') {
       $group_discount = $db->Execute("select group_name, group_percentage from " . TABLE_GROUP_PRICING . "
@@ -104,11 +117,11 @@
           }
           $adjustedTax = $orderTotalTax * $ratio;
           if ($order->info['tax'] == 0) return $od_amount;
-          $ratioTax = $adjustedTax/$order->info['tax'];
+          $ratioTax = $adjustedTax/$orderTotalTax;
           reset($order->info['tax_groups']);
           $tax_deduct = 0;
-          foreach ($order->info['tax_groups'] as $key=>$value) {
-            $od_amount['tax_groups'][$key] = $order->info['tax_groups'][$key] * $ratioTax;
+          foreach ($taxGroups as $key=>$value) {
+            $od_amount['tax_groups'][$key] = $value * $ratioTax;
             $tax_deduct += $od_amount['tax_groups'][$key];
           }
]
8 Jul 2010, 6:15 PM
#16
antonios avatar

antonios

New Zenner

Join Date:
Sep 2008
Posts:
25
Plugin Contributions:
0

Post #16 by antonios

Wilt!
You've just made a girl extremely happy! :clap:

It works like a charm, with and without tax split lines, with and without shipping costs and with multiple tax rates.

Thank you, thank you, thank you.

I hope the zen-cart team will add this patch to their next update.

Greetings Antonios

8 Jul 2010, 7:36 PM
#17
mooomers avatar

mooomers

New Zenner

Join Date:
Mar 2009
Posts:
33
Plugin Contributions:
0

Post #17 by mooomers

strange.. i put in the changes from your diff, and it's calculating the discount wrong. and the tax is also still wrong.

What did i screw up?

The discount is 20%

Your Total

Sub-Total:$82.50
Group Discount:-$17.00
Canada Post (Expedited, 2010-07-13):$9.59
13% HST # + 13% Shipping HST #:$10.08
Total:$85.17

8 Jul 2010, 7:51 PM
#18
mooomers avatar

mooomers

New Zenner

Join Date:
Mar 2009
Posts:
33
Plugin Contributions:
0

Post #18 by mooomers

i forgot to put what the correct values should have been:

Sub-Total:$82.50
Group Discount:-$17.00 (should be -$16.50)
Canada Post (Expedited, 2010-07-13):$9.59
13% HST # + 13% Shipping HST #:$10.08 (should be $9.83)
Total:$85.17 (should be $85.42)