From 8947eeff7f231ac5d365c4bdb8347b3c2f616d92 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 12:40:50 +0530 Subject: [PATCH] Changed logic in work group master page --- .../Resources/WorkGroupMasterResource.php | 295 +++++++++++++----- 1 file changed, 218 insertions(+), 77 deletions(-) diff --git a/app/Filament/Resources/WorkGroupMasterResource.php b/app/Filament/Resources/WorkGroupMasterResource.php index 523c966..45eb791 100644 --- a/app/Filament/Resources/WorkGroupMasterResource.php +++ b/app/Filament/Resources/WorkGroupMasterResource.php @@ -5,22 +5,27 @@ namespace App\Filament\Resources; use App\Filament\Exports\WorkGroupMasterExporter; use App\Filament\Imports\WorkGroupMasterImporter; use App\Filament\Resources\WorkGroupMasterResource\Pages; -use App\Filament\Resources\WorkGroupMasterResource\RelationManagers; use App\Models\Line; use App\Models\Plant; use App\Models\WorkGroupMaster; +use Filament\Facades\Filament; use Filament\Forms; +use Filament\Forms\Components\Section; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; +use Filament\Tables\Actions\ExportAction; +use Filament\Tables\Actions\ImportAction; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; -use Filament\Forms\Components\Section; -use Filament\Facades\Filament; -use Filament\Tables\Actions\ImportAction; -use Filament\Tables\Actions\ExportAction; use Illuminate\Validation\Rule; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\FileUpload; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Tables\Filters\Filter; class WorkGroupMasterResource extends Resource { @@ -37,68 +42,76 @@ class WorkGroupMasterResource extends Resource return $form ->schema([ Section::make('') - ->schema([ - Forms\Components\Select::make('plant_id') - ->label('Plant') - ->relationship('plant', 'name') - ->reactive() - ->columnSpan(1) - ->required() - ->options(function (callable $get) { - $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); - }) - ->afterStateUpdated(function ($state, $set, callable $get) { - $plantId = $get('plant_id'); + ->schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->searchable() + ->reactive() + ->columnSpan(1) + ->required() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; - if (!$plantId) { - $set('pqPlantError', 'Please select a plant first.'); - $set('name', null); - $set('description', null); - $set('operation_number', null); - return; - } + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->default(function () { + return optional(WorkGroupMaster::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); - $set('validationError', null); - $set('pqPlantError', null); - $set('name', null); - $set('description', null); - $set('operation_number', null); - }) - ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null) - ->hintColor('danger'), - Forms\Components\TextInput::make('name') - ->label('Name') - ->required() - ->minLength(6) - ->columnSpan(1) - ->reactive() - ->rule(function (callable $get) { - return Rule::unique('work_group_masters', 'name') - ->where('plant_id', $get('plant_id')) - ->ignore($get('id')); - }), - Forms\Components\TextInput::make('operation_number') - ->label('Operation Number') - ->numeric() - ->columnSpan(1) - ->reactive() - ->required() - ->rule(function (callable $get) { - return Rule::unique('work_group_masters', 'operation_number') - ->where('plant_id', $get('plant_id')) - ->ignore($get('id')); - }), - Forms\Components\TextInput::make('description') - ->label('Description') - ->required() - ->minLength(5) - ->reactive() - ->columnSpan(['default' => 1, 'sm' => 3]), - Forms\Components\Hidden::make('created_by') - ->default(Filament::auth()->user()?->name), - ]) - ->columns(['default' => 1, 'sm' => 3]), + $set('wgmPlantError', null); + $set('name', null); + $set('description', null); + $set('operation_number', null); + if (! $plantId) { + $set('wgmPlantError', 'Please select a plant first.'); + + return; + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('wgmPlantError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('wgmPlantError') ? $get('wgmPlantError') : null) + ->hintColor('danger'), + Forms\Components\TextInput::make('name') + ->label('Group Work Center') + ->required() + ->minLength(6) + ->columnSpan(1) + ->reactive() + ->rule(function (callable $get) { + return Rule::unique('work_group_masters', 'name') + ->where('plant_id', $get('plant_id')) + ->ignore($get('id')); + }), + Forms\Components\TextInput::make('operation_number') + ->label('Operation Number') + ->numeric() + ->columnSpan(1) + ->reactive() + ->required(), + // ->rule(function (callable $get) { + // return Rule::unique('work_group_masters', 'operation_number') + // ->where('plant_id', $get('plant_id')) + // ->ignore($get('id')); + // }), + Forms\Components\TextInput::make('description') + ->label('Description') + ->required() + ->minLength(5) + ->reactive() + ->columnSpan(['default' => 1, 'sm' => 3]), + Forms\Components\Hidden::make('created_by') + ->default(Filament::auth()->user()?->name), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), + ]) + ->columns(['default' => 1, 'sm' => 3]), ]); } @@ -112,15 +125,16 @@ class WorkGroupMasterResource extends Resource $paginator = $livewire->getTableRecords(); $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; + return ($currentPage - 1) * $perPage + $rowLoop->iteration; }), Tables\Columns\TextColumn::make('plant.name') - ->label('Plant') + ->label('Plant Name') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('name') - ->label('Name') + ->label('Group Work Center') ->alignCenter() ->searchable() ->sortable(), @@ -134,23 +148,21 @@ class WorkGroupMasterResource extends Resource ->alignCenter() ->searchable() ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->label('Created At') + ->alignCenter() + ->dateTime() + ->sortable(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') ->alignCenter() ->searchable() ->sortable(), - Tables\Columns\TextColumn::make('created_at') - ->label('Created At') - ->alignCenter() - ->dateTime() - ->sortable() - ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->alignCenter() ->dateTime() - ->sortable() - ->toggleable(isToggledHiddenByDefault: true), + ->sortable(), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->alignCenter() @@ -160,7 +172,136 @@ class WorkGroupMasterResource extends Resource ]) ->filters([ Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Search by Plant Name') + ->nullable() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('workGroupMasters', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id')->toArray(); + } + }) + ->searchable() + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('code', null); + $set('operator_id', null); + }), + Select::make('name') + ->label('Search by Work Group Center') + ->searchable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + + if(!$plantId){ + return []; + } + else{ + return $plantId ? WorkGroupMaster::where('plant_id', $plantId)->distinct()->pluck('name', 'name')->toArray(): []; + } + }) + ->searchable() + ->reactive(), + TextInput::make('description') + ->label('Description') + ->reactive(), + TextInput::make('operation_number') + ->label('Operation Number') + ->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), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['Plant']) && empty($data['name']) && empty($data['description']) && empty($data['operation_number']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_from']) && empty($data['updated_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['name'])) { + $query->where('name', $data['name']); + } + + if (! empty($data['description'])) { + $query->where('description', $data['description']); + } + + if (! empty($data['operation_number'])) { + $query->where('operation_number', $data['operation_number']); + } + + 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: '.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['name'])) { + $indicators[] = 'Work Group Name: ' . $data['name']; + } + + if (! empty($data['description'])) { + $indicators[] = 'Description: ' . $data['description']; + } + + if (! empty($data['operation_number'])) { + $indicators[] = 'Operation Number: ' . $data['operation_number']; + } + + 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(), @@ -177,14 +318,14 @@ class WorkGroupMasterResource extends Resource ->label('Import Work Group Masters') ->color('warning') ->importer(WorkGroupMasterImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import work group master'); }), ExportAction::make() ->label('Export Work Group Masters') ->color('warning') ->exporter(WorkGroupMasterExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export work group master'); }), ]);