--fixed for CRM-12649
authorPradeep Nayak <pradeep@pradeep.(none)>
Tue, 28 May 2013 16:28:55 +0000 (21:58 +0530)
committerPradeep Nayak <pradeep@pradeep.(none)>
Tue, 28 May 2013 16:28:55 +0000 (21:58 +0530)
----------------------------------------
* CRM-12649: Restore the Select Discount Set option for back-office event registration when event has Discounts by Signup Date
  http://issues.civicrm.org/jira/browse/CRM-12649

CRM/Event/Form/EventFees.php
CRM/Event/Form/Participant.php
CRM/Event/Form/Registration.php
templates/CRM/Event/Form/EventFees.tpl

index c950afc4c6c38c1119fc494d6e9cfbd3e94be5fe..1864f742ee01e8bb6cf96abfee9d27d4c0d291c4 100644 (file)
@@ -95,6 +95,7 @@ class CRM_Event_Form_EventFees {
         $discounts = array();
         if (!empty($form->_values['discount'])) {
           foreach ($form->_values['discount'] as $key => $value) {
+            $value = current($value);
             $discounts[$key] = $value['name'];
           }
         }
@@ -175,7 +176,38 @@ class CRM_Event_Form_EventFees {
       //             $defaults[$form->_pId]['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' );
     }
 
-    if ($priceSetId = CRM_Price_BAO_Set::getFor('civicrm_event', $form->_eventId)) {
+
+    // if user has selected discount use that to set default
+    if (isset($form->_discountId)) {
+      $defaults[$form->_pId]['discount_id'] = $form->_discountId;
+
+      //hack to set defaults for already selected discount value
+      if ($form->_action == CRM_Core_Action::UPDATE && !$form->_originalDiscountId) {
+        $form->_originalDiscountId = $defaults[$form->_pId]['discount_id'];
+        if ($form->_originalDiscountId) {
+          $defaults[$form->_pId]['discount_id'] = $form->_originalDiscountId;
+        }
+      }
+      $discountId = $form->_discountId;
+    }
+    else {
+      $discountId = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
+    }
+    
+    if ($discountId) {
+      $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
+    }
+    else {
+      $priceSetId = CRM_Price_BAO_Set::getFor('civicrm_event', $form->_eventId);
+    }
+
+    if (($form->_action == CRM_Core_Action::ADD) && $form->_eventId && $discountId) {
+      // this case is for add mode, where we show discount automatically
+        $defaults[$form->_pId]['discount_id'] = $discountId;
+    }
+
+
+    if ($priceSetId) {
       // get price set default values, CRM-4090
       if (in_array(get_class($form),
           array(
@@ -189,6 +221,7 @@ class CRM_Event_Form_EventFees {
           $defaults[$form->_pId] = array_merge($defaults[$form->_pId], $priceSetValues);
         }
       }
+              
       if ($form->_action == CRM_Core_Action::ADD && CRM_Utils_Array::value('fields', $form->_priceSet)) {
         foreach ($form->_priceSet['fields'] as $key => $val) {
           foreach ($val['options'] as $keys => $values) {
@@ -218,109 +251,6 @@ class CRM_Event_Form_EventFees {
         $form->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$form->_pId]));
       }
     }
-    else {
-      $optionGroupId = NULL;
-
-      // if user has selected discount use that to set default
-      if (isset($form->_discountId)) {
-        $defaults[$form->_pId]['discount_id'] = $form->_discountId;
-
-        //hack to set defaults for already selected discount value
-        if ($form->_action == CRM_Core_Action::UPDATE && !$form->_originalDiscountId) {
-          $form->_originalDiscountId = $defaults[$form->_pId]['discount_id'];
-          if ($form->_originalDiscountId) {
-            $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount',
-              $form->_originalDiscountId,
-              'option_group_id'
-            );
-            $defaults[$form->_pId]['discount_id'] = $form->_originalDiscountId;
-          }
-        }
-      }
-
-      if (($form->_action == CRM_Core_Action::ADD)) {
-        // this case is for add mode, where we show discount automatically
-        if (!isset($form->_discountId)) {
-          $discountId = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
-        }
-        else {
-          $discountId = $form->_discountId;
-        }
-
-        if ($form->_eventId && $discountId) {
-          $defaultDiscountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
-            $form->_eventId,
-            'default_discount_fee_id'
-          );
-          if ($defaultDiscountId) {
-            $discountKey = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
-              $defaultDiscountId,
-              'weight'
-            );
-          }
-
-          $defaults[$form->_pId]['discount_id'] = $discountId;
-
-          $defaults[$form->_pId]['amount'] = key(array_slice($form->_values['discount'][$discountId],
-              $discountKey - 1,
-              $discountKey,
-              TRUE
-            ));
-          $optionGroupId =
-            CRM_Core_DAO::getFieldValue(
-              'CRM_Core_DAO_Discount',
-              $discountId,
-              'option_group_id'
-            );
-        }
-        else {
-          if ($form->_eventId) {
-            $defaults[$form->_pId]['amount'] =
-              CRM_Core_DAO::getFieldValue(
-                'CRM_Event_DAO_Event',
-                $form->_eventId,
-                'default_fee_id'
-              );
-          }
-        }
-      }
-
-      if (CRM_Utils_Array::value('event_id', $defaults[$form->_pId])
-        && ($form->_action == CRM_Core_Action::UPDATE || $form->_allowConfirmation)
-      ) {
-        if (!empty($form->_feeBlock)) {
-          $feeLevel = CRM_Utils_Array::value('fee_level',
-            $defaults[$form->_pId]
-          );
-          $feeAmount = CRM_Utils_Array::value('fee_amount',
-            $defaults[$form->_pId]
-          );
-          foreach ($form->_feeBlock as $amountId => $amountInfo) {
-            if ($amountInfo['label'] == $feeLevel &&
-              $amountInfo['value'] == $feeAmount
-            ) {
-              $defaults[$form->_pId]['amount'] = $amountInfo['amount_id'];
-            }
-
-            // if amount is not set do fuzzy matching
-            if (!isset($defaults[$form->_pId]['amount'])) {
-              // if only level use that
-              if ($amountInfo['label'] == $feeLevel) {
-                $defaults[$form->_pId]['amount'] = $amountInfo['amount_id'];
-              }
-              elseif (strpos($feeLevel, $amountInfo['label']) !== FALSE) {
-                $defaults[$form->_pId]['amount'] = $amountInfo['amount_id'];
-              }
-              elseif ($amountInfo['value'] == $feeAmount) {
-                // if amount matches use that
-                $defaults[$form->_pId]['amount'] = $amountInfo['amount_id'];
-              }
-            }
-          }
-        }
-      }
-      $form->assign('amountId', CRM_Utils_Array::value('amount', $defaults[$form->_pId]));
-    }
 
     //CRM-4453
     if (CRM_Utils_Array::value('participant_fee_currency', $defaults[$form->_pId])) {
@@ -472,6 +402,7 @@ SELECT  id, html_type
       $discounts = array();
       if (!empty($form->_values['discount'])) {
         foreach ($form->_values['discount'] as $key => $value) {
+          $value = current($value);
           $discounts[$key] = $value['name'];
         }
 
index e69e7f5b4be4a59aa6860879803ce461c094d4a6..f79e29d43f7a1f57cfac84888cfe27f8f7a1d8da 100644 (file)
@@ -1080,22 +1080,9 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} );
 
         //lets carry currency, CRM-4453
         $params['fee_currency'] = $config->defaultCurrency;
-        // fix for CRM-3088
-        if ($discountId &&
-          !empty($this->_values['discount'][$discountId])
-        ) {
-          $params['amount_level'] = $this->_values['discount'][$discountId][$params['amount']]['label'];
-          $params['amount'] = $this->_values['discount'][$discountId][$params['amount']]['value'];
-        }
-        elseif (!isset($params['priceSetId']) && CRM_Utils_Array::value('amount', $params)) {
-          $params['amount_level'] = $this->_values['fee'][$params['amount']]['label'];
-          $params['amount'] = $this->_values['fee'][$params['amount']]['value'];
-        }
-        else {
-          CRM_Price_BAO_Set::processAmount($this->_values['fee'],
-            $params, $lineItem[0]
-          );
-        }
+        CRM_Price_BAO_Set::processAmount($this->_values['fee'],
+          $params, $lineItem[0]
+        );
         //CRM-11529 for quick config backoffice transactions
         //when financial_type_id is passed in form, update the
         //lineitems with the financial type selected in form
index 56a1e1acdd6dc6f07c156c61cbe546e0389f428a..1ee9d041f716cca9f9985804f0e835356e8d6710 100644 (file)
@@ -735,6 +735,9 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
 
     // retrive all active price set fields.
     $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event');
+    if (property_exists($form, '_discountId') && $form->_discountId) {
+      $discountId = $form->_discountId;
+    }
     if ($discountId) {
       $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
       $price = CRM_Price_BAO_Set::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId);
@@ -742,7 +745,24 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
     else {
       $price = CRM_Price_BAO_Set::initSet($form, $eventID, 'civicrm_event', TRUE);
     }
-
+    
+    if (property_exists($form, '_context') && ($form->_context == 'standalone' 
+      || $form->_context == 'participant')) {
+      $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event');
+      if (is_array( $discountedEvent)) {
+        foreach ($discountedEvent as $key => $priceSetId) {
+          $priceSet = CRM_Price_BAO_Set::getSetDetail($priceSetId);
+          $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
+          $form->_values['discount'][$key] = CRM_Utils_Array::value('fields', $priceSet);
+          $fieldID = key($form->_values['discount'][$key]);
+          $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
+            'CRM_Price_DAO_Set',
+            $priceSetId,
+            'title'
+          );
+        }
+      }
+    }
     $eventFee = CRM_Utils_Array::value('fee', $form->_values);
     if (!is_array($eventFee) || empty($eventFee)) {
       $form->_values['fee'] = array();
index 4e981e3bdcfd4ec006350683fe91673ffdf6fb73..0d6cf65f0d0236f7fe91acef449aadc0cda239fe 100644 (file)
 {if $paid} {* We retrieve this tpl when event is selected - keep it empty if event is not paid *}
     <table class="form-layout">
     {if $priceSet}
+      {if $discount and $hasPayment}
+        <tr class="crm-event-eventfees-form-block-discount"><td class="label">&nbsp;&nbsp;{ts}Discount Set{/ts}</td><td class="view-value">{$discount}</td></tr>
+      {elseif $form.discount_id.label}
+        <tr class="crm-event-eventfees-form-block-discount_id"><td class="label">&nbsp;&nbsp;{$form.discount_id.label}</td><td>{$form.discount_id.html}</td></tr>
+      {/if}
       {if $action eq 2 and $hasPayment} {* Updating *}
             {if $lineItem}
                 <tr class="crm-event-eventfees-form-block-line_items">