diff --git a/app/Console/Commands/Scheduler.php b/app/Console/Commands/Scheduler.php index af21e1f4..9e7820c6 100644 --- a/app/Console/Commands/Scheduler.php +++ b/app/Console/Commands/Scheduler.php @@ -544,6 +544,42 @@ class Scheduler extends Command break; } } + + //..Laser Idle Report + + $laserIdleRules = AlertMailRule::where('module', 'LaserIdleHourReport') + ->where('rule_name', 'LaserIdleMail') + ->select('plant', 'schedule_type') + ->distinct() + ->get(); + + foreach ($laserIdleRules as $rule) { + switch ($rule->schedule_type) { + case 'Live': + // Run every minute + \Artisan::call('send:laser-idle-hour-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + break; + case 'Hourly': + if (now()->minute == 0) { + \Artisan::call('send:laser-idle-hour-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + case 'Daily': + if (now()->format('H:i') == '07:59') { + \Artisan::call('send:laser-idle-hour-report', [ + 'schedule_type' => $rule->schedule_type, + 'plant' => $rule->plant, + ]); + } + break; + } + } } /** diff --git a/app/Console/Commands/SendLaserIdleHourReport.php b/app/Console/Commands/SendLaserIdleHourReport.php new file mode 100644 index 00000000..3d5da75a --- /dev/null +++ b/app/Console/Commands/SendLaserIdleHourReport.php @@ -0,0 +1,196 @@ +argument('schedule_type'); + $plantId = (int) $this->argument('plant'); + + $mailRules = \App\Models\AlertMailRule::where('module', 'LaserIdleHourReport') + ->where('rule_name', 'LaserIdleMail') + ->where('schedule_type', $scheduleType) + ->where('plant', $plantId) + ->get(); + + $emails = $mailRules->pluck('email')->unique()->toArray(); + + $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); + } + + $tableData = []; + $no = 1; + + foreach ($plants as $plant) { + + $machineRecords = ClassCharacteristic::where('plant_id', $plant->id) + ->whereBetween('created_at', [$startDate, $endDate]) + ->get(); + + $machineNames = collect() + ->merge( + $machineRecords + ->pluck('motor_machine_name') + ->filter() + ) + ->merge( + $machineRecords + ->pluck('pump_machine_name') + ->filter() + ) + ->merge( + $machineRecords + ->pluck('name_plate_machine_name') + ->filter() + ) + ->unique() + ->values(); + + foreach ($machineNames as $machineName) { + + $motorCount = $machineRecords + ->where('motor_machine_name', $machineName) + ->sum(function ($record) { + return (float) ($record->motor_marked_physical_count ?? 0); + }); + + $pumpCount = $machineRecords + ->where('pump_machine_name', $machineName) + ->sum(function ($record) { + return (float) ($record->pump_marked_physical_count ?? 0); + }); + + $namePlateCount = $machineRecords + ->where('name_plate_machine_name', $machineName) + ->where('name_plate_marked_status', 'Marked') + ->count(); + + $manualCount = $machineRecords + ->filter(function ($record) use ($machineName) { + return $record->motor_machine_name == $machineName + || $record->pump_machine_name == $machineName + || $record->name_plate_machine_name == $machineName; + }) + ->sum(function ($record) { + return (float) ($record->man_marked_status ?? 0); + }); + + $totalExpectedMinutes = + $machineRecords + ->where('motor_machine_name', $machineName) + ->sum(fn ($record) => (float) ($record->motor_expected_time ?? 0)) + + + $machineRecords + ->where('pump_machine_name', $machineName) + ->sum(fn ($record) => (float) ($record->pump_expected_time ?? 0)) + + + $machineRecords + ->where('name_plate_machine_name', $machineName) + ->sum(fn ($record) => (float) ($record->name_plate_expected_time ?? 0)) + + + $machineRecords + ->where('motor_machine_name', $machineName) + ->sum(fn ($record) => (float) ($record->man_mark_expected_time ?? 0)); + + $totalLaserOnHoursValue = $totalExpectedMinutes / 60; + + $totalHours = intdiv((int) $totalExpectedMinutes, 60); + $totalMinutes = (int) $totalExpectedMinutes % 60; + + $totalLaserOnHours = "{$totalHours} hr {$totalMinutes} min"; + + $totalLaserOffHoursValue = 22.5 - $totalLaserOnHoursValue; + + $offHours = floor($totalLaserOffHoursValue); + $offMinutes = round(($totalLaserOffHoursValue - $offHours) * 60); + + $totalLaserOffHours = "{$offHours} hr {$offMinutes} min"; + + $tableData[] = [ + 'no' => $no++, + 'plant' => $plant->name, + 'machine' => $machineName, + + 'motorCount' => $motorCount, + 'pumpCount' => $pumpCount, + 'namePlateCount' => $namePlateCount, + + 'manualCount' => $manualCount, + 'totalLaserOnHours' => $totalLaserOnHours, + 'totalLaserOffHours' => $totalLaserOffHours, + ]; + } + } + + $mail = new LaserIdleMail($scheduleType, $tableData); + + // $contentVars = $mail->content()->with; + + foreach ($mailRules as $rule) + { + $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(); + + if (empty($toEmails)) { + $this->warn("Skipping rule ID {$rule->id} — no valid To emails found."); + + continue; + } + + \Mail::to($toEmails)->cc($ccEmails)->send($mail); + + $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); + + } + } +} diff --git a/app/Mail/LaserIdleMail.php b/app/Mail/LaserIdleMail.php new file mode 100644 index 00000000..b3c78873 --- /dev/null +++ b/app/Mail/LaserIdleMail.php @@ -0,0 +1,89 @@ +scheduleType = $scheduleType; + $this->tableData = $tableData ?? []; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: 'Laser Idle Report', + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + $greeting = "Dear Sir/Madam,

Kindly find the attached Laser Idle Report status details ,"; + + if ($this->scheduleType == 'Daily') { + $fromDate = (new DateTime('yesterday 08:00'))->format('d/m/Y H:i').':000'; + $toDate = (new DateTime('today 07:59'))->format('d/m/Y H:i').':999'; + $reportPeriod = "The following report presents results from: $fromDate to $toDate."; + $greeting .= $reportPeriod; + } + + if ($this->scheduleType == 'Hourly') { + $now = now(); + $fromHour = $now->copy()->subHour()->format('H:i:s'); + $toHour = $now->format('H:i:s'); + $reportDate = $now->format('d/m/Y'); + $greeting .= "The following report presents results from: $reportDate, $fromHour to $toHour."; + } + + if ($this->scheduleType == 'Live') { + $now = now(); + $fromMinute = $now->copy()->subMinute()->format('d/m/Y H:i:s'); + $toMinute = $now->format('d/m/Y H:i:s'); + $greeting .= "The following report presents results from: $fromMinute to $toMinute."; + } + + return new Content( + view: 'mail.laser_idle_hour_report', + with: [ + 'company' => 'CRI Digital Manufacturing Solutions', + 'greeting' => $greeting, + 'tableData' => $this->tableData, + '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_idle_hour_report.blade.php b/resources/views/mail/laser_idle_hour_report.blade.php new file mode 100644 index 00000000..386d4ff6 --- /dev/null +++ b/resources/views/mail/laser_idle_hour_report.blade.php @@ -0,0 +1,88 @@ + + + + Invoice Table + + + + +
+ {{ $company }} +
+
+

{!! $greeting !!}

+ + + + + + + + + + + + + + + + + + + + + + + + @foreach($tableData as $row) + + + + + + + + + + + + + + + + + @endforeach + + +
NoPlantMachine Name + Auto Laser Mark Count + Manual Mark CountTotal Laser On HoursTotal Laser Off Hours
MotorPumpNamePlate
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['machine'] }} + {{ $row['motorCount'] }} + + {{ $row['pumpCount'] }} + + {{ $row['namePlateCount'] }} + + {{ $row['manualCount'] }} + + {{ $row['totalLaserOnHours'] }} + + {{ $row['totalLaserOffHours'] }} +
+ +

{!! $wishes !!}

+ +