CRM-15615
authorRohan Katkar <rohan.katkar@webaccessglobal.com>
Thu, 11 Dec 2014 12:40:08 +0000 (18:10 +0530)
committerRohan Katkar <rohan.katkar@webaccessglobal.com>
Thu, 11 Dec 2014 12:40:08 +0000 (18:10 +0530)
CRM/Event/BAO/Participant.php
CRM/Event/Form/ParticipantFeeSelection.php
CRM/Price/BAO/LineItem.php

index b39f8030fee2a381ce504d815db939b848d21959..a98580173e1f19c2f6094fdda13f088a9d999d66 100644 (file)
@@ -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);
+        }
       }
     }
 
index 577f6ec4450df05dc0d296b7ba0ac7ae66aa98f4..7ec58ad63fbbe850787e279deac700a2b750749f 100644 (file)
@@ -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'];
index f399343cef1e4dd29981530dfafa2e2771edf8fe..7133837d9b688e69092d68b729c92506210aa68f 100644 (file)
@@ -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;
     }