Skip to content

Commit

Permalink
CRM-19585, added function to Calculate Tax for each item when Financi…
Browse files Browse the repository at this point in the history
…al Type is changed.
  • Loading branch information
pradpnayak authored and monishdeb committed Apr 17, 2017
1 parent 4fc3836 commit 92f0dc8
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
49 changes: 49 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5551,4 +5551,53 @@ public static function replaceContributionTokens(
return $contributionDetails;
}

/**
* Calculate Tax for each item when Financial Type is changed.
*
* @param array $lineItem
*
* @param int $contributionId
*
*/
public static function calculateTaxAfterChangeInFinancialTypeForLineItems($lineItem, $contributionId) {
$taxAmount = 0;
$previousLineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contributionId);
foreach ($lineItem as $items) {
foreach ($items as $item) {
$lineTotal = CRM_Utils_Array::value('line_total', CRM_Utils_Array::value($item['id'], $previousLineItem));
$lineTaxAmount = CRM_Contribute_BAO_Contribution_Utils::calculateTaxAmount($lineTotal, $item['tax_rate']);
$taxAmount += $lineTaxAmount['tax_amount'];
}
}
return $taxAmount;
}

/**
* Calculate Tax when Financial Type is changed.
*
* @param array $params
* @param float $totalAmount
* @param array $oldTaxAmounts
* @param float $changeFTAmount
*
*/
public static function calculateTaxForChangeInFinancialType(
&$params, &$totalAmount,
&$oldTaxAmounts, &$changeFTAmount
) {
$taxAmountAfterFTChange = self::calculateTaxAfterChangeInFinancialTypeForLineItems($params['line_item'], $params['contribution']->id);
$previousTaxAmount = 0;
if (isset($params['prevContribution']->tax_amount)) {
$previousTaxAmount = $params['prevContribution']->tax_amount;
}
$taxDiff = $taxAmountAfterFTChange - $previousTaxAmount;
$changeFTAmount += $taxDiff;
$totalAmount -= $taxDiff;
if ($taxDiff > 0) {
$params['tax_amount'] = $taxAmountAfterFTChange;
$oldTaxAmounts['new_tax_amount'] -= $taxDiff;
$oldTaxAmounts['previous_tax_amount'] = $taxAmountAfterFTChange - $taxDiff;
}
}

}
105 changes: 105 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1251,4 +1251,109 @@ public function testReplaceContributionTokens() {
<p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
}

/**
* test for function calculateTaxAfterChangeInFinancialTypeForLineItems()
*/
public function testcalculateTaxAfterChangeInFinancialTypeForLineItems() {
list($contribution, $financialAccount) = $this->createContributionWithTax();
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
foreach ($lineItems as $id => $lineItem) {
$lineItems[$id]['line_total'] = 300;
$lineItems[$id]['tax_rate'] = 20;
$lineItems[$id]['id'] = $id;
}
$taxAmount = CRM_Contribute_BAO_Contribution::calculateTaxAfterChangeInFinancialTypeForLineItems(array($lineItems), $contribution['id']);
$this->assertEquals($taxAmount, 20.00, 'Amount does not match.');
foreach ($lineItems as $id => $lineItem) {
$lineItems[$id]['line_total'] = 300;
$lineItems[$id]['tax_rate'] = 0;
$lineItems[$id]['id'] = $id;
}
$taxAmount = CRM_Contribute_BAO_Contribution::calculateTaxAfterChangeInFinancialTypeForLineItems(array($lineItems), $contribution['id']);
$this->assertEquals($taxAmount, 0, 'Amount does not match.');
}

/**
* test for function calculateTaxForChangeInFinancialType()
*/
public function testcalculateTaxForChangeInFinancialType() {
list($contribution, $financialAccount) = $this->createContributionWithTax();
$params = $this->alterLineItemsAndOtherParams($contribution, 20, 300);
$totalAmount = 360;
$oldTaxAmounts = array('new_tax_amount' => 60);
$changeFTAmount = 110;
CRM_Contribute_BAO_Contribution::calculateTaxForChangeInFinancialType($params, $totalAmount, $oldTaxAmounts, $changeFTAmount);
$this->assertEquals($totalAmount, 350, 'Amount does not match.');
$this->assertEquals($changeFTAmount, 120, 'Amount does not match.');
$this->assertEquals($params['tax_amount'], 20, 'Amount does not match.');
$this->assertEquals($oldTaxAmounts['new_tax_amount'], 50, 'Amount does not match.');
$this->assertEquals($oldTaxAmounts['previous_tax_amount'], 10, 'Amount does not match.');
$params = $this->alterLineItemsAndOtherParams($contribution, 0, 300);
$totalAmount = 300;
$oldTaxAmounts = array('new_tax_amount' => NULL);
$changeFTAmount = 110;
CRM_Contribute_BAO_Contribution::calculateTaxForChangeInFinancialType($params, $totalAmount, $oldTaxAmounts, $changeFTAmount);
$this->assertEquals($totalAmount, 310, 'Amount does not match.');
$this->assertEquals($changeFTAmount, 100, 'Amount does not match.');
$this->assertEquals(CRM_Utils_Array::value('tax_amount', $params), NULL, 'Amount does not match.');
$this->assertEquals($oldTaxAmounts['new_tax_amount'], NULL, 'Amount does not match.');
$this->assertEquals(CRM_Utils_Array::value('previous_tax_amount', $oldTaxAmounts), NULL, 'Amount does not match.');
$financialType = $this->createFinancialType();
$contributionParams = array(
'contact_id' => $contribution['contact_id'],
'financial_type_id' => $financialType['id'],
'total_amount' => 100,
'contribution_status_id' => 1,
);
$contribution = CRM_Contribute_BAO_Contribution::add($contributionParams);
$params = $this->alterLineItemsAndOtherParams($contribution, 10, 300);
$totalAmount = 330;
$oldTaxAmounts = array('new_tax_amount' => 30);
$changeFTAmount = 100;
CRM_Contribute_BAO_Contribution::calculateTaxForChangeInFinancialType($params, $totalAmount, $oldTaxAmounts, $changeFTAmount);
$this->assertEquals($totalAmount, 320, 'Amount does not match.');
$this->assertEquals($changeFTAmount, 110, 'Amount does not match.');
$this->assertEquals($params['tax_amount'], 10, 'Amount does not match.');
$this->assertEquals($oldTaxAmounts['new_tax_amount'], 20, 'Amount does not match.');
$this->assertEquals($oldTaxAmounts['previous_tax_amount'], 0, 'Amount does not match.');
}

/**
* Alter Line Item array to calculate tax change and other contribution attributes.
*
* @param array $contribution
* contribution array
* @param float $taxRate
* tax rate of line item
* @param float $totalAmount
* Contribution total amount
* @param float $taxAmount
* Tax amount
*
* @return array
*/
public function alterLineItemsAndOtherParams($contribution, $taxRate, $totalAmount, $taxAmount = NULL) {
if (is_array($contribution)) {
$contributionId = $contribution['id'];
$contribution = new CRM_Contribute_DAO_Contribution();
$contribution->id = $contributionId;
$contribution->find(TRUE);
}
$params = array(
'contribution' => $contribution,
'prevContribution' => $contribution,
);
$lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution->id);
foreach ($lineItems as $id => $lineItem) {
$lineItems[$id]['tax_rate'] = $taxRate;
$lineItems[$id]['line_total'] = $totalAmount;
$lineItems[$id]['id'] = $id;
}
$params['line_item'][1] = $lineItems;
if (isset($taxAmount)) {
$params['tax_amount'] = $taxAmount;
}
return $params;
}

}

0 comments on commit 92f0dc8

Please sign in to comment.