From 132e4deafbbf57992b00bb4143fd0527b9cac732 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 25 Jan 2022 16:22:21 +1300 Subject: [PATCH] Remove isset from online contribution receipt This removes the isset checks that re failing around totalTaxAmount (solved the same way as offline https://github.com/civicrm/civicrm-core/pull/22560/files#diff-fd5668d5492e5f0ec55b7f8a10eccfa4ea9de249e4cf2383a2e0d36dbd92ebe0R134 ) It also removes the isset around contribution.amount_level (switching to a token) and the enotice on isQuickConfig - this switches to loading that information & also using a more meaningful variable. Note that line items should always be assigned so that check is silly. --- CRM/Contribute/BAO/ContributionPage.php | 4 +- .../WorkflowMessage/ContributionTrait.php | 47 +++++++++++++++++++ tests/phpunit/api/v3/ContributionPageTest.php | 6 ++- tests/phpunit/api/v3/ContributionTest.php | 10 ++-- .../contribution_online_receipt_html.tpl | 8 ++-- .../contribution_online_receipt_text.tpl | 8 ++-- 6 files changed, 68 insertions(+), 15 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 5b9e725c9a..f184d19116 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -415,10 +415,10 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not $sendTemplateParams = [ - 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', - 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt', + 'workflow' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt', 'contactId' => $contactID, 'tplParams' => $tplParams, + 'tokenContext' => $tplParams['contributionID'] ? ['contributionId' => (int) $tplParams['contributionID'], 'contactId' => $contactID] : ['contactId' => $contactID], 'isTest' => $isTest, 'PDFFilename' => 'receipt.pdf', ]; diff --git a/CRM/Contribute/WorkflowMessage/ContributionTrait.php b/CRM/Contribute/WorkflowMessage/ContributionTrait.php index 765fa8c0d3..4ecf20565b 100644 --- a/CRM/Contribute/WorkflowMessage/ContributionTrait.php +++ b/CRM/Contribute/WorkflowMessage/ContributionTrait.php @@ -28,6 +28,53 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait { */ public $isShowTax; + /** + * @var CRM_Financial_BAO_Order + */ + private $order; + + /** + * Get order, if available. + * + * The order is used within the class to calculate line items etc. + * + * @return \CRM_Financial_BAO_Order|null + */ + private function getOrder(): ?CRM_Financial_BAO_Order { + if ($this->contributionId) { + $this->order = new CRM_Financial_BAO_Order(); + $this->order->setTemplateContributionID($this->contributionId); + } + return $this->order; + } + + /** + * Should line items be displayed for the contribution. + * + * This determination is based on whether the price set is quick config. + * + * @var bool + * + * @scope tplParams + */ + public $isShowLineItems; + + /** + * Get bool for whether a line item breakdown be displayed. + * + * @return bool + */ + public function getIsShowLineItems(): bool { + $order = $this->getOrder(); + if (!$order) { + // This would only be the case transitionally. + // Since this is a trait it is used by templates which don't (yet) + // always have the contribution ID available as well as migrated ones. + return FALSE; + } + return $this->order->getPriceSetMetadata()['is_quick_config']; + } + /** * Set contribution object. * diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 6f1241a817..2f755d531a 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -626,6 +626,10 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { /** * Test submit with a membership block in place. * + * This test uses a quick config price set - which means line items + * do not show on the receipts. Separate payments are only supported + * with quick config. + * * We are expecting a separate payment for the membership vs the contribution. * * @throws \API_Exception @@ -662,7 +666,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { } // The total string is currently absent & it seems worse with - although at some point // it may have been intended - $mut->checkAllMailLog(['$2.00', 'Contribution Amount', '$88.00'], ['Total:']); + $mut->checkAllMailLog(['$2.00', 'Contribution Information', '$88.00'], ['Total:']); $mut->stop(); $mut->clearMessages(); } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 87981fa8e9..720e48f52e 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -3141,6 +3141,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { * * If passed in it will override the default from contribution page. * + * @throws \API_Exception * @throws \CRM_Core_Exception */ public function testCompleteTransactionWithEmailReceiptInputTrue(): void { @@ -3150,15 +3151,16 @@ class api_v3_ContributionTest extends CiviUnitTestCase { // Create a Contribution Page with is_email_receipt = FALSE $contributionPageID = $this->createQuickConfigContributionPage($contributionPageParams); $this->_params['contribution_page_id'] = $contributionPageID; - $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now']); - $contribution = $this->callAPISuccess('contribution', 'create', $params); - // Complete the transaction overriding is_email_receipt to = TRUE - $this->callAPISuccess('contribution', 'completetransaction', [ + $params = array_merge($this->_params, ['contribution_status_id' => 2, 'receipt_date' => 'now', 'amount_level' => 'amount entered']); + $contribution = $this->callAPISuccess('Contribution', 'create', $params); + // Complete the transaction overriding is_email_receipt to = TRUE. + $this->callAPISuccess('Contribution', 'completetransaction', [ 'id' => $contribution['id'], 'is_email_receipt' => 1, ]); $mut->checkMailLog([ 'Contribution Information', + 'amount entered', ]); $mut->stop(); } diff --git a/xml/templates/message_templates/contribution_online_receipt_html.tpl b/xml/templates/message_templates/contribution_online_receipt_html.tpl index 4fac9a546a..8d0a16a39e 100644 --- a/xml/templates/message_templates/contribution_online_receipt_html.tpl +++ b/xml/templates/message_templates/contribution_online_receipt_html.tpl @@ -43,7 +43,7 @@ - {if !empty($lineItem) and !empty($priceSetID) and empty($is_quick_config)} + {if $isShowLineItems} {foreach from=$lineItem item=value key=priceset} @@ -119,7 +119,7 @@ {/foreach} {/if} - {if isset($totalTaxAmount)} + {if $isShowTax} {ts}Total Tax{/ts} @@ -146,7 +146,7 @@ {ts}Total Tax Amount{/ts} - {$totalTaxAmount|crmMoney:$currency} + {contribution.tax_amount|crmMoney} {/if} @@ -155,7 +155,7 @@ {ts}Amount{/ts} - {$amount|crmMoney:$currency} {if isset($amount_level)} - {$amount_level}{/if} + {$amount|crmMoney:$currency} {if '{contribution.amount_level}'} - {contribution.amount_level}{/if} diff --git a/xml/templates/message_templates/contribution_online_receipt_text.tpl b/xml/templates/message_templates/contribution_online_receipt_text.tpl index 43174b9943..c2122c045f 100644 --- a/xml/templates/message_templates/contribution_online_receipt_text.tpl +++ b/xml/templates/message_templates/contribution_online_receipt_text.tpl @@ -14,7 +14,7 @@ {ts}Contribution Information{/ts} =========================================================== -{if $lineItem and $priceSetID and empty($is_quick_config)} +{if $isShowLineItems} {foreach from=$lineItem item=value key=priceset} --------------------------------------------------------- {capture assign=ts_item}{ts}Item{/ts}{/capture} @@ -45,13 +45,13 @@ {/foreach} {/if} -{if isset($totalTaxAmount)} -{ts}Total Tax Amount{/ts}: {$totalTaxAmount|crmMoney:$currency} +{if $isShowTax} +{ts}Total Tax Amount{/ts}: {contribution.tax_amount|crmMoney} {/if} {ts}Total Amount{/ts}: {$amount|crmMoney:$currency} {else} -{ts}Amount{/ts}: {$amount|crmMoney:$currency} {if isset($amount_level) } - {$amount_level} {/if} +{ts}Amount{/ts}: {$amount|crmMoney:$currency} {if '{contribution.amount_level}'} - {contribution.amount_level}{/if} {/if} {/if} {if !empty($receive_date)} -- 2.25.1