Page 1 of 1

Control over sequence of math operations

Posted: Sat May 13, 2017 9:59 pm
by MrPhil
Something that came up in viewtopic.php?f=20&t=8918 is that the totals didn't quite match those of another accounting product, because (apparently) adding, multiplication by tax rate (non-integer value), and rounding are not done in the same order between OC and the other product. Actually, this can be seen on a hypothetical page where you get cross totals: add up the columns of item extended prices, apply the tax rate to that subtotal, and get the rounded grand total. If you also apply taxes on each line item, round to display the subtotal (per line), and add up that column, you will likely get a slightly different result than in the first case! The same principle applies across software applications or if you have non-integer quantities (yards of cloth or metres of rope, for example).

You would get the same grand total if you carried through the unrounded values and added up those, but then the total of the rounded display values in a column (a customer checking your math) would not match the grand total.

There are two closely-related feature requests that spring from this. 1) control over the sequence of operations (add/subtract, apply a percentage, rounding) in OC to match what the other application is doing, so there is no discrepancy, and 2) when there are cross totals (as described in the first paragraph), specify which is to be used (or if unrounded values are to be summed) and if there is any difference in the values (this row, or the column total) offer a footnote or pop-up to explain it. This should keep customers from complaining that your store can't do basic math! Unfortunately, this might not be something that an extension can handle, but would have to be core feature.

Re: Control over sequence of math operations

Posted: Tue May 16, 2017 10:31 pm
by Johnathan
I don't think Daniel reads the Feature Requests forum much (though I don't know for sure) but I'm sure extension developers do when they need to find new ideas, so it's worth keeping up even if they won't be incorporated into the core.

For your need, I don't think there's an extension that does this, though it could be done. It would be complicated, though, so it's probably not likely someone will take it on. If you do need something in the future and don't mind paying for it, you could post a request in the OpenCart "Commercial Support" forum (forum.opencart.com/viewforum.php?f=88), which is checked by a number of OpenCart developers. You can also try checking out the OpenCart "Partners" area (www.opencart.com/index.php?route=partner/partner).

Re: Control over sequence of math operations

Posted: Wed May 17, 2017 12:43 am
by MrPhil
For my purposes, I'd probably just add a footnote (with * callout) noting that cross totals may be a penny or two off. It's just that some people, such as in the referenced post, are trying to be consistent with other accounting packages, and could really use it for the totals to come out exactly the same. For this purpose, something that looks like spreadsheet cell entries to exactly specify how numbers are produced could be very useful.

Add: come to think of it, color-coding the background of numbers and maybe some arrowheads could show in what order the numbers were arrived at (i.e., emphasizing that the total of row prices may not equal the highlighted subtotal x tax rounded row). That still wouldn't help with the referenced post's problem, but at least it could satisfy customers about to complain that OC can't add a column of numbers!

Re: Control over sequence of math operations

Posted: Wed May 17, 2017 10:45 pm
by Johnathan
This may not solve your issue, but I did post an edit somewhere (that I now can't find :) ) for rounding taxes that solves an error where the total is 0.01 or 0.02 off. Here's the edit:


Code: Select all

IN:
/catalog/model/total/tax.php (or /catalog/model/extension/total/tax.php)

AFTER:
if ($value > 0) {

ADD:
$value = round($value, 2);

Re: Control over sequence of math operations

Posted: Tue May 31, 2022 10:26 pm
by Johnathan
The edit I posted previously ended up not solving the issue, but I've recently had time to look into the problem more in-depth, and I've figured out how to fix it. It was more complicated than I had anticipated, so I've released an extension that will fix it:

https://www.opencartx.com/rounding-bug-fix

If anyone tries it and it doesn't work properly, or anyone has any questions about it, feel free to reach out to me here:

www.getclearthinking.com/contact