From 5387c12858054ef48671bc281b940291c1014ac6 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Tue, 30 May 2023 15:12:01 -0400 Subject: [PATCH] crmMoney filter --- Civi/Token/StandardFilters.php | 15 ++++++++++ Civi/Token/TokenProcessor.php | 2 +- .../phpunit/Civi/Token/TokenProcessorTest.php | 28 ++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Civi/Token/StandardFilters.php b/Civi/Token/StandardFilters.php index 5b77e1cd82fe..d7d3a4ffca73 100644 --- a/Civi/Token/StandardFilters.php +++ b/Civi/Token/StandardFilters.php @@ -94,6 +94,21 @@ public static function crmDate($value, array $filter, string $format) { return static::default($value, $filter, $format); } + public static function crmMoney($value, array $filter, string $format) { + if ($value instanceof \Brick\Money\Money) { + $amount = $value->getAmount()->toFloat(); + } + elseif (is_numeric($value)) { + $amount = $value; + } + else { + return $value; + } + $currency = $filter[2] ?? ''; + $locale = $filter[1] ?? NULL; + return \Civi::format()->money($amount, $currency, $locale); + } + /** * Return value, falling back to default. * diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index c19ca59aef7c..b7b7e75f98e1 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -473,7 +473,7 @@ private function filterTokenValue($value, ?array $filter, TokenRow $row, string } // TODO: Move this to StandardFilters - if ($value instanceof Money) { + if ($value instanceof Money && $filter === NULL) { switch ($filter[0] ?? NULL) { case NULL: case 'crmMoney': diff --git a/tests/phpunit/Civi/Token/TokenProcessorTest.php b/tests/phpunit/Civi/Token/TokenProcessorTest.php index 07c93dc70119..47404aaa173d 100644 --- a/tests/phpunit/Civi/Token/TokenProcessorTest.php +++ b/tests/phpunit/Civi/Token/TokenProcessorTest.php @@ -476,6 +476,16 @@ public function getFilterExamples() { 'empty_string' => '', 'and_such' => 'testing & such', ], + 'my_numbers' => [ + 'amount' => 123, + 'currency' => 'EUR', + 'locale' => 'fr_FR', + ], + 'my_currencies' => [ + 'amount' => \Brick\Money\Money::of(123, 'USD', new \Brick\Money\Context\DefaultContext()), + 'currency' => 'EUR', + 'locale' => 'fr_FR', + ], ]; $testCases = []; @@ -523,6 +533,20 @@ public function getFilterExamples() { ], $exampleTokens, ]; + $testCases['crmMoney testing'] = [ + 'text/plain', + [ + 'Amount: {my_numbers.amount}' => 'Amount: 123', + 'Amount as money: {my_numbers.amount|crmMoney}' => 'Amount as money: $123.00', + 'Amount as money in France: {my_numbers.amount|crmMoney:"fr_FR"}' => 'Amount as money in France: 123,00 $US', + 'Amount as money in France (Euros): {my_numbers.amount|crmMoney:"fr_FR":"EUR"}' => 'Amount as money in France (Euros): 123,00 €', + 'Amount: {my_currencies.amount}' => 'Amount: $123.00', + 'Amount as money: {my_currencies.amount|crmMoney}' => 'Amount as money: $123.00', + 'Amount as money in France: {my_currencies.amount|crmMoney:"fr_FR"}' => 'Amount as money in France: 123,00 $US', + 'Amount as money in France (Euros): {my_currencies.amount|crmMoney:"fr_FR":"EUR"}' => 'Amount as money in France (Euros): 123,00 €', + ], + $exampleTokens, + ]; return $testCases; } @@ -546,7 +570,9 @@ public function testFilters(string $messageFormat, array $exampleMessages, array $p->addMessage('example', $inputMessage, $messageFormat); $p->addRow() ->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_text'])) - ->format('text/html')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_rich_text'])); + ->format('text/html')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_rich_text'])) + ->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_numbers'])) + ->format('text/plain')->tokens(\CRM_Utils_Array::subset($exampleTokens, ['my_currencies'])); foreach ($p->evaluate()->getRows() as $row) { $this->assertEquals($expectOutput, $row->render('example')); $actualExampleCount++;