From cfe8ac34964b28fce6e401ba9659c7cd7f1b1cf6 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 5 Jul 2025 20:53:31 +0530 Subject: [PATCH] Added invoicemail ad productionmail report views pages and commands pages --- app/Console/Commands/ScheduleList.php | 55 +++++ app/Console/Commands/SendInvoiceReport.php | 223 ++++++++++++++++++ app/Console/Commands/SendProductionReport.php | 133 +++++++++++ app/Mail/ProductionMail.php | 61 +++++ app/Mail/test.php | 77 ++++++ .../views/mail/production_report.blade.php | 82 +++++++ resources/views/mail/test_mail.blade.php | 169 +++++++++++++ routes/console.php | 5 + 8 files changed, 805 insertions(+) create mode 100644 app/Console/Commands/ScheduleList.php create mode 100644 app/Console/Commands/SendInvoiceReport.php create mode 100644 app/Console/Commands/SendProductionReport.php create mode 100644 app/Mail/ProductionMail.php create mode 100644 app/Mail/test.php create mode 100644 resources/views/mail/production_report.blade.php create mode 100644 resources/views/mail/test_mail.blade.php diff --git a/app/Console/Commands/ScheduleList.php b/app/Console/Commands/ScheduleList.php new file mode 100644 index 000000000..2c0b9610f --- /dev/null +++ b/app/Console/Commands/ScheduleList.php @@ -0,0 +1,55 @@ +where('rule_name', 'InvoiceMailAlert') + ->value('schedule_type'); + + switch ($scheduleType) { + case 'Live': + $schedule->command('send:invoice-report')->everyMinute()->runInBackground(); + break; + case 'Hourly': + $schedule->command('send:invoice-report')->hourly()->runInBackground(); + break; + // case 'Daily': + // $schedule->command('send:invoice-report')->daily()->runInBackground(); //->dailyAt('07:59') + // break; + case 'Daily': + $schedule->command('send:invoice-report')->dailyAt('07:59')->runInBackground(); //->dailyAt('07:59') + break; + default: + $schedule->command('send:invoice-report')->hourly()->runInBackground(); + break; + } + + // Production Report Scheduling + $productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities') + ->where('rule_name', 'ProductionMailAlert') + ->value('schedule_type'); + + switch ($productionScheduleType) { + case 'Live': + $schedule->command('send:production-report')->everyMinute()->runInBackground(); + break; + case 'Hourly': + $schedule->command('send:production-report')->hourly()->runInBackground(); + break; + case 'Daily': + $schedule->command('send:production-report')->dailyAt('07:59')->runInBackground(); + break; + default: + $schedule->command('send:production-report')->hourly()->runInBackground(); + break; + } + } +} diff --git a/app/Console/Commands/SendInvoiceReport.php b/app/Console/Commands/SendInvoiceReport.php new file mode 100644 index 000000000..a44e75942 --- /dev/null +++ b/app/Console/Commands/SendInvoiceReport.php @@ -0,0 +1,223 @@ +where('rule_name', 'InvoiceMailAlert') + // ->get(); + + // $emails = $mailRules->pluck('email')->unique()->toArray(); + // $plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id'); + // $tableData = []; + // $no = 1; + + // foreach ($plants as $plantId) { + // $plant = Plant::find($plantId); + // $plantName = $plant ? $plant->name : $plantId; + + // $totalInvoice = InvoiceValidation::where('plant_id', $plantId) + // ->distinct('invoice_number') + // ->count('invoice_number'); + + // $startDate = now()->setTime(8, 0, 0); + // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + + // $scannedInvoice = InvoiceValidation::select('invoice_number') + // ->where('plant_id', $plantId) + // ->whereNull('quantity') + // ->whereBetween('updated_at', [$startDate, $endDate]) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)") + // ->count(); + + // $tableData[] = [ + // 'no' => $no++, + // 'plant' => $plantName, + // 'totalInvoice' => $totalInvoice, + // 'scannedInvoice' => $scannedInvoice, + // ]; + // } + + // // Mail::to('jothikumar.padmanaban@cripumps.com')->send( + // // new test($tableData) + // // ); + + // if (!empty($emails)) + // { + // // Mail::to($emails)->send(new test($tableData)); + // // $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails)); + // foreach ($emails as $email) { + // Mail::to($email)->send(new test($tableData)); + // } + + // } + + // else + // { + // $this->info('No recipients found for InvoiceMailAlert.'); + // } + + // } + + + public function handle() + { + // Group emails by rule name + $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name'); + + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + $plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id'); + + // Build all tables once + $serialTableData = []; + $materialTableData = []; + $bundleTableData = []; + $noSerial = 1; + $noMaterial = 1; + $noBundle = 1; + + foreach ($plants as $plantId) { + $plant = Plant::find($plantId); + $plantName = $plant ? $plant->name : $plantId; + + //..Serial Invoice + + $totalSerialCount = InvoiceValidation::where('plant_id', $plantId) + ->whereNull('quantity') + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); + + $scannedSerialCount = InvoiceValidation::select('invoice_number') + ->where('plant_id', $plantId) + ->whereNull('quantity') + ->whereBetween('updated_at', [$startDate, $endDate]) + ->groupBy('invoice_number') + ->havingRaw( + "COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)" + ) + ->count(); + + $serialTableData[] = [ + 'no' => $noSerial++, + 'plant' => $plantName, + 'totalInvoice' => $totalSerialCount, + 'scannedInvoice' => $scannedSerialCount, + ]; + + //..Individual Invoice + + $TotalMatCount = InvoiceValidation::where('plant_id', $plantId) + ->where('quantity', 1) + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); + + $scannedMatCount = InvoiceValidation::select('invoice_number') + ->where('plant_id', $plantId) + ->where('quantity', 1) + ->whereBetween('updated_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(); + + $materialTableData[] = [ + 'no' => $noMaterial++, + 'plant' => $plantName, + 'totalInvoice' => $TotalMatCount, + 'scannedInvoice' => $scannedMatCount, + ]; + + //..BUndle Invoice + + $totalBundleCount = InvoiceValidation::where('plant_id', $plantId) + ->where('quantity', '>', 1) + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); + + $scannedBundleCount = InvoiceValidation::select('invoice_number') + ->where('plant_id', $plantId) + ->where('quantity', '>', 1) + ->whereBetween('updated_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(); + + $bundleTableData[] = [ + 'no' => $noBundle++, + 'plant' => $plantName, + 'totalInvoice' => $totalBundleCount, + 'scannedInvoice' => $scannedBundleCount, + ]; + } + + // 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, [], [])); + } + } + + // 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)); + } + } + + // 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)); + } + } + + //$this->info(json_encode($materialTableData)); + + } + +} diff --git a/app/Console/Commands/SendProductionReport.php b/app/Console/Commands/SendProductionReport.php new file mode 100644 index 000000000..8e67cd854 --- /dev/null +++ b/app/Console/Commands/SendProductionReport.php @@ -0,0 +1,133 @@ +where('rule_name', 'ProductionMail') + ->get(); + + $emails = $mailRules->pluck('email')->unique()->toArray(); + + $lines = Line::all(); + $fgLineIds = []; + $nonFgLineIds = []; + $lineNames = []; + + foreach ($lines as $line) { + $lineNames[$line->id] = $line->name; + if ($line->type == 'FG Line') { + $fgLineIds[] = $line->id; + } else { + $nonFgLineIds[] = $line->id; + } + } + + // 2. Set date range + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + + // 3. Get all plants + $plants = ProductionQuantity::select('plant_id')->distinct()->pluck('plant_id'); + + // 4. Get production quantities for FG and non-FG lines in one go + // $fgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) + // ->whereBetween('created_at', [$startDate, $endDate]) + // ->whereIn('line_id', $fgLineIds) + // ->groupBy('line_id') + // ->pluck('total_quantity', 'line_id') + // ->toArray(); + + $nonFgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) + ->whereBetween('created_at', [$startDate, $endDate]) + ->whereIn('line_id', $nonFgLineIds) + ->groupBy('line_id') + ->pluck('total_quantity', 'line_id') + ->toArray(); + + $tableData = []; + $no = 1; + + foreach ($plants as $plantId) + { + $plant = Plant::find($plantId); + $plantName = $plant ? $plant->name : $plantId; + + //Get all unique line_ids for this plant + $lineIds = ProductionQuantity::where('plant_id', $plantId) + ->select('line_id') + ->distinct() + ->pluck('line_id'); + + foreach ($lineIds as $lineId) { + $lineName = $lineNames[$lineId] ?? $lineId; + + $targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->whereBetween('created_at', [$startDate, $endDate]) + ->sum('plan_quantity'); + + // Use the correct production quantity array based on line type + if (in_array($lineId, $nonFgLineIds)) + { + $productionQuantity = $nonFgProduction[$lineId] ?? 0; + } + // else + // { + // $productionQuantity = $nonFgProduction[$lineId] ?? 0; + // } + $tableData[] = [ + 'no' => $no++, + 'plant' => $plantName, + 'line' => $lineName, + 'targetQuantity' => $targetQuantity, + 'productionQuantity' => $productionQuantity, + ]; + } + } + //$this->info(json_encode($tableData)); + + if (!empty($emails)) + { + // Mail::to($emails)->send(new test($tableData)); + // $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails)); + foreach ($emails as $email) + { + Mail::to($email)->send(new ProductionMail($tableData)); + } + } + else + { + $this->info('No recipients found for InvoiceMailAlert.'); + } + //$this->info(implode(', ', $emails)); + } +} diff --git a/app/Mail/ProductionMail.php b/app/Mail/ProductionMail.php new file mode 100644 index 000000000..93c508423 --- /dev/null +++ b/app/Mail/ProductionMail.php @@ -0,0 +1,61 @@ +tableData = $tableData ?? []; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: 'Production Mail', + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + view: 'mail.production_report', + with: [ + 'company' => "CRI Digital Manufacturing Solutions", + 'greeting' => "Dear Sir/Madam,

Kindly find the attached production report status details for the 'Target Quantity' and 'Production Quantity' count,", + 'tableData' => $this->tableData, + 'wishes' => "Thanks & Regards,
CRI Digital Manufacturing Solutions" + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/app/Mail/test.php b/app/Mail/test.php new file mode 100644 index 000000000..167297eac --- /dev/null +++ b/app/Mail/test.php @@ -0,0 +1,77 @@ +tableData = $tableData ?? []; + // } + public function __construct($serialTableData, $materialTableData, $bundleTableData) + { + $this->serialTableData = $serialTableData; + $this->materialTableData = $materialTableData; + $this->bundleTableData = $bundleTableData; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: 'Invoice Table Report' + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + view: 'mail.test_mail', + // with: [ + // 'name' => "CRI Digital Manufacturing Solutions

Dear Sir/Madam,

Kindly find the attached invoice status details for the 'Total Number Of Invoices' and 'Scanned Number Of Invoices' count,", + // 'tableData' => $this->tableData, + // 'wishes' => "Thanks & Regards

CRI Digital Manufacturing Solutions" + // ], + with: [ + 'company' => "CRI Digital Manufacturing Solutions", + 'greeting' => "Dear Sir/Madam,

Kindly find the attached invoice status details for the 'Total Number of Invoices' and 'Scanned Number of Invoices' count,", + //'tableData' => $this->tableData, + 'serialTableData' => $this->serialTableData, + 'materialTableData' => $this->materialTableData, + 'bundleTableData' => $this->bundleTableData, + 'wishes' => "Thanks & Regards,
CRI Digital Manufacturing Solutions" + ], + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/resources/views/mail/production_report.blade.php b/resources/views/mail/production_report.blade.php new file mode 100644 index 000000000..aedf641b3 --- /dev/null +++ b/resources/views/mail/production_report.blade.php @@ -0,0 +1,82 @@ + + + + Invoice Table + + + + {{--
{{ $name }}
--}} + {{--
{!! $name !!}
--}} +
+ {{ $company }} +
+
+

{!! $greeting !!}

+ {{-- + + + + + + + + + + + @foreach($tableData as $row) + {{-- + + + + + --}} + {{-- + + + + + --}} + {{-- --}} + {{-- + @endforeach + +
NoPlantLineTarget QuantityProduction Quantity
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['line'] }}{{ $row['targetQuantity'] }}{{ $row['productionQuantity'] }}
--}} + + + + + + + + + + + + @foreach($tableData as $row) + + + + + + + + @endforeach + +
NoPlantLineTarget QuantityProduction Quantity
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['line'] }}{{ $row['targetQuantity'] }}{{ $row['productionQuantity'] }}
+ +

{!! $wishes !!}

+ + diff --git a/resources/views/mail/test_mail.blade.php b/resources/views/mail/test_mail.blade.php new file mode 100644 index 000000000..c21432428 --- /dev/null +++ b/resources/views/mail/test_mail.blade.php @@ -0,0 +1,169 @@ +{{-- {{ $name }} --}} + +{{-- + + + Invoice Table + + + + {{--
{{ $name }}
--}} + {{--
{!! $name !!}
--}} + {{--
+ {{ $company }} +
+
+
{!! $greeting !!}
+ + + + + + + + + + + @foreach($tableData as $row) --}} + {{-- + + + + + --}} + {{-- + + + + + + @endforeach + +
NoPlantTotal Number Of InvoiceScanned Number Of Invoice
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
+
{!! $wishes !!}
+ + --}} + + + + + + Invoice Tables + + + +
+ {{ $company }} +
+
+

{!! $greeting !!}

+ + {{-- Serial Invoice Table --}} + @if(!empty($serialTableData)) +

Serial Invoice

+ + + + + + + + + + + @foreach($serialTableData as $row) + + + + + + + @endforeach + +
NoPlantTotal Number of InvoiceScanned Number of Invoice
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
+ @endif + + {{-- Material Invoice Table --}} + @if(!empty($materialTableData)) +

Material Invoice

+ + + + + + + + + + + @foreach($materialTableData as $row) + + + + + + + @endforeach + +
NoPlantTotal Number of InvoiceScanned Number of Invoice
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
+ @endif + + {{-- Bundle Invoice Table --}} + @if(!empty($bundleTableData)) +

Bundle Invoice

+ + + + + + + + + + + @foreach($bundleTableData as $row) + + + + + + + @endforeach + +
NoPlantTotal Number of InvoiceScanned Number of Invoice
{{ $row['no'] }}{{ $row['plant'] }}{{ $row['totalInvoice'] }}{{ $row['scannedInvoice'] }}
+ @endif +

{!! $wishes !!}

+ + + + diff --git a/routes/console.php b/routes/console.php index 3c9adf1af..7d6d685dc 100644 --- a/routes/console.php +++ b/routes/console.php @@ -1,8 +1,13 @@ comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); + +Schedule::command('send:invoice-report'); +Schedule::command('send:production-report'); +