Пример #1
0
 public function doFindNextSlot(Request $request, $override_start_date = 0, $override_duration = 0, $override_appointment_type = 0, $override_redirect = 0)
 {
     if ($override_start_date) {
         $start_date = Carbon::createFromFormat('d/m/Y', $override_start_date);
     } else {
         $start_date = Carbon::createFromFormat('d/m/Y', $request->input('start_date'));
     }
     if ($override_duration) {
         $duration = $override_duration;
     } else {
         $duration = $request->input('minutes');
     }
     if ($override_appointment_type) {
         $appointment_type = $override_appointment_type;
     } else {
         $appointment_type = $request->input('appointment_type');
     }
     $dentist_id = $request->input('dentist');
     $found_slot = false;
     for ($i = 8; $i < 19; $i++) {
         if ($i < 10) {
             $times[] = "0" . $i . ":00";
             $times[] = "0" . $i . ":05";
             $times[] = "0" . $i . ":10";
             $times[] = "0" . $i . ":15";
             $times[] = "0" . $i . ":20";
             $times[] = "0" . $i . ":25";
             $times[] = "0" . $i . ":30";
             $times[] = "0" . $i . ":35";
             $times[] = "0" . $i . ":40";
             $times[] = "0" . $i . ":45";
             $times[] = "0" . $i . ":50";
             $times[] = "0" . $i . ":55";
         } else {
             $times[] = $i . ":00";
             $times[] = $i . ":05";
             $times[] = $i . ":10";
             $times[] = $i . ":15";
             $times[] = $i . ":20";
             $times[] = $i . ":25";
             $times[] = $i . ":30";
             $times[] = $i . ":35";
             $times[] = $i . ":40";
             $times[] = $i . ":45";
             $times[] = $i . ":50";
             $times[] = $i . ":55";
         }
         if ($i == 18) {
             $times[] = "19:00";
         }
     }
     $today = $start_date->copy();
     \DB::enableQueryLog();
     $slot_start = 'beep';
     $slots_found = 0;
     $slots = array();
     $leave_clear = 0;
     while ($slots_found < 12) {
         $today_with_timeslot = $today->copy();
         foreach ($times as $time) {
             if ($leave_clear == 0) {
                 $time_exploded = explode(":", $time);
                 $today_with_timeslot->hour = $time_exploded[0];
                 $today_with_timeslot->minute = $time_exploded[1];
                 $today_with_timeslot->second = 0;
                 $end_of_slot = $today_with_timeslot->copy()->addMinutes($duration);
                 $end_of_slot->second = 59;
                 if (Appointment::where('dentist_id', $dentist_id)->where(function ($query) use($today_with_timeslot, $end_of_slot) {
                     $query->whereBetween('start_date', array($today_with_timeslot->format('Y-m-d H:i:s'), $end_of_slot->format('Y-m-d H:i:s')))->orWhereBetween('end_date', array($today_with_timeslot->format('Y-m-d H:i:s'), $end_of_slot->format('Y-m-d H:i:s')));
                 })->count() == 0 && ($end_of_slot->hour < 19 || $end_of_slot->hour == 19 && $end_of_slot->minute == 19)) {
                     $slot_is_good = true;
                     $interval_start = $today_with_timeslot->copy();
                     $interval_end = $end_of_slot->copy();
                     $duration = $interval_start->diffInMinutes($interval_end);
                     while ($interval_start->format('H:i') != $interval_end->format('H:i')) {
                         if (CalendarRestriction::where('day', $today_with_timeslot->format('l'))->where('appointment_type_id', '!=', 0)->where('time', $interval_start->format('H:i'))->where('appointment_type_id', '!=', $appointment_type)->count() > 0) {
                             $slot_is_good = false;
                             break;
                         }
                         $interval_start = $interval_start->copy()->addMinutes(5);
                     }
                     if ($slot_is_good) {
                         $slots[$slots_found]['start'] = $today_with_timeslot->copy()->format('H:i');
                         $slots[$slots_found]['end'] = $end_of_slot->copy()->format('H:i');
                         $slots[$slots_found]['date'] = $today->copy()->format('d-m-Y');
                         $slots[$slots_found]['dentist_id'] = $dentist_id;
                         $slots_found++;
                         $leave_clear = $duration / 5 - 1;
                         if ($slots_found == 12) {
                             break;
                             break;
                         }
                     }
                 }
             } else {
                 $leave_clear--;
             }
         }
         $today = $today->copy()->addDay();
     }
     $request->session()->put('find_slot_trigger_open', true);
     $request->session()->put('slots', $slots);
     // $request->session()->put('selected_calendar_day', $slot_date->format('Y-m-d H:i:s'));
     // $request->session()->put('selected_calendar_layout', 'week');
     // $request->session()->put('single_filtered_dentist', $dentist_id);
     // $request->session()->put('prefilled_start_time', $slot_start->format('H:i'));
     // $request->session()->put('prefilled_end_time', $slot_end->format('H:i'));
     // $request->session()->put('prefilled_date', $slot_end->format('d/m/Y'));
     if (!$override_redirect) {
         return redirect('dashboard');
     }
 }
Пример #2
0
 public function showDashboard(Request $request)
 {
     if (!$request->session()->has('selected_calendar_day')) {
         $request->session()->put('selected_calendar_day', Carbon::today()->format('Y-m-d H:i:s'));
     }
     if ($request->session()->has('selected_calendar_layout')) {
         $calendar_view = $request->session()->get('selected_calendar_layout');
     } else {
         $calendar_view = "day";
     }
     if ($request->session()->has('find_slot_trigger_open')) {
         $request->session()->forget('find_slot_trigger_open');
         $find_slot_trigger = true;
     } else {
         $find_slot_trigger = false;
     }
     if ($request->session()->has('slots')) {
         $slots = $request->session()->get('slots');
         $request->session()->forget('slots');
     } else {
         $slots = array();
     }
     $dentists = $this->getDentists($request);
     $data = array();
     $restrictions = CalendarRestriction::where('appointment_type_id', '!=', 0)->get();
     $data["restrictions_json"] = $restrictions->toJSON();
     if ($calendar_view == 'day') {
         $filtered_dentists = array();
         if ($request->session()->has('filtered_dentists')) {
             foreach ($request->session()->get('filtered_dentists') as $d) {
                 foreach ($dentists as $fd) {
                     if ($d->id == $fd->id) {
                         $filtered_dentists[] = $fd;
                     }
                 }
             }
         } else {
             $filtered_dentists = $dentists;
         }
         $calendar_range['start'] = Carbon::createFromFormat('Y-m-d H:i:s', $request->session()->get('selected_calendar_day'));
         $calendar_range['end'] = $calendar_range['start']->copy()->addDay();
         for ($i = 8; $i < 19; $i++) {
             if ($i < 10) {
                 $times[] = "0" . $i . ":00";
                 $times[] = "0" . $i . ":05";
                 $times[] = "0" . $i . ":10";
                 $times[] = "0" . $i . ":15";
                 $times[] = "0" . $i . ":20";
                 $times[] = "0" . $i . ":25";
                 $times[] = "0" . $i . ":30";
                 $times[] = "0" . $i . ":35";
                 $times[] = "0" . $i . ":40";
                 $times[] = "0" . $i . ":45";
                 $times[] = "0" . $i . ":50";
                 $times[] = "0" . $i . ":55";
             } else {
                 $times[] = $i . ":00";
                 $times[] = $i . ":05";
                 $times[] = $i . ":10";
                 $times[] = $i . ":15";
                 $times[] = $i . ":20";
                 $times[] = $i . ":25";
                 $times[] = $i . ":30";
                 $times[] = $i . ":35";
                 $times[] = $i . ":40";
                 $times[] = $i . ":45";
                 $times[] = $i . ":50";
                 $times[] = $i . ":55";
             }
             if ($i == 18) {
                 $times[] = "19:00";
             }
         }
         $appointments = array();
         $appointment_ids = array();
         foreach ($filtered_dentists as $dentist) {
             $current_day = $calendar_range['start']->copy();
             $end_date = $calendar_range['end']->copy();
             //Start from start of date range, add 1 day, get appointments for day, add day until end of range
             while ($current_day->ne($end_date)) {
                 $next_day = $current_day->copy()->addDay();
                 $appointment_list = DB::table('appointments')->select('id')->where('dentist_id', $dentist->id)->where('start_date', '>', $current_day->format('Y-m-d H:i:s'))->where('end_date', '<', $next_day->format('Y-m-d H:i:s'))->get();
                 foreach ($appointment_list as $appointment_id) {
                     $appointment_ids[] = $appointment_id->id;
                     $appointment = Appointment::findOrFail($appointment_id->id);
                     $appointments[$dentist->id][$current_day->format('Y-m-d')][] = $appointment;
                 }
                 $current_day = $current_day->addDay();
             }
         }
         $day_name = $calendar_range['start']->format('l');
         $restrictions = CalendarRestriction::where('day', $day_name)->where('appointment_type_id', '!=', 0)->get();
         $data['appointment_ids'] = $appointment_ids;
         $data["restrictions"] = $restrictions;
         $data["times"] = $times;
         $data["date_start"] = $calendar_range['start'];
         $data["filtered_dentists"] = $filtered_dentists;
     } elseif ($calendar_view == 'week') {
         for ($i = 8; $i < 19; $i++) {
             if ($i < 10) {
                 $times[] = "0" . $i . ":00";
                 $times[] = "0" . $i . ":05";
                 $times[] = "0" . $i . ":10";
                 $times[] = "0" . $i . ":15";
                 $times[] = "0" . $i . ":20";
                 $times[] = "0" . $i . ":25";
                 $times[] = "0" . $i . ":30";
                 $times[] = "0" . $i . ":35";
                 $times[] = "0" . $i . ":40";
                 $times[] = "0" . $i . ":45";
                 $times[] = "0" . $i . ":50";
                 $times[] = "0" . $i . ":55";
             } else {
                 $times[] = $i . ":00";
                 $times[] = $i . ":05";
                 $times[] = $i . ":10";
                 $times[] = $i . ":15";
                 $times[] = $i . ":20";
                 $times[] = $i . ":25";
                 $times[] = $i . ":30";
                 $times[] = $i . ":35";
                 $times[] = $i . ":40";
                 $times[] = $i . ":45";
                 $times[] = $i . ":50";
                 $times[] = $i . ":55";
             }
             if ($i == 18) {
                 $times[] = "19:00";
             }
         }
         $data["times"] = $times;
         $calendar_range['start'] = Carbon::createFromFormat('Y-m-d H:i:s', $request->session()->get('selected_calendar_day'))->startOfWeek();
         $calendar_range['end'] = $calendar_range['start']->copy()->endOfWeek();
         $tmp_start = $calendar_range['start']->copy();
         $tmp_end = $calendar_range['end']->copy();
         $days = array();
         $i = 0;
         while ($i < 7) {
             $days[$tmp_start->format('d')] = $tmp_start->format('l');
             $tmp_start->addDay();
             $i++;
         }
         $data['days'] = $days;
         $data["date_start"] = $calendar_range['start'];
         $data["date_end"] = $calendar_range['end'];
         if ($request->session()->has('single_filtered_dentist')) {
             $dentist = User::findOrFail($request->session()->get('single_filtered_dentist'));
         } else {
             $dentist = $dentists->first();
         }
         $current_day = $calendar_range['start']->copy();
         $end_date = $calendar_range['end']->copy();
         $restrictions = CalendarRestriction::where('appointment_type_id', '!=', 0)->get();
         $data["restrictions"] = $restrictions;
         $appointments = array();
         while ($current_day->day != $end_date->day) {
             $next_day = $current_day->copy()->addDay();
             $appointment_list = DB::table('appointments')->select('id')->where('dentist_id', $dentist->id)->where('start_date', '>', $current_day->format('Y-m-d H:i:s'))->where('end_date', '<', $next_day->format('Y-m-d H:i:s'))->get();
             foreach ($appointment_list as $appointment_id) {
                 $appointment = Appointment::findOrFail($appointment_id->id);
                 $appointments[$current_day->format('d')][] = $appointment;
             }
             $current_day = $current_day->addDay();
             $data["dentist"] = $dentist;
         }
         if ($request->session()->has('prefilled_start_time')) {
             $data['trigger_appointment_box_on_load'] = true;
             $data['prefilled_start_time'] = $request->session()->get('prefilled_start_time');
             $request->session()->forget('prefilled_start_time');
         }
         if ($request->session()->has('prefilled_end_time')) {
             $data['prefilled_end_time'] = $request->session()->get('prefilled_end_time');
             $request->session()->forget('prefilled_end_time');
         }
         if ($request->session()->has('prefilled_date')) {
             $data['prefilled_date'] = $request->session()->get('prefilled_date');
             $request->session()->forget('prefilled_date');
         }
     } elseif ($calendar_view == 'month') {
         $calendar_range['start'] = Carbon::createFromFormat('Y-m-d H:i:s', $request->session()->get('selected_calendar_day'))->startOfMonth();
         $calendar_range['end'] = $calendar_range['start']->copy()->endOfMonth();
         $data["date_start"] = $calendar_range['start'];
         $data["date_end"] = $calendar_range['end'];
         if ($request->session()->has('single_filtered_dentist')) {
             $dentist = User::findOrFail($request->session()->get('single_filtered_dentist'));
         } else {
             $dentist = $dentists->first();
         }
         $current_day = $calendar_range['start']->copy();
         $end_date = $calendar_range['end']->copy();
         $appointments = array();
         while ($current_day->day != $end_date->day) {
             $next_day = $current_day->copy()->addDay();
             $appointment_list = DB::table('appointments')->select('id')->where('dentist_id', $dentist->id)->where('start_date', '>', $current_day->format('Y-m-d H:i:s'))->where('end_date', '<', $next_day->format('Y-m-d H:i:s'))->get();
             foreach ($appointment_list as $appointment_id) {
                 $appointment = Appointment::findOrFail($appointment_id->id);
                 $appointments[$current_day->format('d')][] = $appointment;
             }
             $current_day = $current_day->addDay();
             $data["dentist"] = $dentist;
             $data["first_day_of_month"] = $calendar_range['start']->format('l');
         }
     }
     $data["find_slot_trigger"] = $find_slot_trigger;
     $data["slots"] = $slots;
     $data["dentists"] = $dentists;
     $data["calendar_view"] = $calendar_view;
     $data["appointments"] = $appointments;
     $data['today'] = Carbon::now();
     return view('backend.page.dashboard')->with($data);
 }