Skip to content

Commit

Permalink
Merge pull request civicrm#9948 from KarinG/CRM-19966
Browse files Browse the repository at this point in the history
Fixes for CRM-19966 Unfair Double Taxation and net_amounts.
  • Loading branch information
totten authored Mar 10, 2017
2 parents 9869aec + e363872 commit e15e409
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 72 deletions.
16 changes: 0 additions & 16 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5376,22 +5376,6 @@ public static function calculateFinancialItemAmount($params, $amountParams, $con
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');
}

/**
* Retrieve Sales Tax Financial Accounts.
*
Expand Down
20 changes: 6 additions & 14 deletions CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,11 @@ public function preProcess() {
else {
$lineItem = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution', 1, TRUE, TRUE);
}
// wtf?
empty($lineItem) ? NULL : $this->_lineItems[] = $lineItem;
}

$this->assign('lineItem', empty($this->_lineItems) ? FALSE : $this->_lineItems);
$this->assign('lineItem', empty($lineItem) ? FALSE : array($lineItem));

// Set title
if ($this->_mode && $this->_id) {
Expand Down Expand Up @@ -377,10 +378,6 @@ public function setDefaultValues() {
$defaults['fee_amount'] = CRM_Utils_Money::format($defaults['fee_amount'], NULL, '%a');
}

if (isset($defaults['net_amount'])) {
$defaults['net_amount'] = CRM_Contribute_BAO_Contribution::calculateNetAmount($defaults['net_amount'], CRM_Utils_Array::value('tax_amount', $defaults));
}

if ($this->_contributionType) {
$defaults['financial_type_id'] = $this->_contributionType;
}
Expand Down Expand Up @@ -1437,7 +1434,7 @@ public function testSubmit($params, $action, $creditCardMode = NULL) {
* @throws \Exception
*/
protected function submit($submittedValues, $action, $pledgePaymentID) {
$softIDs = array();

$pId = $contribution = $isRelatedId = FALSE;
$this->_params = $submittedValues;
$this->beginPostProcess();
Expand Down Expand Up @@ -1590,7 +1587,7 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
}

if (!isset($submittedValues['total_amount'])) {
$submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values);
$submittedValues['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_values) - CRM_Utils_Array::value('tax_amount', $this->_values);
}
$this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE);

Expand Down Expand Up @@ -1802,13 +1799,8 @@ protected function invoicingPostProcessHook($submittedValues, $action, $lineItem
}
$taxRate = array();
$getTaxDetails = FALSE;
if ($action & CRM_Core_Action::ADD) {
$line = $lineItem;
}
elseif ($action & CRM_Core_Action::UPDATE) {
$line = $this->_lineItems;
}
foreach ($line as $key => $value) {

foreach ($lineItem as $key => $value) {
foreach ($value as $v) {
if (isset($taxRate[(string) CRM_Utils_Array::value('tax_rate', $v)])) {
$taxRate[(string) $v['tax_rate']] = $taxRate[(string) $v['tax_rate']] + CRM_Utils_Array::value('tax_amount', $v);
Expand Down
33 changes: 0 additions & 33 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,39 +1005,6 @@ public function testcalculateFinancialItemAmount() {
}
}

/**
* 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.');
}
}

/**
* Test recording of amount with comma separator.
*/
Expand Down
71 changes: 71 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,4 +851,75 @@ public function testSubmitWithOutSaleTax() {
$this->assertTrue(empty($lineItem['tax_amount']));
}

/**
* Create a contribution & then edit it via backoffice form, checking tax with: default price_set
*
* @throws \Exception
*/
public function testReSubmitSaleTax() {
$this->enableTaxAndInvoicing();
$this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
$form = new CRM_Contribute_Form_Contribution();

$form->testSubmit(array(
'total_amount' => 100,
'financial_type_id' => $this->_financialTypeId,
'receive_date' => '04/21/2015',
'receive_date_time' => '11:27PM',
'contact_id' => $this->_individualId,
'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
'contribution_status_id' => 1,
'price_set_id' => 0,
),
CRM_Core_Action::ADD
);
$contribution = $this->callAPISuccessGetSingle('Contribution',
array(
'contribution_id' => 1,
'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(110, $contribution['net_amount']);

$mut = new CiviMailUtils($this, TRUE);
// Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
$form->testSubmit(array(
'id' => $contribution['id'],
'tax_amount' => $contribution['tax_amount'],
'financial_type_id' => $contribution['financial_type_id'],
'receive_date' => $contribution['receive_date'],
'payment_instrument_id' => $contribution['payment_instrument_id'],
'price_set_id' => 0,
'check_number' => 12345,
'contribution_status_id' => 1,
'is_email_receipt' => 1,
'from_email_address' => '[email protected]',
),
CRM_Core_Action::UPDATE
);
$contribution = $this->callAPISuccessGetSingle('Contribution',
array(
'contribution_id' => 1,
'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
)
);
$this->assertEquals(110, $contribution['total_amount']);
$this->assertEquals(10, $contribution['tax_amount']);
$this->assertEquals(110, $contribution['net_amount']);

$strings = array(
'Total Tax Amount : $ 10.00',
'Total Amount : $ 110.00',
'Date Received: April 21st, 2015',
'Paid By: Check',
'Check Number: 12345',
);

$mut->checkMailLog($strings);
$this->callAPISuccessGetCount('FinancialTrxn', array(), 3);
$this->callAPISuccessGetCount('FinancialItem', array(), 2);
}

}
18 changes: 9 additions & 9 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,6 @@ public function testCreateUpdateWithoutChangingPendingStatus() {
* CHANGE: we require the API to do an incremental update
*/
public function testCreateUpdateContribution() {

$contributionID = $this->contributionCreate(array(
'contact_id' => $this->_individualId,
'trxn_id' => 212355,
Expand Down Expand Up @@ -1529,13 +1528,12 @@ public function testCreateUpdateContribution() {
$params = array(
'id' => $contributionID,
'contact_id' => $this->_individualId,
'total_amount' => 110.00,
'total_amount' => 105.00,
'fee_amount' => 7.00,
'financial_type_id' => $this->_financialTypeId,
'non_deductible_amount' => 10.00,
'net_amount' => 100.00,
'non_deductible_amount' => 22.00,
'contribution_status_id' => 1,
'note' => 'Donating for Noble Cause',

);

$contribution = $this->callAPISuccess('contribution', 'create', $params);
Expand All @@ -1547,17 +1545,19 @@ public function testCreateUpdateContribution() {
$contribution = $this->callAPISuccessGetSingle('contribution', $new_params);

$this->assertEquals($contribution['contact_id'], $this->_individualId);
$this->assertEquals($contribution['total_amount'], 110.00);
$this->assertEquals($contribution['total_amount'], 105.00);
$this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
$this->assertEquals($contribution['financial_type'], 'Donation');
$this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
$this->assertEquals($contribution['non_deductible_amount'], 10.00);
$this->assertEquals($contribution['fee_amount'], $old_fee_amount);
$this->assertEquals($contribution['net_amount'], 100.00);
$this->assertEquals($contribution['non_deductible_amount'], 22.00);
$this->assertEquals($contribution['fee_amount'], 7.00);
$this->assertEquals($contribution['trxn_id'], $old_trxn_id);
$this->assertEquals($contribution['invoice_id'], $old_invoice_id);
$this->assertEquals($contribution['contribution_source'], $old_source);
$this->assertEquals($contribution['contribution_status'], 'Completed');

$this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);

$params = array(
'contribution_id' => $contributionID,

Expand Down

0 comments on commit e15e409

Please sign in to comment.