From 469b47d2dc75961633172cea0c80910a357be444 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 4 Feb 2026 09:03:51 +0530 Subject: [PATCH] Refactored plant selection logic and added 'updated_by' tracking in ProcessOrderResource form --- .../Resources/ProcessOrderResource.php | 126 +++++++++--------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/app/Filament/Resources/ProcessOrderResource.php b/app/Filament/Resources/ProcessOrderResource.php index fdf04c5..9750b99 100644 --- a/app/Filament/Resources/ProcessOrderResource.php +++ b/app/Filament/Resources/ProcessOrderResource.php @@ -48,13 +48,13 @@ class ProcessOrderResource extends Resource ->schema([ Forms\Components\Select::make('plant_id') ->label('Plant') - ->reactive() ->relationship('plant', 'name') ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) + ->reactive() ->afterStateUpdated(function ($state, $set, callable $get, $livewire) { $plantId = $get('plant_id'); $set('coil_number', null); @@ -65,6 +65,7 @@ class ProcessOrderResource extends Resource $set('coilNumberError', null); $set('sfgNumberError', null); } + $set('updated_by', Filament::auth()->user()?->name); }) ->extraAttributes(fn ($get) => [ 'class' => $get('poPlantError') ? 'border-red-500' : '', @@ -74,7 +75,6 @@ class ProcessOrderResource extends Resource ->required(), Forms\Components\Select::make('line_id') ->label('Line') - ->reactive() ->options(function (callable $get) { $plantId = $get('plant_id'); if (empty($plantId)) { @@ -83,6 +83,7 @@ class ProcessOrderResource extends Resource return Line::where('plant_id', $plantId)->pluck('name', 'id'); }) + ->reactive() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('item_id', null); $set('item_description', null); @@ -92,13 +93,13 @@ class ProcessOrderResource extends Resource $set('received_quantity', null); $set('sfg_number', null); $set('machine_name', null); + $set('updated_by', Filament::auth()->user()?->name); }) ->required(), Forms\Components\Select::make('item_id') ->label('Item Code') // ->relationship('item', 'id') // ->required(), - ->reactive() ->searchable() ->options(function (callable $get) { $plantId = $get('plant_id'); @@ -108,6 +109,7 @@ class ProcessOrderResource extends Resource return Item::where('plant_id', $plantId)->pluck('code', 'id'); }) + ->reactive() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $plantId = $get('plant_id'); $itemId = $get('item_id'); @@ -133,6 +135,7 @@ class ProcessOrderResource extends Resource $set('item_description', null); $set('item_uom', null); } + $set('updated_by', Filament::auth()->user()?->name); }) ->required(), @@ -150,8 +153,10 @@ class ProcessOrderResource extends Resource $set('item_description', null); } } + }) + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); }), - Forms\Components\TextInput::make('item_uom') ->label('UOM') ->readOnly() @@ -167,13 +172,10 @@ class ProcessOrderResource extends Resource $set('item_uom', null); } } + }) + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); }), - - Forms\Components\TextInput::make('id') - ->hidden() - ->readOnly(), - // ->readOnly(true), - Forms\Components\TextInput::make('process_order') ->label('Process Order') ->reactive() @@ -188,6 +190,7 @@ class ProcessOrderResource extends Resource $set('coilNumberError', null); $set('sfgNumberError', null); } + $set('updated_by', Filament::auth()->user()?->name); }) ->rule(function (callable $get) { return function (string $attribute, $value, \Closure $fail) use ($get) { @@ -255,6 +258,7 @@ class ProcessOrderResource extends Resource $set('coilNumberError', null); } } + $set('updated_by', Filament::auth()->user()?->name); }) ->extraAttributes(fn ($get) => [ 'class' => $get('coilNumberError') ? 'border-red-500' : '', @@ -264,13 +268,27 @@ class ProcessOrderResource extends Resource ->required(), Forms\Components\TextInput::make('order_quantity') ->label('Order Quantity') - ->required(), + ->default('1.000') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); + }), Forms\Components\TextInput::make('received_quantity') ->label('Received Quantity') - ->default('0') - ->required(), + ->default('0.000') + ->required() + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); + }), Forms\Components\TextInput::make('scrap_quantity') - ->label('Scrap Quantity'), + ->label('Scrap Quantity') + ->default('0.000') + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); + }), Forms\Components\TextInput::make('sfg_number') ->label('SFG Number') ->reactive() @@ -297,6 +315,7 @@ class ProcessOrderResource extends Resource $set('sfgNumberError', null); } } + $set('updated_by', Filament::auth()->user()?->name); }) ->extraAttributes(fn ($get) => [ 'class' => $get('sfgNumberError') ? 'border-red-500' : '', @@ -304,10 +323,27 @@ class ProcessOrderResource extends Resource ->hint(fn ($get) => $get('sfgNumberError') ? $get('sfgNumberError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('machine_name') - ->label('Machine ID'), - Forms\Components\TextInput::make('rework_status') + ->label('Machine ID') + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + $set('updated_by', Filament::auth()->user()?->name); + }), + Forms\Components\Select::make('rework_status') ->label('Rework Status') - ->default(0), + ->default(0) + ->reactive() + ->options([0 => 'No', 1 => 'Yes']) + ->reactive() + ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { + // if ($get('id') && $get('rework_status') == 1) { + // $set('rework_status', 1); + // } + $set('updated_by', Filament::auth()->user()?->name); + }) + ->visible(function () { + return Filament::auth()->user()->hasRole('Super Admin'); // || Filament::auth()->user()->can('update process order rework status') + }) + ->required(), Forms\Components\FileUpload::make('attachment') ->label('PDF Upload') ->acceptedFileTypes(['application/pdf']) @@ -327,6 +363,7 @@ class ProcessOrderResource extends Resource }) ->action(function ($get, callable $set) { $uploadedFiles = $get('attachment'); + $set('updated_by', Filament::auth()->user()?->name); if (is_array($uploadedFiles) && count($uploadedFiles) > 0) { $uploaded = reset($uploadedFiles); @@ -415,8 +452,9 @@ class ProcessOrderResource extends Resource ->visible(function () { return Filament::auth()->user()->can('view process order packing slip'); }) - ->action(function ($get) { + ->action(function ($get, callable $set) { $equipmentNumber = $get('process_order'); + $set('updated_by', Filament::auth()->user()?->name); if (! $equipmentNumber) { Notification::make() @@ -448,54 +486,16 @@ class ProcessOrderResource extends Resource return response()->download(Storage::disk('local')->path($fileToDownload)); }), - - // Action::make('removeAttachment') - // ->label('Remove PDF') - // ->action(function ($get) { - // $equipmentNumber = $get('process_order'); - - // if (!$equipmentNumber) { - // Notification::make() - // ->title('No process order entered') - // ->danger() - // ->send(); - // return; - // } - - // // Get all files from uploads/temp - // $files = Storage::disk('local')->files('uploads/ProcessOrder'); - - // $fileToDelete = null; - // foreach ($files as $file) { - // if (str_contains($file, $equipmentNumber)) { - // $fileToDelete = $file; - // break; - // } - // } - - // if (!$fileToDelete) { - // Notification::make() - // ->title('PDF not found for this process order') - // ->danger() - // ->send(); - // return; - // } - - // // Delete the matched file - // Storage::disk('local')->delete($fileToDelete); - - // Notification::make() - // ->title('PDF removed successfully') - // ->body("File for process order {$equipmentNumber} has been deleted.") - // ->success() - // ->send(); - // }), ]), Forms\Components\Hidden::make('created_by') ->label('Created By') ->default(Filament::auth()->user()?->name), Forms\Components\Hidden::make('updated_by') + ->label('Updated By') ->default(Filament::auth()->user()?->name), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), ]); } @@ -616,7 +616,7 @@ class ProcessOrderResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { @@ -765,7 +765,7 @@ class ProcessOrderResource extends Resource ->options(function (callable $get) { $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); }) ->required() ->reactive()