Zen Cart Logo
Forums / Bug Reports / Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Views: 29,268

Results 41 to 59 of 59
9 Jun 2016, 12:18 PM
#41
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

GoverT:

Solved! (At least for us)

Line 456 of includes/classes reads:

'final_price' => zen_round($products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']), $decimals),

It should be without rounding:

'final_price' => $products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']),

Then everything is fine! :D

Hope this works for you too frank18!

Thanks G/T - I will revert to the standard code and then apply your suggestion (code looks sort of familiar from older releases).

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

10 Jun 2016, 12:06 AM
#42
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

frank18:

Thanks G/T - I will revert to the standard code and then apply your suggestion (code looks sort of familiar from older releases).

Didn't work for me.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

10 Jun 2016, 5:23 AM
#43
govert avatar

govert

New Zenner

Join Date:
May 2016
Location:
Netherlands
Posts:
17
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Hello frank18,
You did adjust the Tax decimal places in Admim-store to 2?

10 Jun 2016, 8:22 AM
#44
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

GoverT:

Hello frank18,
You did adjust the Tax decimal places in Admim-store to 2?

No, changing the tax decimals only changes the padding in the display of the tax amount. Eg, you set the tax decimals to '0' then the customer is presented with say 10%, now set the tax decimals to '2' then the customer sees 10.00% on the invoice. You can test this by loading an existing invoice in admin, don't close the tab in your browser, then change the tax decimals from '2' to '0' or whatever. Next you reload the invoice in the browser and you see the padding change. But, it has no influence on the underlying code calculations.

I reverted back to my own suggestions as per post #14 but made one additional change in includes/classes/order.php (code starts around line 449):

      $taxRates = zen_get_multiple_tax_rates($products[$i]['tax_class_id'], $taxCountryId, $taxZoneId);
      $this->products[$index] = array('qty' => $products[$i]['quantity'],
                                      'name' => $products[$i]['name'],
                                      'model' => $products[$i]['model'],
                                      'tax_groups'=>$taxRates,
                                      'tax_description' => zen_get_tax_description($products[$i]['tax_class_id'], $taxCountryId, $taxZoneId),
                                      'price' => $products[$i]['price'],
                                      //'final_price' => zen_round($products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']), $decimals),
                                      'final_price' => zen_round($products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']), 4), // changed to 4 decimals for rounding calcs -- frank18
                                      'onetime_charges' => $_SESSION['cart']->attributes_price_onetime_charges($products[$i]['id'], $products[$i]['quantity']),
                                      'weight' => $products[$i]['weight'],
                                      'products_priced_by_attribute' => $products[$i]['products_priced_by_attribute'],
                                      'product_is_free' => $products[$i]['product_is_free'],
                                      'products_discount_type' => $products[$i]['products_discount_type'],
                                      'products_discount_type_from' => $products[$i]['products_discount_type_from'],
                                      'id' => $products[$i]['id'],
                                      'rowClass' => $rowClass);

Next I did tests with

  1. a tax free product
  2. a product on sale (special price)
  3. a product with quantity discounts (attributes)

all with varying quantities in the cart

Every single scenario showed up correct in the cart, the checkout pages, the final order and in the invoice, there were no rounding errors at all no matter which scenario I chose.

I will leave it at that until someone can come up with a more elegant (and thoroughly tested) solution.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

10 Jun 2016, 9:11 AM
#45
govert avatar

govert

New Zenner

Join Date:
May 2016
Location:
Netherlands
Posts:
17
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Okay what works for you is good enough. Again, I'm no programmer but I think the tax decimal places affect the tax amount. Tax decimal places also pad the tax AMOUNT this amount of decimal places. For example, if the amount of tax was $3.05 and tax decimal places was set to 0, I think it would display and calculate as $3. You could at least try. ;) My solution is thoroughly tested and you can see it at work in our webshop. It also makes sense that the not rounded amount excluding tax and the not rounded tax is exactly the right amount including tax, if you show prices including tax in your webshop. I will leave it at this too but it would be great to get the elegant credit for it :smile:

20 Aug 2016, 8:32 PM
#46
webchills avatar

webchills

Zen Follower

Join Date:
Sep 2005
Location:
Austria
Posts:
99
Plugin Contributions:
1

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

First of all I want to thank frank18 for the suggested fixes in includes/classes/shopping_cart.php, includes/classes/order.php, includes/classes/currencies.php and includes/functions/functions_prices.php.
I have implemented these changes in a 1.5.5a store and the sub-total calculations are now fine.

To demonstrate that this is a big issue especially for European store owners that persists now for years and is still alive in the latest 1.5.5a look at this Vanilla 1.5.5a store:
https://www.webchills.at/155-en-vanilla/

The store is setup to display prices with tax both in frontend and admin.
The tax rate is 20%
Look at the product Subtotal Wrong
The price is set to 4.9167 net which is 5.90 gross
Add 10 units to the cart and go to checkout (login credentials provided in the product description) and you get a Sub-Total of 59.04
No customer will understand such a calculation, this is a nightmare for shop owners.

Now look at the same store with the frank18 fixes here:
https://www.webchills.at/155-en-roundingfix/
The product Subtotal Wrong is now as it should be
I would really appreciate if these fixes would make their way to the current 1.5.5

Unfortunately there is a further issue which the frank18 fixes do not cure:
Look at the Priced by Attributes product in the vanilla store.
Product is priced by attributes
Product Price: 0
Attribute 1 Price: 0.5833 (net)
Attribute 2 Price: 0.6250 (net)

Attribute 2 Price is shown correctly as 0.75 (gross)
Select Attribute 2 (Size Large) and add it to cart
Price in cart and checkout grows up to 0.76 which is wrong

21 Aug 2016, 1:27 AM
#47
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

webchills:

First of all I want to thank frank18 for the suggested fixes in includes/classes/shopping_cart.php, includes/classes/order.php, includes/classes/currencies.php and includes/functions/functions_prices.php.

Thanks for that! Glad that I get this confirmed ....

webchills:

Unfortunately there is a further issue which the frank18 fixes do not cure:
Look at the Priced by Attributes product in the vanilla store.
Product is priced by attributes
Product Price: 0
Attribute 1 Price: 0.5833 (net)
Attribute 2 Price: 0.6250 (net)

Attribute 2 Price is shown correctly as 0.75 (gross)
Select Attribute 2 (Size Large) and add it to cart
Price in cart and checkout grows up to 0.76 which is wrong

Zum Teufel!!! :shocking: What I don't get is that Attr. 1 is showing correctly and Attr. 2 does not...... why??

Could you do 2 things please:

  1. compare the 2 attribute configurations in admin and check that they have been set up identically.

and if they are identical then

  1. change the value of 0.5833 to say 0.5875 net and see what happens - should show 0.71 gross with 20% tax applied (it is 0.705 in real terms which mathematically rounds up to 0.71)

FWIW Many other applications which are using tax / roundings etc, the end result shows up as out by a few cents. I am seeing this sometimes on invoices received from my suppliers.... so ZC is not alone.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

21 Aug 2016, 6:59 AM
#48
webchills avatar

webchills

Zen Follower

Join Date:
Sep 2005
Location:
Austria
Posts:
99
Plugin Contributions:
1

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

frank18:

Zum Teufel!!! :shocking: What I don't get is that Attr. 1 is showing correctly and Attr. 2 does not...... why??
I don' t get this as well :-)

frank18:

  1. compare the 2 attribute configurations in admin and check that they have been set up identically.

They are identical, see attached screens

frank18:

  1. change the value of 0.5833 to say 0.5875 net and see what happens - should show 0.71 gross with 20% tax applied (it is 0.705 in real terms which mathematically rounds up to 0.71)
    Did that for you, see the new product Priced by Attributes 2

frank18:

FWIW Many other applications which are using tax / roundings etc, the end result shows up as out by a few cents. I am seeing this sometimes on invoices received from my suppliers.... so ZC is not alone.
I know that Zen Cart is not alone with that. We did come a long way and have a long history of rounding issues and fixes. The attribute example is coming from a clients store which depends on attribute pricing. To avoid such issues I normally do not use attribute pricing at all. But sometimes it's inevitable and we should be able to calculate it correctly.

8 Dec 2016, 11:50 AM
#49
sarahl avatar

sarahl

Zen Follower

Join Date:
May 2004
Location:
UK
Posts:
471
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Thank you again - upgraded to 1.5.5a - still have decimal issue - your changes agian seem to have resolved it - tad frustrating still a problem but THANK YOU for keeping this up to date with newer versions

28 Dec 2016, 5:26 PM
#50
reneetje avatar

reneetje

New Zenner

Join Date:
Jun 2009
Posts:
42
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

I never had any issues with this untill I recently had my shop upgraded tot 1.5.5b. Suddenly prices show incorrectly at checkout-page and are a cent off...in 1.5.4 all worked fine.

I tried the solution as mentioned by FRANK18 (post 14 and 44 )and see some differences but at the end when checking out the final confirmation does still not match.

How to fix...anyone has an idea?

29 Dec 2016, 4:05 AM
#51
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Reneetje:

I never had any issues with this untill I recently had my shop upgraded tot 1.5.5b. Suddenly prices show incorrectly at checkout-page and are a cent off...in 1.5.4 all worked fine.

I tried the solution as mentioned by FRANK18 (post 14 and 44 )and see some differences but at the end when checking out the final confirmation does still not match.

How to fix...anyone has an idea?

Just tested my modifications (post 14 and 44) in a 1.5.5b installation and it still works as desired. Are you sure you applied all the changes as outlined?

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

24 Feb 2017, 6:22 PM
#52
evilgrin avatar

evilgrin

New Zenner

Join Date:
Aug 2016
Location:
Essex, UK
Posts:
1
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Running 1.5.5d here and we are seeing these rounding issues. Have applied the changes as suggested in post 14 and 44.

Attachment 16952

As you can see from the screenshot, the shopping cart total differs from the sub-totals at the checkout, although the overall total is correct. Have I missed something obvious? I've double checked, checked again..... Any tips would be appreciated.

20 Mar 2017, 12:31 PM
#53
paulm avatar

paulm

Totally Zenned

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

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

I just applied the changes to includes/classes/order.php and includes/classes/currencies.php as described in post #14 on a 1.5.5d store, and it looks like the rounding errors are fixed. I didn't apply any of the the other edits mentioned (as everything seemed to work already after I made those two changes).

Thanks for the fix and sharing! :smile:

18 May 2017, 6:22 PM
#54
wolfderby avatar

wolfderby

Zen Follower

Join Date:
Dec 2008
Location:
Pittsburgh, PA
Posts:
241
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

After Franks post 14 and 44 changes, (in 1.5.5e) I have a product priced at "0.9346" (so that w/ 7% sales tax it'd be exactly $1 (well calcs to 1.000022) and with (quantity) 3 of them in the shopping cart, the 7% sales tax calculated correctly at $.20, but the sub-total was always $2.99

...basically I changed all of the round( ..., 2) ...to two decimal places to rounding to 4 decimal places ...", 4)"
...I also didn't make (undid) the function_prices.php 4 to 2 changes (i.e. > "4, '.', '')" becomes "2, '.', '')" I didn't do it)

...and it works for me as it should in my opinion calculating to $3 for (3 x .9346) * 1.07 = (3.000066) aka $3 not $2.99

18 May 2017, 11:43 PM
#55
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

wolfderby:

After Franks post 14 and 44 changes, (in 1.5.5e) I have a product priced at "0.9346" (so that w/ 7% sales tax it'd be exactly $1 (well calcs to 1.000022) and with (quantity) 3 of them in the shopping cart, the 7% sales tax calculated correctly at $.20, but the sub-total was always $2.99

...basically I changed all of the round( ..., 2) ...to two decimal places to rounding to 4 decimal places ...", 4)"
...I also didn't make (undid) the function_prices.php 4 to 2 changes (i.e. > "4, '.', '')" becomes "2, '.', '')" I didn't do it)

...and it works for me as it should in my opinion calculating to $3 for (3 x .9346) * 1.07 = (3.000066) aka $3 not $2.99

Could you please take a peek at post #34 and then post your corresponding settings (the actual settings in your store) here.

Thanks

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

19 May 2017, 3:04 PM
#56
wolfderby avatar

wolfderby

Zen Follower

Join Date:
Dec 2008
Location:
Pittsburgh, PA
Posts:
241
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

frank18:

Could you please take a peek at post #34 and then post your corresponding settings (the actual settings in your store) here.

Thanks

Here are my settings:
Tax Decimal Places 0
Display Prices with Tax false
Display Prices with Tax in Admin false
Basis of Product Tax Store
Basis of Shipping Tax Store
Sales Tax Display Status 0
Show Split Tax Lines false

Actually frank, I think I messed something up w/ this because now my payment modules aren't loading properly, I'm going to keep digging through the code today in hopes of figuring out why and post back.

13 Jun 2017, 5:22 PM
#58
jpda avatar

jpda

Zen Follower

Join Date:
Jun 2014
Posts:
157
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

GoverT:

Solved! (At least for us)

Line 456 of includes/classes reads:

'final_price' => zen_round($products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']), $decimals),

It should be without rounding:

'final_price' => $products[$i]['price'] + $_SESSION['cart']->attributes_price($products[$i]['id']),

Then everything is fine! :D

Hope this works for you too frank18!

Trying to get rid of the subtotal error in zc v1.5.5e, I found this post, among many other posts(!).
I only changed this line in v1.5.5e. and the subtotal error disappeared.
Still have to look at all the other changes that need to be done related to rounding errors.

However, I wonder why rounding was introduced at this line in the first place. ZC v1.5.4 didn't have it. I checked in previous v155's and I saw it introduced in v155, includes/classes/order.php, @version $Id: Author: DrByte Fri Jan 1 12:23:19 2016 -0500 Modified in v1.5.5 $.
I wonder why that was needed at that time.
Anybody any thoughts?

Thanks,

jpda

30 Aug 2018, 4:47 PM
#59
sarahl avatar

sarahl

Zen Follower

Join Date:
May 2004
Location:
UK
Posts:
471
Plugin Contributions:
0

Re: Shopping cart Subtotal not equal Checkout Subtotal (rounding error) with GST/VAT

Ok so once again I am struggling with this and not sure where to go - I have had -frank18 changes working perfectly in ZC153 for ages (14 & 44) - however only when we started to look at changing prices have we spotted that there are still 2 places where the price is incorrect - not sure if these are new or old changes though... and not sure which PHP files to check

So ZC 153
UK VAT
Prices inclusive shown

you can see from my 3 attachments that when you look at the

Product Price Manager page the price next to the dropdown is wrong - rounding up
The price in catalog > Categories/Products - inline price shown here is also wrong - again by 1p
and lastly when the order has been completed the invoice itself - the inline prices in the table are also wrong - but sub total and vat total etc are correct

Any ideas which files to check to try to correct these 3 locations please?

Thank you
Sarah