1
0
forked from poc/pds

Added logic for production mail report

This commit is contained in:
dhanabalan
2025-07-09 10:04:54 +05:30
parent 5ab109006e
commit 29de91e03d
2 changed files with 266 additions and 120 deletions

View File

@@ -29,46 +29,204 @@ class SendProductionReport extends Command
/** /**
* Execute the console 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() public function handle()
{ {
$now = now();
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities') $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail') ->where('rule_name', 'ProductionMail')
->get(); ->get();
$emails = $mailRules->pluck('email')->unique()->toArray(); // 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(); $lines = Line::all();
$fgLineIds = []; $lineMeta = [];
$nonFgLineIds = [];
$lineNames = [];
foreach ($lines as $line) { foreach ($lines as $line) {
$lineNames[$line->id] = $line->name; $lineMeta[$line->id] = [
if ($line->type == 'FG Line') { 'name' => $line->name,
$fgLineIds[] = $line->id; 'type' => $line->type,
} else { 'plant_id' => $line->plant_id,
$nonFgLineIds[] = $line->id; ];
}
} }
// 2. Set date range
$startDate = now()->setTime(8, 0, 0); $startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// 3. Get all plants $plants = Plant::all()->keyBy('id');
$plants = ProductionQuantity::select('plant_id')->distinct()->pluck('plant_id');
// 4. Get production quantities for FG and non-FG lines in one go $uniqueLineIds = ProductionQuantity::select('line_id')->distinct()->pluck('line_id');
$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')) $allProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
->whereIn('line_id', $nonFgLineIds)
->groupBy('line_id') ->groupBy('line_id')
->pluck('total_quantity', 'line_id') ->pluck('total_quantity', 'line_id')
->toArray(); ->toArray();
@@ -76,59 +234,40 @@ class SendProductionReport extends Command
$tableData = []; $tableData = [];
$no = 1; $no = 1;
foreach ($plants as $plantId) foreach ($plants as $plantId => $plant) {
{ foreach ($uniqueLineIds as $lineId) {
$plant = Plant::find($plantId); if (!isset($lineMeta[$lineId]) || $lineMeta[$lineId]['plant_id'] != $plantId) {
$plantName = $plant ? $plant->name : $plantId; continue;
}
//Get all unique line_ids for this plant $lineInfo = $lineMeta[$lineId];
$lineIds = ProductionQuantity::where('plant_id', $plantId) $lineName = $lineInfo['name'];
->select('line_id')
->distinct()
->pluck('line_id');
foreach ($lineIds as $lineId) {
$lineName = $lineNames[$lineId] ?? $lineId;
$targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId) $targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId)
->where('line_id', $lineId) ->where('line_id', $lineId)
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
->sum('plan_quantity'); ->sum('plan_quantity');
$productionQuantity = 0;
$productionQuantity = $allProduction[$lineId] ?? 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[] = [ $tableData[] = [
'no' => $no++, 'no' => $no++,
'plant' => $plantName, 'plant' => $plant->name,
'line' => $lineName, 'line' => $lineName,
'targetQuantity' => $targetQuantity, 'targetQuantity' => $targetQuantity,
'productionQuantity' => $productionQuantity, 'productionQuantity' => $productionQuantity,
]; ];
} }
} }
//$this->info(json_encode($tableData));
if (!empty($emails)) if (!empty($filteredEmails)) {
{ $this->info('Production report sent to: ' . implode(', ', $filteredEmails));
// Mail::to($emails)->send(new test($tableData)); foreach ($filteredEmails as $email) {
$this->info('production report email sent successfully to: ' . implode(', ', $emails));
foreach ($emails as $email)
{
Mail::to($email)->send(new ProductionMail($tableData)); 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));
} }
} }

View File

@@ -74,68 +74,75 @@ Artisan::command('inspire', function () {
// break; // break;
// } // }
// app()->booted(function () {
// $schedule = app(Schedule::class);
// // Dynamic rules for invoice reports
// $rules = [
// [
// 'module' => 'InvoiceValidation',
// 'rule_name' => 'InvoiceMail',
// 'argument' => 'InvoiceMail',
// ],
// [
// 'module' => 'InvoiceValidation',
// 'rule_name' => 'SerialInvoiceMail',
// 'argument' => 'SerialInvoiceMail',
// ],
// [
// 'module' => 'InvoiceValidation',
// 'rule_name' => 'MaterialInvoiceMail',
// 'argument' => 'MaterialInvoiceMail',
// ],
// ];
// foreach ($rules as $rule) {
// $scheduleType = AlertMailRule::where('module', $rule['module'])
// ->where('rule_name', $rule['rule_name'])
// ->value('schedule_type');
// switch ($scheduleType) {
// case 'Live':
// $schedule->command("send:invoice-report {$rule['argument']}")->everyMinute();
// break;
// case 'Hourly':
// $schedule->command("send:invoice-report {$rule['argument']}")->hourly();
// break;
// case 'Daily':
// $schedule->command("send:invoice-report {$rule['argument']}")->dailyAt('07:59');
// break;
// default:
// $schedule->command("send:invoice-report {$rule['argument']}")->hourly();
// break;
// }
// }
// // Production Report Scheduling
// $productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities')
// ->where('rule_name', 'ProductionMail')
// ->value('schedule_type');
// switch ($productionScheduleType) {
// case 'Live':
// $schedule->command('send:production-report')->everyMinute();
// break;
// case 'Hourly':
// $schedule->command('send:production-report')->hourly();
// break;
// case 'Daily':
// $schedule->command('send:production-report')->dailyAt('07:59');
// break;
// default:
// $schedule->command('send:production-report')->hourly();
// break;
// }
// });
app()->booted(function () { app()->booted(function () {
$schedule = app(Schedule::class); $schedule = app(Schedule::class);
// Dynamic rules for invoice reports //Always run every minute — the command itself will filter recipients
$rules = [ $schedule->command('send:production-report')->everyMinute();
[
'module' => 'InvoiceValidation',
'rule_name' => 'InvoiceMail',
'argument' => 'InvoiceMail',
],
[
'module' => 'InvoiceValidation',
'rule_name' => 'SerialInvoiceMail',
'argument' => 'SerialInvoiceMail',
],
[
'module' => 'InvoiceValidation',
'rule_name' => 'MaterialInvoiceMail',
'argument' => 'MaterialInvoiceMail',
],
];
foreach ($rules as $rule) {
$scheduleType = AlertMailRule::where('module', $rule['module'])
->where('rule_name', $rule['rule_name'])
->value('schedule_type');
switch ($scheduleType) {
case 'Live':
$schedule->command("send:invoice-report {$rule['argument']}")->everyMinute();
break;
case 'Hourly':
$schedule->command("send:invoice-report {$rule['argument']}")->hourly();
break;
case 'Daily':
$schedule->command("send:invoice-report {$rule['argument']}")->dailyAt('07:59');
break;
default:
$schedule->command("send:invoice-report {$rule['argument']}")->hourly();
break;
}
}
// Production Report Scheduling
$productionScheduleType = AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail')
->value('schedule_type');
switch ($productionScheduleType) {
case 'Live':
$schedule->command('send:production-report')->everyMinute();
break;
case 'Hourly':
$schedule->command('send:production-report')->hourly();
break;
case 'Daily':
$schedule->command('send:production-report')->dailyAt('07:59');
break;
default:
$schedule->command('send:production-report')->hourly();
break;
}
}); });