dev/translation#48 Implement Brick/Money to better handle currecny values
authorSeamus Lee <seamuslee001@gmail.com>
Sun, 14 Jun 2020 04:24:36 +0000 (14:24 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 19 Jun 2020 01:17:07 +0000 (11:17 +1000)
Added in test case as per Eileen comment so we know we can handle large floats

CRM/Utils/Money.php
composer.json
composer.lock
tests/phpunit/CRM/Utils/MoneyTest.php

index 551024153f7577d1019fca5ebf63de91e8cab6e4..f5fb16244e43c2a67a4b736ffea615d7f35b2d12 100644 (file)
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Brick\Money\Money;
+use Brick\Money\Context\DefaultContext;
+use Brick\Math\RoundingMode;
+
 /**
  * Money utilties
  */
@@ -135,8 +139,8 @@ class CRM_Utils_Money {
    */
   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();
     }
   }
 
index fc89c367c7166789bdd4e44be129750e3ce838a4..c56dd0c5e7953bb5f1926b5f57b17559685f7d4c 100644 (file)
@@ -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": [
index a2264757026af635bcf7450730fa2dcc70d453fe..f81fe02aa968be184f77855f3bc1a7a1edd50f33 100644 (file)
@@ -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": "96209031150a0c16ca42aeb4422abe56",
+    "content-hash": "0e6f527fa9032d1f8445c8ec2963c3c9",
     "packages": [
         {
             "name": "adrienrn/php-mimetyper",
             "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",
     "prefer-stable": false,
     "prefer-lowest": false,
     "platform": {
-        "php": "~7.1"
+        "php": "~7.1",
+        "ext-intl": "*"
     },
     "platform-dev": [],
     "platform-overrides": {
         "php": "7.1"
-    },
-    "plugin-api-version": "1.1.0"
+    }
 }
index ba6a3dc8ba7320686ecc688ba2a2f7de59dd0926..7b97b20b2f7e1314c94a7ae394a33f816270fd06 100644 (file)
@@ -41,6 +41,7 @@ class CRM_Utils_MoneyTest extends CiviUnitTestCase {
       [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],
     ];