From b507f0d3b7a4e50bcb483b991be610101b78ef9f Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Fri, 23 Dec 2016 19:40:38 +0530 Subject: [PATCH] CRM-19585, added function to calculate net amount if contribution has tax amount added test for the same ---------------------------------------- * CRM-19585: Sales tax issue https://issues.civicrm.org/jira/browse/CRM-19585 CRM-19585, removed space ---------------------------------------- * CRM-19585: Sales tax issue https://issues.civicrm.org/jira/browse/CRM-19585 --- CRM/Contribute/BAO/Contribution.php | 16 +++++++++ CRM/Contribute/Form/Contribution.php | 12 +------ .../CRM/Contribute/BAO/ContributionTest.php | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 7e20ab4e55..301e98a39f 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -5360,4 +5360,20 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co return $amount; } + /** + * Calculate net amount. + * + * @param array $netAmount + * + * @param float $taxAmount + * + * @return array + */ + public static function calculateNetAmount($netAmount, $taxAmount) { + if ($taxAmount) { + $netAmount -= $taxAmount; + } + return CRM_Utils_Money::format($netAmount, NULL, '%a'); + } + } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 757073e6fe..8bf943c5b8 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -378,7 +378,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } if (isset($defaults['net_amount'])) { - $defaults['net_amount'] = CRM_Utils_Money::format($defaults['net_amount'], NULL, '%a'); + $defaults['net_amount'] = CRM_Contribute_BAO_Contribution::calculateNetAmount($defaults['net_amount'], $defaults['tax_amount']); } if ($this->_contributionType) { @@ -970,16 +970,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP if (!empty($fields['total_amount']) && (!empty($fields['net_amount']) || !empty($fields['fee_amount']))) { $sum = CRM_Utils_Rule::cleanMoney($fields['net_amount']) + CRM_Utils_Rule::cleanMoney($fields['fee_amount']); - // For taxable contribution we need to deduct taxable amount from - // (net amount + fee amount) before comparing it with total amount - if (!empty($self->_values['tax_amount'])) { - $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($self->_id); - if (!(CRM_Utils_Array::value('membership', $componentDetails) || - CRM_Utils_Array::value('participant', $componentDetails)) - ) { - $sum = CRM_Utils_Money::format($sum - $self->_values['tax_amount'], NULL, '%a'); - } - } if (CRM_Utils_Rule::cleanMoney($fields['total_amount']) != $sum) { $errors['total_amount'] = ts('The sum of fee amount and net amount must be equal to total amount'); } diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 0e9395417e..6144348e84 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -938,6 +938,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; } /** +<<<<<<< HEAD * Test calculateFinancialItemAmount(). */ public function testcalculateFinancialItemAmount() { @@ -1005,4 +1006,37 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; } } + /** + * Test calculateNetAmount. + */ + public function testcalculateNetAmount() { + $testParams = array( + array( + 'net_amount' => 100, + 'tax_amount' => 10, + 'expectedNetAmount' => 90, + ), + array( + 'net_amount' => 200, + 'tax_amount' => 0, + 'expectedNetAmount' => 200, + ), + array( + 'net_amount' => 300, + 'tax_amount' => NULL, + 'expectedNetAmount' => 300, + ), + array( + 'net_amount' => -100, + 'tax_amount' => 20, + 'expectedNetAmount' => -120, + ), + ); + + foreach ($testParams as $params) { + $netAmount = CRM_Contribute_BAO_Contribution::calculateNetAmount($params['net_amount'], $params['tax_amount']); + $this->assertEquals($netAmount, $params['expectedNetAmount'], 'Invalid Net amount.'); + } + } + } -- 2.25.1