diff --git a/app/Console/Commands/SendInvoiceReport.php b/app/Console/Commands/SendInvoiceReport.php index a44e75942..a7c53b826 100644 --- a/app/Console/Commands/SendInvoiceReport.php +++ b/app/Console/Commands/SendInvoiceReport.php @@ -16,7 +16,7 @@ class SendInvoiceReport extends Command * @var string */ // protected $signature = 'app:send-invoice-report'; - protected $signature = 'send:invoice-report'; + protected $signature = 'send:invoice-report{schedule_type} {plant}'; /** @@ -29,168 +29,220 @@ class SendInvoiceReport extends Command /** * Execute the console command. */ - // public function handle() - // { - // // - // } // public function handle() // { - // $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation') - // ->where('rule_name', 'InvoiceMailAlert') - // ->get(); + // $schedule = $this->argument('schedule_type'); + // $plantid = $this->argument('plant'); - // $emails = $mailRules->pluck('email')->unique()->toArray(); + // $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'); - // $tableData = []; - // $no = 1; + + // $serialTableData = []; + // $materialTableData = []; + // $bundleTableData = []; + // $noSerial = 1; + // $noMaterial = 1; + // $noBundle = 1; // foreach ($plants as $plantId) { // $plant = Plant::find($plantId); // $plantName = $plant ? $plant->name : $plantId; - // $totalInvoice = InvoiceValidation::where('plant_id', $plantId) + // //..Serial Invoice + + // $totalSerialCount = InvoiceValidation::where('plant_id', $plantId) + // ->whereNull('quantity') + // ->whereBetween('created_at', [$startDate, $endDate]) // ->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') + // $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)") + // ->havingRaw( + // "COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)" + // ) // ->count(); - // $tableData[] = [ - // 'no' => $no++, + // $serialTableData[] = [ + // 'no' => $noSerial++, // 'plant' => $plantName, - // 'totalInvoice' => $totalInvoice, - // 'scannedInvoice' => $scannedInvoice, + // '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, // ]; // } - // // 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)); + // // 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($tableData)); + // Mail::to($email)->send(new test($serialTableData, [], [])); // } - // } - // else - // { - // $this->info('No recipients found for InvoiceMailAlert.'); + // // 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)); // } - public function handle() { - // Group emails by rule name + $schedule = $this->argument('schedule_type'); + $plantId = $this->argument('plant'); + $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; + $plant = Plant::find($plantId); + $plantName = $plant ? $plant->name : $plantId; - //..Serial Invoice + // Serial Invoice + $totalSerialCount = InvoiceValidation::where('plant_id', $plantId) + ->whereNull('quantity') + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); - $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(); - $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' => 1, + 'plant' => $plantName, + 'totalInvoice' => $totalSerialCount, + 'scannedInvoice' => $scannedSerialCount, + ]; - $serialTableData[] = [ - 'no' => $noSerial++, - 'plant' => $plantName, - 'totalInvoice' => $totalSerialCount, - 'scannedInvoice' => $scannedSerialCount, - ]; + // Individual Material Invoice + $TotalMatCount = InvoiceValidation::where('plant_id', $plantId) + ->where('quantity', 1) + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); - //..Individual Invoice + $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(); - $TotalMatCount = InvoiceValidation::where('plant_id', $plantId) - ->where('quantity', 1) - ->whereBetween('created_at', [$startDate, $endDate]) - ->distinct('invoice_number') - ->count('invoice_number'); + $materialTableData[] = [ + 'no' => 1, + 'plant' => $plantName, + 'totalInvoice' => $TotalMatCount, + 'scannedInvoice' => $scannedMatCount, + ]; - $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(); + // Bundle Invoice + $totalBundleCount = InvoiceValidation::where('plant_id', $plantId) + ->where('quantity', '>', 1) + ->whereBetween('created_at', [$startDate, $endDate]) + ->distinct('invoice_number') + ->count('invoice_number'); - $materialTableData[] = [ - 'no' => $noMaterial++, - 'plant' => $plantName, - 'totalInvoice' => $TotalMatCount, - 'scannedInvoice' => $scannedMatCount, - ]; + $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(); - //..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, - ]; - } + $bundleTableData[] = [ + 'no' => 1, + 'plant' => $plantName, + 'totalInvoice' => $totalBundleCount, + 'scannedInvoice' => $scannedBundleCount, + ]; // Send to SerialInvoiceMail recipients if ($mailRules->has('SerialInvoiceMail')) { @@ -216,8 +268,10 @@ class SendInvoiceReport extends Command } } - //$this->info(json_encode($materialTableData)); - + // $this->table( + // ['No', 'Plant', 'Total Invoice', 'Scanned Invoice'], + // $serialTableData + // ); } } diff --git a/routes/console.php b/routes/console.php index c7478c1ec..4b03034bf 100644 --- a/routes/console.php +++ b/routes/console.php @@ -12,57 +12,55 @@ Artisan::command('inspire', function () { // Schedule::command('send:invoice-report'); // Schedule::command('send:production-report'); -// app()->booted(function () { -// $schedule = app(Schedule::class); + app()->booted(function () { + $schedule = app(Schedule::class); -// $productionRules = AlertMailRule::where('module', 'ProductionQuantities') -// ->where('rule_name', 'ProductionMail') -// ->get(); + //Production report scheduling -// $types = $productionRules->pluck('schedule_type')->unique(); + $productionRules = AlertMailRule::where('module', 'ProductionQuantities') + ->where('rule_name', 'ProductionMail') + ->get(); -// foreach ($types as $type) { -// $command = $schedule->command("send:production-report", [$type]); -// switch ($type) { -// case 'Live': -// $command->everyMinute(); -// break; -// case 'Hourly': -// $command->hourly(); -// break; -// case 'Daily': -// $command->dailyAt('07:59'); -// break; -// } -// } -// }); + foreach ($productionRules as $rule) { + $type = $rule->schedule_type; + $plantId = $rule->plant; -app()->booted(function () { - $schedule = app(Schedule::class); + $command = $schedule->command('send:production-report', [$type, $plantId]); - $productionRules = AlertMailRule::where('module', 'ProductionQuantities') - ->where('rule_name', 'ProductionMail') - ->get(); - - // Loop each mail rule individually (plant + schedule_type) - foreach ($productionRules as $rule) { - $type = $rule->schedule_type; - $plantId = $rule->plant; - - $command = $schedule->command('send:production-report', [$type, $plantId]); - - switch ($type) { - case 'Live': - $command->everyMinute(); - break; - case 'Hourly': - $command->hourly(); - break; - case 'Daily': - $command->dailyAt('07:59'); - break; + switch ($type) { + case 'Live': + $command->everyMinute(); + break; + case 'Hourly': + $command->hourly(); + break; + case 'Daily': + $command->dailyAt('07:59'); + break; + } } - } -}); + + //Invoice report scheduling + $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')->get(); + + foreach ($invoiceRules as $rule) { + $type = $rule->schedule_type; + $plantId = $rule->plant; + + $command = $schedule->command('send:invoice-report', [$type, $plantId]); + + switch ($type) { + case 'Live': + $command->everyMinute(); + break; + case 'Hourly': + $command->hourly(); + break; + case 'Daily': + $command->dailyAt('07:59'); + break; + } + } + });