From 122250ecb51d832a43c2896b633cdd36bc173307 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 1 Sep 2016 09:40:25 +1000 Subject: [PATCH] fix issue https://issues.civicrm.org/jira/browse/CRM-19149 --- CRM/Contribute/BAO/Contribution.php | 14 ++++++-- tests/phpunit/api/v3/ContributionTest.php | 41 ++++++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index a8223d8089..21a57f404d 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3471,12 +3471,22 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC', FALSE, NULL, $deferredFinancialAccount); if (!empty($lastFinancialTrxnId['financialTrxnId'])) { $params['trxnParams']['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $lastFinancialTrxnId['financialTrxnId'], 'to_financial_account_id'); - $params['trxnParams']['payment_instrument_id'] = $params['prevContribution']->payment_instrument_id; + if ($params['total_amount'] > 0) { + $params['trxnParams']['payment_instrument_id'] = $params['prevContribution']->payment_instrument_id; + } + else { + $params['trxnParams']['payment_instrument_id'] = $params['contribution']->payment_instrument_id; + } } } else { $params['trxnParams']['to_financial_account_id'] = $params['to_financial_account_id']; - $params['trxnParams']['payment_instrument_id'] = $params['contribution']->payment_instrument_id; + if ($params['total_amount'] < 0) { + $params['trxnParams']['payment_instrument_id'] = $params['prevContribution']->payment_instrument_id; + } + else { + $params['trxnParams']['payment_instrument_id'] = $params['contribution']->payment_instrument_id; + } } } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index cad444a6b9..546089a78e 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1058,6 +1058,31 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId); } + /** + * Function tests that financial records are updated when Payment Instrument is changed when amount is negative. + */ + public function testCreateUpdateNegativeContributionPaymentInstrument() { + $instrumentId = $this->_addPaymentInstrument(); + $contribParams = array( + 'contact_id' => $this->_individualId, + 'total_amount' => -100.00, + 'financial_type_id' => $this->_financialTypeId, + 'payment_instrument_id' => 4, + 'contribution_status_id' => 1, + + ); + $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); + + $newParams = array_merge($contribParams, array( + 'id' => $contribution['id'], + 'payment_instrument_id' => $instrumentId, + ) + ); + $contribution = $this->callAPISuccess('contribution', 'create', $newParams); + $this->assertAPISuccess($contribution); + $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00')); + } + /** * Function tests that financial records are added when Contribution is Refunded. */ @@ -2798,10 +2823,18 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $trxnParams1 = array( 'id' => $trxn['financial_trxn_id'], ); - $compareParams = array( - 'total_amount' => -100, - 'status_id' => 1, - ); + if (empty($extraParams)) { + $compareParams = array( + 'total_amount' => -100, + 'status_id' => 1, + ); + } + else { + $compareParams = array( + 'total_amount' => 100, + 'status_id' => 1, + ); + } if ($context == 'paymentInstrument') { $compareParams += array( 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4), -- 2.25.1