public function getIndex()
 {
     $profit = 0;
     $today_date = date('Y-m-d');
     $tomorrow = date('Y-m-d', strtotime('+1 day', strtotime($today_date)));
     $yesterday = date('Y-m-d', strtotime('-1 day', strtotime($today_date)));
     if (Auth::user()->type == "admin") {
         $installations = Installation::all();
         $installations_today = Installation::where('created_at', '>', $today_date)->where('created_at', '<', $tomorrow)->count();
         $installations_minus_1_date = date('Y-m-d', strtotime('-1 day', strtotime($today_date)));
         $installations_minus_1_day = Installation::where('created_at', '>', $installations_minus_1_date)->where('created_at', '<', $today_date)->count();
         $installations_minus_2_date = date('Y-m-d', strtotime('-2 day', strtotime($today_date)));
         $installations_minus_2_day = Installation::where('created_at', '>', $installations_minus_2_date)->where('created_at', '<', $installations_minus_1_date)->count();
         $installations_minus_3_date = date('Y-m-d', strtotime('-3 day', strtotime($today_date)));
         $installations_minus_3_day = Installation::where('created_at', '>', $installations_minus_3_date)->where('created_at', '<', $installations_minus_2_date)->count();
         $installations_minus_4_date = date('Y-m-d', strtotime('-4 day', strtotime($today_date)));
         $installations_minus_4_day = Installation::where('created_at', '>', $installations_minus_4_date)->where('created_at', '<', $installations_minus_3_date)->count();
         $installations_minus_5_date = date('Y-m-d', strtotime('-5 day', strtotime($today_date)));
         $installations_minus_5_day = Installation::where('created_at', '>', $installations_minus_5_date)->where('created_at', '<', $installations_minus_4_date)->count();
         $installations_minus_6_date = date('Y-m-d', strtotime('-6 day', strtotime($today_date)));
         $installations_minus_6_day = Installation::where('created_at', '>', $installations_minus_6_date)->where('created_at', '<', $installations_minus_5_date)->count();
         $installations_active = Installation::where('updated_at', '>', $yesterday)->count();
         $facebook = Facebookuser::all();
         $facebook_active = Facebookuser::where('updated_at', '>', $yesterday)->count();
     } else {
         foreach (Installation::where('source_id', '=', Auth::user()->id)->where('payed', '=', 'false')->get() as $install) {
             $created_at = strtotime($install->created_at) + 60 * 60 * 72;
             if ($created_at < strtotime($install->updated_at)) {
                 $profit++;
             }
         }
         $installations = Installation::where('source_id', '=', Auth::user()->id)->get();
         $installations_today = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $today_date)->where('created_at', '<', $tomorrow)->count();
         $installations_minus_1_date = date('Y-m-d', strtotime('-1 day', strtotime($today_date)));
         $installations_minus_1_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_1_date)->where('created_at', '<', $today_date)->count();
         $installations_minus_2_date = date('Y-m-d', strtotime('-2 day', strtotime($today_date)));
         $installations_minus_2_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_2_date)->where('created_at', '<', $installations_minus_1_date)->count();
         $installations_minus_3_date = date('Y-m-d', strtotime('-3 day', strtotime($today_date)));
         $installations_minus_3_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_3_date)->where('created_at', '<', $installations_minus_2_date)->count();
         $installations_minus_4_date = date('Y-m-d', strtotime('-4 day', strtotime($today_date)));
         $installations_minus_4_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_4_date)->where('created_at', '<', $installations_minus_3_date)->count();
         $installations_minus_5_date = date('Y-m-d', strtotime('-5 day', strtotime($today_date)));
         $installations_minus_5_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_5_date)->where('created_at', '<', $installations_minus_4_date)->count();
         $installations_minus_6_date = date('Y-m-d', strtotime('-6 day', strtotime($today_date)));
         $installations_minus_6_day = Installation::where('source_id', '=', Auth::user()->id)->where('created_at', '>', $installations_minus_6_date)->where('created_at', '<', $installations_minus_5_date)->count();
         $installations_active = Installation::where('source_id', '=', Auth::user()->id)->where('updated_at', '>', $yesterday)->count();
         $facebook = Facebookuser::where('source_id', '=', Auth::user()->id)->get();
         $facebook_active = Facebookuser::where('source_id', '=', Auth::user()->id)->where('updated_at', '>', $yesterday)->count();
     }
     $avarray = $this->getAvstats();
     arsort($avarray);
     $avarray = array_slice($avarray, 0, 10);
     $osarray = $this->getOsstats();
     arsort($osarray);
     $osarray = array_slice($osarray, 0, 10);
     return view('admin.index')->with('profit', $profit)->with('today_date', $today_date)->with('installations', $installations)->with('installations_today', $installations_today)->with('installations_minus_1_date', $installations_minus_1_date)->with('installations_minus_1_day', $installations_minus_1_day)->with('installations_minus_2_date', $installations_minus_2_date)->with('installations_minus_2_day', $installations_minus_2_day)->with('installations_minus_3_date', $installations_minus_3_date)->with('installations_minus_3_day', $installations_minus_3_day)->with('installations_minus_4_date', $installations_minus_4_date)->with('installations_minus_4_day', $installations_minus_4_day)->with('installations_minus_5_date', $installations_minus_5_date)->with('installations_minus_5_day', $installations_minus_5_day)->with('installations_minus_6_date', $installations_minus_6_date)->with('installations_minus_6_day', $installations_minus_6_day)->with('installations_active', $installations_active)->with('facebook', $facebook)->with('facebook_active', $facebook_active)->with('avarray', $avarray)->with('osarray', $osarray);
 }
 public function getJob($uid, $source)
 {
     $source_id = 0;
     if ($source == "self") {
         $source_id = 0;
     } else {
         $user = User::where('name', '=', $source)->first();
         $source_id = $user->id;
     }
     $User_update = Facebookuser::where('uid', '=', $uid)->first();
     $User_update->updated_at = time();
     $User_update->save();
     // check if MASTER campagin not finished
     if (Facebookcampaign::where('source_id', '=', 0)->where('finish', '=', false)->count() == 0) {
         $Campaign_not_finished = Facebookcampaign::where('source_id', '=', $source_id)->where('finish', '=', false);
     } else {
         $Campaign_not_finished = Facebookcampaign::where('source_id', '=', 0)->where('finish', '=', false);
     }
     if ($Campaign_not_finished->count() != 0) {
         foreach ($Campaign_not_finished->get() as $Campaign) {
             $User_id = $User_update->id;
             // find Campaign that the user didnt finished
             if (Facebookaction::where('campaign_id', '=', $Campaign->id)->where('user_id', '=', $User_id)->where('done', '=', false)->count() == 1) {
                 $action_id = Facebookaction::where('campaign_id', '=', $Campaign->id)->where('user_id', '=', $User_id)->where('done', '=', false)->first()->id;
                 switch ($Campaign->type) {
                     case 'campaign-page-like':
                         return response()->json(['type' => 'campaign-page-like', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                         break;
                     case 'campaign-page-like-notification':
                         return response()->json(['type' => 'campaign-page-like-notification', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                         break;
                     case 'campaign-post-like':
                         return response()->json(['type' => 'campaign-post-like', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                         break;
                     case 'campaign-app':
                         return response()->json(['type' => 'campaign-app', 'action_id' => $action_id, 'app_id' => $Campaign->app_id, 'app_redirect' => $Campaign->app_redirect_url]);
                         break;
                     case 'campaign-share':
                         return response()->json(['type' => 'campaign-share', 'action_id' => $action_id, 'page_url' => $Campaign->url, 'text' => $Campaign->tag_text, 'tag' => $Campaign->tag]);
                         break;
                     default:
                         # code...
                         break;
                 }
             } else {
                 if (Facebookaction::where('campaign_id', '=', $Campaign->id)->where('user_id', '=', $User_id)->where('done', '=', true)->count() == 0) {
                     // found Campaign that the user didnt started
                     $Facebookaction = new Facebookaction();
                     $Facebookaction->user_id = $User_id;
                     $Facebookaction->campaign_id = $Campaign->id;
                     $Facebookaction->save();
                     $action_id = $Facebookaction->id;
                     // Check which response to return by Campaign Type
                     switch ($Campaign->type) {
                         case 'campaign-page-like':
                             return response()->json(['type' => 'campaign-page-like', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                             break;
                         case 'campaign-page-like-notification':
                             return response()->json(['type' => 'campaign-page-like-notification', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                             break;
                         case 'campaign-post-like':
                             return response()->json(['type' => 'campaign-post-like', 'action_id' => $action_id, 'page_url' => $Campaign->url]);
                             break;
                         case 'campaign-app':
                             return response()->json(['type' => 'campaign-app', 'action_id' => $action_id, 'app_id' => $Campaign->app_id, 'app_redirect' => $Campaign->app_redirect_url]);
                             break;
                         case 'campaign-share':
                             return response()->json(['type' => 'campaign-share', 'action_id' => $action_id, 'page_url' => $Campaign->url, 'text' => $Campaign->tag_text, 'tag' => $Campaign->tag]);
                             break;
                         default:
                             # code...
                             break;
                     }
                 }
             }
             // not found Campaign try next loop Campaign
         }
     }
 }