/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $input = Input::all();
     $validation = Validator::make($input, Reservation::$rules);
     if ($validation->passes()) {
         $this->reservation->create($input);
         return Redirect::route('admin.reservations.index');
     }
     return Redirect::route('admin.reservations.create')->withInput()->withErrors($validation)->with('message', 'There were validation errors.');
 }
 public function run()
 {
     $faker = Faker::create();
     foreach (range(1, 10) as $index) {
         Reservation::create([]);
     }
 }
 public function get_reservation_status($reservation_id)
 {
     $response_data = $this->core_call("get_reservation_status", array("reservation_id" => array("id" => $reservation_id)));
     if (!$response_data['is_exception']) {
         return Reservation::create($response_data['result']);
     } else {
         die("ERROR: " . $response_data['message'] . "\n");
     }
 }
 public function run()
 {
     $tableIds = Table::whereAvailable(1)->lists('id');
     $userIds = User::lists('id');
     $faker = Faker::create();
     $tablesUsed = [];
     foreach ($tableIds as $tableId) {
         $start = Carbon::now()->startOfDay();
         $reservationStart = $start->addHours(rand(0, 720));
         //$active = ($reservationStart->isToday()) ? 1 : 0;
         Reservation::create(['user_id' => $faker->randomElement($userIds), 'table_id' => $tableId, 'reservation_start' => $reservationStart, 'reservation_end' => $start->addHours(3), 'seats' => $faker->randomElement([2, 4, 8]), 'active' => 1]);
     }
 }
 /**
  * Display a listing of the resource.
  * GET /reservations
  *
  * @return Response
  */
 public function getIndex()
 {
     $execResult = ['updated' => 0, 'created' => 0, 'bookings' => 0, 'cancelled' => 0, 'not_mapped' => 0];
     $propertiesChannels = PropertiesChannel::where('property_id', Property::getLoggedId())->get();
     foreach ($propertiesChannels as $channelSettings) {
         Log::debug($channelSettings);
         $channel = ChannelFactory::create($channelSettings);
         $result = $channel->getReservations();
         Log::debug($result);
         if ($result['reservations']) {
             foreach ($result['reservations'] as $reservation) {
                 $reservation['channel_id'] = $channelSettings->channel_id;
                 $reservation['property_id'] = $channelSettings->property_id;
                 $resModel = Reservation::getByKeys($channelSettings->channel_id, $channelSettings->property_id)->where('res_id', $reservation['res_id'])->first();
                 if (isset($reservation['cc_details']) && !empty($reservation['cc_details'])) {
                     $reservation['cc_details'] = Crypt::encrypt($reservation['cc_details']);
                 }
                 switch ($reservation['status']) {
                     case 'cancelled':
                         if ($resModel) {
                             $resModel->status = 'cancelled';
                             if ($reservation['res_cancel_fee']) {
                                 $resModel->res_cancel_fee = $reservation['res_cancel_fee'];
                             }
                             $resModel->cancelled_at = $resModel->freshTimestamp();
                             $resModel->save();
                             $execResult['cancelled']++;
                             //TODO: send email about cancellation
                         }
                         break;
                     case 'booked':
                         $needAddRooms = true;
                         if ($resModel) {
                             if (isset($reservation['modified']) && $reservation['modified']) {
                                 $resModel->update($reservation);
                                 $execResult['updated']++;
                                 $resModel->bookings()->delete();
                                 //TODO: send email about modification
                             } else {
                                 $needAddRooms = false;
                             }
                         } else {
                             $resModel = Reservation::create($reservation);
                             $execResult['created']++;
                         }
                         if ($reservation['rooms'] && $needAddRooms) {
                             foreach ($reservation['rooms'] as $room) {
                                 $room['reservation_id'] = $resModel->id;
                                 $room['channel_id'] = $reservation['channel_id'];
                                 $room['property_id'] = $reservation['property_id'];
                                 $mapping = InventoryMap::getMappedRoom($channelSettings->channel_id, $channelSettings->property_id, $room['inventory'], isset($room['plan']) ? $room['plan'] : null)->first();
                                 if ($mapping) {
                                     $room['room_id'] = $mapping->room_id;
                                 } else {
                                     $execResult['not_mapped']++;
                                     //TODO: send email about NOT MAPPED ROOM
                                 }
                                 Booking::create($room);
                                 $execResult['bookings']++;
                             }
                         }
                         break;
                 }
                 if ($resModel && $resModel->id) {
                     $type = $resModel->status;
                     if ($type != 'cancelled' && $resModel->modified) {
                         $type = 'modify';
                     }
                     $channel->setReservationConfirmation($resModel->id, $resModel->res_id, $type);
                 }
             }
         }
     }
     return View::make('index', compact('execResult'));
 }
 $user_phone = $_POST['user_phone'];
 if (isset($_POST['guest_reservation']) && $_POST['guest_reservation'] == 'true') {
     $fields = array('restaurant_id' => $_POST['restaurant_id'], 'people_size' => $_POST['people_size'], 'date_of_reservation' => $_POST['date'], 'time_of_reservation' => $time_to_24_hours[$_POST['time']], 'effective' => 1, 'guest_info' => $_POST['guest_info']);
 } else {
     $fields = array('user_id' => $_POST['user_id'], 'restaurant_id' => $_POST['restaurant_id'], 'people_size' => $_POST['people_size'], 'date_of_reservation' => $_POST['date'], 'time_of_reservation' => $time_to_24_hours[$_POST['time']], 'effective' => 1);
 }
 try {
     $query_tool = new DataAccessLayer();
     $sql = 'select ' . $time_to_slots_hash[$_POST['time']] . ', date, restaurant_id from time_slots_capacity where date = "' . $fields['date_of_reservation'] . '" and restaurant_id = "' . $fields['restaurant_id'] . '"';
     $results = $query_tool->query($sql);
     if ($results->num_rows > 0) {
         $row = $results->fetch_row();
         $previous_capacity = $row[0];
         if ($previous_capacity >= $fields['people_size']) {
             $new_reservation = new Reservation();
             $new_reservation->create($fields);
             $next_capacity = $previous_capacity - $fields['people_size'];
             $sql = 'update time_slots_capacity set ' . $time_to_slots_hash[$_POST['time']] . ' = ' . $next_capacity . ' where date = "' . $fields['date_of_reservation'] . '" and restaurant_id = "' . $fields['restaurant_id'] . '"';
             if ($query_tool->query($sql)) {
                 // send email to user to confirm reservation
                 $restaurant = new Restaurant();
                 $restaurant->find($fields['restaurant_id']);
                 $mail = Mail::getInstance();
                 $mailto = $_POST['user_email'];
                 $subject = 'FineTable - Restaurant Reservation Confirmation!';
                 if (isset($_POST['guest_reservation']) && $_POST['guest_reservation'] == 'true') {
                     $body = '
                     <div class="mail-body" style="padding: 20px; border: 5px solid rgb(254, 127, 65); border-radius: 4px; margin: 0 auto; width: 90%;">
                         <p>Hello ' . $_POST['guest_fullname'] . ',</p>
                         <p>Congratulations!</p>
                         <p>You have made a reservation with restaurant: ' . $restaurant->get_restaurant_name() . '.</p>
 /**
  * Create a new reservation for a authenticated user.
  * @param $clustername : cluster's name from url.
  *
  */
 public function createReservation(Cluster $cluster)
 {
     $content = Request::instance()->getContent();
     if (empty($content)) {
         return $this->_sendErrorMessage(400, "Payload.Null", "Received payload is empty.");
     }
     if (Input::json() == null) {
         return $this->_sendErrorMessage(400, "Payload.Invalid", "Received payload is invalid.");
     }
     if (!strcmp($cluster->clustername, Auth::user()->clustername) || Auth::user()->isAdmin()) {
         $thing_uri = Input::json()->get('thing');
         $thing_name = explode('/', $thing_uri);
         $thing_name = $thing_name[count($thing_name) - 1];
         $thing_uri = str_replace($thing_name, '', $thing_uri);
         Input::json()->set('thing', $thing_uri);
         $reservation_validator = Validator::make(Input::json()->all(), array('thing' => 'required|url', 'type' => 'required', 'time' => 'required|time', 'subject' => 'required', 'announce' => 'required', 'customer' => 'required|customer'));
         if (!$reservation_validator->fails()) {
             $thing = Entity::where('name', '=', $thing_name)->where('type', '=', Input::json()->get('type'))->where('user_id', '=', $cluster->user->id)->first();
             if (!isset($thing)) {
                 return $this->_sendErrorMessage(404, "Thing.NotFound", "Thing not found.");
             } else {
                 $time = Input::json()->get('time');
                 if ($this->isAvailable(json_decode($thing->body)->opening_hours, $time)) {
                     //timestamps are UTC so we convert dates to UTC timezone
                     $from = new DateTime($time['from']);
                     $to = new DateTime($time['to']);
                     $from->setTimezone(new DateTimeZone('UTC'));
                     $to->setTimezone(new DateTimeZone('UTC'));
                     $reservation = Reservation::activatedOrBlocking()->where('user_id', '=', $cluster->user->id)->where('entity_id', '=', $thing->id)->where('from', '<', $to)->where('to', '>', $from)->first();
                     if (!empty($reservation)) {
                         return $this->_sendErrorMessage(404, "Thing.AlreadyReserved", "The thing is already reserved at that time.");
                     } else {
                         // Generate an activation code
                         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                         $code = substr(str_shuffle(str_repeat($pool, 5)), 0, 16);
                         // All systems go, create reservation
                         $reservation = Reservation::create(array('from' => $from->getTimestamp(), 'to' => $to->getTimestamp(), 'subject' => Input::json()->get('subject'), 'comment' => Input::json()->get('comment'), 'announce' => json_encode(Input::json()->get('announce')), 'customer' => json_encode(Input::json()->get('customer')), 'entity_id' => $thing->id, 'user_id' => $cluster->user->id, 'activated' => false, 'code' => $code));
                         // Get customer
                         $customer = Input::json()->get('customer');
                         // Email data
                         $from = new DateTime($reservation->from);
                         $to = new DateTime($reservation->to);
                         $data = array('thing_name' => $thing_name, 'from' => $from->format('d-m-Y H:i'), 'to' => $to->format('d-m-Y H:i'), 'reservation' => $reservation, 'customer' => $customer, 'confirm_url' => $cluster->clustername . '/reservations/confirm/' . $code, 'cancel_url' => $cluster->clustername . '/reservations/cancel/' . $code);
                         // Send confirmation email
                         Mail::send('emails.confirm', $data, function ($message) use($customer) {
                             $message->to($customer['email'], $customer['email'])->subject("Confirm your reservation.");
                         });
                         // Send object back to API
                         return $reservation;
                     }
                 } else {
                     return $this->_sendErrorMessage(404, "Thing.Unavailable", "The thing is unavailable at that time.");
                 }
             }
         } else {
             return $this->_sendValidationErrorMessage($reservation_validator);
         }
     } else {
         return $this->_sendErrorMessage(403, "WriteAccessForbiden", "You can't make reservations on behalf of another user.");
     }
 }
<?php

require_once '../lib/Reservation.class.php';
require_once '../lib/Review.class.php';
// Since reviews need to have reservations first, put tests together.
$fields = array('user_id' => '1', 'restaurant_id' => '3', 'people_size' => '5', 'time_of_reservation' => '01-02-2015 19:03:55', 'time_made_reservation' => '01-01-2015 11:09:11');
echo "Testing for creating a reservation: <br>";
$rsv_id = 0;
$rsv = new Reservation();
try {
    $rsv_id = $rsv->create($fields);
    $success = true;
    $message = 'New reservation creation is complete.<br>';
    echo $message;
} catch (Exception $e) {
    $success = false;
    $message = $e->getMessage();
    echo "Creating a new reservation Error: " . $message . "<br>";
}
echo ($success == true ? "SUCCESS" : "FAILURE") . "<br>";
try {
    echo "<br>Testing find reservation:<br>";
    $success = $rsv->find($rsv_id);
    print_r($rsv->get_reservation_info());
    echo "<br>";
    echo $rsv->get_reservation_info_json();
    echo "<br>";
} catch (Exception $e) {
    $success = false;
    $message = $e->getMessage();
    echo "Find reservation Error: " . $message . "<br>";