Skip to content

Commit

Permalink
dev/core#1603 Add tpl level formattinng for tax_amount
Browse files Browse the repository at this point in the history
This is a partial fix for https://lab.civicrm.org/dev/core/-/issues/1603 because
we should be doing our formatting at the presentation layer rather than the BAO layer.
I suspect the wrong values might still be saved in the database but this gives us a bit
more space to fix that without breakin the display.

It also includes the brickMoney library per civicrm#17608
as part of the fix. Note that the rounding I chose was RoundingMode::CEILING
- this is not set in stone but the sum of the individual amounts come closer
to the total than with rounding down.

Note there were 2 params passed to the function that I removed - I think they are unnused but
I am digging further. Also note that this uses the default locale but we might prefer to
pass the user's locale when we know it as the locale can alter formatting e.g for Euro
  • Loading branch information
eileenmcnaughton committed Jun 14, 2020
1 parent 3045e66 commit 82a8681
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
23 changes: 11 additions & 12 deletions CRM/Core/Smarty/plugins/modifier.crmMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
* $Id$
*
*/
use Brick\Money\Money;
use Brick\Money\Context\DefaultContext;
use Brick\Math\RoundingMode;

/**
* Format the given monetary amount (and currency) for display
Expand All @@ -25,12 +21,15 @@
* @param string $currency
* The (optional) currency.
*
* @param null $format
* @param bool $onlyNumber
*
* @return string
* formatted monetary amount
*/
function smarty_modifier_crmMoney($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE) {
return CRM_Utils_Money::format($amount, $currency, $format, $onlyNumber);
function smarty_modifier_crmMoney($amount, $currency = NULL) {
if (!$amount) {
return $amount;
}
$currency = $currency ?? CRM_Core_Config::singleton()->defaultCurrency;
$money = Money::of($amount, $currency, new DefaultContext(), RoundingMode::CEILING);
$formatter = new \NumberFormatter(CRM_Core_I18n::singleton()->getLocale(), \NumberFormatter::CURRENCY);
return $money->formatWith($formatter);
}
5 changes: 4 additions & 1 deletion templates/CRM/Price/Page/LineItem.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
<td class="right">{$line.line_total|crmMoney:$currency}</td>
{if $line.tax_rate != "" || $line.tax_amount != ""}
<td class="right">{$taxTerm} ({$line.tax_rate}%)</td>
<td class="right">{$line.tax_amount|crmMoney:$currency}</td>
<td class="right">
{math equation="(x * y) / 100" x=$line.line_total y=$line.tax_rate assign=tax_value}
{$tax_value|crmMoney:$currency}
</td>
{else}
<td></td>
<td></td>
Expand Down

0 comments on commit 82a8681

Please sign in to comment.