/** * Function to process the form * * @access public * * @return None */ public function postProcess() { CRM_Utils_System::flushCache(); $params = $this->exportValues(); if ($this->_action & CRM_Core_Action::DELETE) { CRM_Booking_BAO_ResourceConfigOption::del($this->_id); CRM_Core_Session::setStatus(ts('Selected resource configuration option has been deleted.'), ts('Record Deleted'), 'success'); } else { $params = $this->exportValues(); $params['set_id'] = $this->_sid; // If the is_active (enabled) checkbox is NOT set, it is NOT sent down in the form // The DAO definition for is_active has a default of '1' // So if not set it is by default ENABLED when in fact it should be DISABLED if (!isset($params['is_active'])) { $params['is_active'] = 0; } if ($this->_id) { $params['id'] = $this->_id; } $resource = CRM_Booking_BAO_ResourceConfigOption::create($params); CRM_Core_Session::setStatus(ts('The Record \'%1\' has been saved.', array(1 => $resource->label)), ts('Saved'), 'success'); } $url = CRM_Utils_System::url('civicrm/admin/resource/config_set/config_option', 'reset=1&action=browse&sid=' . $this->_sid); $session = CRM_Core_Session::singleton(); $session->replaceUserContext($url); }
/** * 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; }