changed logici n production mail report
This commit is contained in:
@@ -8,7 +8,9 @@ use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use App\Mail\ProductionMail;
|
||||
use App\Models\Line;
|
||||
use App\Models\ProductionPlan;
|
||||
use App\Models\ProductionQuantity;
|
||||
use DB;
|
||||
|
||||
class SendProductionReport extends Command
|
||||
{
|
||||
@@ -18,7 +20,7 @@ class SendProductionReport extends Command
|
||||
* @var string
|
||||
*/
|
||||
//protected $signature = 'send:production-report';
|
||||
protected $signature = 'send:production-report {schedule_type}';
|
||||
protected $signature = 'send:production-report {schedule_type} {plant}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@@ -30,147 +32,45 @@ class SendProductionReport extends Command
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
||||
$scheduleType = $this->argument('schedule_type');
|
||||
|
||||
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||
->where('rule_name', 'ProductionMail')
|
||||
->where('schedule_type', $scheduleType)
|
||||
->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();
|
||||
// $scheduleType = $this->argument('schedule_type');
|
||||
// //$plant = $this->argument('plant');
|
||||
// $plant = (int) $this->argument('plant');
|
||||
|
||||
// // 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();
|
||||
// $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||
// ->where('rule_name', 'ProductionMail')
|
||||
// ->where('plant', $plant)
|
||||
// ->where('schedule_type', $scheduleType)
|
||||
// ->get();
|
||||
|
||||
// $lines = Line::all();
|
||||
// $lineMeta = [];
|
||||
// foreach ($lines as $line) {
|
||||
// $lineMeta[$line->id] = [
|
||||
// 'name' => $line->name,
|
||||
// 'type' => $line->type,
|
||||
// 'plant_id' => $line->plant_id,
|
||||
// ];
|
||||
// }
|
||||
// $emails = $mailRules->pluck('email')->unique()->toArray();
|
||||
|
||||
// $plants = Plant::all()->keyBy('id');
|
||||
// $lines = Line::all()->groupBy('plant_id'); // Group lines by plant for easy lookup
|
||||
|
||||
// $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;
|
||||
// }
|
||||
// $plantLines = $lines->get($plantId, collect());
|
||||
// foreach ($plantLines as $line) {
|
||||
// $lineId = $line->id;
|
||||
// $lineName = $line->name;
|
||||
|
||||
// $lineInfo = $lineMeta[$lineId];
|
||||
// $lineName = $lineInfo['name'];
|
||||
|
||||
// $targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId)
|
||||
// $targetQuantity = ProductionPlan::where('plant_id', $plant)
|
||||
// ->where('line_id', $lineId)
|
||||
// ->whereBetween('created_at', [$startDate, $endDate])
|
||||
// ->sum('plan_quantity');
|
||||
|
||||
// $productionQuantity = $allProduction[$lineId] ?? 0;
|
||||
// $productionQuantity = ProductionQuantity::where('plant_id', $plant)
|
||||
// ->where('line_id', $lineId)
|
||||
// ->whereBetween('created_at', [$startDate, $endDate])
|
||||
// ->count();
|
||||
|
||||
// $tableData[] = [
|
||||
// 'no' => $no++,
|
||||
@@ -182,14 +82,86 @@ class SendProductionReport extends Command
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!empty($filteredEmails)) {
|
||||
// $this->info('Production report sent to: ' . implode(', ', $filteredEmails));
|
||||
// foreach ($filteredEmails as $email) {
|
||||
// $this->table(
|
||||
// ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
|
||||
// $tableData
|
||||
// );
|
||||
|
||||
// if (!empty($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 ProductionMailAlert.');
|
||||
// }
|
||||
// }
|
||||
public function handle()
|
||||
{
|
||||
$scheduleType = $this->argument('schedule_type');
|
||||
$plantId = (int) $this->argument('plant'); // Only one plant
|
||||
|
||||
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
||||
->where('rule_name', 'ProductionMail')
|
||||
->where('plant', $plantId)
|
||||
->where('schedule_type', $scheduleType)
|
||||
->get();
|
||||
|
||||
$emails = $mailRules->pluck('email')->unique()->toArray();
|
||||
|
||||
$plant = Plant::find($plantId);
|
||||
|
||||
if (!$plant) {
|
||||
$this->error("Invalid plant ID: $plantId");
|
||||
return;
|
||||
}
|
||||
|
||||
$lines = Line::where('plant_id', $plantId)->get();
|
||||
|
||||
$startDate = now()->setTime(8, 0, 0);
|
||||
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
||||
|
||||
$tableData = [];
|
||||
$no = 1;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$lineId = $line->id;
|
||||
$lineName = $line->name;
|
||||
|
||||
$targetQuantity = ProductionPlan::where('plant_id', $plantId)
|
||||
->where('line_id', $lineId)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->sum('plan_quantity');
|
||||
|
||||
$productionQuantity = ProductionQuantity::where('plant_id', $plantId)
|
||||
->where('line_id', $lineId)
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->count();
|
||||
|
||||
$tableData[] = [
|
||||
'no' => $no++,
|
||||
'plant' => $plant->name,
|
||||
'line' => $lineName,
|
||||
'targetQuantity' => $targetQuantity,
|
||||
'productionQuantity' => $productionQuantity,
|
||||
];
|
||||
}
|
||||
|
||||
// $this->table(
|
||||
// ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
|
||||
// $tableData
|
||||
// );
|
||||
|
||||
if (!empty($emails)) {
|
||||
foreach ($emails as $email) {
|
||||
Mail::to($email)->send(new ProductionMail($tableData));
|
||||
}
|
||||
} else {
|
||||
$this->info('No recipients found for ProductionMailAlert.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user