From 148b6444b63a5fc6c44ce2e8af27fe8bf6164aa8 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 2 Jul 2025 11:20:56 +0530 Subject: [PATCH] Added filter functionalty in rework locator invoice validation page --- ...ReworkLocatorInvoiceValidationResource.php | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) diff --git a/app/Filament/Resources/ReworkLocatorInvoiceValidationResource.php b/app/Filament/Resources/ReworkLocatorInvoiceValidationResource.php index ab4c167..921faba 100644 --- a/app/Filament/Resources/ReworkLocatorInvoiceValidationResource.php +++ b/app/Filament/Resources/ReworkLocatorInvoiceValidationResource.php @@ -9,6 +9,7 @@ use App\Filament\Resources\ReworkLocatorInvoiceValidationResource\RelationManage use App\Models\InvoiceValidation; use App\Models\LocatorInvoiceValidation; use App\Models\PalletValidation; +use App\Models\Plant; use App\Models\ReworkLocatorInvoiceValidation; use Filament\Facades\Filament; use Filament\Forms; @@ -24,6 +25,10 @@ use Filament\Forms\Get; use Filament\Notifications\Notification; use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ImportAction; +use Filament\Forms\Components\DateTimePicker; +use Filament\Tables\Filters\Filter; +use Filament\Forms\Components\TextInput; +use Filament\Forms\Components\Select; class ReworkLocatorInvoiceValidationResource extends Resource { @@ -318,9 +323,195 @@ class ReworkLocatorInvoiceValidationResource extends Resource return Filament::auth()->user()->can('view export rework invoice validation'); }), ]) + // ->filters([ + // Tables\Filters\TrashedFilter::make(), + // ]) ->filters([ Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Select Plant') + ->nullable() + ->options(function () { + return Plant::pluck('name', 'id'); + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('pallet_number', null); + $set('invoice_number', null); + $set('serial_number', null); + $set('scanned_status', null); + $set('locator_number', null); + $set('created_from', null); + $set('created_to', null); + $set('created_by', null); + $set('scanned_from', null); + $set('scanned_to', null); + $set('scanned_by', null); + }), + Select::make('invoice_number') + ->label('Invoice Number') + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (!$plantId) { + return []; + } + return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) + ->whereNotNull('invoice_number') + ->where('invoice_number','!=', '') + ->orderBy('invoice_number', 'asc') + ->get() + ->unique('invoice_number') + ->pluck('invoice_number', 'invoice_number') + ->toArray(); + }) + ->searchable() + ->reactive(), + TextInput::make('serial_number') + ->label('Serial Number') + ->placeholder(placeholder: 'Enter Serial Number'), + + Select::make('pallet_number') + ->label('Pallet Number') + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (!$plantId) { + return []; + } + return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) + ->whereNotNull('pallet_number') + ->where('pallet_number','!=', '') + ->orderBy('pallet_number', 'asc') + ->get() + ->unique('pallet_number') + ->pluck('pallet_number', 'pallet_number') + ->toArray(); + }) + ->searchable() + ->reactive(), + Select::make('locator_number') + ->label('Locator Number') + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (!$plantId) { + return []; + } + return ReworkLocatorInvoiceValidation::where('plant_id', $plantId) + ->whereNotNull('locator_number') + ->where('locator_number','!=', '') + ->orderBy('locator_number', 'asc') + ->get() + ->unique('locator_number') + ->pluck('locator_number', 'locator_number') + ->toArray(); + }) + ->searchable() + ->reactive(), + Select::make('scanned_status') + ->label('Scanned Status') + ->options([ + 'Scanned' => 'Scanned', + ]), + DateTimePicker::make(name: 'rework_from') + ->label('Rework From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('rework_to') + ->label('Rework To') + ->placeholder(placeholder: 'Select To DateTime') + ->reactive() + ->native(false), + TextInput::make('rework_by') + ->label('Rework By') + ->placeholder(placeholder: 'Enter Rework By'), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['Plant']) && empty($data['invoice_number']) && empty($data['serial_number']) && empty($data['pallet_number']) && empty($data['locator_number']) && empty($data['scanned_status']) && empty($data['rework_from']) && empty($data['rework_to']) && empty($data['rework_by'])) { + return $query->whereRaw('1 = 0'); + } + + if (!empty($data['Plant'])) { //$plant = $data['Plant'] ?? null + $query->where('plant_id', $data['Plant']); + } + + if (!empty($data['invoice_number'])) { + $query->where('invoice_number', $data['invoice_number']); + } + + if (!empty($data['serial_number'])) { + $query->where('serial_number', $data['serial_number']); + } + + if (!empty($data['pallet_number'])) { + $query->where('pallet_number', $data['pallet_number']); + } + + if (!empty($data['locator_number'])) { + $query->where('locator_number', $data['locator_number']); + } + if (!empty($data['scanned_status'])) { + $query->where('scanned_status', $data['scanned_status']); + } + + if (!empty($data['rework_from'])) { + $query->where('reworked_at', '>=', $data['rework_from']); + } + + if (!empty($data['rework_to'])) { + $query->where('reworked_at', '<=', $data['rework_to']); + } + + if (!empty($data['rework_by'])) { + $query->where('reworked_by', $data['rework_by']); + } + + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (!empty($data['Plant'])) { + $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); + } + + if (!empty($data['invoice_number'])) { + $indicators[] = 'Invoice Number: ' . $data['invoice_number']; + } + + if (!empty($data['serial_number'])) { + $indicators[] = 'Serial Number: ' . $data['serial_number']; + } + + if (!empty($data['pallet_number'])) { + $indicators[] = 'Pallet Number: ' . $data['pallet_number']; + } + + if (!empty($data['locator_number'])) { + $indicators[] = 'Locator Number: ' . $data['locator_number']; + } + + if (!empty($data['scanned_status'])) { + $indicators[] = 'Scanned Status: ' . $data['scanned_status']; + } + + if (!empty($data['rework_from'])) { + $indicators[] = 'From: ' . $data['rework_from']; + } + + if (!empty($data['rework_to'])) { + $indicators[] = 'To: ' . $data['rework_to']; + } + + if (!empty($data['rework_by'])) { + $indicators[] = 'Reworked By: ' . $data['rework_by']; + } + return $indicators; + }) ]) + ->filtersFormMaxHeight('280px') ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(),