From 7cfa2dae0758745dfaa483ee7bb64d1e533ab125 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 7 Apr 2026 22:54:59 +0530 Subject: [PATCH] Added filter in production characteristics resource page --- .../ProductionCharacteristicResource.php | 234 ++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/app/Filament/Resources/ProductionCharacteristicResource.php b/app/Filament/Resources/ProductionCharacteristicResource.php index 5c60cbf..9740e09 100644 --- a/app/Filament/Resources/ProductionCharacteristicResource.php +++ b/app/Filament/Resources/ProductionCharacteristicResource.php @@ -23,6 +23,11 @@ use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Forms\Get; +use Str; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Tables\Filters\Filter; class ProductionCharacteristicResource extends Resource { @@ -312,7 +317,236 @@ class ProductionCharacteristicResource 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('productionCharacteristics', 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) { + $set('Item', null); + $set('Line', null); + }), + Select::make('Line') + ->label('Search by Line Name') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Line::whereHas('productionCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('name', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('process_order', null); + }), + Select::make('Item') + ->label('Search by Item Code') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if (empty($plantId)) { + return []; + } + + return Item::whereHas('productionCharacteristics', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('code', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('production_order', null); + }), + Select::make('Machine') + ->label('Search by Work Center') + ->nullable() + ->searchable() + ->reactive() + ->options(function (callable $get) { + $plantId = $get('Plant'); + $lineId = $get('Line'); + + if (empty($plantId)) { + return []; + } + + return Machine::whereHas('productionCharacteristics', function ($query) use ($plantId, $lineId) { + if ($plantId) { + $query->where('plant_id', $plantId)->where('line_id', $lineId); + } + })->pluck('work_center', 'id'); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('production_order', null); + }), + TextInput::make('production_order') + ->label('Production Order') + ->reactive() + ->placeholder('Enter Production Order') + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('serial_number', null); + }), + TextInput::make('serial_number') + ->label('Serial Number') + ->reactive() + ->placeholder('Enter Serial Number'), + Select::make('status') + ->label('Search by Status') + ->nullable() + ->searchable() + ->reactive() + ->options([ + 'Ok' => 'Ok', + 'NotOk' => 'Not Ok', + 'ConditionallyAccepted' => 'Conditionally Accepted', + ]), + 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['Line']) && empty($data['Item']) && empty($data['production_order']) && Str::length($data['serial_number']) <= 0 && ($data['status'] == null || $data['status'] == '') && empty($data['created_from']) && empty($data['created_to'])) { + return $query->whereRaw('1 = 0'); + } + + if (! empty($data['Plant'])) { + $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['Line'])) { + $query->where('line_id', $data['Line']); + } + + if (! empty($data['Item'])) { + $query->where('item_id', $data['Item']); + } + + if (! empty($data['Machine'])) { + $query->where('machine_id', $data['Machine']); + } + + if (! empty($data['production_order'])) { + $query->where('production_order', 'like', '%'.$data['production_order'].'%'); + } + + if (Str::length($data['serial_number']) > 0) { + // $query->where('machine_name', $data['machine_name']); + $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); + } + + if ($data['status'] != null && $data['status'] != '') { + $query->where('status', $data['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']); + } + // $query->orderBy('created_at', 'asc'); + }) + ->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 Name: Choose plant to filter records.'; + } + } + + if (! empty($data['Line'])) { + $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name'); + } + + if (! empty($data['Item'])) { + $indicators[] = 'Item Code: '.Item::where('id', $data['Item'])->value('code'); + } + + if (! empty($data['Machine'])) { + $indicators[] = 'Machine: '.Machine::where('id', $data['Machine'])->value('work_center'); + } + + if (! empty($data['production_order'])) { + $indicators[] = 'Production Order: '.$data['production_order']; + } + + if (Str::length($data['serial_number']) > 0) { + $indicators[] = 'Serial Number: '.$data['serial_number']; + } + + // if ($data['status'] != null && $data['status'] != '') { + // $indicators[] = ($data['status'] == 'Ok') ? 'Status: Ok' : 'Status: Not Ok'; + // } + + if ($data['status'] != null && $data['status'] != '') { + if ($data['status'] == 'Ok') { + $indicators[] = 'Status: Ok'; + } elseif ($data['status'] == 'NotOk') { + $indicators[] = 'Status: Not Ok'; + } elseif ($data['status'] == 'ConditionallyAccepted') { + $indicators[] = 'Status: Conditionally Accepted'; + } + } + + 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(),