From 9b2e7b4e98ecec547045654e19cfe76b9c8f4ac5 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 5 Nov 2025 11:17:17 +0530 Subject: [PATCH] Refactor SendInvoiceDataReport command to streamline email recipient handling and improve clarity in processing mail rules --- .../Commands/SendInvoiceDataReport.php | 72 ++++++++++++++----- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/app/Console/Commands/SendInvoiceDataReport.php b/app/Console/Commands/SendInvoiceDataReport.php index eac07a0..73c8785 100644 --- a/app/Console/Commands/SendInvoiceDataReport.php +++ b/app/Console/Commands/SendInvoiceDataReport.php @@ -55,17 +55,20 @@ class SendInvoiceDataReport extends Command ->where('plant', $plantId) ->get(); - $toEmails = $mailRules->pluck('email')->filter()->unique()->toArray(); - - $ccEmails = $mailRules->pluck('cc_emails') - ->filter() // remove null or empty - ->flatMap(function ($emails) { - return array_map('trim', explode(',', $emails)); - }) - ->filter() - ->unique() - ->toArray(); + // $toEmails = $mailRules->pluck('email') + // ->flatMap(function ($emails) { + // return array_map('trim', explode(',', $emails)); + // }) + // ->filter() + // ->toArray(); + // $ccEmails = $mailRules->pluck('cc_emails') + // ->filter() // remove null or empty + // ->flatMap(function ($emails) { + // return array_map('trim', explode(',', $emails)); + // }) + // ->filter() + // ->toArray(); $plants = $plantId == 0 ? Plant::all() @@ -212,15 +215,48 @@ class SendInvoiceDataReport extends Command // } // $this->info("Invoice data report sent to " . count($emails) . " recipient(s)."); // } - if (!empty($toEmails)) { - foreach ($toEmails as $email){ - \Mail::to($email)->cc($ccEmails)->send(new InvoiceDataMail($scheduleType, $tableData)); + // if (!empty($toEmails)) { + // foreach ($toEmails as $email){ + // \Mail::to($email)->cc($ccEmails)->send(new InvoiceDataMail($scheduleType, $tableData)); + // } + // $this->info("Invoice data report sent to " . count($toEmails) . " recipient(s)."); + // } + // else { + // $this->warn('No recipients found for InvoiceDataMail.'); + // } + + //.. + + 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("Invoice data report sent to " . count($toEmails) . " recipient(s)."); - } - else { - $this->warn('No recipients found for InvoiceDataMail.'); + + $mail = new InvoiceDataMail($scheduleType, $tableData); + + foreach ($toEmails as $email) { + \Mail::to($email)->cc($ccEmails)->send($mail); + } + + $this->info("Mail sent for rule ID {$rule->id} → To: " . implode(', ', $toEmails) . + ($ccEmails ? " | CC: " . implode(', ', $ccEmails) : '')); } } - }