/** * 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 Site Attendance for specific site */ public function getSiteAttendance(Request $request, $site_id, $date) { $today = Carbon::now()->format('Y-m-d'); $carbon_date = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00'); $weekend = $carbon_date->isWeekend() ? 1 : 0; $planner = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->whereDate('from', '<=', $date)->whereDate('to', '>=', $date)->where('site_id', $site_id)->where('weekend', $weekend)->get(); $site = Site::find($site_id); $dayplan = []; $r_entities = []; $planner_ids = []; $user_list = []; foreach ($planner as $plan) { $planner_ids[] = $plan->id; $array = $this->getPlanData($plan); $key = $plan->entity_type . '.' . $plan->entity_id; // Add task to Entity's existing task else add Entity to list if (isset($r_entities[$key])) { $r_entities[$key]['tasks'] .= ', ' . $array['task_name']; $r_entities[$key]['plan_ids'] .= ', ' . $plan->id; } else { $attendance = []; if ($plan->entity_type == 'c') { // Get Staff Attendance on Current Site $staff = Company::find($plan->entity_id)->staff->pluck('id')->toArray(); $roster = SiteRoster::where('site_id', $plan->site_id)->where('date', '=', $date)->whereIn('user_id', $staff)->get(); // If today Roster will be all active company to give us the ability to add/remove them from Roster database if ($date == $today) { $roster = Company::findOrFail($plan->entity_id)->staffActive(); } foreach ($roster as $rostered) { // If today then determine if user is on planner otherwise we know they already are if ($date == $today) { $user = User::find($rostered->id); $roster_id = $site->isUserOnRoster($user->id, $date); //echo "rid:" . $roster_id . ' user:'******' date:' . $date . "<br>"; } else { $user = User::find($rostered->user_id); $roster_id = $rostered->id; //echo "rid:" . $rostered->id . ' user:'******' date:' . $rostered->date . "<br>"; } $user_list[] = $user->id; // add to user_list to determine non-rostered users later // Current Site attendance $attended = ($onsite = $site->isUserOnsite($user->id, $date)) ? $onsite->date->format('H:i:s') : ''; // Other Site attendance $attend_other = SiteAttendance::where('user_id', $user->id)->where('site_id', '<>', $site_id)->whereDate('date', '=', $date)->orderBy('date')->get(); $other_sites = ''; foreach ($attend_other as $attend) { $other_site = Site::find($attend->site_id); $other_sites ? $other_sites .= ', ' . $other_site->nameShort . ' (' . $attend->date->format('g:i a') . ')' : ($other_sites = $other_site->nameShort . ' (' . $attend->date->format('g:i a') . ')'); } $attendance[] = ['user_id' => $user->id, 'name' => $user->fullname, 'roster_id' => $roster_id, 'attended' => $attended, 'other_sites' => $other_sites]; //echo "Company:".$array['entity_name']."<br>"; } } $r_entities[$key] = ['key' => $key, 'entity_type' => $plan->entity_type, 'entity_id' => $plan->entity_id, 'entity_name' => $array['entity_name'], 'tasks' => $array['task_name'], 'plan_ids' => $plan->id, 'attendance' => $attendance, 'open' => false]; } $dayplan[] = $array; } // Non-Rostered attendees $n_entities = []; $non_rostered = SiteAttendance::where('site_id', $site_id)->whereDate('date', '=', $date)->whereNotIn('user_id', $user_list)->get(); foreach ($non_rostered as $non) { $company = User::find($non->user_id)->company; $key = 'c.' . $company->id; $attendance = []; // Get All Staff Non Rostered Attendance $staff = $company->staff->pluck('id')->toArray(); foreach ($staff as $s) { $user = User::find($s); // Current Site attendance $attended = ($onsite = $site->isUserOnsite($user->id, $date)) ? $onsite->date->format('H:i:s') : ''; if ($attended) { // Other Site attendance $attend_other = SiteAttendance::where('user_id', $user->id)->where('site_id', '<>', $site_id)->whereDate('date', '=', $date)->orderBy('date')->get(); $other_sites = ''; foreach ($attend_other as $attend) { $other_site = Site::find($attend->site_id); $other_sites ? $other_sites .= ', ' . $other_site->nameShort . ' (' . $attend->date->format('g:i a') . ')' : ($other_sites = $other_site->nameShort . ' (' . $attend->date->format('g:i a') . ')'); } $attendance[] = ['user_id' => $user->id, 'name' => $user->fullname, 'attended' => $attended, 'other_sites' => $other_sites]; } } if (!isset($n_entities[$key])) { $n_entities[$key] = ['key' => $key, 'entity_type' => 'c', 'entity_id' => $company->id, 'entity_name' => $company->name, 'tasks' => 'Unrostered', 'attendance' => $attendance, 'open' => false]; } } // // Get Site List 'Select' options + bold sites currently on planner for date. $sel_site = []; if (Auth::user()->company->subscription) { $site_list = Auth::user()->supervisorSiteListSelect('1', 'prompt'); } else { $site_list = Auth::user()->company->sitePlannerListSelect('prompt'); } foreach ($site_list as $key => $value) { $s = Site::find($key); if ($s) { if (Auth::user()->company->subscription) { $site_name = $s->anyTasksOnDate($date) ? '<b>' . $value . '</b>' : $value; } else { $site_name = $s->isCompanyOnPlanner(Auth::user()->company->id, $date) ? '<b>' . $value . '</b>' : $value; } $sel_site[] = ['value' => $key, 'text' => $site_name]; } else { $sel_site[] = ['value' => $key, 'text' => $value]; } } // Sort Rostered $roster = []; foreach ($r_entities as $entity) { usort($entity['attendance'], 'sortName'); $roster[] = $entity; } usort($roster, 'sortEntityName'); // Sort Non Rostered $non_roster = []; foreach ($n_entities as $entity) { usort($entity['attendance'], 'sortName'); $non_roster[] = $entity; } usort($non_roster, 'sortEntityName'); // Get Users permissions $permission = ''; if (Auth::user()->can('view.attendance|view.p.attendance')) { $permission = 'view'; } if (Auth::user()->can('edit.attendance|edit.p.attendance')) { $permission = 'edit'; } $json = []; $json[] = $dayplan; $json[] = $roster; $json[] = $non_roster; $json[] = $sel_site; $json[] = $permission; return $json; }
/** * Is a specific user onsite * * @return record */ public function isUserOnsite($user_id, $date = '') { if (!$date) { $date = Carbon::today()->format('Y-m-d'); } $onsite = SiteAttendance::where('site_id', $this->id)->where('user_id', $user_id)->whereDate('date', '=', $date)->first(); return $onsite; }
/** * Migrate Site Attend */ public function attend() { echo "<h1>Migrating Attendance</h1>"; DB::table('site_attendance')->truncate(); set_time_limit(60); $attendance = zSiteAttend::all(); $attendance->each(function ($attend) { // echo "<b>" . $attend->sub_id . "</b> ($attend->date)<br>"; echo "."; if ($attend->jobnum == '003') { $jobnum = '0003'; } else { $jobnum = $attend->jobnum; } $user = User::where('username', $attend->sub_id)->first(); $site = Site::where('code', $jobnum)->first(); if (!$site) { $siteid = '0003'; } else { $siteid = $site->id; } $safe = '0'; if ($attend->safesite == "y") { $safe = '1'; } if ($user) { $newAttend = SiteAttendance::create(array('site_id' => $siteid, 'user_id' => $user->id, 'date' => $attend->date, 'safe_site' => $safe, 'accept_whs' => '1')); } }); echo "<h1>Completed</h1>"; }