public static function nonattendees() { $log = ''; $yesterday = Carbon::now()->subDays(1); $lastweek = Carbon::now()->subDays(7); echo "<h2>Adding Non-Attendees to the Non-Logged in list (" . $lastweek->format('d/m/Y') . ' - ' . $yesterday->format('d/m/Y') . ")</h2>"; $log .= "Adding Non-Attendees to the Non-Logged in list (" . $lastweek->format('d/m/Y') . ' - ' . $yesterday->format('d/m/Y') . ")\n"; $log .= "-------------------------------------------------------------------------\n\n"; $allowedSites = Site::all()->pluck('id')->toArray(); if (Auth::check()) { $allowedSites = Auth::user()->company->siteList('1')->pluck('id')->toArray(); } $roster = SiteRoster::where('date', '>=', $lastweek->format('Y-m-d'))->where('date', '<=', $yesterday->format('Y-m-d'))->whereIn('site_id', $allowedSites)->orderBy('site_id')->get(); $found = false; foreach ($roster as $rost) { $site = Site::find($rost->site_id); $user = User::find($rost->user_id); $date = Carbon::createFromFormat('Y-m-d H:i:s', $rost->date); // if date is weekday if ($date->isWeekday()) { if (!$site->isUserOnsite($rost->user_id, $rost->date) && !$site->isUserOnCompliance($rost->user_id, $rost->date)) { echo $rost->date->format('d/m/Y') . " {$site->name} ({$site->code}) - <b>{$user->fullname}</b> (" . $user->company->name . ") was absent<br>"; $log .= $rost->date->format('d/m/Y') . " {$site->name} ({$site->code}) - {$user->fullname} (" . $user->company->name . ") was absent\n"; SiteCompliance::create(array('site_id' => $site->id, 'user_id' => $user->id, 'date' => $rost->date, 'reason' => null, 'status' => 0, 'resolved_date' => '0000-00-00 00:00:00')); $found = true; } } } if (!$found) { echo "There were no Non-Attendees to add or they were already on the list<br>"; $log .= "There were no Non-Attendees to add or they were already on the list\n"; } echo "<h4>Completed</h4>"; $log .= "\nCompleted\n\n\n"; $bytes_written = File::append(public_path('filebank/log/nightly/' . Carbon::now()->format('Ymd') . '.txt'), $log); if ($bytes_written === false) { die("Error writing to file"); } }
/** * Is a specific user on site roster * * @return record */ public function isUserOnRoster($user_id, $date = '') { if (!$date) { $date = Carbon::today()->format('Y-m-d'); } $onsite = SiteRoster::where('site_id', $this->id)->where('user_id', $user_id)->whereDate('date', '=', $date)->first(); return $onsite ? $onsite->id : 0; }
/** * 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; }
/** * Migrate Site Roster */ public function roster() { echo "<h1>Migrating Roster</h1>"; DB::table('site_roster')->truncate(); set_time_limit(180); $rosters = zSiteRoster::all(); $rosters->each(function ($roster) { //echo $roster->id." <b>" . $roster->sub_id . "</b> $roster->planner_id - ($roster->updated)<br>"; echo "."; $user = User::where('username', $roster->sub_id)->first(); $created = User::where('username', $roster->updated_by)->first(); if (!$created) { $created = User::find('1'); } $lookup = DB::table('z_lookup_planner')->where('old', $roster->planner_id)->first(); if ($user && $lookup) { // Check if already existing ie. old planner had bug of multiple entrys so we removing duplicates $date = Carbon::createFromFormat('Y-m-d H:i:s', substr($lookup->date, 0, 10) . ' 00:00:00')->format('Y-m-d'); $exists = SiteRoster::where('site_id', $lookup->site_id)->where('user_id', $user->id)->whereDate('date', '=', $date)->first(); if (!$exists) { $newRoster = SiteRoster::create(array('site_id' => $lookup->site_id, 'user_id' => $user->id, 'date' => $date . ' 00:00:00', 'created_by' => $created->id, 'updated_by' => $created->id, 'created_at' => $roster->updated, 'updated_at' => $roster->updated)); //echo "id:".$newRoster->id." date: ".$date."<br>"; } } }); echo "<h1>Completed</h1>"; }