Skip to content

CRM-17157 support multiple decimalplaces formrule money #10827

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CRM/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,10 @@ public static function money($value) {
return TRUE;
}

return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE;
// Allow values such as -0, 1.024555, -.1
// We need to support multiple decimal places here, not just the number allowed by locale
// otherwise tax calculations break when you want the inclusive amount to be a round number (eg. £10 inc. VAT requires 8.333333333 here).
return preg_match('/(^-?\d+\.?\d*$)|(^-?\.\d+$)/', $value) ? TRUE : FALSE;
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/CRM/Utils/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,37 @@ public function numericDataProvider() {
);
}

/**
* @dataProvider moneyDataProvider
* @param $inputData
* @param $expectedResult
*/
public function testMoney($inputData, $expectedResult) {
$this->assertEquals($expectedResult, CRM_Utils_Rule::money($inputData));
}

/**
* @return array
*/
public function moneyDataProvider() {
return array(
array(10, TRUE),
array('145.0E+3', FALSE),
array('10', TRUE),
array(-10, TRUE),
array('-10', TRUE),
array('-10foo', FALSE),
array('-10.0345619', TRUE),
array('-10.010,4345619', TRUE),
array('10.0104345619', TRUE),
array('-0', TRUE),
array('-.1', TRUE),
array('.1', TRUE),
// Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
array('$500.3333', TRUE),
array('-$500.3333', TRUE),
array('$-500.3333', TRUE),
);
}

}