From f6590591539fda308b12cd7033bdfba97e961a58 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 18 May 2023 10:12:21 +1200 Subject: [PATCH] Fix token boolean filter to work with money & use in templates The |raw comparison is quite confusing because it compares to 0.00 & we are otherwise using the |boolean increasingly --- CRM/Core/EntityTokens.php | 8 ++------ Civi/Token/TokenProcessor.php | 6 +++++- .../phpunit/CRM/Event/Form/Task/BadgeTest.php | 2 +- .../phpunit/CRM/Utils/TokenConsistencyTest.php | 18 ++++++++++++++++++ .../contribution_offline_receipt_html.tpl | 6 +++--- .../contribution_offline_receipt_text.tpl | 8 ++++---- .../contribution_online_receipt_html.tpl | 10 +++++----- .../contribution_online_receipt_text.tpl | 10 +++++----- .../membership_offline_receipt_html.tpl | 10 +++++----- .../membership_offline_receipt_text.tpl | 12 ++++++------ 10 files changed, 54 insertions(+), 36 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 061f964c8a..bf94a19d04 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -120,15 +120,11 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { return $row->customToken($entity, \CRM_Core_BAO_CustomField::getKeyID($field), $this->getFieldValue($row, 'id')); } if ($this->isMoneyField($field)) { - $currency = $this->getCurrency($row); + $currency = $this->getCurrency($row) ?: \Civi::settings()->get('defaultCurrency'); if (empty($fieldValue) && !is_numeric($fieldValue)) { $fieldValue = 0; } - if (!$currency) { - // too hard basket for now - just do what we always did. - return $row->format('text/plain')->tokens($entity, $field, - \CRM_Utils_Money::format($fieldValue, $currency)); - } + return $row->format('text/plain')->tokens($entity, $field, Money::of($fieldValue, $currency)); diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 2472f03b4e..c19ca59aef 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -479,11 +479,15 @@ class TokenProcessor { case 'crmMoney': return \Civi::format()->money($value->getAmount(), $value->getCurrency()); + case 'boolean': + // We resolve boolean to 0 or 1 or smarty chokes on FALSE. + return (int) $value->getAmount()->isGreaterThan(0); + case 'raw': return $value->getAmount(); default: - throw new \CRM_Core_Exception("Invalid token filter: " . json_encode($filter, JSON_UNESCAPED_SLASHES)); + throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES)); } } diff --git a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php index d1f8622ded..3db7e4c6b1 100644 --- a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php +++ b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php @@ -96,7 +96,7 @@ class CRM_Event_Form_Task_BadgeTest extends CiviUnitTestCase { '{participant.register_date}' => 'February 19th, 2007', '{participant.source}' => 'Wimbeldon', '{participant.fee_level}' => 'low', - '{participant.fee_amount}' => '$ 0.00', + '{participant.fee_amount}' => '$0.00', '{participant.registered_by_id}' => NULL, '{participant.transferred_to_contact_id}' => NULL, '{participant.role_id:label}' => 'Attendee', diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index ae0e0dd770..8ea664522d 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -475,6 +475,24 @@ contribution_recur.payment_instrument_id:name :Check } + /** + * Test balance token works can be rendered as raw & boolean. + * + * This is a stand in for money tokens in general but as a pseudo-field + * it has some added charm. + */ + public function testContributionBalanceToken(): void { + $contributionID = $this->contributionCreate(['contact_id' => $this->individualCreate(), 'contribution_status_id' => 'Pending']); + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $contributionID, + 'total_amount' => 1, + ]); + $text = $this->renderText(['contributionId' => $contributionID], + '{contribution.balance_amount} {contribution.balance_amount|raw} {contribution.balance_amount|boolean} {contribution.tax_amount|boolean}' + ); + $this->assertEquals('$99.00 99.00 1 0', $text); + } + /** * Test that membership tokens are consistently rendered. * diff --git a/xml/templates/message_templates/contribution_offline_receipt_html.tpl b/xml/templates/message_templates/contribution_offline_receipt_html.tpl index 89910a777d..f401782dc1 100644 --- a/xml/templates/message_templates/contribution_offline_receipt_html.tpl +++ b/xml/templates/message_templates/contribution_offline_receipt_html.tpl @@ -63,7 +63,7 @@ {ts}Item{/ts} {ts}Qty{/ts} {ts}Each{/ts} - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} {ts}Subtotal{/ts} {ts}Tax Rate{/ts} {ts}Tax Amount{/ts} @@ -81,7 +81,7 @@ {$line.unit_price|crmMoney:'{contribution.currency}'} - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} {$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'} @@ -107,7 +107,7 @@ {/if} - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} {ts} Amount before Tax : {/ts} diff --git a/xml/templates/message_templates/contribution_offline_receipt_text.tpl b/xml/templates/message_templates/contribution_offline_receipt_text.tpl index f4dda781f9..e5692d0642 100644 --- a/xml/templates/message_templates/contribution_offline_receipt_text.tpl +++ b/xml/templates/message_templates/contribution_offline_receipt_text.tpl @@ -17,21 +17,21 @@ {capture assign=ts_item}{ts}Item{/ts}{/capture} {capture assign=ts_qty}{ts}Qty{/ts}{/capture} {capture assign=ts_each}{ts}Each{/ts}{/capture} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && {contribution.tax_amount|boolean}} {capture assign=ts_subtotal}{ts}Subtotal{/ts}{/capture} {capture assign=ts_taxRate}{ts}Tax Rate{/ts}{/capture} {capture assign=ts_taxAmount}{ts}Tax Amount{/ts}{/capture} {/if} {capture assign=ts_total}{ts}Total{/ts}{/capture} -{$ts_item|string_format:"%-30s"} {$ts_qty|string_format:"%5s"} {$ts_each|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate} {$ts_taxAmount|string_format:"%10s"} {/if} {$ts_total|string_format:"%10s"} +{$ts_item|string_format:"%-30s"} {$ts_qty|string_format:"%5s"} {$ts_each|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate} {$ts_taxAmount|string_format:"%10s"} {/if} {$ts_total|string_format:"%10s"} ---------------------------------------------------------- {foreach from=$lineItems item=line} -{capture assign=ts_item}{$line.title}{/capture}{$ts_item|truncate:30:"..."|string_format:"%-30s"} {$line.qty|string_format:"%5s"} {$line.unit_price|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'}{$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} {else} {/if} {/if} {$line.line_total+$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} +{capture assign=ts_item}{$line.title}{/capture}{$ts_item|truncate:30:"..."|string_format:"%-30s"} {$line.qty|string_format:"%5s"} {$line.unit_price|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}}{$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} {else} {/if} {/if} {$line.line_total+$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} {/foreach} {/if} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && {contribution.tax_amount|boolean}} {ts}Amount before Tax{/ts} : {$formValues.total_amount-$totalTaxAmount|crmMoney:$currency} {/if} {foreach from=$taxRateBreakdown item=taxDetail key=taxRate} diff --git a/xml/templates/message_templates/contribution_online_receipt_html.tpl b/xml/templates/message_templates/contribution_online_receipt_html.tpl index a3d4ec3f88..127b8082a7 100644 --- a/xml/templates/message_templates/contribution_online_receipt_html.tpl +++ b/xml/templates/message_templates/contribution_online_receipt_html.tpl @@ -33,7 +33,7 @@ - {if '{contribution.total_amount|raw}' !== '0.00'} + {if {contribution.total_amount|boolean}} - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} @@ -60,7 +60,7 @@ - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} {if $line.tax_rate || $line.tax_amount != ""} @@ -79,7 +79,7 @@ - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} {else} - {if '{contribution.tax_amount|raw}' !== '0.00'} + {if {contribution.tax_amount|boolean}} {/if} - {if '{contribution.total_amount|raw}' !== '0.00'} + {if {contribution.total_amount|boolean}} - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} @@ -106,7 +106,7 @@ - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}} @@ -137,7 +137,7 @@ - {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} + {if $isShowTax && {contribution.tax_amount|boolean}}
{ts}Contribution Information{/ts} @@ -48,7 +48,7 @@ {ts}Item{/ts} {ts}Qty{/ts} {ts}Each{/ts}{ts}Subtotal{/ts} {ts}Tax Rate{/ts} {ts}Tax Amount{/ts}{$line.title} {$line.qty} {$line.unit_price|crmMoney:$currency}{$line.unit_price*$line.qty|crmMoney:$currency}{$line.tax_rate|string_format:"%.2f"}%
{ts} Amount before Tax : {/ts} @@ -116,7 +116,7 @@
{ts}Total Tax Amount{/ts} diff --git a/xml/templates/message_templates/contribution_online_receipt_text.tpl b/xml/templates/message_templates/contribution_online_receipt_text.tpl index 44afc91056..fc41c039d6 100644 --- a/xml/templates/message_templates/contribution_online_receipt_text.tpl +++ b/xml/templates/message_templates/contribution_online_receipt_text.tpl @@ -9,7 +9,7 @@ =========================================================== {/if} -{if '{contribution.total_amount|raw}' !== '0.00'} +{if {contribution.total_amount|boolean}} =========================================================== {ts}Contribution Information{/ts} @@ -20,19 +20,19 @@ {capture assign=ts_item}{ts}Item{/ts}{/capture} {capture assign=ts_qty}{ts}Qty{/ts}{/capture} {capture assign=ts_each}{ts}Each{/ts}{/capture} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && {contribution.tax_amount|boolean}} {capture assign=ts_subtotal}{ts}Subtotal{/ts}{/capture} {capture assign=ts_taxRate}{ts}Tax Rate{/ts}{/capture} {capture assign=ts_taxAmount}{ts}Tax Amount{/ts}{/capture} {/if} {capture assign=ts_total}{ts}Total{/ts}{/capture} -{$ts_item|string_format:"%-30s"} {$ts_qty|string_format:"%5s"} {$ts_each|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate} {$ts_taxAmount|string_format:"%10s"} {/if} {$ts_total|string_format:"%10s"} +{$ts_item|string_format:"%-30s"} {$ts_qty|string_format:"%5s"} {$ts_each|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate} {$ts_taxAmount|string_format:"%10s"} {/if} {$ts_total|string_format:"%10s"} ---------------------------------------------------------- {foreach from=$lineItems item=line} -{capture assign=ts_item}{$line.title}{/capture}{$ts_item|truncate:30:"..."|string_format:"%-30s"} {$line.qty|string_format:"%5s"} {$line.unit_price|crmMoney:$currency|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'}{$line.unit_price*$line.qty|crmMoney:$currency|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:$currency|string_format:"%10s"} {else} {/if} {/if} {$line.line_total+$line.tax_amount|crmMoney:$currency|string_format:"%10s"} +{capture assign=ts_item}{$line.title}{/capture}{$ts_item|truncate:30:"..."|string_format:"%-30s"} {$line.qty|string_format:"%5s"} {$line.unit_price|crmMoney:$currency|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}}{$line.unit_price*$line.qty|crmMoney:$currency|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:$currency|string_format:"%10s"} {else} {/if} {/if} {$line.line_total+$line.tax_amount|crmMoney:$currency|string_format:"%10s"} {/foreach} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && {contribution.tax_amount|boolean}} {ts}Amount before Tax:{/ts} {$amount-$totalTaxAmount|crmMoney:$currency} {foreach from=$taxRateBreakdown item=taxDetail key=taxRate} {if $taxRate == 0}{ts}No{/ts} {$taxTerm}{else}{$taxTerm} {$taxDetail.percentage}%{/if} : {$taxDetail.amount|crmMoney:'{contribution.currency}'} diff --git a/xml/templates/message_templates/membership_offline_receipt_html.tpl b/xml/templates/message_templates/membership_offline_receipt_html.tpl index fe00feeec0..4eed6e5ad9 100644 --- a/xml/templates/message_templates/membership_offline_receipt_html.tpl +++ b/xml/templates/message_templates/membership_offline_receipt_html.tpl @@ -67,7 +67,7 @@
{ts}Membership Fee{/ts} @@ -91,7 +91,7 @@
{ts}Item{/ts} {ts}Fee{/ts}{ts}SubTotal{/ts} {ts}Tax Rate{/ts} {ts}Tax Amount{/ts} {$line.line_total|crmMoney} {$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'}
{ts}Amount Before Tax:{/ts} @@ -154,7 +154,7 @@ {/foreach} {/if} {/if} - {if '{contribution.tax_amount|raw}' !== '0.00'} + {if {contribution.tax_amount|boolean}}
{ts}Total Tax Amount{/ts} diff --git a/xml/templates/message_templates/membership_offline_receipt_text.tpl b/xml/templates/message_templates/membership_offline_receipt_text.tpl index 9aa53964c6..dd5248f741 100644 --- a/xml/templates/message_templates/membership_offline_receipt_text.tpl +++ b/xml/templates/message_templates/membership_offline_receipt_text.tpl @@ -17,7 +17,7 @@ {ts}Membership Expiration Date{/ts}: {membership.end_date|crmDate:"Full"} {/if} -{if '{contribution.total_amount|raw}' !== '0.00'} +{if {contribution.total_amount|boolean}} =========================================================== {ts}Membership Fee{/ts} @@ -28,7 +28,7 @@ {if $isShowLineItems} {capture assign=ts_item}{ts}Item{/ts}{/capture} {capture assign=ts_total}{ts}Fee{/ts}{/capture} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && '{contribution.tax_amount|boolean}'} {capture assign=ts_subtotal}{ts}Subtotal{/ts}{/capture} {capture assign=ts_taxRate}{ts}Tax Rate{/ts}{/capture} {capture assign=ts_taxAmount}{ts}Tax Amount{/ts}{/capture} @@ -36,14 +36,14 @@ {/if} {capture assign=ts_start_date}{ts}Membership Start Date{/ts}{/capture} {capture assign=ts_end_date}{ts}Membership Expiration Date{/ts}{/capture} -{$ts_item|string_format:"%-30s"} {$ts_total|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate|string_format:"%10s"} {$ts_taxAmount|string_format:"%10s"} {$ts_total|string_format:"%10s"} {/if} {$ts_start_date|string_format:"%20s"} {$ts_end_date|string_format:"%20s"} +{$ts_item|string_format:"%-30s"} {$ts_total|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}} {$ts_subtotal|string_format:"%10s"} {$ts_taxRate|string_format:"%10s"} {$ts_taxAmount|string_format:"%10s"} {$ts_total|string_format:"%10s"} {/if} {$ts_start_date|string_format:"%20s"} {$ts_end_date|string_format:"%20s"} -------------------------------------------------------------------------------------------------- {foreach from=$lineItems item=line} -{line.title} {$line.line_total|crmMoney|string_format:"%10s"} {if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} {$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} {else} {/if} {$line.line_total+$line.tax_amount|crmMoney|string_format:"%10s"} {/if} {$line.membership.start_date|string_format:"%20s"} {$line.membership.end_date|string_format:"%20s"} +{line.title} {$line.line_total|crmMoney|string_format:"%10s"} {if $isShowTax && {contribution.tax_amount|boolean}} {$line.unit_price*$line.qty|crmMoney:'{contribution.currency}'|string_format:"%10s"} {if $line.tax_rate || $line.tax_amount != ""} {$line.tax_rate|string_format:"%.2f"} % {$line.tax_amount|crmMoney:'{contribution.currency}'|string_format:"%10s"} {else} {/if} {$line.line_total+$line.tax_amount|crmMoney|string_format:"%10s"} {/if} {$line.membership.start_date|string_format:"%20s"} {$line.membership.end_date|string_format:"%20s"} {/foreach} -{if $isShowTax && '{contribution.tax_amount|raw}' !== '0.00'} +{if $isShowTax && {contribution.tax_amount|boolean}} {ts}Amount before Tax:{/ts} {contribution.tax_exclusive_amount} {foreach from=$taxRateBreakdown item=taxDetail key=taxRate} @@ -53,7 +53,7 @@ -------------------------------------------------------------------------------------------------- {/if} -{if '{contribution.tax_amount|raw}' !== '0.00'} +{if {contribution.tax_amount|boolean}} {ts}Total Tax Amount{/ts}: {contribution.tax_amount} {/if} -- 2.25.1