/**
  * Function to build the form
  *
  * @return None
  * @access public
  */
 public function buildQuickForm($check = FALSE)
 {
     parent::buildQuickForm();
     if ($this->_action & CRM_Core_Action::DELETE) {
         return;
     }
     $this->add('text', 'label', ts('Label'), array('size' => 50, 'maxlength' => 255), TRUE);
     $this->add('text', 'price', ts('Price'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'price '), TRUE);
     $this->add('text', 'max_size', ts('Max Size'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'max_size '), TRUE);
     $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Booking_DAO_ResourceConfigOption', 'weight'), TRUE);
     $this->add('checkbox', 'is_active', ts('Enabled?'));
     $this->addRule("price", ts('Please enter a valid amount.'), 'money');
     $units = CRM_Booking_BAO_ResourceConfigOption::buildOptions('unit_id', 'create');
     $this->add('select', 'unit_id', ts('Unit'), array('' => ts('- select -')) + $units, TRUE, array());
     $this->addFormRule(array('CRM_Admin_Form_ResourceConfigOption', 'formRule'), $this);
     $cancelURL = CRM_Utils_System::url('civicrm/admin/resource/config_set/config_option', "&sid={$this->_sid}&reset=1");
     $cancelURL = str_replace('&', '&', $cancelURL);
     $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel'), 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"))));
 }
 /**
  * This function sets the default values for the form.
  *
  * @access public
  *
  * @return None
  */
 function setDefaultValues()
 {
     $defaults = array();
     if ($this->_id) {
         $result = civicrm_api3('Booking', 'get', array('id' => $this->_id));
         $booking = $result['values'][$result['id']];
         $subResources['sub_resources'] = array();
         $subResources['resources'] = $this->_resourcesPrice;
         $slots = civicrm_api3('Slot', 'get', array('booking_id' => $this->_id, 'is_deleted' => 0));
         $unitPriceList = CRM_Booking_BAO_ResourceConfigOption::buildOptions('unit_id', 'create');
         foreach ($slots['values'] as $key => $slot) {
             $subSlots = civicrm_api3('SubSlot', 'get', array('slot_id' => $slot['id'], 'is_deleted' => 0));
             foreach ($subSlots['values'] as $subSlot) {
                 $subResources['sub_resources'][$subSlot['id']] = array("parent_ref_id" => $slot['id'], "ref_id" => $subSlot['id'], "quantity" => CRM_Utils_Array::value('quantity', $subSlot), "time_required" => "2013-10-21 09:50", "time_required" => CRM_Utils_Array::value('time_required', $subSlot), "note" => CRM_Utils_Array::value('note', $subSlot));
                 $resourceResult = civicrm_api3('Resource', 'get', array('id' => $subSlot['resource_id']));
                 $resource = $resourceResult['values'][$subSlot['resource_id']];
                 $subResources['sub_resources'][$subSlot['id']]['resource'] = array("id" => $resource['id'], "label" => $resource['label']);
                 $configOptionResult = civicrm_api3('ResourceConfigOption', 'get', array('id' => $subSlot['config_id']));
                 $configOption = $configOptionResult['values'][$subSlot['config_id']];
                 $unit = $unitPriceList[$configOption['unit_id']];
                 $subResources['sub_resources'][$subSlot['id']]['configuration'] = array("id" => $configOption['id'], "label" => $configOption['label'] . ' - ' . $configOption['price'] . ' / ' . $unit, "price" => $configOption['price']);
                 $priceEstimate = $configOption['price'] * CRM_Utils_Array::value('quantity', $subSlot);
                 $subResources['sub_resources'][$subSlot['id']]['price_estimate'] = $priceEstimate;
                 $resourceTotalPrice = $subResources['resources'][$slot['id']] + $priceEstimate;
                 $subResources['resources'][$slot['id']] = $resourceTotalPrice;
             }
         }
         $subTotal = 0;
         foreach ($subResources['resources'] as $price) {
             $subTotal += $price;
         }
         $addhocCharges = array("items" => array(), "note" => CRM_Utils_Array::value('adhoc_charges_note', $booking), "total" => 0);
         $addhocChargesResult = civicrm_api3('AdhocCharges', 'get', array('booking_id' => $this->_id, 'is_deleted' => 0));
         foreach ($addhocChargesResult['values'] as $key => $charge) {
             $itemResult = civicrm_api3('AdhocChargesItem', 'get', array('id' => $charge['item_id'], 'is_deleted' => 0));
             if (empty($itemResult['values'])) {
                 //make sure we do not process deleted item
                 continue;
             }
             $item = $itemResult['values'][$charge['item_id']];
             $totalPrice = $item['price'] * $charge['quantity'];
             $addhocCharges['items'][$charge['item_id']] = array("item_id" => $charge['item_id'], "name" => $item['name'], "price" => $totalPrice, "quantity" => $charge['quantity'], "item_price" => $item['price']);
             $addhocCharges['total'] += $totalPrice;
         }
         $defaults['sub_total'] = $subTotal;
         $defaults['adhoc_charge'] = $addhocCharges['total'];
         $defaults['discount_amount'] = CRM_Utils_Array::value('discount_amount', $booking);
         $defaults['discount_amount_dummy'] = CRM_Utils_Array::value('discount_amount', $booking);
         $subResources['sub_total'] = $subTotal;
         $subResources['adhoc_charges'] = $addhocCharges;
         $total = $subTotal - $defaults['discount_amount'] + $addhocCharges['total'];
         $subResources['total_price'] = $total;
         // force JSON to encode empty array as object if there is empty array in $subResources
         $defaults['sub_resources'] = json_encode($subResources, JSON_FORCE_OBJECT);
         $defaults['total_price'] = $total;
     } else {
         $defaults['sub_total'] = $this->_subTotal;
         $defaults['adhoc_charge'] = 0;
         $defaults['discount_amount'] = 0;
         $defaults['total_price'] = $this->_total;
     }
     return $defaults;
 }