From 3a0c728bb2c2ec44938de2b4a0f78e17c7cd8745 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 19 Apr 2026 11:53:40 +0530 Subject: [PATCH] Added filter in temp class charcateristics resource page --- .../TempClassCharacteristicResource.php | 204 +++++++++++++++++- app/Models/Item.php | 5 + app/Models/Machine.php | 5 + app/Models/Plant.php | 5 + 4 files changed, 218 insertions(+), 1 deletion(-) diff --git a/app/Filament/Resources/TempClassCharacteristicResource.php b/app/Filament/Resources/TempClassCharacteristicResource.php index 9f39099..8f31706 100644 --- a/app/Filament/Resources/TempClassCharacteristicResource.php +++ b/app/Filament/Resources/TempClassCharacteristicResource.php @@ -23,6 +23,10 @@ use Filament\Forms\Get; use Illuminate\Validation\Rule; use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ImportAction; +use Filament\Tables\Filters\Filter; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; class TempClassCharacteristicResource extends Resource { @@ -1585,7 +1589,15 @@ class TempClassCharacteristicResource extends Resource Tables\Columns\TextColumn::make('has_work_flow_id') ->label('HAS WORK FLOW ID') ->alignCenter() - ->sortable(), + ->sortable() + ->formatStateUsing(function ($state) { + return match ($state) { + '1' => 'Pending Approval', + '2' => 'Pending Request', + '0' => 'Approved', + default => '-', + }; + }), Tables\Columns\TextColumn::make('created_at') ->dateTime() ->sortable() @@ -1601,7 +1613,197 @@ class TempClassCharacteristicResource extends Resource ]) ->filters([ Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Search by Plant Name') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('tempClassCharacteristics', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id'); + } + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('machine', null); + $set('item_id', null); + // $set('aufnr', null); + }), + Select::make('machine') + ->label('Search by Work Center') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Machine::whereHas('tempClassCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('work_center', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + // $set('item_id', null); + // $set('aufnr', null); + }), + Select::make('item_id') + ->label('Search by Item Code') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Item::whereHas('tempClassCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('code', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + // $set('aufnr', null); + }), + TextInput::make('aufnr') + ->label('Job Number') + ->placeholder('Enter Job Number') + ->minlength(7) + ->maxlength(10), + TextInput::make('gernr') + ->label('Serial Number') + ->placeholder('Enter Serial Number'), + Select::make('work_flow_status') + ->label('Work Flow Status') + ->placeholder('Select Work Flow Status') + ->options([ + '1' => 'Pending Approval', + '2' => 'Pending Request', + '0' => 'Approved', + ]), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder('Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder('Select To DateTime') + ->reactive() + ->native(false), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['Plant']) && empty($data['machine']) && empty($data['item_id']) && empty($data['aufnr']) && empty($data['gernr']) && empty($data['created_from']) && empty($data['created_to']) && !array_key_exists('work_flow_status', $data)) { + return $query->whereRaw('1 = 0'); + } + + if (! empty($data['Plant'])) { // $plant = $data['Plant'] ?? null + $query->where('plant_id', $data['Plant']); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return $query->whereRaw('1 = 0'); + } + } + + if (! empty($data['machine'])) { + $query->where('machine_id', $data['machine']); + } + + if (! empty($data['item_id'])) { + $query->where('item_id', $data['item_id']); + } + + if (! empty($data['aufnr'])) { + $query->where('aufnr', 'like', '%'.$data['aufnr'].'%'); + } + + if (! empty($data['gernr'])) { + $query->where('gernr', 'like', '%'.$data['gernr'].'%'); + } + + if (array_key_exists('work_flow_status', $data) && $data['work_flow_status'] != '') { + $query->where('has_work_flow_id', $data['work_flow_status']); + } + + if (! empty($data['created_from'])) { + $query->where('created_at', '>=', $data['created_from']); + } + + if (! empty($data['created_to'])) { + $query->where('created_at', '<=', $data['created_to']); + } + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (! empty($data['Plant'])) { + $indicators[] = 'Plant Name: '.Plant::where('id', $data['Plant'])->value('name'); + } else { + $userHas = Filament::auth()->user()->plant_id; + + if ($userHas && strlen($userHas) > 0) { + return 'Plant: Choose plant to filter records.'; + } + } + + if (! empty($data['machine'])) { + $indicators[] = 'Work Center: '.Machine::where('id', $data['machine'])->value('work_center'); + } + + if (! empty($data['item_id'])) { + $indicators[] = 'Item Code: '.Item::where('id', $data['item_id'])->value('code'); + } + + if (! empty($data['aufnr'])) { + $indicators[] = 'Job No: '.$data['aufnr']; + } + + if (! empty($data['gernr'])) { + $indicators[] = 'Serial Number: '.$data['gernr']; + } + + if (array_key_exists('work_flow_status', $data) && $data['work_flow_status'] != '') { + $statusMap = [ + '1' => 'Pending Approval', + '2' => 'Pending Request', + '0' => 'Approved', + ]; + + $indicators[] = 'Work Flow Status: ' . ($statusMap[$data['work_flow_status']] ?? ''); + } + + if (! empty($data['created_from'])) { + $indicators[] = 'From: '.$data['created_from']; + } + + if (! empty($data['created_to'])) { + $indicators[] = 'To: '.$data['created_to']; + } + + return $indicators; + }), ]) + ->filtersFormMaxHeight('280px') ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(), diff --git a/app/Models/Item.php b/app/Models/Item.php index 5e4dbaf..962f527 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -79,6 +79,11 @@ class Item extends Model return $this->hasMany(ProductionCharacteristic::class, 'item_id', 'id'); } + public function tempClassCharacteristics() + { + return $this->hasMany(TempClassCharacteristic::class, 'plant_id', 'id'); + } + public function weightValidations() { return $this->hasMany(WeightValidation::class); diff --git a/app/Models/Machine.php b/app/Models/Machine.php index 80b564d..dd21db5 100644 --- a/app/Models/Machine.php +++ b/app/Models/Machine.php @@ -63,6 +63,11 @@ class Machine extends Model return $this->hasMany(TestingPanelReading::class); } + public function tempClassCharacteristics() + { + return $this->hasMany(TempClassCharacteristic::class, 'plant_id', 'id'); + } + public function equipmentMasters() { return $this->hasMany(EquipmentMaster::class, 'machine_id', 'id'); diff --git a/app/Models/Plant.php b/app/Models/Plant.php index 1ebfa04..4b66523 100644 --- a/app/Models/Plant.php +++ b/app/Models/Plant.php @@ -138,6 +138,11 @@ class Plant extends Model return $this->hasMany(ProductionCharacteristic::class, 'plant_id', 'id'); } + public function tempClassCharacteristics() + { + return $this->hasMany(TempClassCharacteristic::class, 'plant_id', 'id'); + } + public function users() { return $this->hasMany(User::class, 'plant_id', 'id');