From dd118b15122acb052f4edd1a5b104aaba20f154c Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 28 Aug 2020 12:52:42 +1200 Subject: [PATCH] dev/core#1972 Fix tax_amount calclation on renewal form --- CRM/Financial/BAO/Order.php | 20 ++++++++++++++++--- .../CRM/Member/Form/MembershipRenewalTest.php | 4 ++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CRM/Financial/BAO/Order.php b/CRM/Financial/BAO/Order.php index 528cdcefb143..43fa9cd2d72a 100644 --- a/CRM/Financial/BAO/Order.php +++ b/CRM/Financial/BAO/Order.php @@ -236,18 +236,17 @@ protected function calculateLineItems(): array { $lineItems[$valueID] = CRM_Price_BAO_PriceSet::getLine($params, $throwAwayArray, $this->getPriceSetID(), $this->getPriceFieldSpec($fieldID), $fieldID, 0)[1][$valueID]; } - $taxRates = CRM_Core_PseudoConstant::getTaxRates(); foreach ($lineItems as &$lineItem) { // Set any pre-calculation to zero as we will calculate. $lineItem['tax_amount'] = 0; if ($this->getOverrideFinancialTypeID() !== FALSE) { $lineItem['financial_type_id'] = $this->getOverrideFinancialTypeID(); } - $taxRate = $taxRates[$lineItem['financial_type_id']] ?? 0; + $taxRate = $this->getTaxRate((int) $lineItem['financial_type_id']); if ($this->getOverrideTotalAmount() !== FALSE) { if ($taxRate) { // Total is tax inclusive. - $lineItem['tax_amount'] = ($taxRate / 100) * $this->getOverrideTotalAmount(); + $lineItem['tax_amount'] = ($taxRate / 100) * $this->getOverrideTotalAmount() / (1 + ($taxRate / 100)); $lineItem['line_total'] = $lineItem['unit_price'] = $this->getOverrideTotalAmount() - $lineItem['tax_amount']; } else { @@ -276,4 +275,19 @@ public function getTotalTaxAmount() :float { return $amount; } + /** + * Get the tax rate for the given financial type. + * + * @param int $financialTypeID + * + * @return float + */ + public function getTaxRate(int $financialTypeID) { + $taxRates = CRM_Core_PseudoConstant::getTaxRates(); + if (!isset($taxRates[$financialTypeID])) { + return 0; + } + return $taxRates[$financialTypeID]; + } + } diff --git a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php index c990c6318f90..d70f497c2f1d 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php @@ -241,7 +241,7 @@ public function testSubmitWithTax() { ], 'credit_card_type' => 'Visa', 'billing_first_name' => 'Test', - 'billing_middlename' => 'Last', + 'billing_middle_name' => 'Last', 'billing_street_address-5' => '10 Test St', 'billing_city-5' => 'Test', 'billing_state_province_id-5' => '1003', @@ -250,7 +250,7 @@ public function testSubmitWithTax() { ]); $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualId, 'is_test' => TRUE, 'return' => ['total_amount', 'tax_amount']]); $this->assertEquals(50, $contribution['total_amount']); - $this->assertEquals(5, $contribution['tax_amount']); + $this->assertEquals(4.55, $contribution['tax_amount']); } /**