示例#1
0
 /**
  * Display the room schedules for a particular month/year - default this month.
  *
  * @param  int  $ym
  * @return \Illuminate\Http\Response
  */
 public function schedule($ym = null, $building = null)
 {
     //dd($ym);
     //
     if (!isset($ym) or $ym == 0) {
         $dt = Carbon::now();
         //dd($dt);
     } else {
         if (!($dt = Carbon::parse($ym))) {
             return view('404');
         }
     }
     $upcoming = clone $dt;
     $previous_dt = clone $dt;
     $path = url('rooms/' . $previous_dt->subDays(31)->format('Ymd'));
     $previous_link = '<a href="' . $path . '">&#171;</a>';
     $dts[0] = $dt;
     //dd($dts);
     for ($i = 1; $i <= 31; $i++) {
         $dts[$i] = Carbon::parse($upcoming->addDays(1));
     }
     $path = url('rooms/' . $upcoming->format('Ymd'));
     $next_link = '<a href="' . $path . '">&#187;</a>';
     //dd($dts);
     $rooms = \montserrat\Room::with('location')->get();
     //dd($rooms);
     //foreach ($rooms as $room) {
     //    $room->building = \montserrat\Location::findOrFail($room->building_id)->name;
     //}
     $roomsort = $rooms->sortBy(function ($room) {
         return sprintf('%-12s%s', $room->building_id, $room->name);
     });
     //dd($dts);
     $registrations_start = \montserrat\Registration::with('room', 'room.location', 'retreatant', 'retreat')->whereNull('canceled_at')->where('room_id', '>', 0)->whereHas('retreat', function ($query) use($dts) {
         $query->where('start_date', '>=', $dts[0])->where('start_date', '<=', $dts[30]);
     })->get();
     $registrations_end = \montserrat\Registration::with('room', 'room.location', 'retreatant', 'retreat')->whereNull('canceled_at')->where('room_id', '>', 0)->whereHas('retreat', function ($query) use($dts) {
         $query->where('end_date', '>=', $dts[0])->where('start_date', '<=', $dts[0]);
     })->get();
     //dd($registrations_start, $registrations_end);
     //$endregistrations = \montserrat\Registration::where('end','>=',$dts[0])->where('end','<=',$dts[30])->with('room','room.location','retreatant','retreat')->where('room_id','>',0)->get();
     /* get registrations that are not inclusive of the date range */
     // dd($endregistrations);
     // $registrations->merge($endregistrations);
     // create matrix of rooms and dates
     foreach ($rooms as $room) {
         foreach ($dts as $dt) {
             //dd($dt);
             $m[$room->id][$dt->toDateString()]['status'] = 'A';
             $m[$room->id][$dt->toDateString()]['registration_id'] = NULL;
             $m[$room->id][$dt->toDateString()]['retreatant_id'] = NULL;
             $m[$room->id][$dt->toDateString()]['retreatant_name'] = NULL;
             $m[$room->id]['room'] = $room->name;
             $m[$room->id]['building'] = $room->location->name;
             $m[$room->id]['occupancy'] = $room->occupancy;
         }
     }
     /* 
      * for each registration, get the number of days 
      * for each day, check if the status is set (in other words it is in the room schedule matrix)
      * if it is in the matrix update the status to reserved
      */
     foreach ($registrations_start as $registration) {
         $numdays = $registration->retreat->end_date->diffInDays($registration->retreat->start_date);
         for ($i = 0; $i <= $numdays; $i++) {
             $matrixdate = $registration->retreat->start_date->copy()->addDays($i);
             if (array_key_exists($matrixdate->toDateString(), $m[$registration->room_id])) {
                 $m[$registration->room_id][$matrixdate->toDateString()]['status'] = 'R';
                 if (!empty($registration->arrived_at) && empty($registration->departed_at)) {
                     $m[$registration->room_id][$matrixdate->toDateString()]['status'] = 'O';
                 }
                 $m[$registration->room_id][$matrixdate->toDateString()]['registration_id'] = $registration->id;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreatant_id'] = $registration->contact_id;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreatant_name'] = $registration->retreatant->display_name;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreat_name'] = $registration->retreat_name;
                 /* For now just handle marking the room as reserved with a URL to the registration and name in the title when hovering over it
                  * I am thinking about using diffInDays to see if the retreatant arrived on the day that we are looking at or sooner
                  * If they have not yet arrived then the first day should be reserved but not occupied. 
                  * Occupied will be the same link to the registration. 
                  */
             }
         }
     }
     foreach ($registrations_end as $registration) {
         $numdays = $registration->retreat->end_date->diffInDays($registration->retreat->start_date);
         for ($i = 0; $i <= $numdays; $i++) {
             $matrixdate = $registration->retreat->start_date->copy()->addDays($i);
             if (array_key_exists($matrixdate->toDateString(), $m[$registration->room_id])) {
                 $m[$registration->room_id][$matrixdate->toDateString()]['status'] = 'R';
                 if (!empty($registration->arrived_at) && empty($registration->departed_at)) {
                     $m[$registration->room_id][$matrixdate->toDateString()]['status'] = 'O';
                 }
                 $m[$registration->room_id][$matrixdate->toDateString()]['registration_id'] = $registration->id;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreatant_id'] = $registration->contact_id;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreatant_name'] = $registration->retreatant->display_name;
                 $m[$registration->room_id][$matrixdate->toDateString()]['retreat_name'] = $registration->retreat_name;
                 /* For now just handle marking the room as reserved with a URL to the registration and name in the title when hovering over it
                  * I am thinking about using diffInDays to see if the retreatant arrived on the day that we are looking at or sooner
                  * If they have not yet arrived then the first day should be reserved but not occupied. 
                  * Occupied will be the same link to the registration. 
                  */
             }
         }
     }
     //dd($m);
     return view('rooms.sched2', compact('dts', 'roomsort', 'm', 'previous_link', 'next_link'));
 }