From: Eileen McNaughton Date: Sat, 4 Sep 2021 21:42:44 +0000 (+1200) Subject: dev/core#2715 Move 2 more functions to financial processor class X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6a1dbda61c11a1dc7537880b02b7a28b5db10510;p=civicrm-core.git dev/core#2715 Move 2 more functions to financial processor class Note that we have a huge amount of test cover of these - the main risk of breakage is missing a 'self' in the calling class - but that should cause a hard fail. These are functions we have always felt safe changing without regards to extension usage as they are deep within the financials and we have been really clear that the only way to interact is to use the api functions --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 19b6a2e800..cc44a430a2 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1012,7 +1012,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { return FALSE; } - if (self::isContributionUpdateARefund($params['prevContribution']->contribution_status_id, $params['contribution']->contribution_status_id)) { + if (CRM_Contribute_BAO_FinancialProcessor::isContributionUpdateARefund($params['prevContribution']->contribution_status_id, $params['contribution']->contribution_status_id)) { // @todo we should stop passing $params by reference - splitting this out would be a step towards that. $params['trxnParams']['total_amount'] = -$params['total_amount']; } @@ -1118,21 +1118,6 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { CRM_Core_DAO::executeQuery($query, $fparams); } - /** - * Does this contributtion status update represent a refund. - * - * @param int $previousContributionStatusID - * @param int $currentContributionStatusID - * - * @return bool - */ - private static function isContributionUpdateARefund($previousContributionStatusID, $currentContributionStatusID): bool { - if ('Completed' !== CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $previousContributionStatusID)) { - return FALSE; - } - return self::isContributionStatusNegative($currentContributionStatusID); - } - /** * Get transaction information about the contribution. * @@ -3430,7 +3415,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $params['trxnParams']['to_financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialTrxn', $lastFinancialTrxnId['financialTrxnId'], 'to_financial_account_id'); } } - self::updateFinancialAccounts($params, 'changeFinancialType'); + CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params, 'changeFinancialType'); $params['skipLineItem'] = FALSE; foreach ($params['line_item'] as &$lineItems) { foreach ($lineItems as &$line) { @@ -3445,7 +3430,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac if (isset($params['fee_amount'])) { $params['trxnParams']['fee_amount'] = $params['fee_amount']; } - self::updateFinancialAccounts($params); + CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params); CRM_Core_BAO_FinancialTrxn::createDeferredTrxn(CRM_Utils_Array::value('line_item', $params), $params['contribution'], TRUE); $params['trxnParams']['to_financial_account_id'] = $trxnParams['to_financial_account_id']; $updated = TRUE; @@ -3467,7 +3452,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac //Update Financial Records $callUpdateFinancialAccounts = self::updateFinancialAccountsOnContributionStatusChange($params); if ($callUpdateFinancialAccounts) { - self::updateFinancialAccounts($params, 'changedStatus'); + CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params, 'changedStatus'); CRM_Core_BAO_FinancialTrxn::createDeferredTrxn(CRM_Utils_Array::value('line_item', $params), $params['contribution'], TRUE, 'changedStatus'); } $updated = TRUE; @@ -3496,7 +3481,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac ) { //Update Financial Records $params['trxnParams']['from_financial_account_id'] = NULL; - self::updateFinancialAccounts($params, 'changedAmount'); + CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params, 'changedAmount'); CRM_Core_BAO_FinancialTrxn::createDeferredTrxn(CRM_Utils_Array::value('line_item', $params), $params['contribution'], TRUE, 'changedAmount'); $updated = TRUE; } @@ -3567,39 +3552,6 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac return $return; } - /** - * Update all financial accounts entry. - * - * @param array $params - * Contribution object, line item array and params for trxn. - * - * @param string $context - * Update scenarios. - * - * @todo stop passing $params by reference. It is unclear the purpose of doing this & - * adds unpredictability. - * - */ - public static function updateFinancialAccounts(&$params, $context = NULL) { - $inputParams = $params; - $isARefund = self::isContributionUpdateARefund($params['prevContribution']->contribution_status_id, $params['contribution']->contribution_status_id); - - if ($context === 'changedAmount' || $context === 'changeFinancialType') { - // @todo we should stop passing $params by reference - splitting this out would be a step towards that. - $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = ($params['total_amount'] - $params['prevContribution']->total_amount); - } - - $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); - // @todo we should stop passing $params by reference - splitting this out would be a step towards that. - $params['entity_id'] = $trxn->id; - - $trxnIds['id'] = $params['entity_id']; - $previousLineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution']->id); - foreach ($params['line_item'] as $fieldId => $fields) { - $params = CRM_Contribute_BAO_FinancialProcessor::createFinancialItemsForLine($params, $context, $fields, $previousLineItems, $inputParams, $isARefund, $trxnIds, $fieldId); - } - } - /** * Is this contribution status a reversal. * diff --git a/CRM/Contribute/BAO/FinancialProcessor.php b/CRM/Contribute/BAO/FinancialProcessor.php index 35df6ba3cc..e9c28ef83e 100644 --- a/CRM/Contribute/BAO/FinancialProcessor.php +++ b/CRM/Contribute/BAO/FinancialProcessor.php @@ -193,4 +193,52 @@ class CRM_Contribute_BAO_FinancialProcessor { } } + /** + * Update all financial accounts entry. + * + * @param array $params + * Contribution object, line item array and params for trxn. + * + * @param string $context + * Update scenarios. + * + * @todo stop passing $params by reference. It is unclear the purpose of doing this & + * adds unpredictability. + * + */ + public static function updateFinancialAccounts(&$params, $context = NULL) { + $inputParams = $params; + $isARefund = self::isContributionUpdateARefund($params['prevContribution']->contribution_status_id, $params['contribution']->contribution_status_id); + + if ($context === 'changedAmount' || $context === 'changeFinancialType') { + // @todo we should stop passing $params by reference - splitting this out would be a step towards that. + $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = ($params['total_amount'] - $params['prevContribution']->total_amount); + } + + $trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']); + // @todo we should stop passing $params by reference - splitting this out would be a step towards that. + $params['entity_id'] = $trxn->id; + + $trxnIds['id'] = $params['entity_id']; + $previousLineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution']->id); + foreach ($params['line_item'] as $fieldId => $fields) { + $params = CRM_Contribute_BAO_FinancialProcessor::createFinancialItemsForLine($params, $context, $fields, $previousLineItems, $inputParams, $isARefund, $trxnIds, $fieldId); + } + } + + /** + * Does this contribution status update represent a refund. + * + * @param int $previousContributionStatusID + * @param int $currentContributionStatusID + * + * @return bool + */ + public static function isContributionUpdateARefund($previousContributionStatusID, $currentContributionStatusID): bool { + if ('Completed' !== CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $previousContributionStatusID)) { + return FALSE; + } + return CRM_Contribute_BAO_Contribution::isContributionStatusNegative($currentContributionStatusID); + } + }