From 6d6d3f2d605e283b6096bcee191bd16aed0d7d51 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 9 Jul 2025 10:18:57 +0530 Subject: [PATCH] chnaged logic for prooduction mail report --- app/Console/Commands/SendProductionReport.php | 276 ++++++------------ routes/console.php | 18 +- 2 files changed, 114 insertions(+), 180 deletions(-) diff --git a/app/Console/Commands/SendProductionReport.php b/app/Console/Commands/SendProductionReport.php index 455c3b0..3e2065e 100644 --- a/app/Console/Commands/SendProductionReport.php +++ b/app/Console/Commands/SendProductionReport.php @@ -29,187 +29,17 @@ class SendProductionReport extends Command /** * Execute the console command. */ - // public function handle() - // { - // $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities') - // ->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; - - // //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'); - // $productionQuantity = 0; - - // // Use the correct production quantity array based on line type - // if (in_array($lineId, $nonFgLineIds)) - // { - // $productionQuantity = $nonFgProduction[$lineId] ?? 0; - // } - // else - // { - // $fgproductionQuan = $fgProduction[$lineId] ?? 0; - // } - // $tableData[] = [ - // 'no' => $no++, - // 'plant' => $plantName, - // 'line' => $lineName, - // 'targetQuantity' => $targetQuantity, - // 'productionQuantity' => $productionQuantity, - // ]; - // } - // } - // $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities') - // ->where('rule_name', 'ProductionMail') - // ->get(); - - // $emails = $mailRules->pluck('email')->unique()->toArray(); - - // $lines = Line::all(); - // $lineMeta = []; // Store line name, type, and plant_id - // foreach ($lines as $line) { - // $lineMeta[$line->id] = [ - // 'name' => $line->name, - // 'type' => $line->type, - // 'plant_id' => $line->plant_id, - // ]; - // } - - // $startDate = now()->setTime(8, 0, 0); - // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); - - // $plants = Plant::all()->keyBy('id'); - - // $uniqueLineIds = ProductionQuantity::select('line_id')->distinct()->pluck('line_id'); - - // // Preload production quantities grouped by line_id - // $allProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) - // ->whereBetween('created_at', [$startDate, $endDate]) - // ->groupBy('line_id') - // ->pluck('total_quantity', 'line_id') - // ->toArray(); - - // $tableData = []; - // $no = 1; - - // foreach ($plants as $plantId => $plant) { - // foreach ($uniqueLineIds as $lineId) { - // if (!isset($lineMeta[$lineId]) || $lineMeta[$lineId]['plant_id'] != $plantId) { - // continue; - // } - - // $lineInfo = $lineMeta[$lineId]; - // $lineName = $lineInfo['name']; - - // $targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId) - // ->where('line_id', $lineId) - // ->whereBetween('created_at', [$startDate, $endDate]) - // ->sum('plan_quantity'); - - // $productionQuantity = $allProduction[$lineId] ?? 0; - - // $tableData[] = [ - // 'no' => $no++, - // 'plant' => $plant->name, - // 'line' => $lineName, - // 'targetQuantity' => $targetQuantity, - // 'productionQuantity' => $productionQuantity, - // ]; - // } - // } - - // //$this->info(json_encode($tableData)); - // if (!empty($emails)) - // { - // // Mail::to($emails)->send(new test($tableData)); - // $this->info('production 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)); - // } - public function handle() { - $now = now(); $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities') - ->where('rule_name', 'ProductionMail') - ->get(); + ->where('rule_name', 'ProductionMail') + ->get(); - // Filter emails based on current time and schedule_type - $filteredEmails = $mailRules->filter(function ($rule) use ($now) { - return match ($rule->schedule_type) { - 'Live' => true, - 'Hourly' => $now->minute === 0, - 'Daily' => $now->format('H:i') === '07:59', - default => false, - }; - })->pluck('email')->unique()->toArray(); + $emails = $mailRules->pluck('email')->unique()->toArray(); $lines = Line::all(); - $lineMeta = []; + $lineMeta = []; // Store line name, type, and plant_id foreach ($lines as $line) { $lineMeta[$line->id] = [ 'name' => $line->name, @@ -225,6 +55,7 @@ class SendProductionReport extends Command $uniqueLineIds = ProductionQuantity::select('line_id')->distinct()->pluck('line_id'); + // Preload production quantities grouped by line_id $allProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) ->whereBetween('created_at', [$startDate, $endDate]) ->groupBy('line_id') @@ -260,14 +91,101 @@ class SendProductionReport extends Command } } - if (!empty($filteredEmails)) { - $this->info('Production report sent to: ' . implode(', ', $filteredEmails)); - foreach ($filteredEmails as $email) { + //$this->info(json_encode($tableData)); + if (!empty($emails)) + { + // Mail::to($emails)->send(new test($tableData)); + //$this->info('production report email sent successfully to: ' . implode(', ', $emails)); + foreach ($emails as $email) + { Mail::to($email)->send(new ProductionMail($tableData)); } - } else { - $this->info('No matching recipients for this minute.'); } + else + { + $this->info('No recipients found for InvoiceMailAlert.'); + } + //$this->info(implode(', ', $emails)); } + // public function handle() + // { + // $now = now(); + + // $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities') + // ->where('rule_name', 'ProductionMail') + // ->get(); + + // // Filter emails based on current time and schedule_type + // $filteredEmails = $mailRules->filter(function ($rule) use ($now) { + // return match ($rule->schedule_type) { + // 'Live' => true, + // 'Hourly' => $now->minute === 0, + // 'Daily' => $now->format('H:i') === '07:59', + // default => false, + // }; + // })->pluck('email')->unique()->toArray(); + + // $lines = Line::all(); + // $lineMeta = []; + // foreach ($lines as $line) { + // $lineMeta[$line->id] = [ + // 'name' => $line->name, + // 'type' => $line->type, + // 'plant_id' => $line->plant_id, + // ]; + // } + + // $startDate = now()->setTime(8, 0, 0); + // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + + // $plants = Plant::all()->keyBy('id'); + + // $uniqueLineIds = ProductionQuantity::select('line_id')->distinct()->pluck('line_id'); + + // $allProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity')) + // ->whereBetween('created_at', [$startDate, $endDate]) + // ->groupBy('line_id') + // ->pluck('total_quantity', 'line_id') + // ->toArray(); + + // $tableData = []; + // $no = 1; + + // foreach ($plants as $plantId => $plant) { + // foreach ($uniqueLineIds as $lineId) { + // if (!isset($lineMeta[$lineId]) || $lineMeta[$lineId]['plant_id'] != $plantId) { + // continue; + // } + + // $lineInfo = $lineMeta[$lineId]; + // $lineName = $lineInfo['name']; + + // $targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId) + // ->where('line_id', $lineId) + // ->whereBetween('created_at', [$startDate, $endDate]) + // ->sum('plan_quantity'); + + // $productionQuantity = $allProduction[$lineId] ?? 0; + + // $tableData[] = [ + // 'no' => $no++, + // 'plant' => $plant->name, + // 'line' => $lineName, + // 'targetQuantity' => $targetQuantity, + // 'productionQuantity' => $productionQuantity, + // ]; + // } + // } + + // if (!empty($filteredEmails)) { + // $this->info('Production report sent to: ' . implode(', ', $filteredEmails)); + // foreach ($filteredEmails as $email) { + // Mail::to($email)->send(new ProductionMail($tableData)); + // } + // } else { + // $this->info('No matching recipients for this minute.'); + // } + // } + } diff --git a/routes/console.php b/routes/console.php index 3621b12..d847ca2 100644 --- a/routes/console.php +++ b/routes/console.php @@ -142,7 +142,23 @@ app()->booted(function () { $schedule = app(Schedule::class); //Always run every minute — the command itself will filter recipients - $schedule->command('send:production-report')->everyMinute(); + //$schedule->command('send:production-report')->everyMinute(); + + // Production Report Scheduling + $productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities') + ->where('rule_name', 'ProductionMail') + ->value('schedule_type'); + + if ($productionScheduleType) { + $command = $schedule->command('send:production-report'); + + match ($productionScheduleType) { + 'Live' => $command->everyMinute(), + 'Hourly' => $command->hourly(), + 'Daily' => $command->dailyAt('07:59'), + default => null, + }; + } });