From f47f5182a20e183b49e4b50f8154427580a2e398 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 2 Nov 2025 17:37:02 +0530 Subject: [PATCH] Refactor SendInvoiceDataReport command to correctly handle CC emails and improve email fetching logic --- .../Commands/SendInvoiceDataReport.php | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/app/Console/Commands/SendInvoiceDataReport.php b/app/Console/Commands/SendInvoiceDataReport.php index 0da2e87..eac07a0 100644 --- a/app/Console/Commands/SendInvoiceDataReport.php +++ b/app/Console/Commands/SendInvoiceDataReport.php @@ -36,18 +36,36 @@ class SendInvoiceDataReport extends Command $plantId = (int) $this->argument('plant'); // cast to int for safety - // Fetch mail rules based on schedule type + // // Fetch mail rules based on schedule type + // $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceDataReport') + // ->where('rule_name', 'InvoiceDataMail') + // ->where('schedule_type', $scheduleType) + // ->where('plant', $plantId) + // ->get(); + + // $emails = $mailRules->pluck('email')->unique()->toArray(); + + // $ccEmails = $mailRules->pluck('cc_emails')->filter()->unique()->toArray(); + + // $this->info('Found emails: ' . implode(', ', $ccEmails)); + $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceDataReport') ->where('rule_name', 'InvoiceDataMail') ->where('schedule_type', $scheduleType) ->where('plant', $plantId) ->get(); - $emails = $mailRules->pluck('email')->unique()->toArray(); + $toEmails = $mailRules->pluck('email')->filter()->unique()->toArray(); - //$ccEmails = $mailRules->pluck('cc_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(); - //$this->info('Found emails: ' . implode(', ', $emails)); $plants = $plantId == 0 ? Plant::all() @@ -187,13 +205,20 @@ class SendInvoiceDataReport extends Command ); $this->info($contentVars['wishes'] ?? ''); - // Send Mail //->cc($ccEmails) - if (!empty($emails)) { - foreach ($emails as $email){ - \Mail::to($email)->send(new InvoiceDataMail($scheduleType, $tableData)); + // Send Mail // + // if (!empty($emails)) { + // foreach ($emails as $email){ + // \Mail::to($email)->cc($ccEmails)->send(new InvoiceDataMail($scheduleType, $tableData)); + // } + // $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)); } - $this->info("Invoice data report sent to " . count($emails) . " recipient(s)."); - } else { + $this->info("Invoice data report sent to " . count($toEmails) . " recipient(s)."); + } + else { $this->warn('No recipients found for InvoiceDataMail.'); } }