Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function makeExportData($period)
- {
- if(!is_array($period)) {
- if ($period == 'day') {
- $dt = time() - 60 * 60 * 24;
- } elseif ($period == 'week') {
- $dt = time() - 60 * 60 * 24 * 7;
- } elseif ($period == 'month') {
- $dt = time() - 60 * 60 * 24 * 30;
- } else {
- $dt = 0;
- // $filename = "all_surveys";
- }
- $dt = date('Y-m-d', $dt);
- $dEnd = date("Y-m-d");
- } else{
- $dt = date("Y-m-d", $period[0]);
- $dEnd = date("Y-m-d", $period[1]);
- }
- $filename = 'CONTAINERHISTORY_UPLOAD_' . date('d-M-Y', strtotime(now())) . '.xlsx';
- $filepath = storage_path('app/tmp/');
- // $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"];
- $csv = Pdf::whereHas('tank', function ($query){
- $query->where('tankId', 'like', 'slzu%');
- })
- ->select(['tank_id', DB::raw('"" as "Container Number", test_type as "Test Type", certificateDate, last_hydro_test ')])
- ->whereBetween('created_at', [$dt, $dEnd])
- ->whereNotNull('last_hydro_test')
- ->whereIn('test_type', ['New Construction', '2.5-Yr', '5.0-Yr']);
- // ->where('ownerOperator', 'like', 'csequipment%')
- // ->orWhere('ownerOperator', 'like', 'csl%');
- $csv = $csv->orderBy('certificateDate', "DESC")->get()->each(function ($item, $key) {
- $t = $item['tank']['tankId'];
- $item["Container Number"] = strtoupper($t);
- if ($item['Test Type'] == '2.5-Yr') {
- $item['Test Type'] = 'AIR';
- $item['Last Test Date(DD-MMM-YYYY)'] = date('d-M-Y', strtotime($item['certificateDate']));
- }
- if ($item['Test Type'] == '5.0-Yr' || $item['Test Type'] == 'New Construction') {
- $item['Test Type'] = 'HYDRO';
- $item['Last Test Date(DD-MMM-YYYY)'] = date('d-M-Y', strtotime($item['last_hydro_test']));
- }
- unset($item['tank'], $item['tank_id'], $item['certificateDate'], $item['last_hydro_test']);
- })->toArray();
- usort($csv, function ($a, $b) {
- return new \DateTime($b['Last Test Date(DD-MMM-YYYY)']) <=> new \DateTime($a['Last Test Date(DD-MMM-YYYY)']);
- });
- if (!count($csv)) {
- $csv[0] = [
- 'Container Number' => '',
- 'Test Type' => '',
- 'Last Test Date(DD-MMM-YYYY)' => '',
- ];
- }
- // echo "<pre>";
- // print_r($csv);
- // die;
- $sheets = new SheetCollection([
- 'CONTAINERHISTORY_UPLOAD' => collect($csv)
- ]);
- (new FastExcel($sheets))->export($filepath . $filename);
- // chmod($filepath . $filename, 0777);
- return ['data' => $csv, 'filename' => $filename, 'filepath' => $filepath];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement