From 667f909124f110c8530bb9cfb5213e6cb0022954 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 20 Nov 2017 10:43:03 +1300 Subject: [PATCH] CRM-19273 extract code to get financial items and filter those already cancelled. This is the first of the broken-out changes in https://github.com/civicrm/civicrm-core/pull/10962 that results in an actual functionality change. The code that retrieves the relevant financial items is separated into it's own function. But then a php loop iterates through and eliminates any items that have already been reversed. This is a scenario identified in the unit tests on 10962 and occurs when the the option value has is changed and then changed again - the items from the first change should not be re-changed. --- CRM/Price/BAO/LineItem.php | 50 +++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index 8926a5d1fd..e1da094a6d 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -821,15 +821,7 @@ WHERE li.contribution_id = %1"; return $financialItemsArray; } - // gathering necessary info to record negative (deselected) financial_item - $updateFinancialItem = " - SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount - FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') - WHERE (li.entity_table = '{$entityTable}' AND li.entity_id = {$entityID}) - GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id - "; - $updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem); - $financialItemResult = $updateFinancialItemInfoDAO->fetchAll(); + $financialItemResult = $this->getNonCancelledFinancialItems($entityID, $entityTable); $invoiceSettings = Civi::settings()->get('contribution_invoice_settings'); $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings); @@ -1218,4 +1210,44 @@ WHERE li.contribution_id = %1"; return $changedFinancialTypeID; } + /** + * Get Financial items, culling out any that have already been reversed. + * + * @param int $entityID + * @param string $entityTable + * + * @return array + * Array of financial items that have not be reversed. + */ + protected function getNonCancelledFinancialItems($entityID, $entityTable) { + $updateFinancialItem = " + SELECT fi.*, SUM(fi.amount) as differenceAmt, price_field_value_id, financial_type_id, tax_amount + FROM civicrm_financial_item fi LEFT JOIN civicrm_line_item li ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') + WHERE (li.entity_table = '{$entityTable}' AND li.entity_id = {$entityID}) + GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id + "; + $updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem); + + $financialItemResult = $updateFinancialItemInfoDAO->fetchAll(); + $items = array(); + foreach ($financialItemResult as $index => $financialItem) { + $items[$financialItem['price_field_value_id']][$index] = $financialItem['amount']; + + if (!empty($items[$financialItem['price_field_value_id']])) { + foreach ($items[$financialItem['price_field_value_id']] as $existingItemID => $existingAmount) { + if ($financialItem['amount'] + $existingAmount == 0) { + // Filter both rows as they cancel each other out. + unset($financialItemResult[$index]); + unset($financialItemResult[$existingItemID]); + unset($items['price_field_value_id'][$existingItemID]); + unset($items[$financialItem['price_field_value_id']][$index]); + } + } + + } + + } + return $financialItemResult; + } + } -- 2.25.1