public function assign_rooms($id)
 {
     //get this retreat's information
     $retreat = \montserrat\Retreat::with('retreatmasters', 'assistant', 'innkeeper', 'captains')->findOrFail($id);
     $registrations = \montserrat\Registration::where('event_id', '=', $id)->with('retreatant.parish')->orderBy('register_date', 'DESC')->get();
     $rooms = \montserrat\Room::orderby('name')->pluck('name', 'id');
     $rooms->prepend('Unassigned', 0);
     return view('retreats.assign_rooms', compact('retreat', 'registrations', 'rooms'));
 }
 public function edit_group($id)
 {
     //
     $registration = \montserrat\Registration::with('retreatant', 'retreat', 'room')->findOrFail($id);
     //        $retreat = \montserrat\Retreat::findOrFail($registration->event_id);
     $retreatant = \montserrat\Contact::findOrFail($registration->contact_id);
     $retreats = \montserrat\Retreat::select(\DB::raw('CONCAT(idnumber, "-", title, " (",DATE_FORMAT(start_date,"%m-%d-%Y"),")") as description'), 'id')->where("end_date", ">", \Carbon\Carbon::today())->orderBy('start_date')->pluck('description', 'id');
     //dd($retreats);
     //TODO: we will want to be able to switch between types when going from a group registration to individual room assignment
     if ($retreatant->contact_type == CONTACT_TYPE_INDIVIDUAL) {
         $retreatants = \montserrat\Contact::whereContactType(CONTACT_TYPE_INDIVIDUAL)->orderBy('sort_name')->pluck('sort_name', 'id');
     }
     if ($retreatant->contact_type == CONTACT_TYPE_ORGANIZATION) {
         $retreatants = \montserrat\Contact::whereContactType(CONTACT_TYPE_ORGANIZATION)->whereSubcontactType($retreatant->subcontact_type)->orderBy('sort_name')->pluck('sort_name', 'id');
     }
     $rooms = \montserrat\Room::orderby('name')->pluck('name', 'id');
     $rooms->prepend('Unassigned', 0);
     /* Check to see if the current registration is for a past retreat and if so, add it to the collection */
     // $retreats[0] = 'Unassigned';
     if ($registration->retreat->end < \Carbon\Carbon::now()) {
         $retreats[$registration->event_id] = $registration->retreat->idnumber . '-' . $registration->retreat->title . " (" . date('m-d-Y', strtotime($registration->retreat->start_date)) . ")";
     }
     return view('registrations.edit_group', compact('registration', 'retreats', 'rooms', 'retreatants'));
 }
 /**
  * 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'));
 }