/**
  * @Post("reservar/{id}")
  */
 public function doReserve(Request $request, $id)
 {
     // 1. Comprobar que el usuario tenga una tarifa (si no tiene no tiene sentido hacer mas comprobaciones)
     if (\Auth::user()->payment == null && \Auth::user()->bonus == null) {
         Session::flash('message-error', 'No tiene ninguna tarifa o bono asignada. Por favor contacte con administración de su centro');
         return Redirect::to("horario");
     }
     // 2. Comprobar que la tarifa del usuario no ha caducado aun
     $paymentDay = \Auth::user()->payment_day;
     $paymentDaySegmented = explode('-', $paymentDay);
     $limitToPay = date_create($paymentDaySegmented[0] . "-" . ($paymentDaySegmented[1] + 1) . "-7");
     $today = date_create(date('Y-m-d', strtotime("now")));
     $aWeek = array('M' => 1, 'T' => 2, 'W' => 3, 'Tu' => 4, 'F' => 5, 'S' => 6, 'Su' => 7);
     $event = ClaseSchedule::find($id);
     $todayWeek = date('N');
     $reserveDay = $aWeek[$event->date];
     $daysBetween = $this->subtractDate($todayWeek, $reserveDay);
     $reserveDate = date('Y-m-d', strtotime("now + " . $daysBetween . " day"));
     if ($today > $limitToPay) {
         //2.1 Comprobar que el usuario tenga bono, si no tiene tarifa
         if (\Auth::user()->bonus != null) {
             if (\Auth::user()->bonus_days == 0) {
                 Session::flash('message-error', 'Su tarifa ha caducado y su bono no tiene clases restantes. Por favor contacte con administración de su centro');
                 return Redirect::to("horario");
             } else {
                 $user = User::find(\Auth::user()->id);
                 $user->bonus_days = $user->bonus_days - 1;
                 $user->save();
                 $reserve = new Reserve();
                 $reserve->user_id = $user->id;
                 $reserve->event_id = $id;
                 $reserve->reserve_day = $reserveDate;
                 $reserve->save();
                 Session::flash('message-success', 'Reserva realizada con exito');
                 return Redirect::to("horario");
             }
         } else {
             Session::flash('message-error', 'Su tarifa ha caducado. Por favor contacte con administración de su centro');
             return Redirect::to("horario");
         }
     }
     // 3. Comprobar que el usuario no ha reservado mas clases de las que puede reservar en esa semana
     $limiInf = strtotime($reserveDate);
     while (date('w', $limiInf) > 1) {
         $limiInf -= 86400;
     }
     $limitSup = date('Y-m-d', $limiInf + 6 * 86400);
     $limiInf = date('Y-m-d', $limiInf);
     $reservesDone = Reserve::where('user_id', \Auth::user()->id)->whereBetween('reserve_day', [$limiInf, $limitSup])->count();
     if (\Auth::user()->payment == null || \Auth::user()->payment->weekReserves - $reservesDone == 0) {
         if (\Auth::user()->bonus != null) {
             if (\Auth::user()->bonus_days == 0) {
                 Session::flash('message-error', 'Ha agotado las reservas de su tarifa semanal y su bono no tiene clases restantes. Por favor contacte con administración de su centro');
                 return Redirect::to("horario");
             } else {
                 $user = User::find(\Auth::user()->id);
                 $user->bonus_days = $user->bonus_days - 1;
                 $user->save();
                 $reserve = new Reserve();
                 $reserve->user_id = $user->id;
                 $reserve->event_id = $id;
                 $reserve->reserve_day = $reserveDate;
                 $reserve->save();
                 Session::flash('message-success', 'Reserva realizada con exito con su bono');
                 return Redirect::to("horario");
             }
         }
         Session::flash('message-error', 'Ha agotado las reservas de su tarifa semanal.');
         return Redirect::to("horario");
     } else {
         //4. Comprobar que el usuario no haya reservado ya esa clase
         if (Reserve::where('user_id', \Auth::user()->id)->where('reserve_day', $reserveDate)->where('event_id', $id)->count() > 0) {
             Session::flash('message-error', 'Ya ha reservado esta clase.');
             return Redirect::to("horario");
         } else {
             if (Reserve::where('user_id', \Auth::user()->id)->where('reserve_day', $reserveDate)->count() > 0) {
                 Session::flash('message-error', 'Lo sentimos, su tarifa solo le permite reservar ' . \Auth::user()->payment->dayReserves . ' vez por día');
                 return Redirect::to("horario");
             } else {
                 $reserve = new Reserve();
                 $reserve->user_id = \Auth::user()->id;
                 $reserve->event_id = $id;
                 $reserve->reserve_day = $reserveDate;
                 $reserve->save();
                 Session::flash('message-success', 'Reserva realizada con exito');
                 return Redirect::to("horario");
             }
         }
     }
 }
 /**
  * @Get("horario")
  */
 public function showSchedule()
 {
     $aWeek = array('M' => 1, 'T' => 2, 'W' => 3, 'Tu' => 4, 'F' => 5, 'S' => 6, 'Su' => 7);
     $aOcuppied = array();
     $days = array(1 => "", 2 => "", 3 => "", 4 => "", 5 => "", 6 => "", 7 => "");
     $schedule = Schedule::where('active', 1)->get()->first();
     $events = ClaseSchedule::where('schedule_id', $schedule->id)->orderBy('date')->orderBy('startTime')->get();
     foreach ($events as $event) {
         $todayWeek = date('N');
         $reserveDay = $aWeek[$event->date];
         $daysBetween = $this->subtractDate($todayWeek, $reserveDay);
         $reserveDate = date('Y-m-d', strtotime("now + " . $daysBetween . " day"));
         $placesOcuppied = Reserve::where('reserve_day', $reserveDate)->where('event_id', $event->id)->count();
         $aOcuppied[$event->date][$event->id] = $event->capMax - $placesOcuppied;
         if ($days[$aWeek[$event->date]] == "") {
             $dateExplode = explode('-', $reserveDate);
             $days[$aWeek[$event->date]] = $dateExplode[2] . "/" . $dateExplode[1] . "/" . $dateExplode[0];
         }
     }
     // foreach ($days as $key => $value) {
     //     if($value == "")
     //         $days[$key] = date('Y-m-d', strtotime($days[($key-1 == 0)?7:$key-1]." + 1 day"));
     // }
     return view('schedule.viewSchedule', ['schedule' => $schedule, 'clases' => Clase::lists('name', 'id'), 'schedules' => Schedule::lists('name', 'id'), 'rooms' => Room::lists('name', 'id'), 'instructors' => User::where('type_id', 2)->lists('name', 'id'), 'places' => $aOcuppied, 'events' => $events, 'days' => $days]);
 }
 /**
  * @Get("user/profile/{id}/{tab?}")
  */
 public function profile($id, $tab = 1)
 {
     $today = date('Y-m-d', strtotime('now'));
     $reserves = Reserve::where('user_id', $id)->where('reserve_day', '>', $today)->orderBy('reserve_day')->get();
     $allReserves = Reserve::where('user_id', $id)->orderBy('reserve_day')->get();
     $user = User::find($id);
     $user_exercises = $user->exercise;
     $aData = null;
     $tipos = ActivityType::all();
     foreach ($tipos as $tipo) {
         foreach ($tipo->exercises as $exercise) {
             $flag = false;
             foreach ($user_exercises as $exer) {
                 if ($exer->id == $exercise->id) {
                     $aData['' . $tipo->name][] = [$exercise, $exer->pivot->number];
                     $flag = true;
                 }
             }
             if ($flag == false) {
                 $aData['' . $tipo->name][] = [$exercise, '-'];
             }
         }
     }
     $limiInf = strtotime('now');
     while (date('w', $limiInf) > 1) {
         $limiInf -= 86400;
     }
     $limitSup = date('Y-m-d', $limiInf + 6 * 86400);
     $limiInf = date('Y-m-d', $limiInf);
     $reservesDone = Reserve::where('user_id', \Auth::user()->id)->whereBetween('reserve_day', [$limiInf, $limitSup])->count();
     return view('user.profile', ['user' => $user, 'tipos' => $aData, 'tab' => $tab, 'reserves' => $reserves, 'allReserves' => $allReserves, 'leftOverReserves' => $user->payment == null ? "" : $user->payment->weekReserves - $reservesDone]);
 }