예제 #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 extras info
  * 
  * @return mixed
  * @throws Exception
  */
 private function loadExtras()
 {
     // init extras array
     $this->extras = (object) [];
     $this->extras->total = 0;
     $this->extras->currency_total = 0;
     $this->extras->units = [];
     $this->extras->amount = [];
     $this->extras->currency_amount = [];
     // get & check selection
     $rooms_extras = CHClient::loadExtrasFromRequest();
     if (!$rooms_extras) {
         return;
     }
     // load extras info
     foreach ($this->rooms->units as $i => $room) {
         // check extras are selected for the room
         $this->extras->units[$i] = [];
         $this->extras->amount[$i] = 0;
         $this->extras->currency_amount[$i] = 0;
         if (!isset($rooms_extras[$i])) {
             continue;
         }
         // get extras info
         $extras_data = CHLibData::getObjectFromList($this->availability->results->search_rooms[$i]->available_rooms, $room->id, 'room_id')->extras;
         foreach ($this->availability->hotel->extras as $extra_info) {
             // get extra input
             $input = CHLibData::getObjectFromList($rooms_extras[$i], $extra_info->id, 'id');
             if (!$input) {
                 continue;
             }
             // create extra object
             $extra_data = CHLibData::getObjectFromList($extras_data, $extra_info->id, 'extra_id');
             $data = (object) [];
             $data->id = $extra_info->id;
             $data->title = $extra_info->title;
             $data->info = $extra_info->info;
             $data->image = $extra_info->image;
             $data->units = $input->units;
             $data->type = $extra_data->type;
             $data->amount = $extra_data->amount * $input->units;
             $data->currency_amount = $extra_data->currency->amount * $input->units;
             $data->data = $extra_data;
             $this->extras->units[$i][] = $data;
             $this->extras->amount[$i] += $data->amount;
             $this->extras->currency_amount[$i] += $data->currency_amount;
             $this->extras->total += $data->amount;
             $this->extras->currency_total += $data->currency_amount;
         }
     }
 }