Skip to content

Commit

Permalink
Merge pull request #18416 from seamuslee001/dev_core_2003
Browse files Browse the repository at this point in the history
dev/core#2003 Use Brick/Money to ensure that we display all possible …
  • Loading branch information
eileenmcnaughton authored Oct 1, 2020
2 parents 89b4fe4 + 096110f commit 8f3d97b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 1 addition & 4 deletions CRM/Price/Form/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ public function setDefaultValues() {

// fix the display of the monetary value, CRM-4038
foreach ($this->_moneyFields as $field) {
// @todo use formatLocaleNumericRoundedByOptionalPrecision - but note that there is an issue where php doesn't handle
// money strings beyond a certain total length - per https://github.com/civicrm/civicrm-core/pull/18409
// & https://github.com/civicrm/civicrm-core/pull/18409
$defaults[$field] = CRM_Utils_Money::format(CRM_Utils_Array::value($field, $defaults), NULL, '%a');
$defaults[$field] = isset($defaults[$field]) ? CRM_Utils_Money::formatLocaleNumericRoundedByOptionalPrecision($defaults[$field], 9) : '';
}
}

Expand Down
18 changes: 17 additions & 1 deletion CRM/Utils/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Brick\Money\Money;
use Brick\Money\Context\DefaultContext;
use Brick\Money\Context\CustomContext;
use Brick\Math\RoundingMode;

/**
Expand Down Expand Up @@ -201,7 +202,15 @@ protected static function formatLocaleNumeric($amount) {
* @return string
*/
protected static function formatLocaleNumericRounded($amount, $numberOfPlaces) {
return self::formatNumericByFormat($amount, '%!.' . $numberOfPlaces . 'i');
if (!extension_loaded('intl')) {
self::missingIntlNotice();
return self::formatNumericByFormat($amount, '%!.' . $numberOfPlaces . 'i');
}
$money = Money::of($amount, CRM_Core_Config::singleton()->defaultCurrency, new CustomContext($numberOfPlaces), RoundingMode::CEILING);
$formatter = new \NumberFormatter(CRM_Core_I18n::getLocale(), NumberFormatter::CURRENCY);
$formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, '');
$formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $numberOfPlaces);
return $money->formatWith($formatter);
}

/**
Expand Down Expand Up @@ -306,4 +315,11 @@ protected static function formatNumericByFormat($amount, $valueFormat) {
return $amount;
}

/**
* Emits a notice indicating we have fallen back to a less accurate way of formatting money due to missing intl extension
*/
public static function missingIntlNotice() {
CRM_Core_Session::singleton()->setStatus(ts('As this system does not include the PHP intl extension, CiviCRM has fallen back onto a slightly less accurate and deprecated method to format money'), ts('Missing PHP INTL extension'));
}

}

0 comments on commit 8f3d97b

Please sign in to comment.