Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFC code tidy up preparatory to CRM-19273 #11272

Merged
merged 2 commits into from
Nov 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions CRM/Price/BAO/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,20 +639,19 @@ public static function changeFeeSelections(
$lineItemObj = new CRM_Price_BAO_LineItem();

// fetch submitted LineItems from input params and feeBlock information
$submittedLineItems = $lineItemObj->_getSubmittedLineItems($params, $feeBlock);
$submittedLineItems = $lineItemObj->getSubmittedLineItems($params, $feeBlock);

// retrieve the submitted price field value IDs from $submittedLineItems array keys
$submittedPriceFieldValueIDs = empty($submittedLineItems) ? array() : array_keys($submittedLineItems);

// get lineItems need to be updated and added to record changed fee
list($lineItemsToAdd, $lineItemsToUpdate) = $lineItemObj->_getLineItemsToAddAndUpdate($submittedLineItems, $entityID, $entity);
$requiredChanges = $lineItemObj->getLineItemsToAlter($submittedLineItems, $entityID, $entity);

// cancel previous line item
$additionalWhereClause = empty($submittedPriceFieldValueIDs) ? NULL : sprintf("price_field_value_id NOT IN (%s)", implode(', ', $submittedPriceFieldValueIDs));
$lineItemObj->_cancelLineItems($entityID, $entityTable, $additionalWhereClause);
$lineItemObj->cancelLineItems($entityID, $entityTable, $additionalWhereClause);

// get financial information that need to be recorded on basis on submitted price field value IDs
$financialItemsArray = $lineItemObj->_getFinancialItemsToRecord(
$financialItemsArray = $lineItemObj->getReverseFinancialItemsToRecord(
$entityID,
$entityTable,
$contributionId,
Expand All @@ -662,8 +661,8 @@ public static function changeFeeSelections(
// update line item with changed line total and other information
$totalParticipant = $participantCount = 0;
$amountLevel = array();
if (!empty($lineItemsToUpdate)) {
foreach ($lineItemsToUpdate as $priceFieldValueID => $value) {
if (!empty($requiredChanges['line_items_to_update'])) {
foreach ($requiredChanges['line_items_to_update'] as $priceFieldValueID => $value) {
$taxAmount = "NULL";
if (isset($value['tax_amount'])) {
$taxAmount = $value['tax_amount'];
Expand Down Expand Up @@ -691,7 +690,7 @@ public static function changeFeeSelections(
// insert new 'adjusted amount' transaction entry and update contribution entry.
// ensure entity_financial_trxn table has a linking of it.
// insert new line items
$lineItemObj->_addLineItemOnChangeFeeSelection($lineItemsToAdd, $entityID, $entityTable, $contributionId);
$lineItemObj->addLineItemOnChangeFeeSelection($requiredChanges['line_items_to_add'], $entityID, $entityTable, $contributionId);

// the recordAdjustedAmt code would execute over here
$count = 0;
Expand Down Expand Up @@ -721,7 +720,7 @@ public static function changeFeeSelections(
if (!empty($amountLevel)) {
$updateAmountLevel = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR;
}
$trxn = $lineItemObj->_recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel);
$trxn = $lineItemObj->recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount, $updateAmountLevel);

$contributionCompletedStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Completed');
if (!empty($financialItemsArray)) {
Expand Down Expand Up @@ -761,10 +760,10 @@ public static function changeFeeSelections(
if (!empty($trxn->id)) {
$trxnId['id'] = $trxn->id;
}
$lineItemObj->_addLineItemOnChangeFeeSelection($lineItemsToAdd, $entityID, $entityTable, $contributionId, $trxnId, TRUE);
$lineItemObj->addLineItemOnChangeFeeSelection($requiredChanges['line_items_to_add'], $entityID, $entityTable, $contributionId, $trxnId, TRUE);

// update participant fee_amount column
$lineItemObj->_updateEntityRecordOnChangeFeeSelection($params, $entityID, $entity);
$lineItemObj->updateEntityRecordOnChangeFeeSelection($params, $entityID, $entity);
}

/**
Expand All @@ -776,7 +775,7 @@ public static function changeFeeSelections(
* @param string $additionalWhereClause
*
*/
protected function _cancelLineItems($entityID, $entityTable, $additionalWhereClause = NULL) {
protected function cancelLineItems($entityID, $entityTable, $additionalWhereClause = NULL) {
$whereClauses = array(
"li.entity_id = %1",
"li.entity_table = %2",
Expand Down Expand Up @@ -813,7 +812,7 @@ protected function _cancelLineItems($entityID, $entityTable, $additionalWhereCla
* @return array
* List of formatted Financial Items to be recorded
*/
protected function _getFinancialItemsToRecord($entityID, $entityTable, $contributionID, $submittedPriceFieldValueIDs) {
protected function getReverseFinancialItemsToRecord($entityID, $entityTable, $contributionID, $submittedPriceFieldValueIDs) {
$previousLineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, str_replace('civicrm_', '', $entityTable));

$financialItemsArray = array();
Expand All @@ -830,12 +829,11 @@ protected function _getFinancialItemsToRecord($entityID, $entityTable, $contribu
GROUP BY li.entity_table, li.entity_id, price_field_value_id, fi.id
";
$updateFinancialItemInfoDAO = CRM_Core_DAO::executeQuery($updateFinancialItem);
$financialItemResult = $updateFinancialItemInfoDAO->fetchAll();

$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
$updateFinancialItemInfoValues = array();
while ($updateFinancialItemInfoDAO->fetch()) {
$updateFinancialItemInfoValues = (array) $updateFinancialItemInfoDAO;
foreach ($financialItemResult as $updateFinancialItemInfoValues) {
$updateFinancialItemInfoValues['transaction_date'] = date('YmdHis');
// the below params are not needed
$previousFinancialItemID = $updateFinancialItemInfoValues['id'];
Expand All @@ -846,7 +844,7 @@ protected function _getFinancialItemsToRecord($entityID, $entityTable, $contribu
// INSERT negative financial_items
$updateFinancialItemInfoValues['amount'] = -$updateFinancialItemInfoValues['amount'];
// reverse the related financial trxn too
$updateFinancialItemInfoValues['financialTrxn'] = $this->_getRelatedCancelFinancialTrxn($previousFinancialItemID);
$updateFinancialItemInfoValues['financialTrxn'] = $this->getRelatedCancelFinancialTrxn($previousFinancialItemID);
if ($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']) {
$updateFinancialItemInfoValues['tax']['amount'] = -($previousLineItems[$updateFinancialItemInfoValues['entity_id']]['tax_amount']);
$updateFinancialItemInfoValues['tax']['description'] = $taxTerm;
Expand Down Expand Up @@ -886,7 +884,7 @@ protected function _getFinancialItemsToRecord($entityID, $entityTable, $contribu
* @return array
* List of submitted line items
*/
protected function _getSubmittedLineItems($inputParams, $feeBlock) {
protected function getSubmittedLineItems($inputParams, $feeBlock) {
$submittedLineItems = array();
foreach ($feeBlock as $id => $values) {
CRM_Price_BAO_LineItem::format($id, $inputParams, $values, $submittedLineItems);
Expand All @@ -896,16 +894,16 @@ protected function _getSubmittedLineItems($inputParams, $feeBlock) {
}

/**
* Helper function to retrieve formatted lineitems need to be added and/or updated
* Helper function to retrieve formatted line items that need to be altered.
*
* @param array $submittedLineItems
* @param int $entityID
* @param string $entity
*
* @return array
* Array of formatted lineitems
* Array of formatted line items
*/
protected function _getLineItemsToAddAndUpdate($submittedLineItems, $entityID, $entity) {
protected function getLineItemsToAlter($submittedLineItems, $entityID, $entity) {
$previousLineItems = CRM_Price_BAO_LineItem::getLineItems($entityID, $entity);

$lineItemsToAdd = $submittedLineItems;
Expand Down Expand Up @@ -934,7 +932,10 @@ protected function _getLineItemsToAddAndUpdate($submittedLineItems, $entityID, $
}
}

return array($lineItemsToAdd, $lineItemsToUpdate);
return array(
'line_items_to_add' => $lineItemsToAdd,
'line_items_to_update' => $lineItemsToUpdate,
);
}

/**
Expand All @@ -949,7 +950,7 @@ protected function _getLineItemsToAddAndUpdate($submittedLineItems, $entityID, $
*
* @return void
*/
protected function _addLineItemOnChangeFeeSelection(
protected function addLineItemOnChangeFeeSelection(
$lineItemsToAdd,
$entityID,
$entityTable,
Expand Down Expand Up @@ -1030,7 +1031,7 @@ protected function _addLineItemOnChangeFeeSelection(
* @param string $entity
*
*/
protected function _updateEntityRecordOnChangeFeeSelection($inputParams, $entityID, $entity) {
protected function updateEntityRecordOnChangeFeeSelection($inputParams, $entityID, $entity) {
$entityTable = "civicrm_{$entity}";

if ($entity == 'participant') {
Expand Down Expand Up @@ -1062,7 +1063,7 @@ protected function _updateEntityRecordOnChangeFeeSelection($inputParams, $entity
* @return array $financialTrxn
*
*/
protected function _getRelatedCancelFinancialTrxn($financialItemID) {
protected function getRelatedCancelFinancialTrxn($financialItemID) {
$financialTrxn = civicrm_api3('EntityFinancialTrxn', 'getsingle', array(
'entity_table' => 'civicrm_financial_item',
'entity_id' => $financialItemID,
Expand Down Expand Up @@ -1099,7 +1100,7 @@ protected function _getRelatedCancelFinancialTrxn($financialItemID) {
*
* @return bool|\CRM_Core_BAO_FinancialTrxn
*/
protected function _recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) {
protected function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) {
$pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId);
$pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0);
$balanceAmt = $updatedAmount - $paidAmount;
Expand Down