-
-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dev/financial#104 Use rounding and integers to compare monetary values… #15856
Conversation
(Standard links)
|
Test failure is relevant, although the message doesn't make sense. CRM_Utils_MoneyTest::testEquals
|
@agileware-fj Francis what is is saying that in this test https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CRM/Utils/MoneyTest.php#L32 rather than the Money::equals function returning false it returned TRUE or some other value instead. I think even if you return 0 it will fail because it didn't get an actual FALSE |
@seamuslee001 Thanks. The failure message is still Less Than Awesome. Anyway I'll review and update the PR. |
Ah, I see that the problem is actually that I disagree with the test in terms of what this function is doing for the use case I'm trying to fix. I need to find out what else is calling this function and whether the test's truth can be adjusted to match mine... |
…g behaviour. Existing test parameters were wrong.
78cdfcc
to
3d5246e
Compare
I've updated the test
|
I don't have time to review this right now, however this was the original PR when the equals function was added: #12352 So the reviewer should check that the logic and discussion there is still met by this improvement |
Ping @MiyaNoctem @JoeMurray would you be able to take some time to review this PR? Thanks! |
@MiyaNoctem @JoeMurray @mattwire anyone have any time coming up they could review this in? Thanks. |
Seems fine to me as far as the description is concerned. Tested the PR with various money comparisons and confirmed that it works fine in all the cases. Below are the manual testing values with the returned result -
This looks fine for the 2 decimal digits. Not sure if it's worth to consider 0.001 precision as mentioned in #12352. Is it really required? Ping @MiyaNoctem in case of any objections pls. |
…with precision
Overview
Replace the questionable epsilon algorithm to compare two monetary amounts with rounded int casts
Ref dev/financial#104
The existing implementation causes issues e.g. when an external application provides, as required, a pre-rounded post-tax amount to the Order.create API and pre-tax amounts to the embedded line items.
Before
Comparing two amounts using
CRM_Utils_Money::equals
would defer toCRM_Utils_Money::subtractCurrencies
to get a delta between the amounts, and then make sure they were within 1/1000th of a currency unit (an "epsilon" check) to verify that the amounts are indeed the same.There's not much makes sense about this; notably, the
subtractCurrencies
function doesn't actually do what it says it does (integer subtraction with currency precision), but just juggles floats about.After
CRM_Utils_Money::equals
multiplies and casts both values to a precision shifted integer, and compares if equalComments
Also discussed on chat.civicrm.org
Agileware Ref CIVICRM-1368