示例#1
0
 /**
  * Get Trades -> Tasks options for 'select' dropdown in Vuejs format
  */
 public function getTradeTasks(Request $request, $trade_id)
 {
     $tasks = Task::where('trade_id', '=', $trade_id)->where('status', '1')->orderBy('name')->get();
     $trade = Trade::find($trade_id);
     $array = [];
     $array[] = ['value' => '', 'text' => 'Select task'];
     // Create array in specific Vuejs 'select' format.
     foreach ($tasks as $task) {
         $array[] = ['value' => $task->id, 'text' => $task->name, 'name' => $task->name, 'code' => $task->code, 'trade_id' => $trade->id, 'trade_name' => $trade->name];
     }
     return $array;
 }
 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();
     }
 }