From be47d4893153349dad4882128fe797b060202041 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 31 May 2025 17:33:19 +0530 Subject: [PATCH] Added id column to load default plant, line, machine with its validation and using motor_testing_master_id instead of item_id remove item_id functionality --- .../Resources/TestingPanelReadingResource.php | 96 ++++++++++++++----- 1 file changed, 70 insertions(+), 26 deletions(-) diff --git a/app/Filament/Resources/TestingPanelReadingResource.php b/app/Filament/Resources/TestingPanelReadingResource.php index eb0f85200..93d2ebad1 100644 --- a/app/Filament/Resources/TestingPanelReadingResource.php +++ b/app/Filament/Resources/TestingPanelReadingResource.php @@ -11,6 +11,7 @@ use App\Models\Configuration; use App\Models\Item; use App\Models\Line; use App\Models\Machine; +use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\QualityValidation; use App\Models\StickerMaster; @@ -22,6 +23,7 @@ use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Actions\BulkAction; @@ -46,17 +48,40 @@ class TestingPanelReadingResource extends Resource return $form ->schema([ Forms\Components\Select::make('plant_id') + ->label('Plant') ->relationship('plant', 'name') ->required() ->reactive() // When plant changes, reset the dependent fields - ->afterStateUpdated(function (callable $set) { - $set('line_id', null); - $set('item_id', null); - $set('machine_id', null); - }), + ->default(function () { + return optional(TestingPanelReading::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => !empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + if (!$plantId) { + $set('line_id', null); + $set('item_id', null); + $set('machine_id', null); + $set('tPrError', 'Please select a plant first.'); + return; + } + else + { + $set('line_id', null); + $set('item_id', null); + $set('machine_id', null); + $set('tPrError', null); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('tPrError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('tPrError') ? $get('tPrError') : null) + ->hintColor('danger'), Forms\Components\Select::make('line_id') ->label('Line') + ->relationship('line', 'name') ->options(function (callable $get) { $plantId = $get('plant_id'); if (!$plantId) { @@ -66,26 +91,16 @@ class TestingPanelReadingResource extends Resource ->pluck('name', 'id') ->toArray(); }) + ->default(function () { + return optional(TestingPanelReading::latest()->first())->line_id; + }) + ->disabled(fn (Get $get) => !empty($get('id'))) ->required() ->reactive() ->afterStateUpdated(fn (callable $set) => $set('item_id', null)), - Forms\Components\Select::make('item_id') - ->label('Item') - ->options(function (callable $get) { - $plantId = $get('plant_id'); - if (!$plantId) { - return []; - } - // Adjust this query as per your item-plant relationship - return Item::where('plant_id', $plantId) - ->pluck('code', 'id') - ->toArray(); - }) - ->required() - ->searchable() - ->reactive(), Forms\Components\Select::make('machine_id') ->label('Machine') + ->relationship('machine', 'name') ->options(function (callable $get) { $lineId = $get('line_id'); if (!$lineId) { @@ -96,8 +111,33 @@ class TestingPanelReadingResource extends Resource ->pluck('name', 'id') ->toArray(); }) + ->default(function () { + return optional(TestingPanelReading::latest()->first())->machine_id; + }) + ->disabled(fn (Get $get) => !empty($get('id'))) ->required() ->reactive(), + Forms\Components\Select::make('motor_testing_master_id') + ->label('Item Code') + ->relationship('motorTestingMaster', 'item.code') + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if (!$plantId) { + return []; + } + + // return MotorTestingMaster::with('item') + // ->whereHas('motorTestingMasters', function ($query) use ($plantId) { + // $query->where('plant_id', $plantId); + // })->pluck('item.code', 'id')->toArray(); + // // ->get(); + + return Item::where('plant_id', $plantId)->whereHas('motorTestingMasters')->pluck('code', 'id')->toArray(); + // return Item::whereHas('motorTestingMasters')->pluck('code', 'id')->toArray(); + }) + ->required() + ->searchable() + ->reactive(), Forms\Components\TextInput::make('output') ->label('Output') ->required(), @@ -190,7 +230,9 @@ class TestingPanelReadingResource extends Resource ->label('Tested By'), Forms\Components\TextInput::make('updated_by') ->label('Updated By'), - + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), ]); } @@ -432,13 +474,15 @@ class TestingPanelReadingResource extends Resource ->options(function (callable $get) { $plantId = $get('Plant'); - $query = Item::query(); + return Item::where('plant_id', $plantId)->whereHas('motorTestingMasters')->pluck('code', 'id')->toArray(); - if ($plantId) { - $query->where('plant_id', $plantId); - } + // $query = Item::query(); - return $query->pluck('code', 'id')->toArray(); + // if ($plantId) { + // $query->where('plant_id', $plantId); + // } + + // return $query->pluck('code', 'id')->toArray(); }) ->reactive(), Select::make('machine_name')