diff --git a/app/Filament/Resources/ClassCharacteristicResource.php b/app/Filament/Resources/ClassCharacteristicResource.php index a2ba316..fa8cb52 100644 --- a/app/Filament/Resources/ClassCharacteristicResource.php +++ b/app/Filament/Resources/ClassCharacteristicResource.php @@ -23,6 +23,10 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Validation\Rule; use Str; +use Filament\Tables\Filters\Filter; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; class ClassCharacteristicResource extends Resource { @@ -1248,18 +1252,22 @@ class ClassCharacteristicResource extends Resource Tables\Columns\TextColumn::make('plant.name') ->label('PLANT NAME') ->alignCenter() + ->searchable() ->sortable(), Tables\Columns\TextColumn::make('machine.work_center') ->label('WORK CENTER') ->alignCenter() + ->searchable() ->sortable(), Tables\Columns\TextColumn::make('item.code') ->label('ITEM CODE') ->alignCenter() + ->searchable() ->sortable(), Tables\Columns\TextColumn::make('aufnr') ->label('AUFNR') ->alignCenter() + ->searchable() ->sortable(), Tables\Columns\TextColumn::make('class') ->label('CLASS') @@ -1943,7 +1951,175 @@ class ClassCharacteristicResource 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('ClassCharacteristics', 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('ClassCharacteristics', 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('ClassCharacteristics', 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'), + 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'])) { + 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 (! 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 (! 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(),