diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 4add99086198..aee8f8beba12 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3115,16 +3115,14 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues = $partialAmtPay = CRM_Utils_Rule::cleanMoney($params['partial_amount_pay']); $partialAmtTotal = CRM_Utils_Rule::cleanMoney($params['partial_payment_total']); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $fromFinancialAccountId = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + $fromFinancialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], 'Accounts Receivable Account is'); $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); $params['total_amount'] = $partialAmtPay; $balanceTrxnInfo = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($params['contribution']->id, $params['financial_type_id']); if (empty($balanceTrxnInfo['trxn_id'])) { // create new balance transaction record - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], 'Accounts Receivable Account is'); $balanceTrxnParams['total_amount'] = $partialAmtTotal; $balanceTrxnParams['to_financial_account_id'] = $toFinancialAccount; @@ -3258,14 +3256,13 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues = if (!empty($params['revenue_recognition_date']) || $params['prevContribution']->revenue_recognition_date) { $accountRelationship = 'Deferred Revenue Account is'; } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '$accountRelationship' ")); - $oldFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId); - $newFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id'], $relationTypeId); + $oldFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['prevContribution']->financial_type_id, $accountRelationship); + $newFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], $accountRelationship); if ($oldFinancialAccount != $newFinancialAccount) { $params['total_amount'] = 0; if (in_array($params['contribution']->contribution_status_id, $pendingStatus)) { - $params['trxnParams']['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( - $params['prevContribution']->financial_type_id, $relationTypeId); + $params['trxnParams']['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount( + $params['prevContribution']->financial_type_id, $accountRelationship); } else { $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC'); @@ -3477,8 +3474,7 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT && $params['prevContribution']->is_pay_later) || $previousContributionStatus == 'In Progress' ) { $financialTypeID = CRM_Utils_Array::value('financial_type_id', $params) ? $params['financial_type_id'] : $params['prevContribution']->financial_type_id; - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $arAccountId = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeID, $relationTypeId); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeID, 'Accounts Receivable Account is'); if ($params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) { $params['trxnParams']['to_financial_account_id'] = $arAccountId; @@ -3498,8 +3494,7 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT $params['trxnParams']['net_amount'] = $params['trxnParams']['total_amount']; $deferredFinancialAccount = CRM_Utils_Array::value('deferred_financial_account_id', $params); if (empty($deferredFinancialAccount)) { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' ")); - $deferredFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($params['prevContribution']->financial_type_id, $relationTypeId); + $deferredFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['prevContribution']->financial_type_id, 'Deferred Revenue Account is'); } $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['prevContribution']->id, 'DESC', FALSE, NULL, $deferredFinancialAccount); if (!empty($lastFinancialTrxnId['financialTrxnId'])) { @@ -3766,8 +3761,7 @@ public static function buildOptions($fieldName, $context = NULL, $props = array( * @return array|bool */ public static function validateFinancialType($financialTypeId, $relationName = 'Expense Account is') { - $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '{$relationName}' ")); - $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); + $financialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, $relationName); if (!$financialAccount) { return CRM_Contribute_PseudoConstant::financialType($financialTypeId); @@ -3804,8 +3798,7 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay $params = array_merge($defaults, $params); $params['skipLineItem'] = TRUE; $trxnsData['trxn_date'] = !empty($trxnsData['trxn_date']) ? $trxnsData['trxn_date'] : date('YmdHis'); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $arAccountId = CRM_Contribute_PseudoConstant::financialAccountType($contributionDAO->financial_type_id, $relationTypeId); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionDAO->financial_type_id, 'Accounts Receivable Account is'); if ($paymentType == 'owed') { $params['partial_payment_total'] = $contributionDAO->total_amount; $params['partial_amount_pay'] = $trxnsData['total_amount']; @@ -3814,7 +3807,6 @@ public static function recordAdditionalPayment($contributionId, $trxnsData, $pay // record the entry $financialTrxn = CRM_Contribute_BAO_Contribution::recordFinancialAccounts($params, $trxnsData); $toFinancialAccount = $arAccountId; - $trxnId = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId, $contributionDAO->financial_type_id); if (!empty($trxnId)) { $trxnId = $trxnId['trxn_id']; @@ -4054,9 +4046,8 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total); $contributionIsPayLater = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'is_pay_later'); - $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); - $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); + $feeFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is'); if ($paymentBalance == 0 && $contributionIsPayLater) { $paymentBalance = $total; @@ -4862,8 +4853,7 @@ public static function recordPartialPayment($contribution, $params) { ); $statusId = array_search('Completed', $contributionStatuses); if (in_array(CRM_Utils_Array::value('contribution_status_id', $contribution), $pendingStatus)) { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $balanceTrxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId); + $balanceTrxnParams['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'); } elseif (!empty($params['payment_processor'])) { $balanceTrxnParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($contribution['payment_processor'], 'civicrm_payment_processor', 'financial_account_id'); @@ -4876,8 +4866,7 @@ public static function recordPartialPayment($contribution, $params) { $queryParams = array(1 => array($relationTypeId, 'Integer')); $balanceTrxnParams['to_financial_account_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1", $queryParams); } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $fromFinancialAccountId = CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId); + $fromFinancialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'); $balanceTrxnParams['from_financial_account_id'] = $fromFinancialAccountId; $balanceTrxnParams['total_amount'] = $params['total_amount']; $balanceTrxnParams['contribution_id'] = $params['contribution_id']; @@ -5340,8 +5329,7 @@ public static function recordAlwaysAccountsReceivable(&$trxnParams, $contributio $params = $trxnParams; $financialTypeID = CRM_Utils_Array::value('financial_type_id', $contributionParams) ? $contributionParams['financial_type_id'] : $contributionParams['prevContribution']->financial_type_id; - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $arAccountId = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeID, $relationTypeId); + $arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeID, 'Accounts Receivable Account is'); $params['to_financial_account_id'] = $arAccountId; $params['status_id'] = array_search('Pending', $contributionStatuses); $params['is_payment'] = FALSE; diff --git a/CRM/Contribute/PseudoConstant.php b/CRM/Contribute/PseudoConstant.php index e1c2f121b4dd..522e0cb32869 100644 --- a/CRM/Contribute/PseudoConstant.php +++ b/CRM/Contribute/PseudoConstant.php @@ -44,13 +44,6 @@ class CRM_Contribute_PseudoConstant extends CRM_Core_PseudoConstant { */ private static $financialType; - /** - * Financial types - * @var array - */ - private static $financialTypeAccount; - - /** * Financial types * @var array @@ -379,36 +372,25 @@ public static function &pcpStatus($column = 'label') { } /** - * Get all financial accounts for a Financial type. + * Get financial account for a Financial type. * - * The static array $financialTypeAccount is returned * + * @param int $entityId + * @param string $accountRelationType * - * @param int $financialTypeId - * @param int $relationTypeId - * @return array - * array reference of all financial accounts for a Financial type + * @return int */ - public static function financialAccountType($financialTypeId, $relationTypeId = NULL) { - if (!CRM_Utils_Array::value($financialTypeId, self::$financialTypeAccount)) { - $condition = " entity_id = $financialTypeId "; - CRM_Core_PseudoConstant::populate( - self::$financialTypeAccount[$financialTypeId], - 'CRM_Financial_DAO_EntityFinancialAccount', - $all = TRUE, - $retrieve = 'financial_account_id', - $filter = NULL, - $condition, - NULL, - 'account_relationship' - ); + public static function getRelationalFinancialAccount($entityId, $accountRelationType) { + $result = civicrm_api3('EntityFinancialAccount', 'get', array( + 'return' => array("financial_account_id"), + 'account_relationship.name' => $accountRelationType, + 'entity_table' => 'civicrm_financial_type', + 'entity_id' => $entityId, + )); + if (!$result['count']) { + return NULL; } - - if ($relationTypeId) { - return CRM_Utils_Array::value($relationTypeId, self::$financialTypeAccount[$financialTypeId]); - } - - return self::$financialTypeAccount[$financialTypeId]; + return $result['values'][$result['id']]['financial_account_id']; } /** diff --git a/CRM/Core/BAO/FinancialTrxn.php b/CRM/Core/BAO/FinancialTrxn.php index c1a91e7fa830..1d22a6773214 100644 --- a/CRM/Core/BAO/FinancialTrxn.php +++ b/CRM/Core/BAO/FinancialTrxn.php @@ -97,8 +97,7 @@ public static function getBalanceTrxnAmt($contributionId, $contributionFinancial if (!$contributionFinancialTypeId) { $contributionFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id'); } - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contributionFinancialTypeId, 'Accounts Receivable Account is'); $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') WHERE eft.entity_id = %1 AND ft.to_financial_account_id = %2"; $p[1] = array($contributionId, 'Integer'); @@ -357,14 +356,12 @@ public static function createPremiumTrxn($params) { if (!empty($params['cost'])) { $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); - $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']); - $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name IN ('Premiums Inventory Account is', 'Cost of Sales Account is')"); - $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; - $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is' : 'Premiums Inventory Account is'; + $toFinancialAccountType = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; + $fromFinancialAccountType = !empty($params['isDeleted']) ? 'Cost of Sales Account is' : 'Premiums Inventory Account is'; $accountRelationship = array_flip($accountRelationship); $financialtrxn = array( - 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]], - 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]], + 'to_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], $toFinancialAccountType), + 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($params['financial_type_id'], $fromFinancialAccountType), 'trxn_date' => date('YmdHis'), 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0, 'currency' => CRM_Utils_Array::value('currency', $params), @@ -401,7 +398,6 @@ public static function createPremiumTrxn($params) { * @return bool */ public static function recordFees($params) { - $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); $domainId = CRM_Core_Config::domainID(); $amount = 0; if (!empty($params['prevContribution'])) { @@ -418,7 +414,7 @@ public static function recordFees($params) { else { $financialTypeId = $params['financial_type_id']; } - $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); + $financialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is'); $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id']; $params['trxnParams']['to_financial_account_id'] = $financialAccount; @@ -479,10 +475,8 @@ public static function getPartialPaymentWithType($entityId, $entityName = 'parti $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); - $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); - $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Accounts Receivable Account is'); + $feeFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeId, 'Expense Account is'); if (empty($lineItemTotal)) { $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId); diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index 54ca31c2f4f8..93b0d41c04f0 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -1839,9 +1839,8 @@ public static function createDiscountTrxn($eventID, $contributionParams, $feeLev $checkDiscount = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); if (!empty($checkDiscount)) { $mainAmount = self::getUnDiscountedAmountForEventPriceSetFieldValue($eventID, $discountedPriceFieldOptionID, $feeLevel); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Discounts Account is' ")); - $transactionParams['from_financial_account_id'] = CRM_Contribute_PseudoConstant::financialAccountType( - $financialTypeID, $relationTypeId); + $transactionParams['from_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount( + $financialTypeID, 'Discounts Account is'); if (!empty($transactionParams['trxnParams']['from_financial_account_id'])) { $transactionParams['trxnParams']['total_amount'] = $mainAmount - $total_amount; $transactionParams['trxnParams']['payment_processor_id'] = NULL; @@ -2173,8 +2172,7 @@ public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contribut CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray ); - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($updatedContribution->financial_type_id, 'Accounts Receivable Account is'); $adjustedTrxnValues = array( 'from_financial_account_id' => NULL, 'to_financial_account_id' => $toFinancialAccount, diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 694a2c357a31..6307c3de42eb 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -593,8 +593,7 @@ public function testAddPayments() { * checks db values for financial item */ public function checkItemValues($contribution) { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); - $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType(4, $relationTypeId); + $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is'); $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item') WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 83d271a7554d..4f6ac3f9ed08 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -3118,10 +3118,9 @@ public function _checkFinancialTrxn($contribution, $context, $instrumentId = NUL 'id' => $trxn['financial_trxn_id'], ); if ($context == 'payLater') { - $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $compareParams = array( 'status_id' => 1, - 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId), + 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'), ); } elseif ($context == 'refund') {