Cache payment instrument financial ids.
authoreileen <emcnaughton@wikimedia.org>
Wed, 5 Sep 2018 04:36:05 +0000 (16:36 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 20 Sep 2018 03:06:44 +0000 (15:06 +1200)
I don't really see any value in this calling a shared function as it fails to simplify

CRM/Financial/BAO/FinancialTypeAccount.php
tests/phpunit/CRM/Contribute/PseudoConstantTest.php

index 1eeee8990a78ce5f60ec9e11d4b978725b11c270..86391477e73c37dbc680d3c7030e6af4596d556b 100644 (file)
@@ -154,17 +154,27 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin
    * @return null|int
    */
   public static function getInstrumentFinancialAccount($paymentInstrumentValue) {
-    $paymentInstrument = civicrm_api3('OptionValue', 'getsingle', array(
-      'return' => array("id"),
-      'value' => $paymentInstrumentValue,
-      'option_group_id' => "payment_instrument",
-    ));
-    $financialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(
-      $paymentInstrument['id'],
-      NULL,
-      'civicrm_option_value'
-    );
-    return $financialAccountId;
+    if (!isset(\Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue])) {
+      $paymentInstrumentID = civicrm_api3('OptionValue', 'getvalue', array(
+        'return' => 'id',
+        'value' => $paymentInstrumentValue,
+        'option_group_id' => "payment_instrument",
+      ));
+      $accounts = civicrm_api3('EntityFinancialAccount', 'get', [
+        'return' => 'financial_account_id',
+        'entity_table' => 'civicrm_option_value',
+        'entity_id' => $paymentInstrumentID,
+        'options' => ['limit' => 1],
+        'sequential' => 1,
+      ])['values'];
+      if (empty($accounts)) {
+        \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue] = NULL;
+      }
+      else {
+        \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue] = $accounts[0]['financial_account_id'];
+      }
+    }
+    return \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue];
   }
 
   /**
index 4ce5fbde63a3d862e098b2a0ee5c7b770050cb4d..82716a45ee2a58f8f5deac6c5e6d892990a8f672 100644 (file)
@@ -66,9 +66,8 @@ class CRM_Contribute_PseudoConstantTest extends CiviUnitTestCase {
       );
       $this->assertEquals($accountIDFromBetterFunction, $accountID);
     }
-
   }
-  
+
   /**
    * Test that getRelationalFinancialAccount works and returns the same as the performant alternative.
    *