Skip to content

Commit

Permalink
CRM-19585, added test
Browse files Browse the repository at this point in the history
----------------------------------------
* CRM-19585: Sales tax issue
  https://issues.civicrm.org/jira/browse/CRM-19585
  • Loading branch information
pradpnayak authored and monishdeb committed Apr 17, 2017
1 parent b610400 commit 64c5c54
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,87 @@ public function testcalculateTaxAfterChangeInFinancialTypeForLineItems() {
$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 64c5c54

Please sign in to comment.