Beispiel #1
0
 /**
  * Company Tasks for given site + date
  *
  * @return array
  */
 public function companyTasksForSiteOnDate($company_id, $site_id, $date)
 {
     $carbon_date = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00');
     $weekend = $carbon_date->isWeekend() ? 1 : 0;
     $planner = SitePlanner::where('site_id', $site_id)->whereDate('from', '<=', $date)->whereDate('to', '>=', $date)->where('weekend', $weekend)->get();
     $tasks = [];
     foreach ($planner as $plan) {
         if ($plan->task_id) {
             $task = Task::find($plan->task_id);
             $tasks[$task->id] = $task ? $task->name : 'Task Unassigned';
         }
     }
     return $tasks;
 }
Beispiel #2
0
 public static function dailyUpdate()
 {
     $log = '';
     //echo "<h1>Adding New Entities to Roster</h1>";
     $log .= "Adding New Entities to Roster\n\n";
     $capecod = '3';
     $company = Company::findOrFail($capecod);
     $allowedSites = $company->siteList('1')->pluck('id')->toArray();
     $allowedSites = Site::all()->pluck('id')->toArray();
     $date = Carbon::now()->format('Y-m-d');
     //$date = '2016-08-17';
     $planner = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where('from', '<=', $date)->where('to', '>=', $date)->whereIn('site_id', $allowedSites)->orderBy('site_id')->get();
     foreach ($planner as $plan) {
         if ($plan->entity_type == 'c') {
             $site = Site::find($plan->site_id);
             $company = Company::findOrFail($plan->entity_id);
             $staff = $company->staffActive()->pluck('id')->toArray();
             $log .= "\nf:" . $plan->from->format('Y-m-d') . ' t:' . $plan->to->format('Y-m-d') . ' [' . $plan->id . '] (' . $company->name . ") Task:{$plan->task_id} Site: {$site->name} ({$plan->site_id})\n";
             foreach ($staff as $staff_id) {
                 $user = User::findOrFail($staff_id);
                 if (!$site->isUserOnRoster($staff_id, $date)) {
                     //echo 'adding '.$user->fullname.' ('.$user->username.') to roster<br>';
                     $log .= 'adding ' . $user->fullname . ' (' . $user->username . ") to roster\n";
                     $newRoster = SiteRoster::create(array('site_id' => $site->id, 'user_id' => $staff_id, 'date' => $date . ' 00:00:00', 'created_by' => '1', 'updated_by' => '1'));
                 }
             }
         }
     }
     //echo "<h1>Completed</h1>";
     $log .= "\nCompleted";
     $now = Carbon::now()->format('Y-m-d-G-i-s');
     $bytes_written = File::put(public_path('filebank/log/nightly/' . $now . '.txt'), $log);
     if ($bytes_written === false) {
         die("Error writing to file");
     } else {
         echo 'Logfile filebank/log/nightly/' . $now . '.txt';
     }
     echo $log;
 }
Beispiel #3
0
 /**
  * A list of sites this company is on a Planner for
  *
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function sitePlannerList($status = '')
 {
     $client_list = $this->reportsToCompany()->clients->pluck('id')->toArray();
     if ($status != '') {
         $collection = Site::where('status', $status)->whereIn('client_id', $client_list)->get();
     } else {
         $collection = Site::whereIn('client_id', $client_list)->get();
     }
     // If Company has no Parent then return full collection
     // ie. a list of all sites of all their own clients
     if (!$this->parentCompany) {
         return $collection;
     }
     $logged_site_id = '';
     if (Session::has('siteID')) {
         $site_code = Session::get('siteID');
         $site = Site::where(['code' => $site_code])->first();
         $logged_site_id = $site->id;
     }
     // Otherwise return a filtered collection of sites that company is on Planner for
     $filteredCollection = [];
     foreach ($collection as $site) {
         $onPlanner = SitePlanner::where('site_id', $site->id)->where('entity_type', 'c')->where('entity_id', $this->id)->first();
         if (!$onPlanner && $site->id != $logged_site_id) {
             $filteredCollection[] = $site->id;
         }
     }
     return $collection->except($filteredCollection);
 }
Beispiel #4
0
 /**
  * Company Tasks for given site + date
  *
  * @return array
  */
 public function anyTasksOnDate($date)
 {
     $carbon_date = Carbon::createFromFormat('Y-m-d H:i:s', $date . ' 00:00:00');
     $weekend = $carbon_date->isWeekend() ? 1 : 0;
     $planner = SitePlanner::where('site_id', $this->id)->whereDate('from', '<=', $date)->whereDate('to', '>=', $date)->where('weekend', $weekend)->first();
     $onplan = SitePlanner::where('site_id', $this->id)->whereDate('from', '<=', $date)->whereDate('to', '>=', $date)->where('weekend', $weekend)->first();
     return $planner ? true : false;
 }
 /**
  * Get List of Site Without Job Starts options for 'select' dropdown in Vuejs format
  */
 public function getPlannerForWeek($date_from, $date_to, $allowedSites, $excludeTasks)
 {
     if (Auth::user()->company->subscription) {
         return SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('from', '>=', $date_from->format('Y-m-d'));
             $q->Where('from', '<=', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
         })->orWhere(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('to', '>=', $date_from->format('Y-m-d'));
             $q->Where('to', '<=', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
         })->orWhere(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('from', '<', $date_from->format('Y-m-d'));
             $q->Where('to', '>', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
         })->orderBy('from')->get();
     } else {
         return SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('from', '>=', $date_from->format('Y-m-d'));
             $q->Where('from', '<=', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
             $q->where('entity_type', 'c');
             $q->where('entity_id', Auth::user()->company->id);
         })->orWhere(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('to', '>=', $date_from->format('Y-m-d'));
             $q->Where('to', '<=', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
             $q->where('entity_type', 'c');
             $q->where('entity_id', Auth::user()->company->id);
         })->orWhere(function ($q) use($date_from, $date_to, $allowedSites, $excludeTasks) {
             $q->where('from', '<', $date_from->format('Y-m-d'));
             $q->Where('to', '>', $date_to->format('Y-m-d'));
             $q->whereIn('site_id', $allowedSites);
             $q->whereNotIn('task_id', $excludeTasks);
             $q->where('entity_type', 'c');
             $q->where('entity_id', Auth::user()->company->id);
         })->orderBy('from')->get();
     }
 }
 public function sitePDF(SitePlannerExportRequest $request, $site_id, $date, $weeks)
 {
     $date = Carbon::createFromFormat('d/m/Y H:i:s', $request->get('date') . ' 00:00:00')->format('Y-m-d');
     $weeks = $request->get('weeks');
     /*
      * Export by Company
      */
     if ($request->has('export_site')) {
         $site_id = $request->get('site_id');
         //echo "Site: $site_id Date: $date Weeks: $weeks<br>";
         //exit();
         $sites = [];
         if ($site_id) {
             $sites[] = $site_id;
         } else {
             $sites = Auth::user()->company->reportsToCompany()->siteList('1')->pluck('id')->toArray();
         }
         // Sort Sites by Site Name
         $site_list = [];
         foreach ($sites as $siteID) {
             $site_list[$siteID] = Site::find($siteID)->name;
         }
         asort($site_list);
         // For each Site get Tasks om Planner
         $sitedata = [];
         foreach ($site_list as $siteID => $siteName) {
             $site = Site::findOrFail($siteID);
             $obj_site = (object) [];
             $obj_site->site_id = $site->id;
             $obj_site->site_name = $site->name;
             $obj_site->weeks = [];
             // For each week get Entities on the Planner
             $current_date = $date;
             for ($w = 1; $w <= $weeks; $w++) {
                 $date_from = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00');
                 if ($date_from->isWeekend()) {
                     $date_from->addDays(1);
                 }
                 if ($date_from->isWeekend()) {
                     $date_from->addDays(1);
                 }
                 // Calculate Date to ensuring not a weekend
                 $date_to = Carbon::createFromFormat('Y-m-d H:i:s', $date_from->format('Y-m-d H:i:s'));
                 $dates = [$date_from->format('Y-m-d')];
                 for ($i = 2; $i < 6; $i++) {
                     $date_to->addDays(1);
                     if ($date_to->isWeekend()) {
                         $date_to->addDays(2);
                     }
                     $dates[] = $date_to->format('Y-m-d');
                 }
                 //echo "From: " . $date_from->format('d/m/Y') . " To:" . $date_to->format('d/m/Y') . "<br>";
                 $planner = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where(function ($q) use($date_from, $date_to, $site) {
                     $q->where('from', '>=', $date_from->format('Y-m-d'));
                     $q->Where('from', '<=', $date_to->format('Y-m-d'));
                     $q->where('site_id', $site->id);
                 })->orWhere(function ($q) use($date_from, $date_to, $site) {
                     $q->where('to', '>=', $date_from->format('Y-m-d'));
                     $q->Where('to', '<=', $date_to->format('Y-m-d'));
                     $q->where('site_id', $site->id);
                 })->orWhere(function ($q) use($date_from, $date_to, $site) {
                     $q->where('from', '<', $date_from->format('Y-m-d'));
                     $q->Where('to', '>', $date_to->format('Y-m-d'));
                     $q->where('site_id', $site->id);
                 })->orderBy('from')->get();
                 // Get Unique list of Entities for current week
                 $entities = [];
                 foreach ($planner as $plan) {
                     $key = $plan->entity_type . '.' . $plan->entity_id;
                     if (!isset($entities[$key])) {
                         $entity_name = $plan->entity_type == 'c' ? Company::find($plan->entity_id)->name : Trade::find($plan->entity_id)->name;
                         $entities[$key] = ['key' => $key, 'entity_type' => $plan->entity_type, 'entity_id' => $plan->entity_id, 'entity_name' => $entity_name];
                         for ($i = 0; $i < 5; $i++) {
                             $entities[$key][$dates[$i]] = '';
                         }
                     }
                 }
                 usort($entities, 'sortEntityName');
                 // Create Header Row for Current Week
                 $obj_site->weeks[$w] = [];
                 $obj_site->weeks[$w][0][] = 'COMPANY';
                 foreach ($dates as $d) {
                     $obj_site->weeks[$w][0][] = strtoupper(Carbon::createFromFormat('Y-m-d H:i:s', $d . ' 00:00:00')->format('l d/m'));
                 }
                 // For each Entity on for current week get their Tasks for each day of the week
                 $entity_count = 1;
                 if ($entities) {
                     foreach ($entities as $e) {
                         $obj_site->weeks[$w][$entity_count][] = $e['entity_name'];
                         for ($i = 1; $i <= 5; $i++) {
                             $tasks = $site->entityTasksOnDate($e['entity_type'], $e['entity_id'], $dates[$i - 1]);
                             if ($tasks) {
                                 $str = '';
                                 foreach ($tasks as $task_id => $task_name) {
                                     $str .= $task_name . '<br>';
                                 }
                             } else {
                                 $str = '&nbsp;';
                             }
                             $obj_site->weeks[$w][$entity_count][$i] = $str;
                         }
                         $entity_count++;
                     }
                 } else {
                     $obj_site->weeks[$w][1][] = 'NOTHING-ON-PLAN';
                     $obj_site->weeks[$w][1][1] = '';
                 }
                 $date_next = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00')->addDays(7);
                 $current_date = $date_next->format('Y-m-d');
             }
             $sitedata[] = $obj_site;
         }
         //return view('pdf.plan-site', compact('site', 'date', 'weeks', 'sitedata'));
         $pdf = PDF::loadView('pdf.plan-site', compact('site', 'date', 'weeks', 'sitedata'))->setOption('page-width', 200)->setOption('page-height', 287)->setOption('margin-bottom', 10)->setOrientation('landscape');
         //$file = public_path('filebank/company/' . $doc->for_company_id . '/wms/' . $doc->name . ' v' . $doc->version . ' ref-' . $doc->id . ' ' . '.pdf');
         //if (file_exists($file))
         //    unlink($file);
         //$pdf->save($file);
         return $pdf->stream();
     }
     /*
      * Export by Company
      */
     if ($request->has('export_company')) {
         $company_id = $request->get('company_id');
         $companies = [];
         if ($company_id) {
             $companies[] = $company_id;
         } else {
             $companies = Auth::user()->company->companyList('1')->pluck('id')->toArray();
         }
         // Sort Companies by Name
         $company_list = [];
         foreach ($companies as $cid) {
             $company_list[$cid] = Company::find($cid)->name;
         }
         asort($company_list);
         // For each Company get Tasks om Planner
         $sitedata = [];
         foreach ($company_list as $cid => $cname) {
             $company = Company::find($cid);
             $obj_site = (object) [];
             $obj_site->company_id = $company->id;
             $obj_site->company_name = $company->name;
             $obj_site->weeks = [];
             $obj_site->upcoming = [];
             // For each week get Sites on the Planner
             $current_date = $date;
             for ($w = 1; $w <= $weeks; $w++) {
                 $date_from = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00');
                 if ($date_from->isWeekend()) {
                     $date_from->addDays(1);
                 }
                 if ($date_from->isWeekend()) {
                     $date_from->addDays(1);
                 }
                 // Calculate Date To ensuring not a weekend
                 $date_to = Carbon::createFromFormat('Y-m-d H:i:s', $date_from->format('Y-m-d H:i:s'));
                 $dates = [$date_from->format('Y-m-d')];
                 for ($i = 2; $i < 6; $i++) {
                     $date_to->addDays(1);
                     if ($date_to->isWeekend()) {
                         $date_to->addDays(2);
                     }
                     $dates[] = $date_to->format('Y-m-d');
                 }
                 //echo "From: " . $date_from->format('d/m/Y') . " To:" . $date_to->format('d/m/Y') . "<br>";
                 $planner = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where(function ($q) use($date_from, $date_to, $company) {
                     $q->where('from', '>=', $date_from->format('Y-m-d'));
                     $q->Where('from', '<=', $date_to->format('Y-m-d'));
                     $q->where('entity_type', 'c');
                     $q->where('entity_id', $company->id);
                 })->orWhere(function ($q) use($date_from, $date_to, $company) {
                     $q->where('to', '>=', $date_from->format('Y-m-d'));
                     $q->Where('to', '<=', $date_to->format('Y-m-d'));
                     $q->where('entity_type', 'c');
                     $q->where('entity_id', $company->id);
                 })->orWhere(function ($q) use($date_from, $date_to, $company) {
                     $q->where('from', '<', $date_from->format('Y-m-d'));
                     $q->Where('to', '>', $date_to->format('Y-m-d'));
                     $q->where('entity_type', 'c');
                     $q->where('entity_id', $company->id);
                 })->orderBy('from')->get();
                 // Get Unique list of Sites for current week
                 $sites = [];
                 foreach ($planner as $plan) {
                     if (!isset($sites[$plan->site_id])) {
                         $site = Site::find($plan->site_id);
                         $sites[$plan->site_id] = ['site_id' => $plan->site_id, 'site_name' => $site->name, 'site_supervisor' => $site->supervisorsFirstNameSeparatedByComma()];
                         for ($i = 0; $i < 5; $i++) {
                             $sites[$plan->site_id][$dates[$i]] = '';
                         }
                     }
                 }
                 usort($sites, 'sortSiteName');
                 // Create Header Row for Current Week
                 $obj_site->weeks[$w] = [];
                 $obj_site->weeks[$w][0][] = 'SITE';
                 foreach ($dates as $d) {
                     $obj_site->weeks[$w][0][] = strtoupper(Carbon::createFromFormat('Y-m-d H:i:s', $d . ' 00:00:00')->format('l d/m'));
                 }
                 // For each Site on for current week get the Company Tasks for each day of the week
                 $site_count = 1;
                 if ($sites) {
                     foreach ($sites as $s) {
                         $obj_site->weeks[$w][$site_count][] = $s['site_name'] . ' (' . $s['site_supervisor'] . ')';
                         for ($i = 1; $i <= 5; $i++) {
                             $site = Site::find($s['site_id']);
                             $tasks = $site->entityTasksOnDate('c', $company->id, $dates[$i - 1]);
                             if ($tasks) {
                                 $str = '';
                                 foreach ($tasks as $task_id => $task_name) {
                                     $str .= $task_name . '<br>';
                                 }
                             } else {
                                 $str = '&nbsp;';
                             }
                             $obj_site->weeks[$w][$site_count][$i] = $str;
                         }
                         $site_count++;
                     }
                 } else {
                     $obj_site->weeks[$w][1][] = 'NOTHING-ON-PLAN';
                     $obj_site->weeks[$w][1][1] = '';
                 }
                 $date_next = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00')->addDays(7);
                 $current_date = $date_next->format('Y-m-d');
             }
             /*
              * Upcoming
              */
             //for ($w = 1; $w <= 2; $w++) {
             $date_from = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00');
             $date_to = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00');
             $date_to->addDays(14);
             //echo "From: " . $date_from->format('d/m/Y') . " To:" . $date_to->format('d/m/Y') . "<br>";
             $planner = SitePlanner::select(['id', 'site_id', 'entity_type', 'entity_id', 'task_id', 'from', 'to', 'days'])->where(function ($q) use($date_from, $date_to, $company) {
                 $q->where('from', '>=', $date_from->format('Y-m-d'));
                 $q->Where('from', '<=', $date_to->format('Y-m-d'));
                 $q->where('entity_type', 'c');
                 $q->where('entity_id', $company->id);
             })->orWhere(function ($q) use($date_from, $date_to, $company) {
                 $q->where('to', '>=', $date_from->format('Y-m-d'));
                 $q->Where('to', '<=', $date_to->format('Y-m-d'));
                 $q->where('entity_type', 'c');
                 $q->where('entity_id', $company->id);
             })->orWhere(function ($q) use($date_from, $date_to, $company) {
                 $q->where('from', '<', $date_from->format('Y-m-d'));
                 $q->Where('to', '>', $date_to->format('Y-m-d'));
                 $q->where('entity_type', 'c');
                 $q->where('entity_id', $company->id);
             })->orderBy('from')->get();
             //dd($planner);
             $sites = [];
             foreach ($planner as $plan) {
                 if (!in_array($plan->site_id, $sites)) {
                     $sites[] = $plan->site_id;
                 }
             }
             $current_date = Carbon::createFromFormat('Y-m-d H:i:s', $current_date . ' 00:00:00');
             if ($sites) {
                 for ($x = 1; $x <= 14; $x++) {
                     // Skip Weekends
                     if ($current_date->isWeekend()) {
                         $current_date->addDays(1);
                     }
                     if ($current_date->isWeekend()) {
                         $current_date->addDays(1);
                     }
                     foreach ($sites as $s) {
                         $site = Site::find($s);
                         $tasks = $site->entityTasksOnDate('c', $company->id, $current_date->format('Y-m-d'));
                         if ($tasks) {
                             $task_list = '';
                             foreach ($tasks as $task_id => $task_name) {
                                 $task_list .= $task_name . ', ';
                             }
                             $task_list = rtrim($task_list, ', ');
                             $obj_site->upcoming[] = ['date' => $current_date->format('M j'), 'site' => $site->name, 'tasks' => $task_list];
                         }
                     }
                     $current_date->addDay(1);
                 }
             }
             //}
             $sitedata[] = $obj_site;
         }
         //dd($sitedata);
         //return view('pdf/plan-company', compact('company_id', 'date', 'weeks', 'sitedata'));
         $pdf = PDF::loadView('pdf.plan-company', compact('company_id', 'date', 'weeks', 'sitedata'))->setOption('page-width', 200)->setOption('page-height', 287)->setOption('margin-bottom', 10)->setOrientation('landscape');
         //$file = public_path('filebank/company/' . $doc->for_company_id . '/wms/' . $doc->name . ' v' . $doc->version . ' ref-' . $doc->id . ' ' . '.pdf');
         //if (file_exists($file))
         //    unlink($file);
         //$pdf->save($file);
         return $pdf->stream();
     }
 }
Beispiel #7
0
 public static function roster()
 {
     $log = '';
     echo "<h2>Adding Users to Roster</h2>";
     $log .= "Adding New Users to Roster\n";
     $log .= "------------------------------------------------------------------------\n\n";
     $allowedSites = Site::all()->pluck('id')->toArray();
     if (Auth::check()) {
         $allowedSites = Auth::user()->company->siteList('1')->pluck('id')->toArray();
     }
     $date = Carbon::now()->format('Y-m-d');
     //$date = '2016-08-17';
     $planner = SitePlanner::where('from', '<=', $date)->where('to', '>=', $date)->whereIn('site_id', $allowedSites)->orderBy('site_id')->get();
     foreach ($planner as $plan) {
         if ($plan->entity_type == 'c') {
             $site = Site::find($plan->site_id);
             $company = Company::findOrFail($plan->entity_id);
             $staff = $company->staffActive()->pluck('id')->toArray();
             $task = Task::find($plan->task_id);
             echo "<br><b>Site:{$site->name} ({$plan->site_id}) &nbsp; Company: {$company->name} &nbsp; Task: {$task->name} &nbsp; PID: {$plan->id}</b><br>";
             $log .= "\nSite: {$site->name} ({$plan->site_id}) Company: {$company->name}  Task: {$task->name} PID: {$plan->id}\n";
             $found = false;
             foreach ($staff as $staff_id) {
                 $user = User::findOrFail($staff_id);
                 if (!$site->isUserOnRoster($staff_id, $date)) {
                     echo 'adding ' . $user->fullname . ' (' . $user->username . ') to roster<br>';
                     $log .= 'adding ' . $user->fullname . ' (' . $user->username . ") to roster\n";
                     $newRoster = SiteRoster::create(array('site_id' => $site->id, 'user_id' => $staff_id, 'date' => $date . ' 00:00:00', 'created_by' => '1', 'updated_by' => '1'));
                     $found = true;
                 }
             }
             if (!$found) {
                 echo "There were no users to add or they were already on the roster<br>";
                 $log .= "There were no users to add or they were already on the roster\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");
     }
 }
Beispiel #8
0
 /**
  * Migrate Planner
  */
 public function planner($id)
 {
     echo "<h1>Migrating Planner {$id}</h1>";
     //$p1 = zSitePlanner::where('jobnum', '<', '7610')->orderBy('jobnum')->orderBy('date')->get();
     //$p2 = zSitePlanner::where('jobnum', '>=', '7610')->orderBy('jobnum')->orderBy('date')->get();
     //echo "p1:".count($p1)."<br>";
     //echo "p2:".count($p2)."<br>";
     //dd();
     set_time_limit(60);
     if ($id == '1') {
         //disable foreign key check for this connection before running seeders
         DB::statement('SET FOREIGN_KEY_CHECKS=0;');
         DB::table('site_planner')->truncate();
         DB::table('z_lookup_planner')->truncate();
         DB::statement('SET FOREIGN_KEY_CHECKS=1;');
         $planner = zSitePlanner::where('jobnum', '<', '7610')->orderBy('jobnum')->orderBy('date')->get();
     } else {
         $planner = zSitePlanner::where('jobnum', '>=', '7600')->orderBy('jobnum')->orderBy('date')->get();
     }
     $planner->each(function ($plan) {
         if (true) {
             //$plan->jobnum == '7599') {
             $e = "{$plan->id} <b>" . $plan->company_id . "</b> {$plan->jobnum} - ({$plan->date})";
             $site = Site::where('code', $plan->jobnum)->first();
             if ($plan->company_id) {
                 $type = substr($plan->company_id, 0, 1);
                 if ($type == 'T') {
                     $etype = 't';
                     $tid = DB::table('z_lookup_trades')->where('old', substr($plan->company_id, 3))->get();
                     $tid ? $eid = $tid[0]->new : ($eid = 0);
                     $cid = "TRD";
                 } else {
                     $etype = 'c';
                     $cid = DB::table('z_lookup_company')->where('old', $plan->company_id)->get();
                     $cid ? $eid = $cid[0]->new : ($eid = 1);
                 }
             } else {
                 $cid = '';
             }
             $user = User::where('username', $plan->updated_by)->first();
             if ($user) {
                 $updatedby = $user->id;
             } else {
                 $updatedby = '1';
             }
             if (!$cid) {
                 //echo "<b>NO CID</b> - $e <br>";
             } elseif ($site->id) {
                 $tasks = explode(':', $plan->tasks);
                 foreach ($tasks as $task) {
                     if ($task != '') {
                         if ($task == '0') {
                             $task = null;
                         } else {
                             $lookup = DB::table('z_lookup_tasks')->where('old', $task)->get();
                             $task = $lookup[0]->new;
                         }
                         // If date is a weekday check if Company has same Task previous day
                         $planDate = Carbon::createFromFormat('Y-m-d H:i:s', $plan->date);
                         $planDayofWeek = $planDate->dayOfWeek;
                         $dayBefore = $planDate->subDay();
                         if ($planDayofWeek > 0 && $planDayofWeek < 6) {
                             // 6 = Sat  0 = Sun
                             if ($planDayofWeek == 1) {
                                 $dayBefore = $dayBefore->subDays(2);
                             }
                         } else {
                             $dayBefore = Carbon::createFromFormat('Y-m-d H:i:s', $plan->date);
                         }
                         $sameTask = SitePlanner::where('site_id', $site->id)->where('entity_type', $etype)->where('entity_id', $eid)->where('task_id', $task)->where('to', $dayBefore->toDateTimeString())->get();
                         if (count($sameTask) > 0) {
                             $sameTask[0]->to = $plan->date;
                             $sameTask[0]->days++;
                             $sameTask[0]->save();
                             // Add to lookup
                             zLookupPlanner::create(array('old' => $plan->id, 'site_id' => $site->id, 'date' => $plan->date));
                         } else {
                             $newPlanner = SitePlanner::create(array('site_id' => $site->id, 'from' => $plan->date, 'to' => $plan->date, 'days' => '1', 'entity_type' => $etype, 'entity_id' => $eid, 'task_id' => $task, 'created_by' => '1', 'updated_by' => $updatedby, 'created_at' => '0000-00-00 00:00:00', 'updated_at' => $plan->updated));
                             // Add to lookup
                             zLookupPlanner::create(array('old' => $plan->id, 'site_id' => $site->id, 'date' => $plan->date));
                         }
                         //echo "$task - $e <br>";
                     }
                 }
             } else {
                 //echo "<b>No SITE</b> - $e <br>";
             }
             //echo "$e <br>";
         }
     });
     echo "<h1>Completed</h1>";
 }