/** * A list of Sites a user attended on a certain date * @param $date * @return mixed */ public function attendSitesOnDate($date) { return SiteAttendance::whereDate('date', $date)->where('user_id', $this->id)->get(); }
/** * Get Weekly Planner for date */ public function getWeeklyPlan(Request $request, $date, $super_id) { $plan_type = 'weekly'; // As Weekly + Trade Planner call this function we can determin which one it is by provided 'super_id' if ($super_id == 'alltrade') { $super_id = 'all'; $plan_type = 'trade'; } if (Auth::user()->company->subscription) { if ($super_id == 'all') { $allowedSites = Auth::user()->company->reportsToCompany()->siteList('1')->pluck('id')->toArray(); } else { //if (User::find($super_id)->is('supervisor')) $allowedSites = DB::table('supervisor_site')->select('site_id')->where('user_id', $super_id)->lists('site_id'); } } else { $allowedSites = Auth::user()->company->sitePlannerList('1')->pluck('id')->toArray(); } $date_from = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00'); $date_to = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00')->addDays(7); //$date_to2 = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00')->addDays(7); // // Full Plan // $planner = $this->getPlannerForWeek($date_from, $date_to, $allowedSites, []); $fullplan = []; foreach ($planner as $plan) { $fullplan[] = $this->getPlanData($plan); } // // Non Rostered Users who attended // $non_rostered = []; $attendance = SiteAttendance::whereDate('date', '>=', $date_from->format('Y-m-d'))->whereDate('date', '<=', $date_to->format('Y-m-d'))->get(); foreach ($attendance as $attend) { $site = Site::find($attend->site_id); if (!$site->isUserOnRoster($attend->user_id, $attend->date->format('Y-m-d'))) { $user = User::find($attend->user_id); // For non subscription companies limit to their users on if (Auth::user()->company->subscription || $user->isCompany(Auth::user()->company)) { $key = $attend->site_id . '.' . $attend->date->format('Y-m-d'); if (isset($non_rostered[$key])) { $non_rostered[$key][$user->id] = $user->fullname; } else { $non_rostered[$key] = [$user->id => $user->fullname]; } } } } // // Get a list of Companys on planner that have exceeded their 'maxjobs' - Conflicts // // Exclude Quotes (tasks with code 'Q' from maxjobs $excludeTasks = []; if (Auth::user()->company->reportsToCompany()->id == '3') { $excludeTasks = Task::where('code', 'Q')->lists('id')->toArray(); } $planner2 = $this->getPlannerForWeek($date_from, $date_to, $allowedSites, $excludeTasks); $conflicts = $this->getPlanConflicts($request, $planner2, 'any', 'json'); // Get Companies on leave $company_leave = $this->getCompanyLeave(); // // Get attendance for Today and verify if Company all onsite // $today = Carbon::today(); /*$carbon_date = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00'); $weekend = ($carbon_date->isWeekend() ? 1 : 0); $plannerToday = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days']) ->whereDate('from', '<=', $today)->whereDate('to', '>=', $today) ->whereIn('site_id', $allowedSites)->where('weekend', $weekend) ->where('entity_type', '<>', 't')->orderBy('site_id')->get();*/ $planner = $this->getPlannerForWeek($date_from, $date_to, $allowedSites, []); $company_onsite = []; // Initialise all companies on the planner to be onsite ie. true foreach ($planner as $plan) { $site = Site::find($plan->site_id); $current_date = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00'); for ($x = 0; $x < 5; $x++) { if ($plan->entity_type == 'c' && $site->isCompanyOnPlanner($plan->entity_id, $current_date->format('Y-m-d'))) { $company_onsite[$current_date->format('Y-m-d') . '.' . $plan->site_id . '.' . $plan->entity_type . '.' . $plan->entity_id] = 1; } $current_date->addDay(1); if ($current_date->gt($today)) { break; } } } //dd($company_onsite); // Now determine those who aren't all onsite ie. false foreach ($company_onsite as $key => $value) { list($day_date, $site_id, $etype, $eid) = explode('.', $key); $staff = Company::findOrFail($eid)->staffActive()->pluck('id')->toArray(); $roster = SiteRoster::where('site_id', $site_id)->whereDate('date', '=', $day_date)->whereIn('user_id', $staff)->get(); if (!$roster->isEmpty()) { foreach ($roster as $rost) { $site = Site::find($rost->site_id); if (!$site->isUserOnsite($rost->user_id, $rost->date->format('Y-m-d'))) { $company_onsite[$key] = 0; } } } else { $company_onsite[$key] = -1; } } //dd($company_onsite); // // Get Site Supervisor 'Select' options $supervisors = []; if (Auth::user()->is('full.access|full.read.access|whs.admin|general.admin|construction.manager|supervisor')) { if (Auth::user()->is('full.access|full.read.access|whs.admin|general.admin|construction.manager')) { $supervisors = Auth::user()->company->supervisorSelect(); } else { $supervisors = [Auth::user()->id => Auth::user()->fullname]; } $supervisors = ['all' => 'All Sites'] + $supervisors; } $sel_super = []; foreach ($supervisors as $user_id => $fullname) { $sel_super[] = ['value' => $user_id, 'text' => $fullname]; } // Get Users permissions $permission = ''; if (Auth::user()->can('view.weekly.planner|view.p.weekly.planner')) { $permission = 'view'; } if ($plan_type == 'weekly' && Auth::user()->can('view.site.planner|view.p.site.planner')) { $permission = 'edit'; } if ($plan_type == 'trade' && Auth::user()->can('edit.trade.planner|edit.p.trade.planner')) { $permission = 'edit'; } $json = []; $json[] = $fullplan; $json[] = $non_rostered; $json[] = $conflicts; $json[] = $company_leave; $json[] = $company_onsite; $json[] = $sel_super; $json[] = $permission; return $json; }