From 592a64a007455cb2356f6582eeb6fb1bb9c54236 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 26 Oct 2019 12:44:48 +1300 Subject: [PATCH] [REF] extract chunk of code that creates tthe financial items for a given line. Private function since more refactoring steps can follow --- CRM/Contribute/BAO/Contribution.php | 117 +++++++++++++--------- tests/phpunit/api/v3/ContributionTest.php | 2 + 2 files changed, 69 insertions(+), 50 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index e68194d405..a3a2286c1d 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1237,6 +1237,72 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution { CRM_Core_DAO::executeQuery($query, $fparams); } + /** + * Create the financial items for the line. + * + * @param array $params + * @param string $context + * @param array $fields + * @param array $previousLineItems + * @param array $inputParams + * @param bool $isARefund + * @param array $trxnIds + * @param int $fieldId + * + * @return array + */ + private static function createFinancialItemsForLine($params, $context, $fields, array $previousLineItems, array $inputParams, bool $isARefund, $trxnIds, $fieldId): array { + foreach ($fields as $fieldValueId => $lineItemDetails) { + $prevFinancialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($lineItemDetails['id']); + $receiveDate = CRM_Utils_Date::isoToMysql($params['prevContribution']->receive_date); + if ($params['contribution']->receive_date) { + $receiveDate = CRM_Utils_Date::isoToMysql($params['contribution']->receive_date); + } + + $financialAccount = self::getFinancialAccountForStatusChangeTrxn($params, CRM_Utils_Array::value('financial_account_id', $prevFinancialItem)); + + $currency = $params['prevContribution']->currency; + if ($params['contribution']->currency) { + $currency = $params['contribution']->currency; + } + $previousLineItemTotal = CRM_Utils_Array::value('line_total', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); + $itemParams = [ + 'transaction_date' => $receiveDate, + 'contact_id' => $params['prevContribution']->contact_id, + 'currency' => $currency, + 'amount' => self::getFinancialItemAmountFromParams($inputParams, $context, $lineItemDetails, $isARefund, $previousLineItemTotal), + 'description' => CRM_Utils_Array::value('description', $prevFinancialItem), + 'status_id' => $prevFinancialItem['status_id'], + 'financial_account_id' => $financialAccount, + 'entity_table' => 'civicrm_line_item', + 'entity_id' => $lineItemDetails['id'], + ]; + $financialItem = CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds); + $params['line_item'][$fieldId][$fieldValueId]['deferred_line_total'] = $itemParams['amount']; + $params['line_item'][$fieldId][$fieldValueId]['financial_item_id'] = $financialItem->id; + + if (($lineItemDetails['tax_amount'] && $lineItemDetails['tax_amount'] !== 'null') || ($context == 'changeFinancialType')) { + $taxAmount = (float) $lineItemDetails['tax_amount']; + if ($context == 'changeFinancialType' && $lineItemDetails['tax_amount'] === 'null') { + // reverse the Sale Tax amount if there is no tax rate associated with new Financial Type + $taxAmount = CRM_Utils_Array::value('tax_amount', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); + } + elseif ($previousLineItemTotal != $lineItemDetails['line_total']) { + $taxAmount -= CRM_Utils_Array::value('tax_amount', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); + } + if ($taxAmount != 0) { + $itemParams['amount'] = self::getMultiplier($params['contribution']->contribution_status_id, $context) * $taxAmount; + $itemParams['description'] = CRM_Invoicing_Utils::getTaxTerm(); + if ($lineItemDetails['financial_type_id']) { + $itemParams['financial_account_id'] = CRM_Financial_BAO_FinancialAccount::getSalesTaxFinancialAccount($lineItemDetails['financial_type_id']); + } + CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds); + } + } + } + return $params; + } + /** * @inheritDoc */ @@ -3752,56 +3818,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac $trxnIds['id'] = $params['entity_id']; $previousLineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['contribution']->id); foreach ($params['line_item'] as $fieldId => $fields) { - foreach ($fields as $fieldValueId => $lineItemDetails) { - $prevFinancialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($lineItemDetails['id']); - $receiveDate = CRM_Utils_Date::isoToMysql($params['prevContribution']->receive_date); - if ($params['contribution']->receive_date) { - $receiveDate = CRM_Utils_Date::isoToMysql($params['contribution']->receive_date); - } - - $financialAccount = self::getFinancialAccountForStatusChangeTrxn($params, CRM_Utils_Array::value('financial_account_id', $prevFinancialItem)); - - $currency = $params['prevContribution']->currency; - // @todo we should stop passing $params by reference - splitting this out would be a step towards that. - if ($params['contribution']->currency) { - $currency = $params['contribution']->currency; - } - $previousLineItemTotal = CRM_Utils_Array::value('line_total', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); - $itemParams = [ - 'transaction_date' => $receiveDate, - 'contact_id' => $params['prevContribution']->contact_id, - 'currency' => $currency, - 'amount' => self::getFinancialItemAmountFromParams($inputParams, $context, $lineItemDetails, $isARefund, $previousLineItemTotal), - 'description' => CRM_Utils_Array::value('description', $prevFinancialItem), - 'status_id' => $prevFinancialItem['status_id'], - 'financial_account_id' => $financialAccount, - 'entity_table' => 'civicrm_line_item', - 'entity_id' => $lineItemDetails['id'], - ]; - $financialItem = CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds); - // @todo we should stop passing $params by reference - splitting this out would be a step towards that. - $params['line_item'][$fieldId][$fieldValueId]['deferred_line_total'] = $itemParams['amount']; - $params['line_item'][$fieldId][$fieldValueId]['financial_item_id'] = $financialItem->id; - - if (($lineItemDetails['tax_amount'] && $lineItemDetails['tax_amount'] !== 'null') || ($context == 'changeFinancialType')) { - $taxAmount = (float) $lineItemDetails['tax_amount']; - if ($context == 'changeFinancialType' && $lineItemDetails['tax_amount'] === 'null') { - // reverse the Sale Tax amount if there is no tax rate associated with new Financial Type - $taxAmount = CRM_Utils_Array::value('tax_amount', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); - } - elseif ($previousLineItemTotal != $lineItemDetails['line_total']) { - $taxAmount -= CRM_Utils_Array::value('tax_amount', CRM_Utils_Array::value($fieldValueId, $previousLineItems), 0); - } - if ($taxAmount != 0) { - $itemParams['amount'] = self::getMultiplier($params['contribution']->contribution_status_id, $context) * $taxAmount; - $itemParams['description'] = CRM_Invoicing_Utils::getTaxTerm(); - if ($lineItemDetails['financial_type_id']) { - $itemParams['financial_account_id'] = CRM_Financial_BAO_FinancialAccount::getSalesTaxFinancialAccount($lineItemDetails['financial_type_id']); - } - CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds); - } - } - } + $params = self::createFinancialItemsForLine($params, $context, $fields, $previousLineItems, $inputParams, $isARefund, $trxnIds, $fieldId); } if ($context == 'changeFinancialType') { diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 0a41129aa7..c6da990b3c 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1587,6 +1587,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase { /** * Function tests that financial records are added when Financial Type is Changed. + * + * @throws \CRM_Core_Exception */ public function testCreateUpdateContributionChangeFinancialType() { $contribParams = [ -- 2.25.1