From 5efd7d576282f776c4921366e1a778611b09a99a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 8 Feb 2022 16:38:05 +1300 Subject: [PATCH] Regression fix - be tolerant with smarty money I've seen issues in extended reports and in core where already-formatted money from custom fields is formatted again in the smarty layer and, with recent changes , throwing an exception when the money is greater than 1000 (because the presence of a comma makes it invalid). This adds tolerance that seems consistent with prior code --- CRM/Core/Smarty/plugins/modifier.crmMoney.php | 14 +++++++++++--- Civi/Core/Format.php | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CRM/Core/Smarty/plugins/modifier.crmMoney.php b/CRM/Core/Smarty/plugins/modifier.crmMoney.php index ec5a6a93b6..4a764b9669 100644 --- a/CRM/Core/Smarty/plugins/modifier.crmMoney.php +++ b/CRM/Core/Smarty/plugins/modifier.crmMoney.php @@ -25,9 +25,17 @@ * * @return string * formatted monetary amount - * - * @throws \CRM_Core_Exception */ function smarty_modifier_crmMoney($amount, ?string $currency = NULL): string { - return Civi::format()->money($amount, $currency); + try { + return Civi::format()->money($amount, $currency); + } + catch (CRM_Core_Exception $e) { + // @todo escalate this to a deprecation notice. It turns out to be depressingly + // common for us to double process amount strings - if they are > 1000 then + // they wind up throwing an exception in the money function. + // It would be more correct to format in the smarty layer, only. + Civi::log()->warning('Invalid amount passed in as money - {money}', ['money' => $amount]); + return $amount; + } } diff --git a/Civi/Core/Format.php b/Civi/Core/Format.php index 57bdea8c54..582727cce5 100644 --- a/Civi/Core/Format.php +++ b/Civi/Core/Format.php @@ -30,6 +30,8 @@ class Format { * * @return string * + * @throws \CRM_Core_Exception + * * @noinspection PhpDocMissingThrowsInspection * @noinspection PhpUnhandledExceptionInspection */ @@ -61,6 +63,7 @@ class Format { * add any padding. * * @return string + * @throws \CRM_Core_Exception */ public function number($amount, ?string $locale = NULL, array $attributes = [ NumberFormatter::MIN_FRACTION_DIGITS => 0, @@ -214,7 +217,7 @@ class Format { } // Verify the amount is a number or numeric string/object. // We cast to string because it can be a BigDecimal object. - elseif ($amount === TRUE || !is_numeric((string) $amount)) { + if ($amount === TRUE || !is_numeric((string) $amount)) { throw new \CRM_Core_Exception('Invalid value for type money'); } return (string) $amount; -- 2.25.1