If this is your first visit, check our documentation. To post, register or login.
Forums / Bug Reports / [Done v2.0]Currency Rounding Issues with quantity discounts

[Done v2.0]Currency Rounding Issues with quantity discounts

Locked

Started by redwax on 30 Oct 2009, 12:52 AM · Views: 3,769

Results 1 to 20 of 20
This thread is locked. New replies are disabled.
30 Oct 2009, 12:52 AM
#1
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

[Done v2.0]Currency Rounding Issues with quantity discounts

I am using zen cart 1.3.8a and have an issue with currency rounding that is causing me an issue with quantity discounts.

I have a currency rate value set at 1.14999998

I have a product which has a quantity discount set for 4 @ 2.17 which gets rounded to 2.50 in the display.

Unfortunately the cart total shows as 2.5 * 4 = 9.98 instead of 10.00.

It would appear that rather than the displayed rounded value of 2.5 being multiplied, the un-rounded value of 2.17 * 1.14999998 = 2.4954999566 is being multiplied by 4 which does in fact round to 9.98.

Is there anyway for me to get zen cart to multiply the first rounded number of 2.5?

30 Oct 2009, 11:51 AM
#2
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

I should point out also that I am not charging sales tax so there should be no calculation issue with this aspect.

31 Oct 2009, 4:04 PM
#3
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

You could try this fix here.

http://www.zen-cart.com/forum/showpost.php?p=689143&postcount=25

However, have to ask, the link to your website shows that you are based in the UK. Why are you using any conversion rate at all. ?

31 Oct 2009, 6:31 PM
#4
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Yeah, I tried that already and it didn't help. Presumably the code that generates the price using this function specifies the precision? Is there any reason why this can't be changed to be the same presision as the code that generates the prices that are displayed and where would this be done?

Alternatively simply multiplying the displayed price (which is rounded to 2 decimal places) would seem like a good plan no?

Regarding the reasons why I am usng a currency multiplier whilst residing in the UK. I am not charging tax currently although I perceived that I might be and so I inputted all my prices as net of what the vat would be, set tax at 0% and currency to the current vat rate is (for some reason, and I can't remember why, this came out at a strange number of 1.4999...). I figured If I neded to charge vat I would simply create a vat rate and change the currency rate back to 1 and hey presto (all the products have tax classes set where appropriate to make this work).

I know I could have written a script to amend the the prices in the db but this would have taken longer :smile:

31 Oct 2009, 7:53 PM
#5
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Hi,

The precision is usually based on the currency settings. Which for GBP is always 2, so there's no real reason to change that.

And again, i'm still confused about your reasons for putting the multiplier in.

If all of your products are set as 'taxable' but no tax rates are set, then you won't charge VAT at output.

If later on, you become vat registered, then you only need to set the tax rate, not change all your product prices.

31 Oct 2009, 7:58 PM
#6
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

And in fact,

Surely all the prices you have displayed will include the multiplier. so are not VAT exclusive prices.

?????

31 Oct 2009, 8:06 PM
#7
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

I will continue to charge the same amount for the items when VAT registered, so I would have had to ammend the prices down, otherwise they would appear to have gone up by the VAT rate when applied? The fact that an element of the sale price then become vat is offset by the fact that I payed the vat when I bought the items.

This aside, because it is slightly off topic initself anyway, there still exists a calculation issue and whilst the currency is displayed to 2 decimal places calculations made within the code do not honour this as outlined in my first post where I show how it has calculaed it. There must be somewhere I can change the precision on the price before it is subjected to the quantity multiplier.

I could see this being an issue generally when people are using multiple currencies?

31 Oct 2009, 8:18 PM
#8
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Hi,

I don't disagree that the rounding problem is separate from the issue of how you have set up your tax regimen :)
Was just trying to understand your setup.

I'll try and reproduce this on a stock 138 install and get back to you.

31 Oct 2009, 9:29 PM
#9
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

I know it is a bit of a fudge but it seemed the easiest solution at the time.

Let me know how you get on with reproducing the problem - I eagerly await :smile:

31 Oct 2009, 9:59 PM
#10
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Just another point,

You used 1.14999998 as the rate value. Strictly speaking as the current VAT rate is 15%, that should be 1.15,

Not much difference, but important.

I don't want to harp on about VAT (as this is really a subject for an accountant) , as opposed to the bug itself, but do you realise that if you do become VAT registered, and therefore liable to pay the VAT on your ouptuts you will in fact be making less profit per item. ?

31 Oct 2009, 10:17 PM
#11
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

wilt:

You used 1.14999998 as the rate value. Strictly speaking as the current VAT rate is 15%, that should be 1.15,
I know, I can't remember where I invented 1.14999998 from :blush:
wilt:

I don't want to harp on about VAT (as this is really a subject for an accountant) , as opposed to the bug itself, but do you realise that if you do become VAT registered, and therefore liable to pay the VAT on your ouptuts you will in fact be making less profit per item. ?
Not so! Well, marginally, but obviously I paid VAT on the goods purchased which I would be able to offset if I was registered and charging VAT, so yes, there would be a small deduction in profit directly but ultimately I would only be interested in the net price of goods I bought rather than the gross value.

31 Oct 2009, 10:21 PM
#12
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Just another question.

What is the original value of the product, and what settings do you have for the quantity discount for that product.

31 Oct 2009, 10:28 PM
#13
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Original value is 2.60 and I have qty discount set for actual price of 2.17 for min qty of 4. The admin interface also shows calculated price of £2.50 = 4x £9.98

31 Oct 2009, 10:32 PM
#14
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

and just to double check,

What is your Zen Cart version.

31 Oct 2009, 10:35 PM
#15
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

not that it matters, as I can duplicate the problem in a stock 138 install.

Bear with me while I try and find the prob.

1 Nov 2009, 12:06 AM
#16
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

For the record, it's 1.3.8a.

Thanks a million for your help Wilt :smile:

17 Nov 2009, 2:50 PM
#17
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

wilt:

not that it matters, as I can duplicate the problem in a stock 138 install.

Bear with me while I try and find the prob.

Did you manage to figure out what was going on here Wilt? I have had a good play but am truly stumped.

17 Nov 2009, 6:36 PM
#18
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

I've looked a little deeper....

Zen cart calculates the base price * quantity before applying the currency rate and rounding.

It appears that there are three functions primarily involved with this:

function display_price();
function format();
function show_total();

Getting the correct per item calculation is easy enough by modifying:

  function display_price($products_price, $products_tax, $quantity = 1) {
    return $this->format(zen_add_tax($products_price, $products_tax) * $quantity);
  }
}
```to:

function display_price($products_price, $products_tax, $quantity = 1) {
if (empty($currency_type)) $currency_type = $_SESSION['currency'];

  $rate = (zen_not_null($currency_value)) ? $currency_value : $this->currencies[$currency_type]['value'];
  $products_price = zen_round($products_price * $rate, $this->currencies[$currency_type]['decimal_places']);
  $products_price = $products_price * $quantity;
  $format_string = $this->currencies[$currency_type]['symbol_left'] . number_format($products_price, $this->currencies[$currency_type]['decimal_places'], $this->currencies[$currency_type]['decimal_point'], $this->currencies[$currency_type]['thousands_point']) . $this->currencies[$currency_type]['symbol_right'];

if ((DOWN_FOR_MAINTENANCE=='true' and DOWN_FOR_MAINTENANCE_PRICES_OFF=='true') and (!strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR']))) {
  $format_string= '';
}
return $format_string;

}
}


This uses parts of the format() function hard coded into display_price() rather than calling it.  Not too much of a drama (although I don't know what else this might brake), except the cart totals uses a huge function - show_total() which does the quantity calculations before function format() which applies the currency and rounding calculation.

All this requires a fair bit of time to put right - it has taken me an afternoon writing the above alternate function and realising the issue with the other function.

This would appear to be a fundamental floor in how zen cart calculates pricing.  I am sure there was a clear design objective that steered the development down this path but I can't think what it might have been.
17 Nov 2009, 8:48 PM
#19
wilt avatar

wilt

Oji-san

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

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

Hi,

While your fix will certainly correct cart totals, it breaks when you go to checkout.

The problem here is that the order totals use a slightly different method for calculating their values.

Unfortunately this is not easy to fix (although I am quite willing to be proved wrong)

V1.3.x code does uses a very poor model for the calculation & storage of line item data. unfortunately we are stuck with this from the original osC code.

There may be a workaround in that I believe restricting the currency conversion rate to say 2 decimal places might bypass the discrepancy (although I have not tested that)

If anyone has any better solution I'd be really grateful to see it.

18 Nov 2009, 9:10 AM
#20
redwax avatar

redwax

New Zenner

Join Date:
Feb 2007
Posts:
18
Plugin Contributions:
0

Re: [Done v2.0]Currency Rounding Issues with quantity discounts

wilt:

While your fix will certainly correct cart totals, it breaks when you go to checkout.

Yeah, I know, it doesn't even show the correct sub-total amount. Basically, fixing this problem would require a rewrite of the following functions:

display_price();
format(); (this is used quite extensively from what I can see)
show_total();

Which, it itself isn't too much of a drama but who knows what else will break and owing to the life-cycle of the 1.3x series I'm not sure it is time worth spending.

Is this something that has been addressed in the ever delayed v2 do you know?