public function updateJsonAction() { $data = Zend_Json::decode($this->_getParam('data')); // we don't use the id as this is a shortened version of the booking ref // we use title as this contains the full booking reference. $reservationID = $data['title']; if ($reservationID == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('Admin.Reservations.Edit', 'ReservationIDMissed'))); } $unitID = $data['cid']; if ($unitID == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('Admin.Reservations.Edit', 'UnitIDMissed'))); } $reservationModel = new RM_Reservations(); $reservation = $reservationModel->find($reservationID)->current(); if ($reservation == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('Admin.Reservations.Edit', 'ResDataInvalid'))); } if ($data['uid'] !== $reservation->user_id) { // if the assigned user is changed update the user id. $reservation->user_id = $data['uid']; $reservation->save(); } $unitModel = new RM_Units(); $unit = $unitModel->find($unitID)->current(); if ($unit == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('Admin.Reservations.Edit', 'UnitDataInvalid'))); } $reservationDetailsModel = new RM_ReservationDetails(); $detail = $reservationDetailsModel->fetchAllBy($unit, $reservation)->current(); if ($detail == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('Admin.Reservations.Edit', 'ResDataInvalid'))); } $startDate = $data['start']; if ($startDate == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('StartDate'))); } $endDate = $data['end']; if ($endDate == null) { return array('data' => array('success' => false, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_ERRORS)->_('EndDate'))); } $persons = new RM_Reservation_Persons(array("adults" => 1, "children" => 0, "infants" => 0)); $period = new RM_Reservation_Period(new RM_Date(strtotime($startDate)), new RM_Date(strtotime($endDate))); $information = new RM_Prices_Information($unit, $period, $persons); $priceSystem = RM_Environment::getInstance()->getPriceSystem(); $detail->start_datetime = $startDate; $detail->end_datetime = $endDate; try { $detail->total_price = $priceSystem->getTotalUnitPrice($information); } catch (Exception $e) { $detail->total_price = 0; } $detail->save(); $reservation->recalculate(); $idArray = explode("-", $reservationID); $id = (int) $idArray[1]; return array('data' => array("success" => true, "message" => RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('Admin.Reservations.Edit', "LoadedData"), "data" => array("id" => $id, "cid" => $unitID, "title" => $reservationID, "start" => $startDate, "end" => $endDate, "url" => "RM.Pages.Functions.Reservations_EditJson_Request('" . $period->id . "')", "ad" => true))); }
/** * Invoked from Edit Reservations form via an AJAX request (edit.js) and is used to Update the Reservation * * This copies the data from the temp tables back to the live table and destroys the temp reservation * The reason we have a temp table for reservations and reservation details is that the we need to store * the ajax requests temporarilly until the user is ready to save and commit the reservation. If they * abandon the reservation then the temp data is either re-used when the reservation is re-edited or * refreshed. This allows some freedom to the user to change the reservation but to also cancel out if * they do not want to commit changes. * * @param request id the reservation is linked by the id of the reservation (not the reference_id, this is just for the admins/users information) * @return json boolean (true if success) */ public function updateJsonAction() { $reservationJson = Zend_Json::decode($this->_getParam('reservation', "[]")); if (count($reservationJson) == 0) { return array('data' => array('success' => false)); } $reservationModel = new RM_Reservations(); $reservation = $reservationModel->find($reservationJson['id'])->current(); if ($reservation == null) { return array('data' => array('success' => false)); } foreach ($reservationJson as $fieldName => $value) { $reservation->{$fieldName} = $value; } $reservation->save(); $reservationDetailsModel = new RM_ReservationDetails(); //Delete all disabled reservation details $deletedUnitIDs = Zend_Json::decode($this->_getParam('deleted_unit_ids', "[]")); $unitModel = new RM_Units(); foreach ($deletedUnitIDs as $unitID) { $unit = $unitModel->find($unitID)->current(); if ($unit == null) { continue; } $details = $reservationDetailsModel->fetchAllBy($unit, $reservation); foreach ($details as $detail) { $detail->delete(); } } $detailsJson = Zend_Json::decode($this->_getParam('details', "[]")); foreach ($detailsJson as $detailJson) { if (in_array($detailJson['unit_id'], $deletedUnitIDs)) { continue; } $unit = $unitModel->find($detailJson['unit_id'])->current(); $detail = $reservationDetailsModel->fetchAllBy($unit, $reservation)->current(); /** * Other info management. This code allows other information to be handled * by the price system. Edit.js on save will pass through an array of * Other Info items these must be decoded and must replace the 'otherinfo' * index in the detail array. * * otherinfo is defined in the edit.js in the return param's however this * is just a generic placeholder to pass the data as this could be anything * * for example if need to pass board_type which is used by the hospitality * then we pass this on the otherinfo param as a json array. leaving the otherinfo * index in the array will cause the update to fail as this column will not * be found. So we read in the json array then add the keys for the * other items then finally remove the other info index. * * ie: if board_type = 'hb' and extra_bed = '1' are passed these would be * in the otherinfo json and will be added to the detail array then the * otherinfo value will be removed. */ $otherInfo = Zend_Json::decode($detailJson['otherinfo']); // zend json decode puts the array inside another array so we have to return i0. if (!empty($otherInfo)) { foreach ($otherInfo[0] as $key => $value) { $detailJson[$key] = $value; unset($detailJson['otherinfo']); } } else { // if it's not passed remove it as some // price systems my not have this row unset($detailJson['otherinfo']); } // others systems processing (not to be confused with otherInfo $otherPriceTotal = 0; $othersSystems = Zend_Json::decode($this->_getParam('other_systems', "[]")); foreach ($othersSystems as $system) { $systemName = $system['systemClassName']; // this will be the module/plugin class for updating $systemClassName = "RM_Module_" . $systemName; $lowercaseSystemClassName = strtolower($systemName); $dataArray['id'] = $detail->id; foreach ($system['data'] as $data) { $dataArray[$data['name']] = $data['value']; } $otherModel = new $systemClassName(); $otherModel->updateOtherData($dataArray); $othersNewPrice = $otherModel->getPrice($dataArray); $reservationSummaryModel = new RM_ReservationSummary(); $reservationSummaryRows = $reservationSummaryModel->fetchByReservationDetail($detail, $lowercaseSystemClassName)->current(); $reservationSummaryRows->total_amount = $othersNewPrice['price']; $reservationSummaryRows->save(); } // others system complete if ($detail == null) { $detail = $reservationDetailsModel->createRow($detailJson); } else { foreach ($detailJson as $key => $value) { $detail->{$key} = $value; } } $period = new RM_Reservation_Period(new RM_Date(strtotime($detail->start_datetime)), new RM_Date(strtotime($detail->end_datetime))); $persons = new RM_Reservation_Persons(array("adults" => $detail->adults, "children" => $detail->children, "infants" => $detail->infants)); // group handling (only used when the groups is enabled) $templateUnitID = $unit->isTemplateUnit(); if ($templateUnitID !== null) { if ($templateUnitID !== (int) $unit_id) { // if it's not the main template unit then switch to the main template // unit to get the price information $unit = $unitModel->get($templateUnitID); } } $information = new RM_Prices_Information($unit, $period, $persons, $otherInfo[0]); $priceSystem = RM_Environment::getInstance()->getPriceSystem(); try { $detail->total_price = $priceSystem->getTotalUnitPrice($information); } catch (Exception $e) { $detail->total_price = 0; } //$detail->total_price += $otherPriceTotal; $detail->save(); } //We need to recalculate all information that was saved to reservation $reservation->recalculate(); return array('data' => array('success' => true)); }
/** * Checks if in system exists any reservation with this unitID * * @param int $unitID unit primary key value * @return bool */ public function isReserved($unitID) { $unitModel = new RM_Units(); $unit = $unitModel->find($unitID)->current(); if ($unit == null) { return false; } $model = new RM_ReservationDetails(); $details = $model->fetchAllBy($unit); return count($details) > 0; }