From 5088e30ffa8264cd762a4a3981b0148f2acd4273 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 4 Sep 2026 17:16:03 +0530 Subject: [PATCH] Added send panel box report command page --- app/Console/Commands/SendPanelBoxReport.php | 117 ++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 app/Console/Commands/SendPanelBoxReport.php diff --git a/app/Console/Commands/SendPanelBoxReport.php b/app/Console/Commands/SendPanelBoxReport.php new file mode 100644 index 0000000..115007e --- /dev/null +++ b/app/Console/Commands/SendPanelBoxReport.php @@ -0,0 +1,117 @@ +argument('schedule_type'); + $plantId = (int) $this->argument('plant'); + $machineId = (int) $this->argument('machine_id'); + + $mailRules = \App\Models\AlertMailRule::where('module', 'PanelBoxReport') + ->where('rule_name', 'PanelBoxNotOkReportMail') + ->where('schedule_type', $scheduleType) + ->where('plant', $plantId) + ->where('machine_id', $machineId) + ->get(); + + $emails = $mailRules + ->pluck('email') + ->filter() + ->flatMap(function ($email) { + return array_map('trim', explode(',', $email)); + }) + ->unique() + ->values() + ->toArray(); + + $ccEmails = $mailRules + ->pluck('cc_emails') + ->filter() + ->flatMap(function ($email) { + return array_map('trim', explode(',', $email)); + }) + ->unique() + ->values() + ->toArray(); + + $plants = $plantId == 0 + ? Plant::all() + : Plant::where('id', $plantId)->get(); + + if ($plants->isEmpty()) { + $this->error('No valid plant(s) found.'); + return; + } + + $plant = Plant::find($plantId); + + $plantName = $plant?->name ?? $plantId; + + $machine = Machine::find($machineId); + + $machineName = $machine?->work_center ?? $machineId; + + 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); + } + + $records = ProductionCharacteristic::where('plant_id', $plantId) + ->where('machine_id', $machineId) + ->whereBetween('created_at', [$startDate, $endDate]) + ->where('status', 'NotOk') + ->select('serial_number') + ->distinct() + ->get(); + + if ($records->isEmpty()) { + $this->info('No panel box records found.'); + return; + } + + // Send mail to mapped email IDs + Mail::to($emails) + ->cc($ccEmails) + ->send( + new PanelBoxReportMail( + $records, + $plantId, + $machineId, + $plantName, + $machineName, + ) + ); + + $this->info('Panel box report mail sent successfully.'); + } +}