Skip to content

Commit

Permalink
Merge pull request #17608 from seamuslee001/dev_translation_48
Browse files Browse the repository at this point in the history
dev/translation#48 Implement Brick/Money to better handle money values
  • Loading branch information
seamuslee001 authored Jun 19, 2020
2 parents 8debf42 + ca4cfe7 commit 88bc0c6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Brick\Money\Money;
use Brick\Money\Context\DefaultContext;
use Brick\Math\RoundingMode;

/**
* Money utilties
*/
Expand Down Expand Up @@ -135,8 +139,8 @@ public static function getCurrencyPrecision($currency = NULL) {
*/
public static function subtractCurrencies($leftOp, $rightOp, $currency) {
if (is_numeric($leftOp) && is_numeric($rightOp)) {
$precision = pow(10, self::getCurrencyPrecision($currency));
return (($leftOp * $precision) - ($rightOp * $precision)) / $precision;
$money = Money::of($leftOp, $currency, new DefaultContext(), RoundingMode::CEILING);
return $money->minus($rightOp)->getAmount()->toFloat();
}
}

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@
"league/csv": "^9.2",
"tplaner/when": "~3.0.0",
"xkerman/restricted-unserialize": "~1.1",
"typo3/phar-stream-wrapper": "^2 || ^3.0"
"typo3/phar-stream-wrapper": "^2 || ^3.0",
"brick/money": "~0.4",
"ext-intl": "*"
},
"scripts": {
"post-install-cmd": [
Expand Down
100 changes: 96 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/phpunit/CRM/Utils/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function subtractCurrenciesDataProvider() {
[2, 1, 'USD', 1],
[0, 0, 'USD', 0],
[1, 2, 'USD', -1],
[269.565217391, 1, 'USD', 268.57],
[number_format(19.99, 2), number_format(20.00, 2), 'USD', number_format(-0.01, 2)],
['notanumber', 5.00, 'USD', NULL],
];
Expand Down

0 comments on commit 88bc0c6

Please sign in to comment.