Files
pds/app/Console/Commands/SendLaserIdleHourReport.php
dhanabalan f4f3682784
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 19s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 13s
Laravel Pint / pint (pull_request) Successful in 4m6s
Laravel Larastan / larastan (pull_request) Failing after 4m23s
added table data empty validation in laser idle report
2026-10-04 14:37:18 +05:30

201 lines
6.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Mail\LaserIdleMail;
use App\Models\ClassCharacteristic;
use App\Models\Plant;
use Illuminate\Console\Command;
class SendLaserIdleHourReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:laser-idle-hour-report {schedule_type} {plant}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$scheduleType = $this->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,
];
}
}
if (empty($tableData)) {
return;
}
$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) : ''));
}
}
}