/** CODE VALIDATIE **/
 public function store(Request $request)
 {
     $winners = DB::table('winners')->get();
     if (Auth::check()) {
         /* Code opslagen voor controle bij admin */
         $code = new Code();
         $code->code = Input::get('code');
         $code->FK_user_id = Auth::user()->id;
         $code->save();
         $validCodes = DB::table('validCodes')->get();
         foreach ($validCodes as $validCode) {
             if (Hash::check(Input::get('code'), $validCode->validCode)) {
                 /*het is een valid code */
                 if ($validCode->FK_user_id == 0) {
                     /* het is een valid code die nog niet is ingevoerd */
                     /*                     $validCodeid = $validCode->id;
                                          $validCode = Validcode::find($validCodeid);*/
                     $lotteryImg = '';
                     // variabele meesturen om javascript te laten runnen
                     /* Kijken of het een winnende code is */
                     if ($validCode->winning1_losing0 == 0) {
                         $lotteryImg = 'lose';
                     }
                     if ($validCode->winning1_losing0 == 1) {
                         $lotteryImg = 'win';
                         $winner = new Winner();
                         $winner->winningMonth = Carbon::now()->month;
                         $winner->FK_user_id = Auth::user()->id;
                         $winner->name = Auth::user()->name;
                         $winner->city = Auth::user()->city;
                         $winner->save();
                     }
                     //Code updaten
                     $specificValidCode = Validcode::find($validCode->id);
                     $specificValidCode->FK_user_id = Auth::user()->id;
                     $specificValidCode->save();
                     $jumpSectionA = '';
                     // variabele meesturen om javascript te laten runnen
                     return view('index')->with('lotteryImg', $lotteryImg)->with('jumpSectionA', $jumpSectionA)->with('winners', $winners);
                 } else {
                     /* het is een validcode die al is gebruikt */
                     $jumpSectionA = '';
                     // variabele meesturen om javascript te laten runnen
                     $usedcodeMessage = 'Deze code is al reeds gebruikt !';
                     return view('index')->with('usedcodeMessage', $usedcodeMessage)->with('jumpSectionA', $jumpSectionA)->with('winners', $winners);
                 }
             }
         }
         /* Alle codes zijn gecheckt dus het is geen valid code */
         $wrongcodeMessage = 'Dit is geen juiste code!';
         $jumpSectionA = '';
         // variabele meesturen om javascript te laten runnen
         return view('index')->with('wrongcodeMessage', $wrongcodeMessage)->with('jumpSectionA', $jumpSectionA)->with('winners', $winners);
     } else {
         $message = 'Je moet inloggen of registreren om een code toe te voegen!';
         return view('index')->with('message', $message)->with('winners', $winners);
     }
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // \Log::info('I was here @' . Carbon::now() );
     $thisDate = Date::where('endDate', '<', Carbon::now())->orderby('endDate', 'DESC')->first();
     $competitors = Competitor::where('created_at', '>', $thisDate->startDate)->where('created_at', '<', $thisDate->endDate)->where('is_deleted', '=', false)->get();
     if (!count($thisDate->winner) && count($competitors)) {
         // var_dump($competitors);
         $winner = $competitors->first();
         foreach ($competitors as $competitor) {
             var_dump($competitor->getTotalVotes());
             echo '</br>';
             if ($competitor->getTotalVotes() > $winner->getTotalVotes()) {
                 $winner = $competitor;
             }
         }
         $newWinner = new Winner();
         $newWinner->competitor_id = $winner->id;
         $newWinner->date_id = $thisDate->id;
         $newWinner->save();
     }
 }
 public function test()
 {
     $thisDate = Date::where('endDate', '<', Carbon::now())->orderby('endDate', 'DESC')->first();
     $competitors = Competitor::where('created_at', '>', $thisDate->startDate)->where('created_at', '<', $thisDate->endDate)->get();
     if (!count($thisDate->winner) && count($competitors)) {
         // var_dump($competitors);
         $winner = $competitors->first();
         foreach ($competitors as $competitor) {
             var_dump($competitor->getTotalVotes());
             echo '</br>';
             if ($competitor->getTotalVotes() > $winner->getTotalVotes()) {
                 $winner = $competitor;
             }
         }
         $newWinner = new Winner();
         $newWinner->competitor_id = $winner->id;
         $newWinner->date_id = $thisDate->id;
         $newWinner->save();
     }
     // $thisDate = Date::where('endDate', '<', Carbon::now())->orderby('endDate', 'DESC')->first();
     // var_dump($thisDate);
     // echo '</br>';
     // if (count($thisDate->winner))
     // {
     //     echo 'er is reeds een winnaar';
     // } else
     // {
     //     echo 'er is nog geen winnaar';
     // }
 }
 public function postCode(Request $request)
 {
     //validate input
     $this->validate($request, ['code' => 'required|size:10']);
     // search for code
     $code = WinningCode::withTrashed()->where('code', $request->code)->first();
     $user = Auth::user();
     $username = $user->name;
     $userPoints = $user->points;
     $rewards = Reward::orderBy('created_at', 'desc')->take(8)->get();
     //check if code is a bonus code
     if ($code && $code->used == 0 && $code->deleted_at == null) {
         $title = 'Dashboard';
         $usedCode = new UsedCode();
         $winner = new Winner();
         $codeValue = $code->value;
         //check if user used bonus code
         if ($code->bonus == 1) {
             if ($user->bonus == 0) {
                 // add points to account + save that user used bonus code
                 $userPoints = $userPoints + $codeValue;
                 $user->points = $userPoints;
                 $user->bonus = 1;
                 $usedCode->code = $code->code;
                 $usedCode->value = $code->value;
                 $usedCode->user()->associate($user);
                 // new winner
                 $winner->name = $user->name;
                 $winner->value = $code->value;
                 $winner->period = Period::where('running', 1)->first()->id;
                 $winner->user()->associate($user);
                 $winner->save();
                 $user->save();
                 $usedCode->save();
                 $success = true;
                 return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'success', 'codeValue'));
             } else {
                 $success = false;
                 $codeError = 'You have already used your free code.';
                 return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             }
         }
         // add points to account + save that code is used
         $code->used = 1;
         $code->save();
         $userPoints = $userPoints + $codeValue;
         $user->points = $userPoints;
         $usedCode->code = $code->code;
         $usedCode->value = $code->value;
         $usedCode->user()->associate($user);
         // new winner
         $winner->name = $user->name;
         $winner->value = $code->value;
         $winner->period = Period::where('running', 1)->first()->id;
         $winner->user()->associate($user);
         $winner->save();
         $user->save();
         $usedCode->save();
         $success = true;
         return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'success', 'codeValue'));
     } else {
         //check if code is used before
         if ($code && $code->used == 1 && $code->deleted_at == null) {
             $success = false;
             $codeError = 'This code is alreay used and can only be used once';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             //check if code is deleted
         } elseif ($code && $code->deleted_at != null) {
             $success = false;
             $codeError = 'This code is suspended';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
             // code does not exist
         } else {
             $success = false;
             $codeError = 'This code does not exist, did you make a mistake?';
             return view('dashboard.code.result', compact('title', 'username', 'userPoints', 'rewards', 'codeError', 'success'));
         }
     }
 }