Added logic for all plants in production report
This commit is contained in:
@@ -32,131 +32,135 @@ class SendProductionReport extends Command
|
|||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// public function handle()
|
// public function handle()
|
||||||
// {
|
// {
|
||||||
|
// $scheduleType = $this->argument('schedule_type');
|
||||||
|
// $plantId = $this->argument('plant');
|
||||||
|
|
||||||
// $scheduleType = $this->argument('schedule_type');
|
// $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||||
// //$plant = $this->argument('plant');
|
// ->where('rule_name', 'ProductionMail')
|
||||||
// $plant = (int) $this->argument('plant');
|
// ->where('plant', $plantId)
|
||||||
|
// ->where('schedule_type', $scheduleType)
|
||||||
// $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
// ->get();
|
||||||
// ->where('rule_name', 'ProductionMail')
|
|
||||||
// ->where('plant', $plant)
|
|
||||||
// ->where('schedule_type', $scheduleType)
|
|
||||||
// ->get();
|
|
||||||
|
|
||||||
// $emails = $mailRules->pluck('email')->unique()->toArray();
|
// $emails = $mailRules->pluck('email')->unique()->toArray();
|
||||||
|
|
||||||
// $plants = Plant::all()->keyBy('id');
|
// $plant = Plant::find($plantId);
|
||||||
// $lines = Line::all()->groupBy('plant_id'); // Group lines by plant for easy lookup
|
|
||||||
|
// if (!$plant) {
|
||||||
|
// $this->error("Invalid plant ID: $plantId");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $lines = Line::where('plant_id', $plantId)->get();
|
||||||
|
|
||||||
// $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);
|
||||||
|
|
||||||
|
// $PlanstartDate = now()->setTime(8, 0, 0);
|
||||||
|
// $planendDate = now()->copy()->addDay()->setTime(7, 59, 00);
|
||||||
|
|
||||||
// $tableData = [];
|
// $tableData = [];
|
||||||
// $no = 1;
|
// $no = 1;
|
||||||
|
|
||||||
// foreach ($plants as $plantId => $plant) {
|
// foreach ($lines as $line) {
|
||||||
// $plantLines = $lines->get($plantId, collect());
|
// $lineId = $line->id;
|
||||||
// foreach ($plantLines as $line) {
|
// $lineName = $line->name;
|
||||||
// $lineId = $line->id;
|
|
||||||
// $lineName = $line->name;
|
|
||||||
|
|
||||||
// $targetQuantity = ProductionPlan::where('plant_id', $plant)
|
// $targetQuantity = ProductionPlan::where('plant_id', $plantId)
|
||||||
// ->where('line_id', $lineId)
|
// ->where('line_id', $lineId)
|
||||||
// ->whereBetween('created_at', [$startDate, $endDate])
|
// ->whereBetween('created_at', [$PlanstartDate, $planendDate])
|
||||||
// ->sum('plan_quantity');
|
// ->sum('plan_quantity');
|
||||||
|
|
||||||
// $productionQuantity = ProductionQuantity::where('plant_id', $plant)
|
// $productionQuantity = ProductionQuantity::where('plant_id', $plantId)
|
||||||
// ->where('line_id', $lineId)
|
// ->where('line_id', $lineId)
|
||||||
// ->whereBetween('created_at', [$startDate, $endDate])
|
// ->whereBetween('created_at', [$startDate, $endDate])
|
||||||
// ->count();
|
// ->count();
|
||||||
|
|
||||||
// $tableData[] = [
|
// $tableData[] = [
|
||||||
// 'no' => $no++,
|
// 'no' => $no++,
|
||||||
// 'plant' => $plant->name,
|
// 'plant' => $plant->name,
|
||||||
// 'line' => $lineName,
|
// 'line' => $lineName,
|
||||||
// 'targetQuantity' => $targetQuantity,
|
// 'targetQuantity' => $targetQuantity,
|
||||||
// 'productionQuantity' => $productionQuantity,
|
// 'productionQuantity' => $productionQuantity,
|
||||||
// ];
|
// ];
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// $this->table(
|
// // $this->table(
|
||||||
// ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
|
// // ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
|
||||||
// $tableData
|
// // $tableData
|
||||||
// );
|
// // );
|
||||||
|
|
||||||
// if (!empty($emails))
|
// if (!empty($emails)) {
|
||||||
// {
|
// foreach ($emails as $email) {
|
||||||
// foreach ($emails as $email)
|
|
||||||
// {
|
|
||||||
// Mail::to($email)->send(new ProductionMail($tableData));
|
// Mail::to($email)->send(new ProductionMail($tableData));
|
||||||
// }
|
// }
|
||||||
// }
|
// } else {
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// $this->info('No recipients found for ProductionMailAlert.');
|
// $this->info('No recipients found for ProductionMailAlert.');
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// $this->info("Production report sent to " . count($emails) . " recipient(s).");
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$scheduleType = $this->argument('schedule_type');
|
$scheduleType = $this->argument('schedule_type');
|
||||||
$plantId = (int) $this->argument('plant'); // Only one plant
|
$plantId = (int) $this->argument('plant'); // cast to int for safety
|
||||||
|
|
||||||
|
// Fetch mail rules based on schedule type
|
||||||
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||||
->where('rule_name', 'ProductionMail')
|
->where('rule_name', 'ProductionMail')
|
||||||
->where('plant', $plantId)
|
|
||||||
->where('schedule_type', $scheduleType)
|
->where('schedule_type', $scheduleType)
|
||||||
|
->where('plant', $plantId)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
$emails = $mailRules->pluck('email')->unique()->toArray();
|
$emails = $mailRules->pluck('email')->unique()->toArray();
|
||||||
|
|
||||||
$plant = Plant::find($plantId);
|
$plants = $plantId == 0
|
||||||
|
? Plant::all()
|
||||||
|
: Plant::where('id', $plantId)->get();
|
||||||
|
|
||||||
if (!$plant) {
|
if ($plants->isEmpty()) {
|
||||||
$this->error("Invalid plant ID: $plantId");
|
$this->error("No valid plant(s) found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$lines = Line::where('plant_id', $plantId)->get();
|
|
||||||
|
|
||||||
$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);
|
||||||
|
|
||||||
$PlanstartDate = now()->setTime(8, 0, 0);
|
$PlanstartDate = now()->setTime(8, 0, 0);
|
||||||
$planendDate = now()->copy()->addDay()->setTime(7, 59, 00);
|
$planendDate = now()->copy()->addDay()->setTime(7, 59, 0);
|
||||||
|
|
||||||
$tableData = [];
|
$tableData = [];
|
||||||
$no = 1;
|
$no = 1;
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($plants as $plant) {
|
||||||
$lineId = $line->id;
|
$lines = Line::where('plant_id', $plant->id)->get();
|
||||||
$lineName = $line->name;
|
|
||||||
|
|
||||||
$targetQuantity = ProductionPlan::where('plant_id', $plantId)
|
foreach ($lines as $line) {
|
||||||
->where('line_id', $lineId)
|
$targetQuantity = ProductionPlan::where('plant_id', $plant->id)
|
||||||
->whereBetween('created_at', [$PlanstartDate, $planendDate])
|
->where('line_id', $line->id)
|
||||||
->sum('plan_quantity');
|
->whereBetween('created_at', [$PlanstartDate, $planendDate])
|
||||||
|
->sum('plan_quantity');
|
||||||
|
|
||||||
$productionQuantity = ProductionQuantity::where('plant_id', $plantId)
|
$productionQuantity = ProductionQuantity::where('plant_id', $plant->id)
|
||||||
->where('line_id', $lineId)
|
->where('line_id', $line->id)
|
||||||
->whereBetween('created_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$tableData[] = [
|
$tableData[] = [
|
||||||
'no' => $no++,
|
'no' => $no++,
|
||||||
'plant' => $plant->name,
|
'plant' => $plant->name,
|
||||||
'line' => $lineName,
|
'line' => $line->name,
|
||||||
'targetQuantity' => $targetQuantity,
|
'targetQuantity' => $targetQuantity,
|
||||||
'productionQuantity' => $productionQuantity,
|
'productionQuantity' => $productionQuantity,
|
||||||
];
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// $this->table(
|
// Optional: show table in console
|
||||||
// ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
|
$this->table(['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'], $tableData);
|
||||||
// $tableData
|
|
||||||
// );
|
|
||||||
|
|
||||||
if (!empty($emails)) {
|
if (!empty($emails)) {
|
||||||
foreach ($emails as $email) {
|
foreach ($emails as $email) {
|
||||||
@@ -165,6 +169,9 @@ class SendProductionReport extends Command
|
|||||||
} else {
|
} else {
|
||||||
$this->info('No recipients found for ProductionMailAlert.');
|
$this->info('No recipients found for ProductionMailAlert.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->info("Production report sent to " . count($emails) . " recipient(s).");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ Artisan::command('inspire', function () {
|
|||||||
|
|
||||||
$productionRules = AlertMailRule::where('module', 'ProductionQuantities')
|
$productionRules = AlertMailRule::where('module', 'ProductionQuantities')
|
||||||
->where('rule_name', 'ProductionMail')
|
->where('rule_name', 'ProductionMail')
|
||||||
|
//->select('plant', 'schedule_type')
|
||||||
|
->select('plant', 'schedule_type')
|
||||||
|
->distinct()
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
foreach ($productionRules as $rule) {
|
foreach ($productionRules as $rule) {
|
||||||
|
|||||||
Reference in New Issue
Block a user