From 5b22467ac4d706d4f80387e8501b9302e856c51f Mon Sep 17 00:00:00 2001 From: Rohan Katkar Date: Thu, 11 Dec 2014 18:10:08 +0530 Subject: [PATCH] CRM-15615 --- CRM/Event/BAO/Participant.php | 81 +++++++++++++--------- CRM/Event/Form/ParticipantFeeSelection.php | 9 ++- CRM/Price/BAO/LineItem.php | 3 + 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index b39f8030fe..a98580173e 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -1839,27 +1839,29 @@ WHERE cpf.price_set_id = %1 AND cpfv.label LIKE %2"; CRM_Price_BAO_LineItem::format($id, $params, $values, $submittedLineItems); $submittedFieldId[] = CRM_Utils_Array::retrieveValueRecursive($submittedLineItems, 'price_field_id'); } - $insertLines = $submittedLineItems; - - $submittedFieldValueIds = array_keys($submittedLineItems); - $updateLines = array(); - foreach ($previousLineItems as $id => $previousLineItem) { - // check through the submitted items if the previousItem exists, - // if found in submitted items, do not use it for new item creations - if (in_array($previousLineItem['price_field_value_id'], $submittedFieldValueIds)) { - // if submitted line items are existing don't fire INSERT query - unset($insertLines[$previousLineItem['price_field_value_id']]); - // for updating the line items i.e. use-case - once deselect-option selecting again - if ($previousLineItem['line_total'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['line_total']) { - $updateLines[$previousLineItem['price_field_value_id']] = $submittedLineItems[$previousLineItem['price_field_value_id']]; - $updateLines[$previousLineItem['price_field_value_id']]['id'] = $id; + if (!empty($submittedLineItems)) { + $insertLines = $submittedLineItems; + + $submittedFieldValueIds = array_keys($submittedLineItems); + $updateLines = array(); + foreach ($previousLineItems as $id => $previousLineItem) { + // check through the submitted items if the previousItem exists, + // if found in submitted items, do not use it for new item creations + if (in_array($previousLineItem['price_field_value_id'], $submittedFieldValueIds)) { + // if submitted line items are existing don't fire INSERT query + unset($insertLines[$previousLineItem['price_field_value_id']]); + // for updating the line items i.e. use-case - once deselect-option selecting again + if (($previousLineItem['line_total'] != $submittedLineItems[$previousLineItem['price_field_value_id']]['line_total']) || + ($submittedLineItems[$previousLineItem['price_field_value_id']]['line_total'] == 0 && $submittedLineItems[$previousLineItem['price_field_value_id']]['qty'] == 1)) { + $updateLines[$previousLineItem['price_field_value_id']] = $submittedLineItems[$previousLineItem['price_field_value_id']]; + $updateLines[$previousLineItem['price_field_value_id']]['id'] = $id; + } } } - } - - $submittedFields = implode(', ', $submittedFieldId); - $submittedFieldValues = implode(', ', $submittedFieldValueIds); + $submittedFields = implode(', ', $submittedFieldId); + $submittedFieldValues = implode(', ', $submittedFieldValueIds); + } if (!empty($submittedFields) && !empty($submittedFieldValues)) { $updateLineItem = "UPDATE civicrm_line_item li INNER JOIN civicrm_financial_item fi @@ -1925,7 +1927,16 @@ GROUP BY li.entity_table, li.entity_id, price_field_value_id } } } - + elseif (empty($submittedFields) && empty($submittedFieldValues)){ + $updateLineItem = "UPDATE civicrm_line_item li + INNER JOIN civicrm_financial_item fi + ON (li.id = fi.entity_id AND fi.entity_table = 'civicrm_line_item') + SET li.qty = 0, + li.line_total = 0.00, + li.tax_amount = NULL + WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantId})"; + CRM_Core_DAO::executeQuery($updateLineItem); + } if (!empty($updateLines)) { foreach ($updateLines as $valueId => $vals) { if (isset($vals['tax_amount'])) { @@ -1950,11 +1961,13 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI // insert new 'adjusted amount' transaction entry and update contribution entry. // ensure entity_financial_trxn table has a linking of it. // insert new line items - foreach ($insertLines as $valueId => $lineParams) { - $lineParams['entity_table'] = 'civicrm_participant'; - $lineParams['entity_id'] = $participantId; - $lineParams['contribution_id'] = $contributionId; - $lineObj = CRM_Price_BAO_LineItem::create($lineParams); + if (!empty($insertLines)) { + foreach ($insertLines as $valueId => $lineParams) { + $lineParams['entity_table'] = 'civicrm_participant'; + $lineParams['entity_id'] = $participantId; + $lineParams['contribution_id'] = $contributionId; + $lineObj = CRM_Price_BAO_LineItem::create($lineParams); + } } // the recordAdjustedAmt code would execute over here @@ -1980,15 +1993,17 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI $fetchCon = array('id' => $contributionId); $updatedContribution = CRM_Contribute_BAO_Contribution::retrieve($fetchCon, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); // insert financial items - foreach ($insertLines as $valueId => $lineParams) { - $lineParams['entity_table'] = 'civicrm_participant'; - $lineParams['entity_id'] = $participantId; - $lineObj = CRM_Price_BAO_LineItem::retrieve($lineParams, CRM_Core_DAO::$_nullArray); - // insert financial items - // ensure entity_financial_trxn table has a linking of it. - $prevItem = CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution); - if (isset($lineObj->tax_amount)) { - CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, TRUE); + if (!empty($insertLines)) { + foreach ($insertLines as $valueId => $lineParams) { + $lineParams['entity_table'] = 'civicrm_participant'; + $lineParams['entity_id'] = $participantId; + $lineObj = CRM_Price_BAO_LineItem::retrieve($lineParams, CRM_Core_DAO::$_nullArray); + // insert financial items + // ensure entity_financial_trxn table has a linking of it. + $prevItem = CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution); + if (isset($lineObj->tax_amount)) { + CRM_Financial_BAO_FinancialItem::add($lineObj, $updatedContribution, TRUE); + } } } diff --git a/CRM/Event/Form/ParticipantFeeSelection.php b/CRM/Event/Form/ParticipantFeeSelection.php index 577f6ec445..7ec58ad63f 100644 --- a/CRM/Event/Form/ParticipantFeeSelection.php +++ b/CRM/Event/Form/ParticipantFeeSelection.php @@ -129,10 +129,17 @@ class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form { $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId); $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId, FALSE); + $priceFieldId = (array_keys($this->_values['fee'])); if (!empty($priceSetValues)) { $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues); } - + else { + foreach($priceFieldId as $key => $value) { + if (!empty($value) && ($this->_values['fee'][$value]['html_type'] == 'Radio' || $this->_values['fee'][$value]['html_type'] == 'Select') && !$this->_values['fee'][$value]['is_required']) { + $defaults[$this->_participantId]['price_'.array_keys($this->_values['fee'])[$key]] = 0; + } + } + } $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId])); if ($this->_action == CRM_Core_Action::UPDATE) { $fee_level = $defaults[$this->_participantId]['fee_level']; diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index f399343cef..7133837d9b 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -533,6 +533,9 @@ AND li.entity_id = {$entityId} * @static */ public static function calculateTaxRate($lineItemId) { + if ($lineItemId['unit_price'] == 0) { + return; + } if ($lineItemId['html_type'] == 'Text') { $tax = $lineItemId['tax_amount']/($lineItemId['unit_price'] * $lineItemId['qty'])*100; } -- 2.25.1