From 8c488e1ced25e2225205918df2e5cd7d56693e01 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 10 Sep 2025 10:30:09 +0530 Subject: [PATCH] Enhance MotorTestingMasterResource with advanced filtering options and improved form validation --- .../Resources/MotorTestingMasterResource.php | 312 +++++++++++++++++- 1 file changed, 300 insertions(+), 12 deletions(-) diff --git a/app/Filament/Resources/MotorTestingMasterResource.php b/app/Filament/Resources/MotorTestingMasterResource.php index 93274d8..be8190b 100644 --- a/app/Filament/Resources/MotorTestingMasterResource.php +++ b/app/Filament/Resources/MotorTestingMasterResource.php @@ -9,8 +9,13 @@ use App\Filament\Resources\MotorTestingMasterResource\RelationManagers; use App\Models\Configuration; use App\Models\Item; use App\Models\MotorTestingMaster; +use App\Models\Plant; use Filament\Facades\Filament; use Filament\Forms; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Radio; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Filament\Forms\Get; use Filament\Resources\Resource; @@ -20,6 +25,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Tables\Actions\ImportAction; use Filament\Tables\Actions\ExportAction; +use Filament\Tables\Filters\Filter; use Illuminate\Validation\Rule; class MotorTestingMasterResource extends Resource @@ -96,7 +102,6 @@ class MotorTestingMasterResource extends Resource ->minLength(6) ->afterStateUpdated(function ($state, callable $set, callable $get) { $code = $get('subassembly_code'); - // Ensure `linestop_id` is not cleared if (!$code) { $set('iCodeError', 'Scan the valid Subassembly Code.'); return; @@ -136,11 +141,6 @@ class MotorTestingMasterResource extends Resource ->options(function (callable $get) { $plantId = $get('plant_id'); - // if (!$plantId || !$lineId) - // { - // return []; - // } - if ($plantId) { return Configuration::where('plant_id', $plantId) @@ -317,13 +317,13 @@ class MotorTestingMasterResource extends Resource }), Tables\Columns\TextColumn::make('plant.name') ->label('Plant') - ->alignCenter() ->searchable() + ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.category') ->label('Category') - ->alignCenter() ->searchable() + ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.code') ->label('Item Code') @@ -331,17 +331,18 @@ class MotorTestingMasterResource extends Resource ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('subassembly_code') - ->label('SubAssembly Code') + ->label('Subassembly Code') ->searchable() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('item.description') ->label('Model') - ->alignCenter() ->searchable() + ->alignCenter() ->sortable(), Tables\Columns\IconColumn::make('isi_model') ->label('ISI Model') + ->searchable() ->alignCenter() ->boolean(), Tables\Columns\TextColumn::make('phase') @@ -371,6 +372,7 @@ class MotorTestingMasterResource extends Resource ->alignCenter(), Tables\Columns\TextColumn::make('connection') ->label('Connection') + ->searchable() ->alignCenter(), Tables\Columns\TextColumn::make('ins_res_limit') ->label('Insulation Resistance Limit') @@ -430,19 +432,25 @@ class MotorTestingMasterResource extends Resource ->label('No Load Speed UL') ->alignCenter(), Tables\Columns\TextColumn::make('created_at') + ->label('Created At') ->dateTime() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') - ->alignCenter(), + ->searchable() + ->alignCenter() + ->sortable(), Tables\Columns\TextColumn::make('updated_at') + ->label('Updated At') ->dateTime() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('updated_by') ->label('Updated By') - ->alignCenter(), + ->searchable() + ->alignCenter() + ->sortable(), Tables\Columns\TextColumn::make('deleted_at') ->dateTime() ->alignCenter() @@ -451,7 +459,287 @@ class MotorTestingMasterResource extends Resource ]) ->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) { + $set('Item', null); + $set('isi_type', null); + $set('created_by', null); + $set('updated_by', null); + }), + Select::make('Item') + ->label('Search by Item Code') + ->nullable() + ->options(function (callable $get) { + $pId = $get('Plant'); + return Item::whereHas('motorTestingMasters', function ($query) use ($pId) { + if ($pId) { + $query->where('plant_id', $pId); + } + })->pluck('code', 'id'); + }) + ->searchable() + ->reactive(), + TextInput::make('description') + ->label('Description') + ->placeholder('Enter Description'), + Radio::make('isi_type') + ->label('ISI Model ?') + ->boolean() + ->options([ + 'All' => 'All', + 'Y' => 'Y', + 'N' => 'N' + ]) + ->default(null) + ->inlineLabel(false) + ->inline(), + Select::make('phase_type') + ->label('Select Phase') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + + if ($plantId) + { + return Configuration::where('plant_id', $plantId) + ->where('c_name', 'MOTOR_PHASE') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } + else + { + return Configuration::where('c_name', 'MOTOR_PHASE') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + + if ($state == 'Single' && $get('connection_type') == 'Star-Delta') { + $set('phase_type', 'Three'); + } + }) + ->reactive(), + Select::make('connection_type') + ->label('Select Connection') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if ($plantId) + { + return Configuration::where('plant_id', $plantId) + ->where('c_name', 'MOTOR_CONNECTION') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } + else + { + return Configuration::where('c_name', 'MOTOR_CONNECTION') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } + }) + ->afterStateUpdated(function ($state, callable $set) { + if ($state == 'Star-Delta') { + $set('phase_type', 'Three'); + } + }) + ->reactive(), + Select::make('created_by') + ->label('Created By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (!$plantId) + { + return MotorTestingMaster::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); + } + else + { + return MotorTestingMaster::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); + } + }) + ->searchable() + ->reactive(), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder(placeholder: 'Select To DateTime') + ->reactive() + ->native(false), + Select::make('updated_by') + ->label('Updated By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (!$plantId) + { + return MotorTestingMaster::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); + } + else + { + return MotorTestingMaster::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); + } + }) + ->searchable() + ->reactive(), + DateTimePicker::make(name: 'updated_from') + ->label('Updated From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('updated_to') + ->label('Updated To') + ->placeholder(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['Item']) && empty($data['description']) && empty($data['isi_type']) && empty($data['phase_type']) && empty($data['connection_type']) && empty($data['created_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && empty($data['updated_from']) && empty($data['updated_to'])) {// || $data['isi_type'] == 'All') + return $query->whereRaw('1 = 0'); + } + + if (!empty($data['Plant'])) { + $query->where('plant_id', $data['Plant']); + } + + if (!empty($data['Item'])) { + $itemIds = Item::where('id', $data['Item']) + ->pluck('id') + ->toArray(); + + if (!empty($itemIds)) { + $query->whereIn('item_id', $itemIds); + } + } + + if (!empty($data['description'])) { + $pId = $data['Plant'] ?? null; + $descIds = Item::where('description', 'like', '%' . $data['description'] . '%')->whereHas('motorTestingMasters', function ($query) use ($pId) { + if ($pId) { $query->where('plant_id', $pId); } + })->pluck('id')->toArray(); + + if (!empty($descIds)) { + $query->whereIn('item_id', $descIds); + } + } + + if ($data['isi_type'] == 'Y') { + $query->where('isi_model', true); + } + else if ($data['isi_type'] == 'N') { + $query->where('isi_model', false); + } + + if (!empty($data['phase_type'])) { + $query->where('phase', $data['phase_type']); + } + + if (!empty($data['connection_type'])) { + $query->where('connection', $data['connection_type']); + } + + if (!empty($data['created_by'])) { + $query->where('created_by', $data['created_by']); + } + + if (!empty($data['created_from'])) { + $query->where('created_at', '>=', $data['created_from']); + } + + if (!empty($data['created_to'])) { + $query->where('created_at', '<=', $data['created_to']); + } + + if (!empty($data['updated_by'])) { + $query->where('updated_by', $data['updated_by']); + } + + if (!empty($data['updated_from'])) { + $query->where('updated_at', '>=', $data['updated_from']); + } + + if (!empty($data['updated_to'])) { + $query->where('updated_at', '<=', $data['updated_to']); + } + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (!empty($data['Plant'])) { + $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); + } + + if (!empty($data['Item'])) { + $itemCode = Item::find($data['Item'])->code ?? 'Unknown'; + $indicators[] = 'Item Codes: ' . $itemCode; + } + + if (!empty($data['description'])) { + $indicators[] = 'Description: ' . $data['description']; + } + + if ($data['isi_type'] == 'Y') { + $indicators[] = 'ISI Model: Yes'; + } + else if ($data['isi_type'] == 'N') { + $indicators[] = 'ISI Model: No'; + } + + if (!empty($data['phase_type'])) { + $indicators[] = 'Phase: ' . $data['phase_type']; + } + + if (!empty($data['connection_type'])) { + $indicators[] = 'Connection: ' . $data['connection_type']; + } + + if (!empty($data['created_by'])) { + $indicators[] = 'Created By: ' . $data['created_by']; + } + + if (!empty($data['created_from'])) { + $indicators[] = 'Created From: ' . $data['created_from']; + } + + if (!empty($data['created_to'])) { + $indicators[] = 'Created To: ' . $data['created_to']; + } + + if (!empty($data['updated_by'])) { + $indicators[] = 'Updated By: ' . $data['updated_by']; + } + + if (!empty($data['updated_from'])) { + $indicators[] = 'Updated From: ' . $data['updated_from']; + } + + if (!empty($data['updated_to'])) { + $indicators[] = 'Updated To: ' . $data['updated_to']; + } + + return $indicators; + }) ]) + ->filtersFormMaxHeight('280px') ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(),