Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
seamuslee001 authored and wahyukodar committed Sep 7, 2016
1 parent 87881da commit 122250e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
14 changes: 12 additions & 2 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3471,12 +3471,22 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
$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;
}
}
}

Expand Down
41 changes: 37 additions & 4 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,31 @@ public function testCreateUpdateContributionPaymentInstrument() {
$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.
*/
Expand Down Expand Up @@ -2798,10 +2823,18 @@ public function _checkFinancialTrxn($contribution, $context, $instrumentId = NUL
$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),
Expand Down

0 comments on commit 122250e

Please sign in to comment.