From 9303364fb7a777b19958b90c78095204fda95b6f Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 3 May 2022 11:35:28 +0100 Subject: [PATCH] Fix calculation and assignment of taxAmount on contribution page confirmation --- CRM/Contribute/Form/Contribution/Confirm.php | 27 ++++++++++++-------- CRM/Contribute/Form/Contribution/Main.php | 4 --- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 2ef3a2a863c5..7e654091c900 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -280,7 +280,10 @@ public function preProcess() { $this->setFormAmountFields($this->_params['priceSetId']); } - $this->_params['tax_amount'] = $this->get('tax_amount'); + if (!empty($this->get('tax_amount'))) { + CRM_Core_Error::deprecatedWarning('tax_amount should be not passed in'); + $this->_params['tax_amount'] = $this->get('tax_amount'); + } $this->_useForMember = $this->get('useForMember'); CRM_Contribute_Form_AbstractEditPayment::formatCreditCardDetails($this->_params); @@ -492,15 +495,19 @@ public function buildQuickForm() { // Make a copy of line items array to use for display only $tplLineItems = $this->_lineItem; if (CRM_Invoicing_Utils::isInvoicingEnabled()) { - // @todo $params seems like exactly the wrong place to get totalTaxAmount from - // this is a calculated variable so we it should be transparent how we - // calculated it rather than coming from 'params' - $this->assign('totalTaxAmount', $params['tax_amount']); + $taxAmount = 0; + foreach ($tplLineItems ?? [] as $lineItems) { + foreach ($lineItems as $lineItem) { + $taxAmount += (float) ($lineItem['tax_amount'] ?? 0); + } + } + + $this->assign('totalTaxAmount', $taxAmount); + $this->assign('taxTerm', CRM_Invoicing_Utils::getTaxTerm()); } - $this->assignLineItemsToTemplate($tplLineItems); - $isDisplayLineItems = $this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); + $isDisplayLineItems = $this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); $this->assign('isDisplayLineItems', $isDisplayLineItems); if (!$isDisplayLineItems) { @@ -509,6 +516,9 @@ public function buildQuickForm() { $this->assign('is_quick_config', 1); $this->_params['is_quick_config'] = 1; } + else { + $this->assignLineItemsToTemplate($tplLineItems); + } if (!empty($params['selectProduct']) && $params['selectProduct'] !== 'no_thanks') { $option = $params['options_' . $params['selectProduct']] ?? NULL; @@ -565,8 +575,6 @@ public function buildQuickForm() { $this->assign('priceSetID', $this->_priceSetId); // The concept of contributeMode is deprecated. - // the is_monetary concept probably should be too as it can be calculated from - // the existence of 'amount' & seems fragile. if ($this->_contributeMode === 'notify' || $this->_amount <= 0.0 || $this->_params['is_pay_later'] ) { @@ -1124,7 +1132,6 @@ protected function processFormContribution( // lets store it in the form variable so postProcess hook can get to this and use it $form->_contributionID = $contribution->id; } - $form->assign('totalTaxAmount', $params['tax_amount'] ?? NULL); // process soft credit / pcp params first CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 3d8c6abafb03..9e82c1f81b03 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -1363,10 +1363,6 @@ public function submit($params) { CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId); } - if ($params['tax_amount']) { - $this->set('tax_amount', $params['tax_amount']); - } - if ($proceFieldAmount) { $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount; $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;