diff --git a/app/Console/Commands/Scheduler.php b/app/Console/Commands/Scheduler.php index 18a468b..105c323 100644 --- a/app/Console/Commands/Scheduler.php +++ b/app/Console/Commands/Scheduler.php @@ -394,11 +394,89 @@ class Scheduler extends Command break; } } + + //..Laser Stop Report + $laserStopReport = AlertMailRule::where('module', 'LaserStopAlert') + ->where('rule_name', 'LaserStopAlertMail') + ->select('plant', 'schedule_type', 'machine_id') + ->distinct() + ->get(); + + foreach ($laserStopReport as $rule) { + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:laser-stop-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + 'machine_id' => $rule->machine_id, + ]); + break; + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:laser-stop-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + 'machine_id' => $rule->machine_id, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '07:59') { + \Artisan::call('send:laser-stop-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + 'machine_id' => $rule->machine_id, + ]); + } + break; + } + } + + + //..Laser Stop Final Report + + // $laserStopFinalReport = AlertMailRule::where('module', 'LaserStopReport') + // ->where('rule_name', 'LaserStopMail') + // ->select('plant', 'schedule_type', 'machine_id') + // ->distinct() + // ->get(); + + // foreach ($laserStopFinalReport as $rule) { + // switch ($rule->schedule_type) { + // case 'Live': + // // Run every minute + // \Artisan::call('send:laser-stop-consolidate-report', [ + // 'schedule_type' => $rule->schedule_type, + // 'plant' => $rule->plant, + // 'machine_id' => $rule->machine_id, + // ]); + // break; + // case 'Hourly': + // if (now()->minute == 0) { + // \Artisan::call('send:laser-stop-consolidate-report', [ + // 'schedule_type' => $rule->schedule_type, + // 'plant' => $rule->plant, + // 'machine_id' => $rule->machine_id, + // ]); + // } + // break; + // case 'Daily': + // if (now()->format('H:i') == '07:59') { + // \Artisan::call('send:laser-stop-consolidate-report', [ + // 'schedule_type' => $rule->schedule_type, + // 'plant' => $rule->plant, + // 'machine_id' => $rule->machine_id, + // ]); + // } + // break; + // } + // } } /** * Helper to call Artisan commands with parameters. - */ + */ protected function callArtisanCommand($commandName, $rule) { \Artisan::call($commandName, [ diff --git a/app/Console/Commands/SendLaserStopReport.php b/app/Console/Commands/SendLaserStopReport.php new file mode 100644 index 0000000..5407051 --- /dev/null +++ b/app/Console/Commands/SendLaserStopReport.php @@ -0,0 +1,113 @@ +argument('schedule_type'); + $plantId = (int) $this->argument('plant'); + $machineId = (int) $this->argument('machine_id'); + + $mailRules = \App\Models\AlertMailRule::where('module', 'LaserStopAlert') + ->where('rule_name', 'LaserStopAlertMail') + ->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; + + $records = ClassCharacteristic::where('is_stopped', 2) + ->where('plant_id', $plantId) + ->where('machine_id', $machineId) + ->get(); + + if ($records->isEmpty()) { + $this->info('No laser stop records found.'); + return; + } + + $recordIds = $records->pluck('id'); + + // Send mail to mapped email IDs + Mail::to($emails) + ->cc($ccEmails) + ->send( + new LaserStopReportMail( + $records, + $plantId, + $machineId, + $plantName, + $machineName, + ) + ); + + ClassCharacteristic::whereIn('id', $recordIds) + ->update([ + 'is_stopped' => 1, + ]); + + $this->info('Laser stop report mail sent successfully.'); + } +} diff --git a/app/Mail/LaserStopReportMail.php b/app/Mail/LaserStopReportMail.php new file mode 100644 index 0000000..6f3114c --- /dev/null +++ b/app/Mail/LaserStopReportMail.php @@ -0,0 +1,67 @@ +records = $records; + $this->plantId = $plantId; + $this->machineId = $machineId; + $this->plantName = $plantName; + $this->machineName = $machineName; + } + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: 'Laser Stopped Alert', + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + $greeting = 'Dear Sir'; + return new Content( + view: 'mail.laser-stop-report', + with: [ + 'company' => 'CRI Digital Manufacturing Solutions', + 'greeting' => $greeting, + 'wishes' => 'Thanks & Regards,
CRI Digital Manufacturing Solutions', + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/resources/views/mail/laser-stop-report.blade.php b/resources/views/mail/laser-stop-report.blade.php new file mode 100644 index 0000000..932b815 --- /dev/null +++ b/resources/views/mail/laser-stop-report.blade.php @@ -0,0 +1,240 @@ + + + + + + + Laser Stopped Report + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+
+ CRI Digital Manufacturing +
+ +
+ Laser Stop Alert +
+ +
+ Laser machine stoppage notification +
+
+
+ ! +
+
+ +
+ +
+ +
+ ⚠ Laser Stop Detected +
+ +
+ The following laser stop records require your attention. +
+ +
+ +
+ +
+ Stopped Machine Records +
+ +
+ Total records: {{ $records->count() }} +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @foreach($records as $index => $record) + + + + + + + + + + + + + + + + + + + + + + + + + @endforeach + + + +
+ No + + Plant + + Machine + + Production Order + + Item Code + + Serial Number + + Status + + Stopped At + + Stopped By +
+ {{ $index + 1 }} + + {{ $plantName }} + + {{ $machineName }} + + {{ $record->aufnr }} + + {{ $record->item?->code ?? $record->item_id }} + + {{ $record->gernr }} + + + + STOPPED + + + + {{ $record->stopped_datetime }} + + {{ $record->stopped_by }} +
+ +
+ +
+ + This is an automated notification from + CRI Digital Manufacturing. + +
+ + Please take the necessary action regarding the stopped machine. + +

+ © {{ date('Y') }} {{ $company }}
+ Automated Laser Stop Alert Mail + + {{-- + Laser Stop Report • {{ now()->format('d M Y, h:i A') }} + --}} + +
+ +
+ +
+ + +