/**
  * Returns the caller to the scheduling page, showing current
  */
 public function schedule()
 {
     $shifts = DB::table('shift_defination')->get();
     $nextSunday = date("Y-m-d", strtotime('next sunday'));
     Log::info('user ID is ' . Auth::user()->id);
     $nextSunday = date("Y-m-d", strtotime('next sunday'));
     $date = new \DateTime($nextSunday);
     $weekNumber = $date->format('W');
     Log::info('->>weekNumber is ' . $weekNumber);
     Log::info('\\n\\n\\n\\n');
     $caller_shifts = \App\CallerShift::where('user_id', Auth::user()->id)->where('weeknumber', $weekNumber)->get();
     Log::info('::::::::::::shifts is ' . $caller_shifts);
     $currentShifts = DB::table('caller_shifts')->select('shift_id', DB::raw('count(*) as total'))->where('weeknumber', $weekNumber)->whereNull('deleted_at')->groupBy('shift_id')->get();
     $shiftAvailability = array();
     Log::info($currentShifts);
     foreach ($currentShifts as $shift) {
         $shiftInfo = new \stdClass();
         $shiftInfo->total = $shift->total;
         $shiftInfo->text = 'Planned';
         $shiftAvailability[$shift->shift_id] = $shiftInfo;
     }
     return view('schedule', ['page' => 'schedule', 'shifts' => $shifts, 'mailed' => false, 'saved' => false, 'shiftAvailability' => $shiftAvailability, 'caller_shifts' => $caller_shifts]);
 }
 public function scheduleShifts()
 {
     // Get the current user
     $user = Auth::user();
     $nextSunday = date("Y-m-d", strtotime('next sunday'));
     $date = new \DateTime($nextSunday);
     $week = $date->format("W");
     $year = $date->format("Y");
     // Get all the selected shifts
     $inputs = Request::all();
     foreach ($inputs as $key => $shift) {
         if ($key == '_token') {
             continue;
         }
         $CallerShift = new \App\CallerShift();
         $CallerShift->user_id = $user->id;
         $CallerShift->shift_id = substr($key, 6);
         Log::info($key . '->' . $shift);
         // The shift is selected by the user
         if ($shift == 1) {
             $CallerShift->weekNumber = $week;
             $CallerShift->year = $year;
             Log::info('shift is ' . substr($key, 6));
             $shiftPresent = \App\CallerShift::withTrashed()->where('user_id', Auth::user()->id)->where('shift_id', substr($key, 6))->where('weekNumber', $week)->where('year', $year)->get();
             // Saving only if the shift not present
             // The shift is not present earlier, thereby saving the shift
             if ($shiftPresent->count() == 0) {
                 Log::info('need to save the shift');
                 $CallerShift->save();
             } else {
                 if ($shiftPresent[0]->trashed()) {
                     Log::info('need to restore the shift' . $shiftPresent[0]->id);
                     $shiftPresent[0]->restore();
                 }
             }
         } else {
             Log::info('unselected' . substr($key, 6));
             Log::info("Weeknummer: {$week}");
             Log::info("Year: {$year}");
             Log::info("User: "******"Shift: " . substr($key, 6));
             $shiftPresent = \App\CallerShift::withTrashed()->where('user_id', Auth::user()->id)->where('shift_id', substr($key, 6))->where('weekNumber', $week)->where('year', $year)->get();
             Log::info('count ' . $shiftPresent->count());
             // Deleting it if the shift not present in the table
             if ($shiftPresent->count() != 0) {
                 Log::info('deleting the shift' . $shiftPresent[0]->id);
                 $shiftPresent[0]->delete();
             }
         }
     }
     $currentShifts = DB::table('caller_shifts')->select('shift_id', DB::raw('count(*) as total'))->where('weeknumber', $week)->whereNull('deleted_at')->groupBy('shift_id')->get();
     $shiftAvailability = array();
     foreach ($currentShifts as $shift) {
         $shiftInfo = new \stdClass();
         $shiftInfo->total = $shift->total;
         $shiftInfo->text = 'Planned';
         $shiftAvailability[$shift->shift_id] = $shiftInfo;
     }
     $caller_shifts = \App\CallerShift::where('user_id', Auth::user()->id)->where('weeknumber', $week)->whereNull('deleted_at')->get();
     Log::info('no. of shifts - ' . $caller_shifts->count());
     $shifts = DB::table('shift_defination')->get();
     $confirmationEmail = Request::input('confirmationEmail');
     $mailed = false;
     if ($confirmationEmail == 'on') {
         $mailed = true;
         $managerEmail = DB::table('preferences')->where('name', 'managerEmail')->get()[0]->value;
         Log::info($managerEmail);
         $shiftScheduled = DB::table('caller_shifts')->join('shift_defination', 'caller_shifts.shift_id', '=', 'shift_defination.id')->where('caller_shifts.user_id', Auth::user()->id)->where('caller_shifts.weeknumber', $week)->whereNull('caller_shifts.deleted_at')->select(DB::raw('substr(shift_defination.name, 1, 3) as name'), 'duration', 'shift_start')->get();
         $dayMapping = array('sun' => 'sunday', 'mon' => 'monday', 'tue' => 'tuesday', 'wed' => 'wednesday', 'thr' => 'thursday', 'fri' => 'friday', 'sat' => 'saturday');
         $sno = 1;
         $confirmationShifts = array();
         $dayWiseShifts = array();
         foreach ($shiftScheduled as $shift) {
             if (array_key_exists($shift->name, $dayWiseShifts)) {
                 $selectedShifts = $dayWiseShifts[$shift->name];
                 array_push($selectedShifts, \Carbon\Carbon::parse($shift->shift_start)->format('h:i A'));
                 $dayWiseShifts[$shift->name] = $selectedShifts;
             } else {
                 $dayWiseShifts[$shift->name] = array(\Carbon\Carbon::parse($shift->shift_start)->format('h:i A'));
             }
         }
         $daysSeen = array();
         $totalShifts = 0;
         foreach ($shiftScheduled as $shift) {
             $callerShift = new \stdClass();
             $callerShift->sno = $sno++;
             $callerShift->date = date("D d F Y", strtotime('next ' . $dayMapping[strtolower($shift->name)]));
             $callerShift->shift = $dayWiseShifts[$shift->name];
             // Skipping the day already covered
             if (!in_array($callerShift->date, $daysSeen)) {
                 $totalShifts += count($callerShift->shift);
                 array_push($daysSeen, $callerShift->date);
                 array_push($confirmationShifts, $callerShift);
             }
         }
         $data = array('name' => Auth::user()->name, 'shifts' => $confirmationShifts, 'managerEmail' => $managerEmail, 'totalShifts' => $totalShifts);
         \Mail::send('mail.shiftConfirmation', $data, function ($message) use($data) {
             $message->subject('Shift Confirmation - ' . Auth::user()->name)->to($data['managerEmail']);
         });
         Log::info('Sending an email to the manager');
     }
     return view('schedule', ['page' => 'manage-schedule', 'shifts' => $shifts, 'saved' => true, 'mailed' => $mailed, 'caller_shifts' => $caller_shifts, 'shiftAvailability' => $shiftAvailability]);
 }