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) : '')); + + } + } +}