Skip to content

Commit

Permalink
Merge pull request #10130 from JMAConsulting/codeCleanup
Browse files Browse the repository at this point in the history
Used Api to get financial account for an entity defined in civicrm_en…
  • Loading branch information
eileenmcnaughton authored Apr 12, 2017
2 parents 60431ac + 876b8ab commit 7fa7be7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 81 deletions.
40 changes: 14 additions & 26 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'];
Expand All @@ -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'];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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'];
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 14 additions & 32 deletions CRM/Contribute/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}

/**
Expand Down
22 changes: 8 additions & 14 deletions CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 7fa7be7

Please sign in to comment.