From 99a4cd32b407c1c1e53dc85b76f387a337abafb5 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 6 Aug 2016 08:21:25 +1000 Subject: [PATCH] CRM-19126 CRM-19152 Attempt to resurect total amount from previous values rather than return params --- CRM/Contribute/BAO/Contribution.php | 33 ++++++++++++++++++----- tests/phpunit/api/v3/ContributionTest.php | 5 +++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 6a4f2221f8..6360c6a2f7 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4112,6 +4112,12 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) // Update contribution. if (!empty($params['id'])) { + + $id = $params['id']; + $values = $ids = array(); + $contrbutionParams = array('id' => $id); + $prevContributionValue = CRM_Contribute_BAO_Contribution::getValues($contrbutionParams, $values, $ids); + // CRM-19126 and CRM-19152, civicrm_line_item.tax_amount incorrectly set when using online payment processor. // It looks like this method is meant to be called in multiple contexts: new & update // When creating, would expect total_amount to be set? Prior to 4.7, when creating online membership @@ -4121,14 +4127,27 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) // Conceptually, if we're "updating", and total_amount is unknown. We're dealing with an incomplete // view, why are we nulling out all line item taxes, or messing with any other tax amounts? if (!isset($params['total_amount'])) { - return $params; + if (!empty($prevContributionValue->tax_amount)) { + $params['total_amount'] = $prevContributionValue->total_amount - $prevContributionValue->tax_amount; + if (isset($params['fee_amount'])) { + $params['net_amount'] = $params['total_amount'] - $params['fee_amount']; + } + else { + $params['net_amount'] = $params['total_amount'] - $prevContributionValue->fee_amount; + $params['fee_amount'] = $prevContributionValue->fee_amount; + } + } + else { + if (isset($params['fee_amount'])) { + $params['net_amount'] = $prevContributionValue->total_amount - $params['fee_amount']; + $params['total_amount'] = $prevContributionValue->total_amount; + } + else { + $params['total_amount'] = $prevContributionValue->total_amount; + $params['fee_amount'] = $prevContributionValue->fee_amount; + } + } } - - $id = $params['id']; - $values = $ids = array(); - $contrbutionParams = array('id' => $id); - $prevContributionValue = CRM_Contribute_BAO_Contribution::getValues($contrbutionParams, $values, $ids); - // To assign pervious finantial type on update of contribution if (!isset($params['financial_type_id'])) { $params['financial_type_id'] = $prevContributionValue->financial_type_id; diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index f03f459106..6b3d9be309 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1794,9 +1794,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->callAPISuccess('contribution', 'completetransaction', array( 'id' => $contribution['id'], 'trxn_id' => '777788888', + 'fee_amount' => '6.00', )); - $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1)); + $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1)); $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']); + $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']); + $this->assertEquals('94.00', $contribution2['values'][0]['net_amount']); } /** -- 2.25.1