Skip to content

Commit

Permalink
dev/translation#48 Implement Brick/Money to better handle currecny va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
seamuslee001 committed Jun 14, 2020
1 parent 9c6d953 commit 6d38345
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
7 changes: 5 additions & 2 deletions CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Brick\Money\Money;
use Brick\Math\RoundingMode;

/**
* Money utilties
*/
Expand Down Expand Up @@ -135,8 +138,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);
return $money->minus($rightOp)->getAmount()->toFloat();
}
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"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"
},
"scripts": {
"post-install-cmd": [
Expand Down
94 changes: 93 additions & 1 deletion composer.lock

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

0 comments on commit 6d38345

Please sign in to comment.