示例#1
0
 public function deletBooking($id)
 {
     $booking = App\Booking::findOrNew($id);
     $userID = $booking::userID;
     \App\Booking::destroy($id);
     returnToUserBooking($userID);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $num_bookings = 50;
     $all_properties = \App\Property::all();
     $users = \App\User::all();
     $statuses = array('requested', 'confirmed', 'rejected');
     for ($i = 0; $i < $num_bookings; $i++) {
         $one_property = $all_properties[mt_rand(0, count($all_properties) - 1)];
         $one_customer = null;
         $customer_id_ok = false;
         while (!$customer_id_ok) {
             $one_customer = $users[mt_rand(0, count($users) - 1)];
             if ($one_customer->id != $one_property->owner_id) {
                 $customer_id_ok = true;
             }
         }
         $one_status = $statuses[mt_rand(0, count($statuses) - 1)];
         $booking_ok = false;
         $start = null;
         $end = null;
         while (!$booking_ok) {
             $start = $this->randomDate(date('2016-01-01'), "2016-04-30");
             $start_time_plus_one_day = strtotime($start);
             $start_time_plus_one_day = strtotime("+1 day", $start_time_plus_one_day);
             $start_time_plus_seven_days = strtotime($start);
             $start_time_plus_seven_days = strtotime("+7 day", $start_time_plus_seven_days);
             $end = $this->randomDate(date('Y-m-d', $start_time_plus_one_day), date('Y-m-d', $start_time_plus_seven_days));
             $select_statement = 'select * from bookings WHERE ' . "'" . $start . "'" . ' <= bookings.end AND ' . "'" . $end . "'" . ' >= start AND bookings.status = ' . "'" . 'confirmed' . "'" . 'AND bookings.property_id = ' . $one_property->id;
             $overlapping_bookings = DB::select($select_statement);
             if (count($overlapping_bookings) == 0) {
                 $booking_ok = true;
             }
         }
         $booking = new \App\Booking();
         $booking->customer_id = $one_customer->id;
         $booking->property_id = $one_property->id;
         $booking->start = $start;
         $booking->end = $end;
         $booking->status = $one_status;
         $booking->save();
     }
 }
示例#3
0
 /**
  * Responds to requests to POST /bookings/create
  */
 public function postCreate(Request $request)
 {
     // check if the hall is booked for the given time
     $startTime = $request->date . 'T08:00:00.000';
     $endTime = $request->date . 'T19:00:00.000';
     $bookings = \App\Booking::where('booking_status_id', '=', '3')->where('booking_time', '>=', $startTime)->where('booking_time', '<=', $endTime)->where('sport_hall_id', '=', $request->hall)->get();
     // selected time for booking
     $currentBookingTime = Carbon::createFromFormat('Y-m-d H:00', $request->date . ' ' . $request->time);
     // hall should be booked atleast 5 hours in advance
     if (Carbon::now()->diffInHours($currentBookingTime, false) <= 5) {
         \Session::flash('flash_message', 'You have to book a hall atleast 5 hours in advance. Please select an other time slot.');
         return redirect('/bookings/create');
     }
     // loop through all the booking on this date and check if the hall is reserved
     foreach ($bookings as $bookingInRange) {
         $startTime = Carbon::createFromFormat('Y-m-d H:00:00', $bookingInRange->booking_time);
         $endTime = $startTime->copy();
         $endTime->addHours($bookingInRange->hours);
         if ($currentBookingTime->between($startTime, $endTime) && !($endTime->diffInHours($currentBookingTime) == 0)) {
             \Session::flash('flash_message', 'Sorry the hall is already reserved for this time slot!');
             return redirect('/bookings/create');
         }
     }
     // make a booking
     $booking = new \App\Booking();
     $booking->sport_hall_id = $request->hall;
     $booking->user_id = \Auth::id();
     $booking->booking_time = Carbon::createFromFormat('Y-m-d H:00', $request->date . ' ' . $request->time)->toDateTimeString();
     $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id');
     $booking->booking_status_id = $bookingStatusId;
     $booking->hours = $request->hours;
     $booking->save();
     // Done
     \Session::flash('flash_message', 'Your booking was created!');
     return redirect('/bookings');
 }