financial#160 - set correct from_financial_account_id on an edited fee amount Financi...
authorJon Goldberg <jon@megaphonetech.com>
Wed, 9 Dec 2020 16:20:05 +0000 (11:20 -0500)
committerJon Goldberg <jon@megaphonetech.com>
Thu, 10 Dec 2020 16:21:03 +0000 (11:21 -0500)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/CRM/Core/BAO/FinancialTrxnTest.php

index b7bbf5f323715235ec3f5d1d65d12254eb2e71e6..2509468953b796f14b5fac08adab99321be339b2 100644 (file)
@@ -3473,6 +3473,10 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       elseif (!empty($params['payment_instrument_id'])) {
         $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($params['payment_instrument_id']);
       }
+      // dev/financial#160 - If this is a contribution update, also check for an existing payment_instrument_id.
+      elseif ($isUpdate && $params['prevContribution']->payment_instrument_id) {
+        $params['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount((int) $params['prevContribution']->payment_instrument_id);
+      }
       else {
         $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
         $queryParams = [1 => [$relationTypeId, 'Integer']];
index eb2d3b89e6fc17c3b50a83e12bfdb910aad756d5..3c414a6315b46635dbdac03f3aa87ebde6f7f70f 100644 (file)
@@ -249,4 +249,71 @@ class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
     $this->assertEquals(0.01, $amountOwed, 'Amount does not match');
   }
 
+  /**
+   * Test that financial trxns for fee amounts have the correct financial account.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testFeeAmountTrxns() {
+    $contactId = $this->individualCreate();
+
+    // Get the expected financial account of a payment made with a credit card.
+    // Also et the financial account of a payment with the default payment method.
+    // I wish we could join here - maybe when API4 EntityBridge is complete.
+    $creditCardOvId = $this->callAPISuccess('OptionValue', 'getvalue', [
+      'return' => "id",
+      'option_group_id' => "payment_instrument",
+      'name' => "Credit Card",
+    ]);
+    $defaultPaymentMethodOvId = $this->callAPISuccess('OptionValue', 'getvalue', [
+      'return' => "id",
+      'option_group_id' => "payment_instrument",
+      'is_default' => 1,
+    ]);
+    $expectedFinancialAccountId = $this->callAPISuccess('EntityFinancialAccount', 'getvalue', [
+      'return' => "financial_account_id",
+      'entity_id' => $creditCardOvId,
+    ]);
+    $wrongFinancialAccountId = $this->callAPISuccess('EntityFinancialAccount', 'getvalue', [
+      'return' => "financial_account_id",
+      'entity_id' => $defaultPaymentMethodOvId,
+    ]);
+    // If these two are the same, there's no bug but this test is no longer valid and needs rewriting.
+    $this->assertNotEquals($expectedFinancialAccountId, $wrongFinancialAccountId, 'invalid test: Financial Account ID of credit card matches default payment method Financial Account ID');
+
+    // Create a credit card contribution with a fee amount.
+    $price = 100;
+    $cParams = [
+      'contact_id' => $contactId,
+      'total_amount' => $price,
+      'financial_type_id' => 1,
+      'is_active' => 1,
+      'payment_instrument_id' => 'Credit Card',
+      'fee_amount' => 3,
+    ];
+    $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
+    // Confirm that the from_financial_account_id amount on the fee trxn matches the expected value.
+    $trxnParams = [
+      'sequential' => 1,
+      'return' => ["financial_trxn_id.from_financial_account_id"],
+      'entity_table' => "civicrm_contribution",
+      'entity_id' => $contribution['id'],
+      'financial_trxn_id.total_amount' => 3,
+    ];
+    $firstFeeTrxnFromAccountId = $this->callAPISuccess('EntityFinancialTrxn', 'get', $trxnParams)['values'][0]['financial_trxn_id.from_financial_account_id'];
+    $this->assertEquals($expectedFinancialAccountId, $firstFeeTrxnFromAccountId);
+
+    // dev/financial#160 - ensure the from_financial_account_id is correct on a trxn generated by a contribution edit.
+    $updatedContributionParams = [
+      'id' => $contribution['id'],
+      'fee_amount' => 5,
+    ];
+    $this->callAPISuccess('Contribution', 'create', $updatedContributionParams);
+
+    // 2 = 5 - 3.
+    $trxnParams['financial_trxn_id.total_amount'] = 2;
+    $secondFeeTrxnFromAccountId = $this->callAPISuccess('EntityFinancialTrxn', 'get', $trxnParams)['values'][0]['financial_trxn_id.from_financial_account_id'];
+    $this->assertEquals($expectedFinancialAccountId, $secondFeeTrxnFromAccountId);
+  }
+
 }