From: Eileen McNaughton Date: Sat, 2 Dec 2023 05:31:24 +0000 (+1300) Subject: Fix notices on event section of payment or refund notification X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=29bdf6e83f97184842be697e19caa96a5fd7a328;p=civicrm-core.git Fix notices on event section of payment or refund notification --- diff --git a/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.php b/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.php index 4c5bdc364f..d2610b6a4f 100644 --- a/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.php +++ b/CRM/Contribute/WorkflowMessage/Contribution/BasicContribution.php @@ -4,6 +4,7 @@ use Civi\Api4\PriceField; use Civi\Api4\PriceFieldValue; use Civi\Api4\PriceSet; use Civi\Api4\WorkflowMessage; +use Civi\Test; use Civi\WorkflowMessage\GenericWorkflowMessage; use Civi\WorkflowMessage\WorkflowMessageExample; @@ -19,20 +20,25 @@ class CRM_Contribute_WorkflowMessage_Contribution_BasicContribution extends Work * Get the examples this class is able to deliver. */ public function getExamples(): iterable { - $workflows = ['contribution_online_receipt', 'contribution_offline_receipt', 'contribution_invoice_receipt']; + $workflows = ['contribution_online_receipt', 'contribution_offline_receipt', 'contribution_invoice_receipt', 'payment_or_refund_notification']; + $defaultCurrency = \Civi::settings()->get('defaultCurrency'); + $currencies = [$defaultCurrency => $defaultCurrency, 'EUR' => 'EUR', 'CAD' => 'CAD']; foreach ($workflows as $workflow) { + foreach ($currencies as $currency) { + yield [ + 'name' => 'workflow/' . $workflow . '/basic_' . $currency, + 'title' => ts('Completed Contribution') . ' : ' . $currency, + 'tags' => $workflow === 'contribution_offline_receipt' ? ['phpunit', 'preview'] : ['preview'], + 'workflow' => $workflow, + ]; + } yield [ - 'name' => 'workflow/' . $workflow . '/basic_eur', - 'title' => ts('Completed Contribution') . ' : ' . 'EUR', - 'tags' => $workflow === 'contribution_offline_receipt' ? ['phpunit', 'preview'] : ['preview'], - 'workflow' => $workflow, - ]; - yield [ - 'name' => 'workflow/' . $workflow . '/' . 'basic_cad', - 'title' => ts('Completed Contribution') . ' : ' . 'CAD', + 'name' => 'workflow/' . $workflow . '/' . 'partially paid' . $currency, + 'title' => ts('Partially Paid Contribution') . ' : ' . $currency, 'tags' => ['preview'], 'workflow' => $workflow, - 'contribution_params' => ['currency' => 'CAD'], + 'is_show_line_items' => TRUE, + 'contribution_params' => ['contribution_status_id' => \CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid')], ]; $priceSet = $this->getNonQuickConfigPriceSet(); if ($priceSet) { @@ -111,14 +117,28 @@ class CRM_Contribute_WorkflowMessage_Contribution_BasicContribution extends Work * @throws \Civi\API\Exception\UnauthorizedException */ private function addExampleData(GenericWorkflowMessage $messageTemplate, $example): void { - $messageTemplate->setContact(\Civi\Test::example('entity/Contact/Barb')); - $contribution = \Civi\Test::example('entity/Contribution/Euro5990/completed'); + $messageTemplate->setContact(Test::example('entity/Contact/Barb')); + $contribution = Test::example('entity/Contribution/Euro5990/completed'); + $example['currency'] = $example['currency'] ?? \Civi::settings()->get('defaultCurrency'); if (isset($example['contribution_params'])) { $contribution = array_merge($contribution, $example['contribution_params']); } $contribution['contribution_status_id:name'] = \CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution['contribution_status_id']); $contribution['contribution_status_id:label'] = \CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution['contribution_status_id']); - + if ($contribution['contribution_status_id:name'] === 'Partially paid') { + $contribution['paid_amount'] = round($contribution['total_amount'] / 2, 2); + } + elseif ($contribution['contribution_status_id:name'] === 'Pending' || $contribution['contribution_status_id:name'] === 'Refunded') { + $contribution['paid_amount'] = 0; + } + else { + $contribution['paid_amount'] = $contribution['total_amount']; + } + $contribution['balance_amount'] = $contribution['total_amount'] - $contribution['paid_amount']; + if ($contribution['contribution_status_id:name'] === 'Refunded') { + $contribution['balance_amount'] = 0; + } + $contribution['net_amount'] = $contribution['total_amount'] - $contribution['fee_amount']; $mockOrder = new CRM_Financial_BAO_Order(); $mockOrder->setTemplateContributionID(50); @@ -152,6 +172,19 @@ United States'; $messageTemplate->setContribution($contribution); $messageTemplate->setOrder($mockOrder); $messageTemplate->setContribution($contribution); + $financialTrxn = [ + 'trxn_date' => date('Y-m-d H:i:s'), + 'total_amount' => $contribution['contribution_status_id:name'] === 'Refunded' ? -$contribution['total_amount'] : $contribution['paid_amount'], + 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_FinancialTrxn', 'payment_instrument_id', 'Credit Card'), + 'card_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_FinancialTrxn', 'card_type_id', 'Visa'), + 'pan_truncation' => 5679, + ]; + $financialTrxn['payment_instrument_id:label'] = \CRM_Core_PseudoConstant::getLabel('CRM_Financial_BAO_FinancialTrxn', 'payment_instrument_id', $financialTrxn['payment_instrument_id']); + $financialTrxn['payment_instrument_id:name'] = \CRM_Core_PseudoConstant::getName('CRM_Financial_BAO_FinancialTrxn', 'payment_instrument_id', $financialTrxn['payment_instrument_id']); + $financialTrxn['card_type_id:label'] = \CRM_Core_PseudoConstant::getLabel('CRM_Financial_BAO_FinancialTrxn', 'card_type_id', $financialTrxn['card_type_id']); + $financialTrxn['card_type_id:name'] = \CRM_Core_PseudoConstant::getName('CRM_Financial_BAO_FinancialTrxn', 'card_type_id', $financialTrxn['card_type_id']); + + $messageTemplate->setFinancialTrxn($financialTrxn); } /** diff --git a/CRM/Contribute/WorkflowMessage/ContributionTrait.php b/CRM/Contribute/WorkflowMessage/ContributionTrait.php index b7baed4edb..26c28f73a7 100644 --- a/CRM/Contribute/WorkflowMessage/ContributionTrait.php +++ b/CRM/Contribute/WorkflowMessage/ContributionTrait.php @@ -3,9 +3,10 @@ use Civi\Api4\Membership; /** - * @method array getContribution() * @method int|null getContributionID() - * @method $this setContributionID(?int $contributionId) + * @method $this setContributionID(?int $contributionID) + * @method int|null getFinancialTrxnID() + * @method $this setFinancialTrxnID(?int $financialTrxnID) */ trait CRM_Contribute_WorkflowMessage_ContributionTrait { /** @@ -17,12 +18,34 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait { */ public $contribution; + /** + * @return array|null + */ + public function getContribution(): ?array { + return $this->contribution; + } + + /** + * Optional financial transaction (payment). + * + * @var array|null + * + * @scope tokenContext as financial_trxn + */ + public $financialTrxn; + /** * @var int * @scope tokenContext as contributionId, tplParams as contributionID */ public $contributionID; + /** + * @var int + * @scope tokenContext as financial_trxnId + */ + public $financialTrxnID; + /** * Is the site configured such that tax should be displayed. * @@ -220,6 +243,21 @@ trait CRM_Contribute_WorkflowMessage_ContributionTrait { return $this; } + /** + * Set contribution object. + * + * @param array $financialTrxn + * + * @return $this + */ + public function setFinancialTrxn(array $financialTrxn): self { + $this->financialTrxn = $financialTrxn; + if (!empty($financialTrxn['id'])) { + $this->financialTrxnID = $financialTrxn['id']; + } + return $this; + } + /** * Set order object. * diff --git a/CRM/Contribute/WorkflowMessage/PaymentOrRefundNotification.php b/CRM/Contribute/WorkflowMessage/PaymentOrRefundNotification.php new file mode 100644 index 0000000000..6b1a250c5e --- /dev/null +++ b/CRM/Contribute/WorkflowMessage/PaymentOrRefundNotification.php @@ -0,0 +1,34 @@ +checkPermissions) { $cacheKey .= '__' . CRM_Core_Session::getLoggedInContactID(); diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php index 82da1f2580..281e78183e 100644 --- a/CRM/Financial/BAO/Payment.php +++ b/CRM/Financial/BAO/Payment.php @@ -288,13 +288,18 @@ class CRM_Financial_BAO_Payment { $entities = self::loadRelatedEntities($params['id']); $sendTemplateParams = [ - 'groupName' => 'msg_tpl_workflow_contribution', 'workflow' => 'payment_or_refund_notification', 'PDFFilename' => ts('notification') . '.pdf', - 'contactId' => $entities['contact']['id'], 'toName' => $entities['contact']['display_name'], 'toEmail' => $entities['contact']['email'], 'tplParams' => self::getConfirmationTemplateParameters($entities), + 'modelProps' => array_filter([ + 'contributionID' => $entities['contribution']['id'], + 'contactID' => $entities['contact']['id'], + 'financialTrxnID' => $params['id'], + 'eventID' => $entities['event']['id'] ?? NULL, + 'participantID' => $entities['participant']['id'] ?? NULL, + ]), ]; if (!empty($params['from']) && !empty($params['check_permissions'])) { // Filter from against permitted emails. @@ -365,7 +370,8 @@ class CRM_Financial_BAO_Payment { 'sequential' => 1, ])['values']; if (!empty($participantRecords)) { - $entities['event'] = civicrm_api3('Event', 'getsingle', ['id' => $participantRecords[0]['api.Participant.get']['values'][0]['event_id']]); + $entities['participant'] = $participantRecords[0]['api.Participant.get']['values'][0]; + $entities['event'] = civicrm_api3('Event', 'getsingle', ['id' => $entities['participant']['event_id']]); if (!empty($entities['event']['is_show_location'])) { $locationParams = [ 'entity_id' => $entities['event']['id'], @@ -396,6 +402,9 @@ class CRM_Financial_BAO_Payment { * @param array $entities * Related entities as an array keyed by the various entities. * + * @deprecated these template variables no longer used in the core template + * from 5.69 - stop assigning them. + * * @return array * Values required for the notification * - contact_id diff --git a/CRM/Financial/FinancialTrxnTokens.php b/CRM/Financial/FinancialTrxnTokens.php index 236be716f1..6aab625b8f 100644 --- a/CRM/Financial/FinancialTrxnTokens.php +++ b/CRM/Financial/FinancialTrxnTokens.php @@ -26,4 +26,11 @@ class CRM_Financial_FinancialTrxnTokens extends CRM_Core_EntityTokens { return 'FinancialTrxn'; } + /** + * @return array + */ + public function getCurrencyFieldName(): array { + return ['currency']; + } + } diff --git a/xml/templates/message_templates/payment_or_refund_notification_html.tpl b/xml/templates/message_templates/payment_or_refund_notification_html.tpl index 9da9560b5c..83600ab429 100644 --- a/xml/templates/message_templates/payment_or_refund_notification_html.tpl +++ b/xml/templates/message_templates/payment_or_refund_notification_html.tpl @@ -22,11 +22,11 @@ {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}

{$greeting},

{/if} - {if $isRefund} + {if {financial_trxn.total_amount|raw} < 0}

{ts}A refund has been issued based on changes in your registration selections.{/ts}

{else}

{ts}Below you will find a receipt for this payment.{/ts}

- {if $paymentsComplete} + {if !{contribution.balance_amount|boolean}}

{ts}Thank you for completing this contribution.{/ts}

{/if} {/if} @@ -35,7 +35,7 @@ - {if $isRefund} + {if {financial_trxn.total_amount|raw} < 0} @@ -44,7 +44,7 @@ {ts}This Refund Amount{/ts} {else} @@ -56,47 +56,47 @@ {ts}This Payment Amount{/ts} {/if} - {if $receive_date} + {if {financial_trxn.trxn_date|boolean}} {/if} - {if !empty($trxn_id)} + {if {financial_trxn.trxn_id|boolean}} {/if} - {if !empty($paidBy)} + {if {financial_trxn.payment_instrument_id|boolean}} {/if} - {if !empty($checkNumber)} + {if {financial_trxn.check_number|boolean}} {/if} @@ -104,34 +104,34 @@ - {if $totalAmount} + {if {contribution.total_amount|boolean}} {/if} - {if $totalPaid} + {if {contribution.paid_amount|boolean}} {/if} - {if $amountOwed} + {if {contribution.balance_amount|boolean}} {* This will be zero after final payment. *} + {contribution.balance_amount} + {/if}
{ts}Refund Details{/ts}
- {$refundAmount|crmMoney:$currency} + {financial_trxn.total_amount}
- {$paymentAmount|crmMoney:$currency} + {financial_trxn.total_amount}
{ts}Transaction Date{/ts} - {$receive_date|crmDate} + {financial_trxn.trxn_date}
{ts}Transaction #{/ts} - {$trxn_id} + {financial_trxn.trxn_id}
{ts}Paid By{/ts} - {$paidBy} + {financial_trxn.payment_instrument_id:label}
{ts}Check Number{/ts} - {$checkNumber} + {financial_trxn.check_number}
{ts}Contribution Details{/ts}
{ts}Total Fee{/ts} - {$totalAmount|crmMoney:$currency} + {contribution.total_amount}
{ts}Total Paid{/ts} - {$totalPaid|crmMoney:$currency} + {contribution.paid_amount}
{ts}Balance Owed{/ts} - {$amountOwed|crmMoney:$currency} -
@@ -142,105 +142,125 @@ {if !empty($billingName) || !empty($address)} - - - - - + + + + - - {/if} - {if !empty($credit_card_number)} - - - - - - + + + {/if} + {if {financial_trxn.pan_truncation|boolean}} + + + + + + {/if} - {if $component eq 'event'} - - - - - - + {if {event.id|boolean}} + + + + + + - {if !empty($event.participant_role)} - - - - - {/if} + {if {participant.role_id|boolean}} + + + + + {/if} - {if !empty($isShowLocation)} - - - - {/if} + {if {event.is_show_location|boolean}} + + + + {/if} + {if {event.loc_block_id.phone_id.phone|boolean} || {event.loc_block_id.email_id.email|boolean}} + + + - {if !empty($location.phone.1.phone) || !empty($location.email.1.email)} - - + + + + {/if} + {if {event.loc_block_id.phone_2_id.phone|boolean}} + + + + + {/if} + + {if {event.loc_block_id.email_id.email|boolean}} + + + + + {/if} + + {if {event.loc_block_id.email_2_id.email|boolean}} + + + + + {/if} + {/if} + + {/if} +
- {ts}Billing Name and Address{/ts} -
+
+ {ts}Billing Name and Address{/ts} +
{if !empty($billingName)}{$billingName}{/if}
{if !empty($address)}{$address|nl2br}{/if} -
- {ts}Credit Card Information{/ts} -
- {$credit_card_type}
- {$credit_card_number}
- {ts}Expires:{/ts} {$credit_card_exp_date|truncate:7:''|crmDate} -
+ {ts}Credit Card Information{/ts} +
+ {financial_trxn.card_type_id:label}
+ ************{financial_trxn.pan_truncation}
+
- {ts}Event Information and Location{/ts} -
- {$event.event_title}
- {$event.event_start_date|crmDate}{if $event.event_end_date}-{if $event.event_end_date|crmDate:"%Y%m%d" == $event.event_start_date|crmDate:"%Y%m%d"}{$event.event_end_date|crmDate:0:1}{else}{$event.event_end_date|crmDate}{/if}{/if} -
+ {ts}Event Information and Location{/ts} +
+ {event.event_title}
+ {event.start_date|crmDate}{if {event.end_date|boolean}}-{if '{event.end_date|crmDate:"%Y%m%d"}' === '{event.start_date|crmDate:"%Y%m%d"}'}{event.end_date|crmDate:"Time"}{else}{event.end_date}{/if}{/if} +
- {ts}Participant Role{/ts} - - {$event.participant_role} -
+ {ts}Participant Role{/ts} + + {participant.role_id:label} +
- {$location.address.1.display|nl2br} -
+ {event.location} +
+ {ts}Event Contacts:{/ts} +
- {ts}Event Contacts:{/ts} + {if {event.loc_block_id.phone_id.phone|boolean}} +
+ {if {event.loc_block_id.phone_id.phone_type_id|boolean}} + {event.loc_block_id.phone_id.phone_type_id:label} + {else} + {ts}Phone{/ts} + {/if} + + {event.loc_block_id.phone_id.phone} {if {event.loc_block_id.phone_id.phone_ext|boolean}} {ts}ext.{/ts} {event.loc_block_id.phone_id.phone_ext}{/if} +
+ {if {event.loc_block_id.phone_2_id.phone_type_id|boolean}} + {event.loc_block_id.phone_2_id.phone_type_id:label} + {else} + {ts}Phone{/ts} + {/if} + + {event.loc_block_id.phone_2_id.phone} {if {event.loc_block_id.phone_2_id.phone_ext|boolean}} {ts}ext.{/ts} {event.loc_block_id.phone_2_id.phone_ext}{/if} +
+ {ts}Email{/ts} + + {event.loc_block_id.email_id.email} +
+ {ts}Email{/ts} + + {event.loc_block_id.email_2_id.email} +
- {foreach from=$location.phone item=phone} - {if $phone.phone} - - - {if $phone.phone_type} - {$phone.phone_type_display} - {else} - {ts}Phone{/ts} - {/if} - - - {$phone.phone} {if $phone.phone_ext} {ts}ext.{/ts} {$phone.phone_ext}{/if} - - - {/if} - {/foreach} - {foreach from=$location.email item=eventEmail} - {if $eventEmail.email} - - - {ts}Email{/ts} - - - {$eventEmail.email} - - - {/if} - {/foreach} - {/if} {*phone block close*} - {/if} - - - - - diff --git a/xml/templates/message_templates/payment_or_refund_notification_subject.tpl b/xml/templates/message_templates/payment_or_refund_notification_subject.tpl index 7fc6a6b084..2e8f8f99ce 100644 --- a/xml/templates/message_templates/payment_or_refund_notification_subject.tpl +++ b/xml/templates/message_templates/payment_or_refund_notification_subject.tpl @@ -1 +1 @@ -{if $isRefund}{ts}Refund Notification{/ts}{else}{ts}Payment Receipt{/ts}{/if}{if $component eq 'event'} - {$event.title}{/if} - {contact.display_name} +{if {financial_trxn.total_amount|raw} < 0}{ts}Refund Notification{/ts}{else}{ts}Payment Receipt{/ts}{/if}{if {event.title|boolean}} - {event.title}{/if} - {contact.display_name}