Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. public static function makeExportData($period)
  2. {
  3. if(!is_array($period)) {
  4. if ($period == 'day') {
  5. $dt = time() - 60 * 60 * 24;
  6. } elseif ($period == 'week') {
  7. $dt = time() - 60 * 60 * 24 * 7;
  8. } elseif ($period == 'month') {
  9. $dt = time() - 60 * 60 * 24 * 30;
  10. } else {
  11. $dt = 0;
  12. // $filename = "all_surveys";
  13. }
  14. $dt = date('Y-m-d', $dt);
  15. $dEnd = date("Y-m-d");
  16. } else{
  17. $dt = date("Y-m-d", $period[0]);
  18. $dEnd = date("Y-m-d", $period[1]);
  19. }
  20.  
  21. $filename = 'CONTAINERHISTORY_UPLOAD_' . date('d-M-Y', strtotime(now())) . '.xlsx';
  22. $filepath = storage_path('app/tmp/');
  23. // $a = ["SLZU2549999", "SLZU2550006", "SLZU2550011", "SLZU2550027", "SLZU2550032", "SLZU2549068", "SLZU2549108", "SLZU2549113", "SLZU2549129", "SLZU2549134", "SLZU2549140", "SLZU2549155", "SLZU2549160", "SLZU2549176", "SLZU2549181", "SLZU2549197", "SLZU2549200", "SLZU2549221", "SLZU2549237", "SLZU2549242", "SLZU2549258", "SLZU2549263", "SLZU2549279", "SLZU2549290", "SLZU2549448", "SLZU2549453", "SLZU2549469", "SLZU2549474", "SLZU2549480", "SLZU2549495", "SLZU2549520", "SLZU2549535", "SLZU2549540", "SLZU2549556", "SLZU2549303", "SLZU2549319", "SLZU2549324", "SLZU2549330", "SLZU2549345", "SLZU2549350", "SLZU2549366", "SLZU2549371", "SLZU2549387", "SLZU2549392", "SLZU2549406", "SLZU2549411", "SLZU2549427", "SLZU2549432", "SLZU2550048", "SLZU2550053", "SLZU2549561", "SLZU2549577", "SLZU2549582", "SLZU2549598", "SLZU2549601", "SLZU2549617", "SLZU2549622", "SLZU2549638", "SLZU2549643", "SLZU2549659", "SLZU2549664", "SLZU2549670", "SLZU2549685", "SLZU2549690", "SLZU2549704", "SLZU2549710", "SLZU2549725", "SLZU2549730", "SLZU2549746", "SLZU2549751", "SLZU2549767", "SLZU2549772", "SLZU2549788", "SLZU2549793", "SLZU2549807", "SLZU2549828", "SLZU2549833", "SLZU2549849", "SLZU2549812", "SLZU2549284", "SLZU2549509", "SLZU2549983", "SLZU2549514", "SLZU2549216", "SLZU2549854", "SLZU2549860", "SLZU2549875", "SLZU2549880", "SLZU2549896", "SLZU2549900", "SLZU2549915", "SLZU2549920", "SLZU2549936", "SLZU2549941", "SLZU2549957", "SLZU2549962", "SLZU2549978"];
  24.  
  25.  
  26. $csv = Pdf::whereHas('tank', function ($query){
  27. $query->where('tankId', 'like', 'slzu%');
  28. })
  29. ->select(['tank_id', DB::raw('"" as "Container Number", test_type as "Test Type", certificateDate, last_hydro_test ')])
  30. ->whereBetween('created_at', [$dt, $dEnd])
  31.  
  32. ->whereNotNull('last_hydro_test')
  33. ->whereIn('test_type', ['New Construction', '2.5-Yr', '5.0-Yr']);
  34. // ->where('ownerOperator', 'like', 'csequipment%')
  35. // ->orWhere('ownerOperator', 'like', 'csl%');
  36.  
  37.  
  38. $csv = $csv->orderBy('certificateDate', "DESC")->get()->each(function ($item, $key) {
  39. $t = $item['tank']['tankId'];
  40. $item["Container Number"] = strtoupper($t);
  41. if ($item['Test Type'] == '2.5-Yr') {
  42. $item['Test Type'] = 'AIR';
  43. $item['Last Test Date(DD-MMM-YYYY)'] = date('d-M-Y', strtotime($item['certificateDate']));
  44. }
  45. if ($item['Test Type'] == '5.0-Yr' || $item['Test Type'] == 'New Construction') {
  46. $item['Test Type'] = 'HYDRO';
  47. $item['Last Test Date(DD-MMM-YYYY)'] = date('d-M-Y', strtotime($item['last_hydro_test']));
  48. }
  49. unset($item['tank'], $item['tank_id'], $item['certificateDate'], $item['last_hydro_test']);
  50. })->toArray();
  51. usort($csv, function ($a, $b) {
  52. return new \DateTime($b['Last Test Date(DD-MMM-YYYY)']) <=> new \DateTime($a['Last Test Date(DD-MMM-YYYY)']);
  53. });
  54. if (!count($csv)) {
  55. $csv[0] = [
  56. 'Container Number' => '',
  57. 'Test Type' => '',
  58. 'Last Test Date(DD-MMM-YYYY)' => '',
  59. ];
  60. }
  61. // echo "<pre>";
  62. // print_r($csv);
  63. // die;
  64. $sheets = new SheetCollection([
  65. 'CONTAINERHISTORY_UPLOAD' => collect($csv)
  66. ]);
  67. (new FastExcel($sheets))->export($filepath . $filename);
  68. // chmod($filepath . $filename, 0777);
  69. return ['data' => $csv, 'filename' => $filename, 'filepath' => $filepath];
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement