From c70c37bfcf43c5d2ebcbb1e8615edb589dc4b2bf Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sun, 20 Sep 2026 12:47:24 +0530 Subject: [PATCH] changed logic in machine page --- app/Filament/Resources/MachineResource.php | 485 ++++++++++++++------- 1 file changed, 337 insertions(+), 148 deletions(-) diff --git a/app/Filament/Resources/MachineResource.php b/app/Filament/Resources/MachineResource.php index c99a1f0..9acd7f4 100644 --- a/app/Filament/Resources/MachineResource.php +++ b/app/Filament/Resources/MachineResource.php @@ -5,22 +5,26 @@ namespace App\Filament\Resources; use App\Filament\Exports\MachineExporter; use App\Filament\Imports\MachineImporter; use App\Filament\Resources\MachineResource\Pages; -use App\Filament\Resources\MachineResource\RelationManagers; use App\Models\Line; use App\Models\Machine; use App\Models\Plant; use App\Models\WorkGroupMaster; use Filament\Facades\Filament; use Filament\Forms; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Section; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; 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\Filters\Filter; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; -use Filament\Tables\Actions\ImportAction; -use Filament\Tables\Actions\ExportAction; use Illuminate\Validation\Rule; use Str; @@ -38,149 +42,163 @@ class MachineResource extends Resource { return $form ->schema([ - Forms\Components\Select::make('plant_id') - ->label('Plant') - ->relationship('plant', 'name') - ->required() - ->reactive() - ->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(); - }) - ->default(function () { - return optional(Machine::latest()->first())->plant_id; - }) - ->disabled(fn (Get $get) => !empty($get('id'))) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $plantId = $get('plant_id'); - if (!$plantId) { - $set('mPlantError', 'Please select a plant first.'); - $set('line_id', null); - $set('work_group_master_id', null); - return; - } - else - { - $set('mPlantError', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('mPlantError') ? 'border-red-500' : '', - ]) - ->hint(fn ($get) => $get('mPlantError') ? $get('mPlantError') : null) - ->hintColor('danger'), - Forms\Components\Select::make('line_id') - ->label('Line') - ->relationship('line', 'name') - ->required() - ->reactive() - ->options(function (callable $get) { - if (!$get('plant_id')) { - return []; - } + Section::make('') + ->schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->searchable() + ->required() + ->columnSpan(1) + ->reactive() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; - return Line::where('plant_id', $get('plant_id'))->where('no_of_operation', '>', 0)->pluck('name', 'id')->toArray(); - }) - ->default(function () { - return optional(Machine::latest()->first())->line_id; - }) - ->disabled(fn (Get $get) => !empty($get('id'))) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $lineId = $get('line_id'); - if (!$lineId) { - $set('mLineError', 'Please select a line first.'); - $set('work_group_master_id', null); - return; - } - else - { - // $grpWrkCnr = Line::find($lineId)->group_work_center; - // if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1) - // { - // $set('mLineError', 'Please select a group work center line.'); - // $set('line_id', null); - // return; - // } - $set('mLineError', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('mLineError') ? 'border-red-500' : '', - ]) - ->hint(fn ($get) => $get('mLineError') ? $get('mLineError') : null) - ->hintColor('danger'), - Forms\Components\Select::make('work_group_master_id') - ->label('Group Work Center') - ->relationship('workGroupMaster', 'name') - ->required() - ->reactive() - ->options(function (callable $get) { - if (!$get('plant_id') || !$get('line_id')) { - 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(Machine::latest()->first())->plant_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + $set('mPlantError', 'Please select a plant first.'); + $set('line_id', null); + $set('work_group_master_id', null); - $line = Line::find($get('line_id')); - $workGroupIds = []; - for ($i = 1; $i <= $line->no_of_operation; $i++) { - $column = "work_group{$i}_id"; - if (!empty($line->$column)) { - $workGroupIds[] = $line->$column; - } - } + return; + } else { + $set('mPlantError', null); + $set('line_id', null); + $set('work_group_master_id', null); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('mPlantError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('mPlantError') ? $get('mPlantError') : null) + ->hintColor('danger'), + Forms\Components\Select::make('line_id') + ->label('Line Name') + // ->relationship('line', 'name') + ->searchable() + ->required() + ->columnSpan(1) + ->reactive() + ->options(function (callable $get) { + if (! $get('plant_id')) { + return []; + } - return WorkGroupMaster::where('plant_id', $get('plant_id'))->whereIn('id', $workGroupIds)->pluck('name', 'id')->toArray(); - }) - ->default(function () { - return optional(Machine::latest()->first())->work_group_master_id; - }) - ->disabled(fn (Get $get) => !empty($get('id'))) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $lineId = $get('line_id'); - if (!$lineId) { - $set('mGroupWorkError', 'Please select a line first.'); - return; - } - else - { - // $grpWrkCnr = Line::find($lineId)->group_work_center; - // if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1) - // { - // $set('mGroupWorkError', 'Please select a group work center line.'); - // $set('line_id', null); - // return; - // } - $set('mGroupWorkError', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('mGroupWorkError') ? 'border-red-500' : '', + return Line::where('plant_id', $get('plant_id'))->where('no_of_operation', '>', 0)->pluck('name', 'id')->toArray(); + }) + ->default(function () { + return optional(Machine::latest()->first())->line_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $lineId = $get('line_id'); + if (! $lineId) { + $set('mLineError', 'Please select a line first.'); + $set('work_group_master_id', null); + + return; + } else { + // $grpWrkCnr = Line::find($lineId)->group_work_center; + // if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1) + // { + // $set('mLineError', 'Please select a group work center line.'); + // $set('line_id', null); + // return; + // } + $set('mLineError', null); + $set('work_group_master_id', null); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('mLineError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('mLineError') ? $get('mLineError') : null) + ->hintColor('danger'), + Forms\Components\Select::make('work_group_master_id') + ->label('Group Work Center') + // ->relationship('workGroupMaster', 'name') + ->searchable() + ->required() + ->columnSpan(1) + ->reactive() + ->options(function (callable $get) { + if (! $get('plant_id') || ! $get('line_id')) { + return []; + } + + $line = Line::find($get('line_id')); + $workGroupIds = []; + for ($i = 1; $i <= $line->no_of_operation; $i++) { + $column = "work_group{$i}_id"; + if (! empty($line->$column)) { + $workGroupIds[] = $line->$column; + } + } + + return WorkGroupMaster::where('plant_id', $get('plant_id'))->whereIn('id', $workGroupIds)->pluck('name', 'id')->toArray(); + }) + ->default(function () { + return optional(Machine::latest()->first())->work_group_master_id; + }) + ->disabled(fn (Get $get) => ! empty($get('id'))) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $lineId = $get('line_id'); + if (! $lineId) { + $set('mGroupWorkError', 'Please select a line first.'); + $set('work_group_master_id', null); + + return; + } else { + // $grpWrkCnr = Line::find($lineId)->group_work_center; + // if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1) + // { + // $set('mGroupWorkError', 'Please select a group work center line.'); + // $set('line_id', null); + // return; + // } + $set('mGroupWorkError', null); + } + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('mGroupWorkError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('mGroupWorkError') ? $get('mGroupWorkError') : null) + ->hintColor('danger'), + Forms\Components\TextInput::make('work_center') + ->label('Work Center') + ->minLength(6) + ->placeholder('Scan the valid Work Center') + ->columnSpan(1) + ->required() + ->rule(function (callable $get) { + return Rule::unique('machines', 'work_center') + ->where('plant_id', $get('plant_id')) + ->ignore($get('id')); // Ignore current record during updates + }), + Forms\Components\TextInput::make('name') + ->label('Description') + ->minLength(5) + ->placeholder('Scan the valid Description') + ->columnSpan(['default' => 1, 'sm' => 2]) + ->required() + ->rule(function (callable $get) { + return Rule::unique('machines', 'name') + // ->where('line_id', $get('line_id')) + ->where('plant_id', $get('plant_id')) + ->ignore($get('id')); // Ignore current record during updates + }), + Forms\Components\TextInput::make('id') + ->hidden() + ->readOnly(), ]) - ->hint(fn ($get) => $get('mGroupWorkError') ? $get('mGroupWorkError') : null) - ->hintColor('danger'), - Forms\Components\TextInput::make('name') - ->label('Name') - ->minLength(5) - ->placeholder('Scan the valid Machine Name') - ->required() - ->rule(function (callable $get) { - return Rule::unique('machines', 'name') - //->where('line_id', $get('line_id')) - ->where('plant_id', $get('plant_id')) - ->ignore($get('id')); // Ignore current record during updates - }), - Forms\Components\TextInput::make('work_center') - ->label('Work Center') - ->minLength(6) - ->placeholder('Scan the valid Work Center') - ->required() - ->rule(function (callable $get) { - return Rule::unique('machines', 'work_center') - ->where('plant_id', $get('plant_id')) - ->ignore($get('id')); // Ignore current record during updates - }), - Forms\Components\TextInput::make('id') - ->hidden() - ->readOnly(), + ->columns(['default' => 1, 'sm' => 2]), ]); } @@ -194,18 +212,19 @@ class MachineResource 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(), Tables\Columns\TextColumn::make('line.name') - ->label('Line') + ->label('Line Name') ->alignCenter() ->searchable(), Tables\Columns\TextColumn::make('name') - ->label('Name') + ->label('Description') ->alignCenter() ->searchable(), Tables\Columns\TextColumn::make('work_center') @@ -214,7 +233,7 @@ class MachineResource extends Resource ->searchable() ->sortable(), Tables\Columns\TextColumn::make('workGroupMaster.name') - ->label('Work Group Center') + ->label('Group Work Center') ->alignCenter() ->searchable() ->sortable(), @@ -237,7 +256,177 @@ class MachineResource 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('machines', 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('Line') + ->label('Search by Line') + ->searchable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (empty($plantId)) { + return []; + } + + return Line::whereHas('machines', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + })->pluck('name', 'id'); + }) + ->searchable() + ->reactive(), + TextInput::make('name') + ->label('Description') + ->reactive(), + Select::make('work_group_master') + ->label('Search by Work Group Center') + ->options(function (callable $get) { + $plantId = $get('Plant'); + + return WorkGroupMaster::whereHas('machines', function ($query) use ($plantId) { + if ($plantId) { + $query->where('plant_id', $plantId); + } + }) + ->pluck('name', 'id') + ->toArray(); + }) + ->searchable() + ->reactive(), + Select::make('work_center') + ->label('Search by Work Center') + ->options(function (callable $get) { + + $plantId = $get('Plant'); + $workGroupMasterId = $get('work_group_master'); + + if (! $workGroupMasterId) { + return []; + } + + return Machine::query() + ->when($plantId, fn ($q) => $q->where('plant_id', $plantId)) + ->where('work_group_master_id', $workGroupMasterId) + ->pluck('work_center', 'work_center') + ->toArray(); + }) + ->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), + ]) + ->query(function ($query, array $data) { + // Hide all records initially if no filters are applied + if (empty($data['Plant']) && empty($data['Line']) && empty($data['name']) && empty($data['work_group_master']) && 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['Line'])) { + $query->where('line_id', $data['Line']); + } + + if (! empty($data['name'])) { + $query->where('name', $data['name']); + } + + if (! empty($data['work_group_master'])) { + $query->where('work_group_master_id', $data['work_group_master']); + } + + if (! empty($data['work_center'])) { + $query->where('work_center', $data['work_center']); + } + + 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['Line'])) { + $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name'); + } + + if (! empty($data['name'])) { + $indicators[] = 'Description: '.$data['name']; + } + + if (! empty($data['work_group_master'])) { + $indicators[] = 'Work Group Center: '.WorkGroupMaster::where('id', $data['work_group_master'])->value('name'); + } + + if (! empty($data['work_center'])) { + $indicators[] = 'Work Center: '.Machine::where('work_center', $data['work_center'])->value('work_center'); + } + + 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(), @@ -254,14 +443,14 @@ class MachineResource extends Resource ->label('Import Machines') ->color('warning') ->importer(MachineImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import machine'); }), ExportAction::make() ->label('Export Machines') ->color('warning') ->exporter(MachineExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export machine'); }), ]);