diff --git a/app/Console/Commands/SendProductionReport.php b/app/Console/Commands/SendProductionReport.php index 21d5f97..c5b24a5 100644 --- a/app/Console/Commands/SendProductionReport.php +++ b/app/Console/Commands/SendProductionReport.php @@ -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.'); + } + } } diff --git a/app/Filament/Resources/AlertMailRuleResource.php b/app/Filament/Resources/AlertMailRuleResource.php index f27d784..dbd2e98 100644 --- a/app/Filament/Resources/AlertMailRuleResource.php +++ b/app/Filament/Resources/AlertMailRuleResource.php @@ -5,8 +5,11 @@ namespace App\Filament\Resources; use App\Filament\Resources\AlertMailRuleResource\Pages; use App\Filament\Resources\AlertMailRuleResource\RelationManagers; use App\Models\AlertMailRule; +use App\Models\Plant; +use Dotenv\Exception\ValidationException; use Filament\Facades\Filament; use Filament\Forms; +use Filament\Forms\Components\Checkbox; use Filament\Forms\Form; use Filament\Resources\Resource; use Filament\Tables; @@ -14,6 +17,7 @@ use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Forms\Components\Section; +use Illuminate\Validation\ValidationException as ValidationValidationException; class AlertMailRuleResource extends Resource { @@ -29,6 +33,13 @@ class AlertMailRuleResource extends Resource ->schema([ Section::make('') ->schema([ + Forms\Components\Select::make('plant') + ->label('Plant') + ->reactive() + ->options(fn () => Plant::pluck('name', 'id')->toArray()) + ->required(fn ($get) => ! $get('is_active')) + ->afterStateUpdated(fn ($state, callable $set) => $state ? $set('is_active', false) : null), + // ->options(fn () => Plant::pluck('id', 'name')->toArray()), Forms\Components\Select::make('module') ->label('Module') ->required() @@ -56,15 +67,29 @@ class AlertMailRuleResource extends Resource 'Hourly' => 'Hourly', 'Daily' => 'Daily', ]), + Checkbox::make('is_active') + ->label('All Plants Reports') + ->afterStateUpdated(fn ($state, callable $set) => $state ? $set('plant', null) : null) + ->reactive(), Forms\Components\Hidden::make('created_by') ->default(fn () => Filament::auth()->user()?->name), Forms\Components\Hidden::make('updated_by') ->default(fn () => Filament::auth()->user()?->name), ]) - ->columns(4), + ->columns(5), ]); } + // Optionally, also override for update/editing + // public static function mutateFormDataBeforeSave(array $data): array + // { + // dd('test'); + // if ($data['is_active']) { + // $data['plant'] = 'All Plants'; + // } + // return $data; + // } + public static function table(Table $table): Table { return $table @@ -73,6 +98,16 @@ class AlertMailRuleResource extends Resource ->label('ID') ->numeric() ->sortable(), + Tables\Columns\TextColumn::make('plant') + ->label('Plant') + ->sortable() + ->formatStateUsing(function ($state) { + static $plants; + if (! $plants) { + $plants = Plant::pluck('name', 'id')->toArray(); + } + return $plants[$state] ?? 'All Plants'; + }), Tables\Columns\TextColumn::make('module') ->label('Module') ->sortable(), diff --git a/app/Filament/Resources/AlertMailRuleResource/Pages/CreateAlertMailRule.php b/app/Filament/Resources/AlertMailRuleResource/Pages/CreateAlertMailRule.php index c534f97..cb233d3 100644 --- a/app/Filament/Resources/AlertMailRuleResource/Pages/CreateAlertMailRule.php +++ b/app/Filament/Resources/AlertMailRuleResource/Pages/CreateAlertMailRule.php @@ -9,4 +9,14 @@ use Filament\Resources\Pages\CreateRecord; class CreateAlertMailRule extends CreateRecord { protected static string $resource = AlertMailRuleResource::class; + + protected function mutateFormDataBeforeCreate(array $data): array + { + + if ($data['is_active']) { + $data['plant'] = '0'; + } + + return $data; + } } diff --git a/routes/console.php b/routes/console.php index e8b884a..c7478c1 100644 --- a/routes/console.php +++ b/routes/console.php @@ -12,130 +12,29 @@ Artisan::command('inspire', function () { // Schedule::command('send:invoice-report'); // Schedule::command('send:production-report'); -// $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'); - -// // $this->info($productionScheduleType); -// // \Log::info('Production Schedule Type: ' . $productionScheduleType); - -// 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 () { // $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', -// ], -// ]; +// $productionRules = AlertMailRule::where('module', 'ProductionQuantities') +// ->where('rule_name', 'ProductionMail') +// ->get(); -// foreach ($rules as $rule) { -// $scheduleType = AlertMailRule::where('module', $rule['module']) -// ->where('rule_name', $rule['rule_name']) -// ->value('schedule_type'); +// $types = $productionRules->pluck('schedule_type')->unique(); -// switch ($scheduleType) { +// foreach ($types as $type) { +// $command = $schedule->command("send:production-report", [$type]); +// switch ($type) { // case 'Live': -// $schedule->command("send:invoice-report {$rule['argument']}")->everyMinute(); +// $command->everyMinute(); // break; // case 'Hourly': -// $schedule->command("send:invoice-report {$rule['argument']}")->hourly(); +// $command->hourly(); // break; // case 'Daily': -// $schedule->command("send:invoice-report {$rule['argument']}")->dailyAt('07:59'); -// break; -// default: -// $schedule->command("send:invoice-report {$rule['argument']}")->hourly(); +// $command->dailyAt('07:59'); // 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 () { @@ -145,10 +44,13 @@ app()->booted(function () { ->where('rule_name', 'ProductionMail') ->get(); - $types = $productionRules->pluck('schedule_type')->unique(); + // Loop each mail rule individually (plant + schedule_type) + foreach ($productionRules as $rule) { + $type = $rule->schedule_type; + $plantId = $rule->plant; + + $command = $schedule->command('send:production-report', [$type, $plantId]); - foreach ($types as $type) { - $command = $schedule->command("send:production-report", [$type]); switch ($type) { case 'Live': $command->everyMinute(); @@ -163,3 +65,4 @@ app()->booted(function () { } }); +