예제 #1
0
 /**
  * Instert a new booking
  */
 public function newBookig()
 {
     // init post validation
     $valid = true;
     $app = CHClient::getApp();
     $config = CHClient::getConfig();
     $post = CHLib::input()->post;
     // build the new booking request
     $request = (object) [];
     $request->hotel_id = $post->getUint('hotel_id');
     // required string fields
     foreach (['start_date', 'end_date', 'first_name', 'last_name', 'email', 'phone'] as $field) {
         $request->{$field} = $post->getString($field);
         if (strlen($request->{$field}) <= 2) {
             $valid = false;
             $errors[] = $field;
         }
     }
     // not required strings
     foreach (['country', 'promo_code', 'checkin_time', 'requests', 'confirm', 'lang', 'currency'] as $field) {
         $request->{$field} = $post->getString($field);
     }
     // bool fields
     $request->pay_full = $post->get('pay_full') ? 1 : 0;
     $request->newsletter = $post->get('newsletter') ? 1 : 0;
     // collect card info
     // @todo server validation
     if ($config->confirm_card_hosted) {
         // set confirmation method for api request
         $request->confirm = 'card_hosted';
         // init card object
         $card = (object) [];
         $card->type = $post->get('card_type');
         $card->cvc = $post->get('card_cvc');
         $card->expiration_date = $post->get('card_expiration_month') . '/' . $post->get('card_expiration_year');
         // split card number details for security
         $card_number = (int) str_replace(' ', '', $post->getString('card_number'));
         $request->card_number = substr($card_number, 0, -4) . '****';
         $card->number = '**** **** **** ' . substr($card_number, -4);
     }
     // invalid data, try again
     if (!$valid) {
         $app->setUserState('chclient.booking_request', $request);
         $app->setUserState('chclient.booking_errors', $errors);
         return false;
     }
     // attach rooms array
     $request->rooms = [];
     $party = CHClient::loadParty($post->get('party'));
     list($rates, $boards) = CHClient::loadRoomsFromRequest($post->get('rooms'));
     $extras = CHClient::loadExtrasFromRequest($post->get('extras'));
     foreach ($party as $i => $r_party) {
         $room = (object) ['party' => $r_party];
         $room->rate = $rates[$i];
         $room->board = $boards[$i];
         $room->extras = isset($extras[$i]) ? $extras[$i] : [];
         $room->guest = (object) ['name' => $post->getString('guest_' . $i), 'bed' => $post->get('bed_' . $i), 'smoking' => $post->get('smoking_' . $i)];
         $request->rooms[] = $room;
     }
     // attach app object
     $user = JFactory::getUser();
     $request->app = (object) [];
     $request->app->app_id = $config->data_source_app_id;
     $request->app->user_id = $user->guest ? null : $user->id;
     $request->app->user_ip = CHLib::getIp();
     // attach tracking object
     $request->tracking = json_decode($app->getUserState('plg_racking.tracking', 'null'));
     // init request
     $api_request = $this->apiRequest('booking_add', $request);
     // check for errors
     if ($api_request->errors->errors) {
         $app->setUserState('chclient.api_errors', $api_request->errors);
         return false;
     }
     // load booking
     $booking = $api_request->response;
     // confirmed & pending bookings (1x, 2x)
     if ($booking->booking_status < 30) {
         // send notification
         CHClientBooking::emailNotification($booking, $card);
     } else {
         // set booking confirm submit
         $app->setUserState('chclient.submit_confirm', true);
     }
     // set new booking state (for analytics tracking)
     $app->setUserState('chclient.new_booking', true);
     // set booking data state for late use in booking view
     $app->setUserState('chclient.booking', $booking);
     return true;
 }
예제 #2
0
 /**
  * Load selected rooms info
  * 
  * @return mixed
  * @throws Exception
  */
 protected function loadRooms()
 {
     // get & check selection
     list($rates, $boards) = CHClient::loadRoomsFromRequest();
     // to determine the worst conditions
     $conditions = [];
     // get rooms units
     $this->rooms = (object) [];
     $this->rooms->total = 0;
     $this->rooms->currency_total = 0;
     $this->rooms->conditions = 'pay_at_hotel';
     $this->rooms->units = [];
     $this->rooms->amount = [];
     $this->rooms->currency_amount = [];
     foreach ($this->availability->results->search_rooms as $i => $search_room) {
         // look in the rates to determine the room_id
         foreach ($search_room->available_rooms as $available_room) {
             $board = $boards[$i];
             $rate = CHLibData::getObjectFromList($available_room->rates, $rates[$i], 'rate_id');
             if ($rate) {
                 $room = CHLibData::getObjectFromList($this->availability->hotel->rooms, $available_room->room_id, 'id');
                 $data = (object) [];
                 $data->id = $available_room->room_id;
                 $data->title = $room->title;
                 $data->info = $room->info;
                 $data->image = $room->image;
                 $data->available = $available_room->available;
                 $data->smoking = $available_room->smoking;
                 $data->bed_preference = $available_room->bed_preference;
                 $data->party = $search_room->party;
                 $data->board = $board;
                 $data->rate_id = $rate->rate_id;
                 $data->rate_master = $rate->master;
                 $data->rate_title = $rate->title;
                 $data->rate_conditions = $rate->conditions;
                 $data->rate_deposit = $rate->deposit;
                 $data->rate_deadline = $rate->deadline;
                 $data->rate_free_cancellation = $rate->free_cancellation;
                 $data->rate_cancellation_policy = $rate->cancellation_policy;
                 if (!isset($rate->currency->amount->{$board})) {
                     throw new Exception(CHClient::string('error_bad_request'), 400);
                 }
                 $data->amount = $rate->amount->{$board};
                 $data->currency_amount = $rate->currency->amount->{$board};
                 $data->currency = $rate->currency;
                 $data->discounts = $rate->discounts;
                 foreach ($data->discounts as $discount) {
                     unset($discount->detail);
                 }
                 $this->rooms->units[] = $data;
                 // determine the dominant (worst) conditions
                 $conditions[] = $rate->conditions;
                 $this->rooms->total += $rate->amount->{$board};
                 $this->rooms->currency_total += $rate->currency->amount->{$board};
                 break;
             }
         }
     }
     // get the worst conditions found
     $this->rooms->conditions = CHClient::worstConditions($conditions);
     // check rooms units
     if (count($this->rooms->units) != count($this->availability->results->search_rooms)) {
         throw new Exception(CHClient::string('error_bad_request'), 400);
     }
 }