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; }