From 2d4f6d80695eba2bf9df998f76ddbe031c577c91 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 21 May 2026 22:24:11 +0530 Subject: [PATCH] Added scheduler logic for vehicle report --- app/Console/Commands/Scheduler.php | 34 ++++++ app/Console/Commands/SendVehicleReport.php | 132 +++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 app/Console/Commands/SendVehicleReport.php diff --git a/app/Console/Commands/Scheduler.php b/app/Console/Commands/Scheduler.php index e32a451..6d75a9a 100644 --- a/app/Console/Commands/Scheduler.php +++ b/app/Console/Commands/Scheduler.php @@ -284,6 +284,40 @@ class Scheduler extends Command break; } } + + $vehicleReports = AlertMailRule::where('module', 'VehicleReport') + ->where('rule_name', 'VehicleReportMail') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($vehicleReports as $rule) { + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:vehicle-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:vehicle-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '07:59') { + \Artisan::call('send:vehicle-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } } /** diff --git a/app/Console/Commands/SendVehicleReport.php b/app/Console/Commands/SendVehicleReport.php new file mode 100644 index 0000000..f255d42 --- /dev/null +++ b/app/Console/Commands/SendVehicleReport.php @@ -0,0 +1,132 @@ +argument('schedule_type'); + $plantId = (int) $this->argument('plant'); + + $mailRules = AlertMailRule::where('module', 'VehicleReport') + ->where('rule_name', 'VehicleReportMail') + ->where('schedule_type', $scheduleType) + ->get(); + + $plants = ($plantId == 0) + ? Plant::all() + : Plant::where('id', $plantId)->get(); + + if ($plants->isEmpty()) { + $this->error('No valid plant(s) found.'); + return; + } + + if (strtolower($scheduleType) == 'daily') { + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + } else { + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + foreach ($plants as $plant) { + + $vehicleEntries = VehicleEntry::select([ + 'vehicle_number', + 'entry_time', + 'exit_time', + 'duration', + 'type', + ]) + ->where('plant_id', $plant->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->get(); + + if ($vehicleEntries->isEmpty()) { + $this->warn("No records for {$plant->name}"); + continue; + } + + $tableData = []; + $no = 1; + + foreach ($vehicleEntries as $vehicleEntry) { + $tableData[] = [ + 'no' => $no++, + 'plant' => $plant->name, + 'vehicleNumber' => $vehicleEntry->vehicle_number, + 'entryTime' => $vehicleEntry->entry_time, + 'exitTime' => $vehicleEntry->exit_time, + 'duration' => $vehicleEntry->duration, + 'type' => $vehicleEntry->type, + ]; + } + + if ($plantId == 0) { + $rule = $mailRules->first(); + } else { + $rule = $mailRules->where('plant', $plant->id)->first(); + } + + if (!$rule) { + $this->warn("No mail rule for {$plant->name}"); + continue; + } + + $toEmails = collect(explode(',', $rule->email)) + ->map(fn($e) => trim($e)) + ->filter() + ->unique() + ->values() + ->toArray(); + + $ccEmails = collect(explode(',', $rule->cc_emails)) + ->map(fn($e) => trim($e)) + ->filter() + ->unique() + ->values() + ->toArray(); + + $mail = new VehicleEntryMail( + $scheduleType, + $tableData, + 'Daily Vehicle Transit Report' + ); + + Mail::to($toEmails)->cc($ccEmails)->send($mail); + + $this->info("Mail sent for {$plant->name}"); + } + + } + + +}