From 55f734c536315cd3dbea9c57bc38783804a32b5c Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 1 Apr 2025 16:17:57 +0530 Subject: [PATCH] Loading foreign key value while update and added restrict production plan if created --- .../Resources/ProductionPlanResource.php | 68 ++++++++++++++++--- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/ProductionPlanResource.php b/app/Filament/Resources/ProductionPlanResource.php index 404905d..d311b10 100644 --- a/app/Filament/Resources/ProductionPlanResource.php +++ b/app/Filament/Resources/ProductionPlanResource.php @@ -8,6 +8,7 @@ use App\Filament\Resources\ProductionPlanResource\RelationManagers; use App\Models\ProductionPlan; use Filament\Forms; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Actions\ImportAction; @@ -38,6 +39,7 @@ class ProductionPlanResource extends Resource ->required() // ->nullable() ->reactive() + ->disabled(fn (Get $get) => !empty($get('id'))) // ->afterStateUpdated(fn ($set) => $set('block_name', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); @@ -72,8 +74,20 @@ class ProductionPlanResource extends Resource ->reactive() //->afterStateUpdated(fn ($set) => $set('shift_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { + if($get('id')) + { + $getShift = ProductionPlan::where('id', $get('id'))->first(); + $getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first(); + if($getBlock->block_id) + { + $set('block_name', $getBlock->block_id); + $set('ppBlockError', null); + } + } + $blockId = $get('block_name'); $set('shift_id', null); + if (!$blockId) { $set('ppBlockError', 'Please select a block first.'); return; @@ -105,8 +119,19 @@ class ProductionPlanResource extends Resource ->reactive() // ->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { + if($get('id')) + { + $getShift = ProductionPlan::where('id', $get('id'))->first(); + if($getShift->shift_id) + { + $set('shift_id', $getShift->shift_id); + $set('ppShiftError', null); + } + } + $shiftId = $get('shift_id'); $set('line_id', null); + if (!$shiftId) { $set('ppShiftError', 'Please select a shift first.'); return; @@ -141,7 +166,18 @@ class ProductionPlanResource extends Resource }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { + if($get('id')) + { + $getShift = ProductionPlan::where('id', $get('id'))->first(); + if($getShift->line_id) + { + $set('line_id', $getShift->line_id); + $set('ppLineError', null); + } + } + $lineId = $get('line_id'); + if (!$lineId) { $set('ppLineError', 'Please select a line first.'); return; @@ -172,19 +208,35 @@ class ProductionPlanResource extends Resource } else { + $currentDate = date('Y-m-d'); $shiftId = \App\Models\Shift::where('id', $get('shift_id')) ->first(); - if($shiftId->start_time > $shiftId->end_time) - { - $set('ppLineError', 'End time goes tomorrow...'); - return; - } - else - { - $set('ppLineError', 'End time is within today...'); + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + $miNs = (int) $miNs; + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $currentDate . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + $set('ppLineError', null); + return; + } else { + // 'Invalid (From: '.$from_dt.', To: '.$to_dt.')' + $set('ppLineError', 'Choosed a invalid shift...'); return; } + + // if($shiftId->start_time > $shiftId->end_time) } } $set('ppLineError', null);