fix issue https://issues.civicrm.org/jira/browse/CRM-19149
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 31 Aug 2016 23:40:25 +0000 (09:40 +1000)
committerWahyu Kodar <wahyukodar@gmail.com>
Wed, 7 Sep 2016 04:00:08 +0000 (11:00 +0700)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index a8223d8089fcbc4ae8b06f96b0402ebbf2e47f19..21a57f404d811e312ef915d345fce51cbe7d61aa 100644 (file)
@@ -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;
+        }
       }
     }
 
index cad444a6b96365998f22e3efe06d62777380a68f..546089a78e119002b6b363b707f2b7c4f783058e 100644 (file)
@@ -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),