From 6d383451c224f65eef1bbe759087bd2c625f1f1b Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 14 Jun 2020 14:24:36 +1000 Subject: [PATCH] dev/translation#48 Implement Brick/Money to better handle currecny values --- CRM/Utils/Money.php | 7 +++- composer.json | 3 +- composer.lock | 94 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/CRM/Utils/Money.php b/CRM/Utils/Money.php index 551024153f75..9c3cd80c174b 100644 --- a/CRM/Utils/Money.php +++ b/CRM/Utils/Money.php @@ -15,6 +15,9 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Brick\Money\Money; +use Brick\Math\RoundingMode; + /** * Money utilties */ @@ -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(); } } diff --git a/composer.json b/composer.json index e2fc32f8fd7d..aa7c5c5afecb 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/composer.lock b/composer.lock index bfcbff172d0d..3bb9a38e7b89 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3c6d377d0dfd5ce11b0a96af56344181", + "content-hash": "0d0bdc9bbbaf0d34db4d7f97986e6a1e", "packages": [ { "name": "adrienrn/php-mimetyper", @@ -42,6 +42,98 @@ "description": "PHP mime type and extension mapping library: compatible with Symfony, powered by jshttp/mime-db", "time": "2018-09-27T09:45:05+00:00" }, + { + "name": "brick/math", + "version": "0.8.15", + "source": { + "type": "git", + "url": "https://github.com/brick/math.git", + "reference": "9b08d412b9da9455b210459ff71414de7e6241cd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/brick/math/zipball/9b08d412b9da9455b210459ff71414de7e6241cd", + "reference": "9b08d412b9da9455b210459ff71414de7e6241cd", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": "^7.1|^8.0" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.2", + "phpunit/phpunit": "^7.5.15|^8.5", + "vimeo/psalm": "^3.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Brick\\Math\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Arbitrary-precision arithmetic library", + "keywords": [ + "Arbitrary-precision", + "BigInteger", + "BigRational", + "arithmetic", + "bigdecimal", + "bignum", + "brick", + "math" + ], + "time": "2020-04-15T15:59:35+00:00" + }, + { + "name": "brick/money", + "version": "0.4.5", + "source": { + "type": "git", + "url": "https://github.com/brick/money.git", + "reference": "91f2b5bc35646f172b038e46bb496ad18db59c3c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/brick/money/zipball/91f2b5bc35646f172b038e46bb496ad18db59c3c", + "reference": "91f2b5bc35646f172b038e46bb496ad18db59c3c", + "shasum": "" + }, + "require": { + "brick/math": "~0.7.3 || ~0.8.0", + "php": "^7.1|^8.0" + }, + "require-dev": { + "brick/varexporter": "~0.2.1", + "ext-dom": "*", + "ext-pdo": "*", + "php-coveralls/php-coveralls": "^2.2", + "phpunit/phpunit": "^7.5.15" + }, + "suggest": { + "ext-intl": "Required to format Money objects" + }, + "type": "library", + "autoload": { + "psr-4": { + "Brick\\Money\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Money and currency library", + "keywords": [ + "brick", + "currency", + "money" + ], + "time": "2020-05-31T14:17:02+00:00" + }, { "name": "cache/integration-tests", "version": "0.16.0",