From: eileen Date: Wed, 5 Sep 2018 04:36:05 +0000 (+1200) Subject: Cache payment instrument financial ids. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=fe74d2b5692e02af31e3346a207c4c89f27acd5d;p=civicrm-core.git Cache payment instrument financial ids. I don't really see any value in this calling a shared function as it fails to simplify --- diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index 1eeee8990a..86391477e7 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -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]; } /** diff --git a/tests/phpunit/CRM/Contribute/PseudoConstantTest.php b/tests/phpunit/CRM/Contribute/PseudoConstantTest.php index 4ce5fbde63..82716a45ee 100644 --- a/tests/phpunit/CRM/Contribute/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Contribute/PseudoConstantTest.php @@ -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. *