Files
pds/app/Console/Commands/SendProductionReport.php

196 lines
6.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\InvoiceValidation;
use App\Models\Plant;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use App\Mail\ProductionMail;
use App\Models\Line;
use App\Models\ProductionQuantity;
class SendProductionReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
//protected $signature = 'send:production-report';
protected $signature = 'send:production-report {schedule_type}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* 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();
// // 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.');
// }
// }
}