$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;
+ }
}
}
$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.
*/
$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),