/** * 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)); }
public function insertNewReservation($user, $unitDetails, $inprogres = 0, $confirmed = 1, $bookingRef = null) { //1. add information into rm_reservation if ($user->id) { $userID = $user->id; } else { // at this point the user does not exist in ResMania let's add it... $userData = array(); foreach ($user as $key => $value) { if ($key == 'name') { $nameArray = explode(" ", $value); $userData['first_name'] = $nameArray[0]; $userData['last_name'] = $nameArray[1] . " " . $nameArray[2]; } $userData[$key] = $value; } $userData['group_id'] = 0; unset($userData['id']); $rmUsers = new RM_Users(); $userID = $rmUsers->insert($userData); } $reservation = $this->createRow(); $reservation->user_id = $userID; $reservation->confirmed = $confirmed; $reservation->in_progress = $inprogres; // this will hide the reservation until we have completed the process if ($bookingRef !== null) { $reservation->id = $bookingRef; } $reservation->creation_datetime = date(RM_Config::MYSQL_DATEFORMAT); //current server datetime $reservationID = $reservation->save(); //2. add information into rm_reservation_details $detailsModel = new RM_ReservationDetails(); $priceSystem = RM_Environment::getInstance()->getPriceSystem(); foreach ($unitDetails as $unitDetail) { $detail = $detailsModel->createRow(); $selectedUnit = $unitDetail->getUnit(); // get the master unit if (class_exists("RM_Groups")) { $groupsObject = new RM_Groups(); $isMaster = $groupsObject->isMain($selectedUnit); if (!$isMaster) { $unitModel = new RM_Units(); $group = $groupsObject->getByUnit($selectedUnit); if ($group != null) { try { $groupID = $group->main_unit_id; $selectedUnit = $unitModel->get($groupID, RM_Environment::getInstance()->getLocale()); } catch (Exception $e) { } } } } $information = new RM_Prices_Information($selectedUnit, $unitDetail->getPeriod(), $unitDetail->getPersons(), $unitDetail->getOtherInfo()); try { $detail->total_price = $priceSystem->getTotalUnitPrice($information); } catch (Exception $e) { $detail->total_price = 0; } $detail->reservation_id = $reservationID; $detail->unit_id = $unitDetail->getUnit()->getId(); $detail->start_datetime = $unitDetail->getPeriod()->getStart()->toMySQL(); $detail->end_datetime = $unitDetail->getPeriod()->getEnd()->toMySQL(); $detail->adults = $unitDetail->getPersons()->getAdults() == 0 ? 1 : $unitDetail->getPersons()->getAdults(); $detail->children = $unitDetail->getPersons()->getChildren(); $detail->infants = $unitDetail->getPersons()->getInfants(); // process other information (this allows the price system to manage // other data i.e: board_types for the hospitality price module $otherInfo = $unitDetail->getOtherInfo(); if ($otherInfo) { foreach ($otherInfo as $key => $value) { $detail->{$key} = $value; } } $detail->save(); $this->_insertDetailExtraData($unitDetail, $detail); } return $reservationID; }