From d04e118bf674991cf61ead0089a20480432e9512 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 20 Jan 2026 18:42:19 +0530 Subject: [PATCH] changed logic in invoice report --- app/Console/Commands/SendInvoiceReport.php | 110 ++++++++++++++++++--- 1 file changed, 95 insertions(+), 15 deletions(-) diff --git a/app/Console/Commands/SendInvoiceReport.php b/app/Console/Commands/SendInvoiceReport.php index 0981564..8e1f6aa 100644 --- a/app/Console/Commands/SendInvoiceReport.php +++ b/app/Console/Commands/SendInvoiceReport.php @@ -70,7 +70,7 @@ class SendInvoiceReport extends Command $scannedSerialCount = InvoiceValidation::select('invoice_number') ->where('plant_id', $plantId) ->whereNull('quantity') - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->groupBy('invoice_number') ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)") ->count(); @@ -86,7 +86,7 @@ class SendInvoiceReport extends Command $query->whereNull('quantity') ->orWhere('quantity', 0); }) - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->count(); $serialTableData[] = [ @@ -108,7 +108,7 @@ class SendInvoiceReport extends Command $scannedMatCount = InvoiceValidation::select('invoice_number') ->where('plant_id', $plantId) ->where('quantity', 1) - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->groupBy('invoice_number') ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)") ->count(); @@ -122,7 +122,7 @@ class SendInvoiceReport extends Command ->where('quantity', 1) ->whereNotNull('serial_number') ->where('serial_number', '!=', '') - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->count(); $materialTableData[] = [ @@ -144,7 +144,7 @@ class SendInvoiceReport extends Command $scannedBundleCount = InvoiceValidation::select('invoice_number') ->where('plant_id', $plantId) ->where('quantity', '>', 1) - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->groupBy('invoice_number') ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)") ->count(); @@ -158,7 +158,7 @@ class SendInvoiceReport extends Command ->where('quantity', '>', 1) ->whereNotNull('serial_number') ->where('serial_number', '!=', '') - ->whereBetween('updated_at', [$startDate, $endDate]) + ->whereBetween('created_at', [$startDate, $endDate]) ->count(); $bundleTableData[] = [ @@ -180,25 +180,104 @@ class SendInvoiceReport extends Command // Send to SerialInvoiceMail recipients if ($mailRules->has('SerialInvoiceMail')) { - $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray(); - foreach ($emails as $email) { - Mail::to($email)->send(new test($serialTableData, [], [], $schedule)); + // $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray(); + // foreach ($emails as $email) { + // Mail::to($email)->send(new test($serialTableData, [], [], $schedule)); + // } + foreach ($mailRules->get('SerialInvoiceMail') 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(new test($serialTableData, [], [], $schedule)); + + $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); } } // Send to MaterialInvoiceMail recipients (material + bundle table) if ($mailRules->has('MaterialInvoiceMail')) { - $emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray(); - foreach ($emails as $email) { - Mail::to($email)->send(new test([], $materialTableData, $bundleTableData, $schedule)); + // $emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray(); + // foreach ($emails as $email) { + // Mail::to($email)->send(new test([], $materialTableData, $bundleTableData, $schedule)); + // } + foreach ($mailRules->get('MaterialInvoiceMail') 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(new test([], $materialTableData, $bundleTableData, $schedule)); + + $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); } } // Send to InvoiceMail recipients (all three tables) if ($mailRules->has('InvoiceMail')) { - $emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray(); - foreach ($emails as $email) { - Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData, $schedule)); + //$emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray(); + // foreach ($emails as $email) { + // Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData, $schedule)); + // $this->info("✅ Sent InvoiceMail to: {$email}"); + // } + foreach ($mailRules->get('InvoiceMail') 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(new test($serialTableData, $materialTableData, $bundleTableData, $schedule)); + + $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); } } @@ -213,5 +292,6 @@ class SendInvoiceReport extends Command $this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice', 'TotalInvoice Quantity', 'ScannedInvoice Quantity'], $bundleTableData); $this->info($contentVars['wishes'] ?? ''); + } }