/** * takes an associative array and creates a cancellation object * * the function extract all the params it needs to initialize the create a * resource object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Booking_BAO_Cancellation object * @access public * @static */ static function create(&$values) { $bookingID = CRM_Utils_Array::value('booking_id', $values); if (!$bookingID) { return; } else { $transaction = new CRM_Core_Transaction(); try { $params = array('option_group_name' => CRM_Booking_Utils_Constants::OPTION_BOOKING_STATUS, 'name' => CRM_Booking_Utils_Constants::OPTION_VALUE_CANCELLED); $result = civicrm_api3('OptionValue', 'get', $params); $params = array(); $params['id'] = $bookingID; $params['status_id'] = CRM_Utils_Array::value('value', CRM_Utils_Array::value($result['id'], $result['values'])); $booking = CRM_Booking_BAO_Booking::create($params); $params = array(); $params['booking_id'] = $bookingID; $percentage = CRM_Utils_Array::value('cancellation_percentage', $values); $bookingTotal = CRM_Utils_Array::value('booking_total', $values); $cancellationFee = $bookingTotal * $percentage / 100; $additionalCharge = CRM_Utils_Array::value('additional_charge', $values); if (is_numeric($additionalCharge)) { $cancellationFee += $additionalCharge; $params['additional_fee'] = $additionalCharge; } $params['cancellation_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('cancellation_date', $values)); $params['comment'] = CRM_Utils_Array::value('comment', $values); $params['cancellation_fee'] = $cancellationFee; self::add($params); $slots = CRM_Booking_BAO_Slot::getBookingSlot($bookingID); foreach ($slots as $slotId => $slots) { $subSlots = CRM_Booking_BAO_SubSlot::getSubSlotSlot($slotId); foreach ($subSlots as $subSlotId => $subSlot) { CRM_Booking_BAO_SubSlot::cancel($subSlotId); } CRM_Booking_BAO_Slot::cancel($slotId); } // return TRUE; } catch (Exception $e) { $transaction->rollback(); CRM_Core_Error::fatal($e->getMessage()); } } }
/** * Get all amount of booking * * Remark: The total_amount has been deducted from discount amount. */ static function getBookingAmount($id) { if (!$id) { return; } $bookingAmount = array('resource_fees' => 0, 'sub_resource_fees' => 0, 'adhoc_charges_fees' => 0, 'discount_amount' => 0, 'total_amount' => 0); $params = array('id' => $id); self::retrieve($params, $booking); $bookingAmount['discount_amount'] = CRM_Utils_Array::value('discount_amount', $booking); $bookingAmount['total_amount'] = CRM_Utils_Array::value('total_amount', $booking); $slots = CRM_Booking_BAO_Slot::getBookingSlot($id); $subSlots = array(); foreach ($slots as $key => $slot) { $subSlotResult = CRM_Booking_BAO_SubSlot::getSubSlotSlot($slot['id']); foreach ($subSlotResult as $key => $subSlot) { $subSlots[$key] = $subSlot; } } $adhocCharges = CRM_Booking_BAO_AdhocCharges::getBookingAdhocCharges($id); $params = array('booking_id' => $id); CRM_Booking_BAO_Payment::retrieve($params, $payment); if (!empty($payment) && isset($payment['contribution_id'])) { // contribution exit so get all price from line item /* $params = array( 'version' => 3, 'id' => $payment['contribution_id'], ); $result = civicrm_api('Contribution', 'get', $params); $contribution = CRM_Utils_Array::value($payment['contribution_id'], $result['values'] ); $bookingAmount['total_amount'] = CRM_Utils_Array::value('total_amount', $contribution); */ foreach ($slots as $slotId => $slot) { $params = array('version' => 3, 'entity_id' => $slotId, 'entity_table' => 'civicrm_booking_slot'); $result = civicrm_api('LineItem', 'get', $params); $lineItem = CRM_Utils_Array::value($result['id'], $result['values']); $bookingAmount['resource_fees'] += CRM_Utils_Array::value('line_total', $lineItem); } foreach ($subSlots as $subSlotId => $subSlots) { $params = array('version' => 3, 'entity_id' => $subSlotId, 'entity_table' => 'civicrm_booking_sub_slot'); $result = civicrm_api('LineItem', 'get', $params); $lineItem = CRM_Utils_Array::value($result['id'], $result['values']); $bookingAmount['sub_resource_fees'] += CRM_Utils_Array::value('line_total', $lineItem); } foreach ($adhocCharges as $charges) { $params = array('version' => 3, 'entity_id' => CRM_Utils_Array::value('id', $charges), 'entity_table' => 'civicrm_booking_adhoc_charges'); $result = civicrm_api('LineItem', 'get', $params); $lineItem = CRM_Utils_Array::value($result['id'], $result['values']); $bookingAmount['adhoc_charges_fees'] += CRM_Utils_Array::value('line_total', $lineItem); } } else { foreach ($slots as $id => $slot) { $bookingAmount['resource_fees'] += CRM_Booking_BAO_Slot::calulatePrice(CRM_Utils_Array::value('config_id', $slot), CRM_Utils_Array::value('quantity', $slot)); } foreach ($subSlots as $id => $subSlot) { $bookingAmount['sub_resource_fees'] += CRM_Booking_BAO_Slot::calulatePrice(CRM_Utils_Array::value('config_id', $subSlot), CRM_Utils_Array::value('quantity', $subSlot)); } foreach ($adhocCharges as $charges) { $price = CRM_Core_DAO::getFieldValue('CRM_Booking_DAO_AdhocChargesItem', CRM_Utils_Array::value('item_id', $charges), 'price', 'id'); $bookingAmount['adhoc_charges_fees'] += $price * CRM_Utils_Array::value('quantity', $charges); } } return $bookingAmount; }
function postProcess() { $bookingInfo = $this->exportValues(); $selectResource = $this->controller->exportValues('SelectResource'); $addSubResource = $this->controller->exportValues('AddSubResource'); $resourcesValue = json_decode($selectResource['resources'], true); $subResourcesValue = json_decode($addSubResource['sub_resources'], true); $subResources = $subResourcesValue['sub_resources']; //Build resources array for passing to Booking APIs $resources = array(); foreach ($resourcesValue as $key => $resource) { //Remove element that used in DTHMLX as we do not need them. unset($resource['id']); unset($resource['label']); unset($resource['text']); unset($resource['readonly']); $resource['start_date'] = $resource['start_date']; $resource['end_date'] = $resource['end_date']; $resource['sub_resources'] = array(); if (isset($subResources)) { foreach ($subResources as $subKey => $subResource) { if ($key == $subResource['parent_ref_id']) { $subResourceTmp['resource_id'] = $subResource['resource']['id']; $subResourceTmp['configuration_id'] = $subResource['configuration']['id']; $subResourceTmp['quantity'] = $subResource['quantity']; $subResourceTmp['time_required'] = $subResource['time_required']; $subResourceTmp['note'] = $subResource['note']; $subResourceTmp['price_estimate'] = $subResource['price_estimate']; array_push($resource['sub_resources'], $subResourceTmp); unset($subResources[$subKey]); } } } array_push($resources, $resource); } $adhocCharges = CRM_Utils_Array::value('adhoc_charges', $subResourcesValue); $booking = array(); if ($this->_id && $this->_action == CRM_Core_Action::UPDATE) { $booking['id'] = $this->_id; } $booking['primary_contact_id'] = CRM_Utils_Array::value('primary_contact_id', $bookingInfo); $booking['secondary_contact_id'] = CRM_Utils_Array::value('secondary_contact_id', $bookingInfo); $booking['po_number'] = CRM_Utils_Array::value('po_no', $bookingInfo); $booking['status_id'] = CRM_Utils_Array::value('booking_status', $bookingInfo); $booking['title'] = CRM_Utils_Array::value('title', $bookingInfo); $booking['description'] = CRM_Utils_Array::value('description', $bookingInfo); $booking['note'] = CRM_Utils_Array::value('note', $bookingInfo); $booking['booking_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('event_start_date', $bookingInfo), CRM_Utils_Array::value('event_start_date_time', $bookingInfo)); $booking['discount_amount'] = CRM_Utils_Array::value('discount_amount', $addSubResource); $booking['total_amount'] = CRM_Utils_Array::value('total_price', $addSubResource); $amountToFloat = floatval($booking['total_amount']); $booking['total_amount'] = round($amountToFloat, 2, PHP_ROUND_HALF_UP); //add adhoc charge $booking['adhoc_charges_note'] = CRM_Utils_Array::value('note', $adhocCharges); $booking['participants_estimate'] = CRM_Utils_Array::value('enp', $bookingInfo); $booking['participants_actual'] = CRM_Utils_Array::value('fnp', $bookingInfo); $now = date('YmdHis'); $session =& CRM_Core_Session::singleton(); $booking['created_by'] = $session->get('userID'); $booking['created_date'] = $now; $booking['updated_by'] = $session->get('userID'); $booking['updated_date'] = $now; //retrieve booking_start_date, booking_end_date from all slots $dates = array(); foreach ($resources as $key => $slot) { array_push($dates, CRM_Utils_Array::value('start_date', $slot)); array_push($dates, CRM_Utils_Array::value('end_date', $slot)); } sort($dates); $bookingStartDate = $dates[0]; $bookingEndDate = $dates[count($dates) - 1]; $booking['booking_start_date'] = CRM_Utils_Date::processDate($bookingStartDate); $booking['booking_end_date'] = CRM_Utils_Date::processDate($bookingEndDate); //make sure we create everything in one transaction, not too nice but it does the job $transaction = new CRM_Core_Transaction(); try { $result = civicrm_api3('Booking', 'Create', $booking); $bookingID = CRM_Utils_Array::value('id', $result); $booking = CRM_Utils_Array::value($bookingID, CRM_Utils_Array::value('values', $result)); $this->_id = $bookingID; //make sure we have the id on create mode $this->_values = $booking; //Now we process slots/subslots and adhoc charges if ($this->_action == CRM_Core_Action::UPDATE) { //booking id was passed from the form so we are on edit mode $currentSlots = CRM_Booking_BAO_Slot::getBookingSlot($bookingID); } $newSlotIds = array(); $newSubSlotIds = array(); foreach ($resources as $key => $resource) { $slot = array('booking_id' => $bookingID, 'config_id' => CRM_Utils_Array::value('configuration_id', $resource), 'start' => CRM_Utils_Date::processDate(CRM_Utils_Array::value('start_date', $resource)), 'end' => CRM_Utils_Date::processDate(CRM_Utils_Array::value('end_date', $resource)), 'resource_id' => CRM_Utils_Array::value('resource_id', $resource), 'quantity' => CRM_Utils_Array::value('quantity', $resource), 'note' => CRM_Utils_Array::value('note', $resource)); if ($this->_action == CRM_Core_Action::UPDATE) { list($isExist, $currentID) = CRM_Booking_BAO_Slot::findExistingSlot($slot, $currentSlots); if ($isExist) { $slot['id'] = $currentID; } } $slotResult = civicrm_api3('Slot', 'create', $slot); $slotID = CRM_Utils_Array::value('id', $slotResult); array_push($newSlotIds, $slotID); if ($this->_action == CRM_Core_Action::UPDATE) { $currentSubSlots = CRM_Booking_BAO_SubSlot::getSubSlotSlot($slotID); } $subResources = $resource['sub_resources']; foreach ($subResources as $subKey => $subResource) { $subSlot = array('resource_id' => CRM_Utils_Array::value('resource_id', $subResource), 'slot_id' => $slotID, 'config_id' => CRM_Utils_Array::value('configuration_id', $subResource), 'time_required' => CRM_Utils_Date::processDate(CRM_Utils_Array::value('time_required', $subResource)), 'quantity' => CRM_Utils_Array::value('quantity', $subResource), 'note' => CRM_Utils_Array::value('note', $subResource)); if ($this->_action == CRM_Core_Action::UPDATE) { list($isExist, $currentSubSlotId) = CRM_Booking_BAO_SubSlot::findExistingSubSlot($subSlot, $currentSubSlots); if ($isExist) { $subSlot['id'] = $currentSubSlotId; } } $subSlotResult = civicrm_api3('SubSlot', 'Create', $subSlot); $subSlotID = CRM_Utils_Array::value('id', $subSlotResult); array_push($newSubSlotIds, $subSlotID); } if ($this->_action == CRM_Core_Action::UPDATE) { //remove subslots that have been removed $subSlotsToBeRemoved = array(); foreach ($currentSubSlots as $key => $currentSubSlot) { if (!in_array($key, $newSubSlotIds)) { $subSlotsToBeRemoved[$key] = $currentSubSlot; } } if (!empty($subSlotsToBeRemoved)) { foreach ($subSlotsToBeRemoved as $key => $slot) { civicrm_api3('SubSlot', 'delete', array('id' => $key)); } } } } if ($this->_action == CRM_Core_Action::UPDATE) { //remove all slots that have been removed $slotsToBeRemoved = array(); foreach ($currentSlots as $key => $currentSlot) { if (!in_array($key, $newSlotIds)) { $slotsToBeRemoved[$key] = $currentSlot; } } if (!empty($slotsToBeRemoved)) { foreach ($slotsToBeRemoved as $key => $slot) { civicrm_api3('Slot', 'delete', array('id' => $key)); } } } if ($adhocCharges) { if ($this->_action == CRM_Core_Action::UPDATE) { $result = civicrm_api3('AdhocCharges', 'get', array('booking_id' => $bookingID, 'is_deleted' => 0)); $currentAdhocCharges = $result['values']; } // fixed bug of CVB-94 // Ad-hoc charges - cannot save if (!is_null(CRM_Utils_Array::value('items', $adhocCharges))) { $items = array_filter(CRM_Utils_Array::value('items', $adhocCharges)); $newAdhocChargesIds = array(); foreach ($items as $key => $item) { $params = array('booking_id' => $bookingID, 'item_id' => CRM_Utils_Array::value('item_id', $item), 'quantity' => CRM_Utils_Array::value('quantity', $item)); if ($this->_action == CRM_Core_Action::UPDATE) { list($isExist, $currentAdhocChargesId) = CRM_Booking_BAO_AdhocCharges::findExistingAdhocCharges($params, $currentAdhocCharges); if ($isExist) { $params['id'] = $currentAdhocChargesId; } } $result = civicrm_api3('AdhocCharges', 'create', $params); $adhocChargesId = CRM_Utils_Array::value('id', $result); array_push($newAdhocChargesIds, $adhocChargesId); } } if ($this->_action == CRM_Core_Action::UPDATE) { //remove adhoc charges that have been removed $adhocChargesToBeRemoved = array(); foreach ($currentAdhocCharges as $key => $adc) { if (!in_array($key, $newAdhocChargesIds)) { $adhocChargesToBeRemoved[$key] = $adc; } } if (!empty($adhocChargesToBeRemoved)) { foreach ($adhocChargesToBeRemoved as $key => $adc) { civicrm_api3('AdhocCharges', 'delete', array('id' => $key)); } } } } //End process parent::postProcess(); $cid = CRM_Utils_Array::value('primary_contact_id', $bookingInfo); // user context $url = CRM_Utils_System::url('civicrm/contact/view/booking', "reset=1&id={$bookingID}&cid={$cid}&action=view"); CRM_Core_Session::setStatus($booking['title'], ts('Saved'), 'success'); CRM_Utils_System::redirect($url); } catch (CiviCRM_API3_Exception $e) { $transaction->rollback(); CRM_Core_Error::fatal($e->getMessage()); } catch (Exception $e) { $transaction->rollback(); CRM_Core_Error::fatal($e->getMessage()); } }