From 9c4722929a3afe970e4a00e7705fabbf2d703cf8 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Fri, 26 Aug 2016 02:34:20 +0530 Subject: [PATCH] CRM-16189, generalized code to create account receivable entries for completed contribution ---------------------------------------- * CRM-16189: https://issues.civicrm.org/jira/browse/CRM-16189 --- CRM/Contribute/BAO/Contribution.php | 64 ++++++++++++++--------------- CRM/Event/Form/Participant.php | 4 -- CRM/Financial/BAO/FinancialItem.php | 36 +++++++++------- CRM/Member/BAO/Membership.php | 4 -- 4 files changed, 53 insertions(+), 55 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2d45168e92..8cf69fa347 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -46,6 +46,13 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { */ static $_exportableFields = NULL; + /** + * Static field to hold financial trxn id's. + * + * @var array + */ + static $_trxnIDs = NULL; + /** * Field for all the objects related to this contribution * @var array of objects (e.g membership object, participant object) @@ -226,9 +233,6 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { //add Account details $params['contribution'] = $contribution; - if (empty($contributionID)) { - self::recordAlwaysAccountsReceivable($params); - } self::recordFinancialAccounts($params); if (self::isUpdateToRecurringContribution($params)) { @@ -3170,7 +3174,6 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (!isset($totalAmount) && !empty($params['prevContribution'])) { $totalAmount = $params['total_amount'] = $params['prevContribution']->total_amount; } - //build financial transaction params $trxnParams = array( 'contribution_id' => $params['contribution']->id, @@ -3363,8 +3366,9 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (!$update) { // records finanical trxn and entity financial trxn // also make it available as return value + self::recordAlwaysAccountsReceivable($trxnParams, $params); $return = $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); - $params['entity_id'] = $financialTxn->id; + $params['entity_id'] = self::$_trxnIDs[] = $financialTxn->id; } } // record line items and financial items @@ -3502,14 +3506,14 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac // & this can be removed return; } + self::recordAlwaysAccountsReceivable($params['trxnParams'], $params); $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); - $params['entity_id'] = $trxn->id; + $params['entity_id'] = self::$_trxnIDs[] = $trxn->id; $query = "UPDATE civicrm_financial_item SET status_id = %1 WHERE entity_id = %2 and entity_table = 'civicrm_line_item'"; $sql = "SELECT id, amount FROM civicrm_financial_item WHERE entity_id = %1 and entity_table = 'civicrm_line_item'"; $entityParams = array( 'entity_table' => 'civicrm_financial_item', - 'financial_trxn_id' => $trxn->id, ); foreach ($params['line_item'] as $fieldId => $fields) { foreach ($fields as $fieldValueId => $fieldValues) { @@ -3525,7 +3529,10 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac while ($financialItem->fetch()) { $entityParams['entity_id'] = $financialItem->id; $entityParams['amount'] = $financialItem->amount; - CRM_Financial_BAO_FinancialItem::createEntityTrxn($entityParams); + foreach (self::$_trxnIDs as $tID) { + $entityParams['financial_trxn_id'] = $tID; + CRM_Financial_BAO_FinancialItem::createEntityTrxn($entityParams); + } } } } @@ -5302,41 +5309,34 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co /** * Create Accounts Receivable financial trxn entry for Completed Contribution. * - * @param array $params - * - * @param string $context + * @param array $trxnParams + * Financial trxn params + * @param string $contributionParams + * Contribution Params * + * @return string */ - public static function recordAlwaysAccountsReceivable(&$params, $context = NULL) { + public static function recordAlwaysAccountsReceivable(&$trxnParams, $contributionParams) { if (!self::checkContributeSettings('always_post_to_accounts_receivable')) { return NULL; } - $statusId = $params['contribution']->contribution_status_id; + $statusId = $contributionParams['contribution']->contribution_status_id; $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $contributionStatus = empty($statusId) ? NULL : $contributionStatuses[$statusId]; // Return if contribution status is not completed. if ($contributionStatus != 'Completed') { return NULL; } - - if (empty($context)) { - $params['contribution_status_id'] = $params['contribution']->contribution_status_id = array_search('Pending', $contributionStatuses); - $params['is_pay_later'] = $params['contribution']->is_pay_later = TRUE; - - self::recordFinancialAccounts($params); - } - - $params['prevContribution'] = self::getOriginalContribution($params['contribution']->id); - $params['prevContribution']->contribution_status_id = array_search('Pending', $contributionStatuses); - $params['prevContribution']->is_pay_later = TRUE; - - $params['contribution_status_id'] = $params['contribution']->contribution_status_id = array_search('Completed', $contributionStatuses); - unset($params['is_pay_later']); - $params['contribution']->is_pay_later = NULL; - $params['id'] = $params['contribution']->id; - if (!empty($context)) { - self::recordFinancialAccounts($params); - } + $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); + $params['to_financial_account_id'] = $arAccountId; + $params['status_id'] = array_search('Pending', $contributionStatuses); + $params['is_payment'] = FALSE; + $trxn = CRM_Core_BAO_FinancialTrxn::create($params); + self::$_trxnIDs[] = $trxn->id; + $trxnParams['from_financial_account_id'] = $params['to_financial_account_id']; } } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 19e00c3f8e..06b04f8d65 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1454,10 +1454,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment CRM_Contribute_BAO_Contribution::addPayments($value, $contributions); } } - if (CRM_Utils_Array::value($num, $contributions)) { - $trxnParams = array('contribution' => $contributions[$num]); - CRM_Contribute_BAO_Contribution::recordAlwaysAccountsReceivable($trxnParams, 'Participant'); - } } } diff --git a/CRM/Financial/BAO/FinancialItem.php b/CRM/Financial/BAO/FinancialItem.php index 28e51b70d3..1da9d066c0 100644 --- a/CRM/Financial/BAO/FinancialItem.php +++ b/CRM/Financial/BAO/FinancialItem.php @@ -126,8 +126,11 @@ class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem { $params['financial_account_id'] = CRM_Utils_Array::value('financial_account_id', $result); } if (empty($trxnId)) { - $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); - $trxnId['id'] = $trxn['financialTrxnId']; + $trxnId['id'] = CRM_Contribute_BAO_Contribution::$_trxnIDs; + if (empty($trxnId['id'])) { + $trxn = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'ASC', TRUE); + $trxnId['id'] = $trxn['financialTrxnId']; + } } $financialItem = self::create($params, NULL, $trxnId); return $financialItem; @@ -161,20 +164,23 @@ class CRM_Financial_BAO_FinancialItem extends CRM_Financial_DAO_FinancialItem { } $financialItem->save(); - if (!empty($trxnIds['id'])) { - $entity_financial_trxn_params = array( - 'entity_table' => "civicrm_financial_item", - 'entity_id' => $financialItem->id, - 'financial_trxn_id' => $trxnIds['id'], - 'amount' => $params['amount'], - ); - - $entity_trxn = new CRM_Financial_DAO_EntityFinancialTrxn(); - $entity_trxn->copyValues($entity_financial_trxn_params); - if (!empty($ids['entityFinancialTrxnId'])) { - $entity_trxn->id = $ids['entityFinancialTrxnId']; + $financialtrxnIDS = CRM_Utils_Array::value('id', $trxnIds); + if (!empty($financialtrxnIDS)) { + if (!is_array($financialtrxnIDS)) { + $financialtrxnIDS = array($financialtrxnIDS); + } + foreach ($financialtrxnIDS as $tID) { + $entity_financial_trxn_params = array( + 'entity_table' => "civicrm_financial_item", + 'entity_id' => $financialItem->id, + 'financial_trxn_id' => $tID, + 'amount' => $params['amount'], + ); + if (!empty($ids['entityFinancialTrxnId'])) { + $entity_financial_trxn_params['id'] = $ids['entityFinancialTrxnId']; + } + self::createEntityTrxn($entity_financial_trxn_params); } - $entity_trxn->save(); } if (!empty($ids['id'])) { CRM_Utils_Hook::post('edit', 'FinancialItem', $financialItem->id, $financialItem); diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 3ed18e20aa..1805b76426 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -340,10 +340,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { if (!empty($params['line_item']) && empty($ids['contribution'])) { CRM_Price_BAO_LineItem::processPriceSet($membership->id, $params['line_item'], CRM_Utils_Array::value('contribution', $params)); - if (!empty($params['contribution'])) { - $trxnParams['contribution'] = $params['contribution']; - CRM_Contribute_BAO_Contribution::recordAlwaysAccountsReceivable($trxnParams, 'Membership'); - } } //insert payment record for this membership -- 2.25.1