diff --git a/app/Console/Commands/SendProductionReport.php b/app/Console/Commands/SendProductionReport.php index 56589b1..ca6689b 100644 --- a/app/Console/Commands/SendProductionReport.php +++ b/app/Console/Commands/SendProductionReport.php @@ -2,15 +2,13 @@ namespace App\Console\Commands; -use App\Models\InvoiceValidation; -use App\Models\Plant; -use Illuminate\Console\Command; -use Illuminate\Support\Facades\Mail; use App\Mail\ProductionMail; use App\Models\Line; +use App\Models\Plant; use App\Models\ProductionPlan; use App\Models\ProductionQuantity; -use DB; +use Illuminate\Console\Command; +use Illuminate\Support\Facades\Mail; class SendProductionReport extends Command { @@ -19,7 +17,7 @@ class SendProductionReport extends Command * * @var string */ - //protected $signature = 'send:production-report'; + // protected $signature = 'send:production-report'; protected $signature = 'send:production-report {schedule_type} {plant}'; /** @@ -32,11 +30,10 @@ class SendProductionReport extends Command /** * Execute the console command. */ - public function handle() { - ini_set('max_execution_time', 0); // disable limit - set_time_limit(0); + // ini_set('max_execution_time', 0); // disable limit + // set_time_limit(0); $scheduleType = $this->argument('schedule_type'); $plantId = (int) $this->argument('plant'); // cast to int for safety @@ -55,33 +52,30 @@ class SendProductionReport extends Command : Plant::where('id', $plantId)->get(); if ($plants->isEmpty()) { - $this->error("No valid plant(s) found."); + $this->error('No valid plant(s) found.'); + return; } // $startDate = now()->setTime(8, 0, 0); // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); - if (strtolower($scheduleType) == 'daily') - { + if (strtolower($scheduleType) == 'daily') { $startDate = now()->subDay()->setTime(8, 0, 0); $endDate = now()->setTime(8, 0, 0); - } - else - { + } else { $startDate = now()->setTime(8, 0, 0); $endDate = now()->copy()->addDay()->setTime(8, 0, 0); } - $PlanstartDate = now()->setTime(8, 0, 0); - $planendDate = now()->copy()->addDay()->setTime(7, 59, 0); + $PlanstartDate = now()->subDay()->setTime(8, 0, 0); + $planendDate = now()->setTime(7, 59, 0); $tableData = []; $no = 1; - //. + // . - foreach ($plants as $plant) - { + foreach ($plants as $plant) { $lines = Line::where('plant_id', $plant->id)->get(); foreach ($lines as $line) { @@ -90,6 +84,7 @@ class SendProductionReport extends Command ->whereBetween('created_at', [$PlanstartDate, $planendDate]) ->sum('plan_quantity'); + /** @phpstan-ignore property.notFound */ if (strtolower($line->type) == 'fg line') { $productionQuantity = \App\Models\QualityValidation::where('plant_id', $plant->id) ->where('line_id', $line->id) @@ -113,9 +108,7 @@ class SendProductionReport extends Command } } - - - //$this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $fgTableData); + // $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $fgTableData); // $this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $tableData); @@ -128,7 +121,7 @@ class SendProductionReport extends Command // } // $this->info("Production report sent to " . count($emails) . " recipient(s)."); - // Preview in console + // Preview in console $mail = new ProductionMail($scheduleType, $tableData); $contentVars = $mail->content()->with; @@ -140,13 +133,39 @@ class SendProductionReport extends Command $this->info($contentVars['wishes'] ?? ''); // Send mails - if (!empty($emails)) { - foreach ($emails as $email) { - Mail::to($email)->send(new ProductionMail($scheduleType, $tableData)); + // if (! empty($emails)) { + // foreach ($emails as $email) { + // Mail::to($email)->send(new ProductionMail($scheduleType, $tableData)); + // } + // $this->info('Production report sent to '.count($emails).' recipient(s).'); + // } else { + // $this->warn('No recipients found for ProductionMailAlert.'); + // } + 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; } - $this->info("Production report sent to " . count($emails) . " recipient(s)."); - } else { - $this->warn('No recipients found for ProductionMailAlert.'); + + \Mail::to($toEmails)->cc($ccEmails)->send($mail); + + $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); } } }