argument('schedule_type'); $plantId = (int) $this->argument('plant'); $machineId = (int) $this->argument('machine_id'); $mailRules = \App\Models\AlertMailRule::where('module', 'LaserStopReport') ->where('rule_name', 'LaserStopMail') ->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; } if (strtolower($scheduleType) == 'daily') { $startDate = now()->subDay()->setTime(8, 0, 0); $endDate = now()->setTime(8, 0, 0); } $plant = Plant::find($plantId); $plantName = $plant?->name ?? $plantId; $machine = Machine::find($machineId); $machineName = $machine?->work_center ?? $machineId; $records = ClassCharacteristic::where('is_stopped', '!=', 0) ->where('plant_id', $plantId) ->where('machine_id', $machineId) ->whereBetween('created_at', [$startDate, $endDate]) ->get(); if ($records->isEmpty()) { $this->info('No laser stop records found.'); return; } // Send mail to mapped email IDs Mail::to($emails) ->cc($ccEmails) ->send( new LaserStopMail( $records, $plantId, $machineId, $plantName, $machineName, ) ); $this->info('Laser stop final report mail sent successfully.'); } }