From 4a4259612bdaf1db2c3b59966a8f038d906dcf87 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 14 Jan 2026 09:26:35 +0530 Subject: [PATCH] Added view rights against plant on view report and Updated alignment on resource --- .../Resources/MotorTestingMasterResource.php | 231 +++++++++--------- 1 file changed, 112 insertions(+), 119 deletions(-) diff --git a/app/Filament/Resources/MotorTestingMasterResource.php b/app/Filament/Resources/MotorTestingMasterResource.php index 1c4de36..4b6d6bf 100644 --- a/app/Filament/Resources/MotorTestingMasterResource.php +++ b/app/Filament/Resources/MotorTestingMasterResource.php @@ -5,7 +5,6 @@ namespace App\Filament\Resources; use App\Filament\Exports\MotorTestingMasterExporter; use App\Filament\Imports\MotorTestingMasterImporter; use App\Filament\Resources\MotorTestingMasterResource\Pages; -use App\Filament\Resources\MotorTestingMasterResource\RelationManagers; use App\Models\Configuration; use App\Models\Item; use App\Models\MotorTestingMaster; @@ -20,12 +19,12 @@ 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 Filament\Tables\Filters\Filter; use Illuminate\Validation\Rule; class MotorTestingMasterResource extends Resource @@ -49,20 +48,20 @@ class MotorTestingMasterResource extends Resource ->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(MotorTestingMaster::latest()->first())->plant_id; }) - ->disabled(fn (Get $get) => !empty($get('id'))) + ->disabled(fn (Get $get) => ! empty($get('id'))) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); - if (!$plantId) { + if (! $plantId) { $set('mTmError', 'Please select a plant first.'); + return; - } - else - { + } else { $set('mTmError', null); } }) @@ -78,10 +77,10 @@ class MotorTestingMasterResource extends Resource ->reactive(), Forms\Components\Select::make('item_id') ->label('Item Code') - //->relationship('item', 'name') + // ->relationship('item', 'name') ->options(function (callable $get) { $plantId = $get('plant_id'); - if (!$plantId) { + if (! $plantId) { return []; } @@ -106,19 +105,19 @@ class MotorTestingMasterResource extends Resource ->minLength(6) ->afterStateUpdated(function ($state, callable $set, callable $get) { $code = $get('subassembly_code'); - if (!$code) { + if (! $code) { $set('iCodeError', 'Scan the valid Subassembly Code.'); + return; - } - else - { + } else { if (strlen($code) < 6) { $set('iCodeError', 'Subassembly code must be at least 6 digits.'); + return; - } - else if (!preg_match('/^[a-zA-Z0-9]{6,}$/', $code)) { - $set('code',null); + } elseif (! preg_match('/^[a-zA-Z0-9]{6,}$/', $code)) { + $set('code', null); $set('iCodeError', 'Subassembly code must contain only alpha-numeric characters.'); + return; } $set('iCodeError', null); @@ -143,24 +142,21 @@ class MotorTestingMasterResource extends Resource Forms\Components\Select::make('phase') ->label('Phase') ->options(function (callable $get) { - $plantId = $get('plant_id'); + $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(); - } - }) + 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(); + } + }) ->selectablePlaceholder(false) ->afterStateUpdated(function ($state, callable $set, callable $get) { @@ -197,16 +193,13 @@ class MotorTestingMasterResource extends Resource ->selectablePlaceholder(false) ->options(function (callable $get) { $plantId = $get('plant_id'); - if ($plantId) - { + if ($plantId) { return Configuration::where('plant_id', $plantId) - ->where('c_name', 'MOTOR_CONNECTION') - ->orderBy('created_at') - ->pluck('c_value', 'c_value') - ->toArray(); - } - else - { + ->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') @@ -230,16 +223,13 @@ class MotorTestingMasterResource extends Resource ->selectablePlaceholder(false) ->options(function (callable $get) { $plantId = $get('plant_id'); - if ($plantId) - { + if ($plantId) { return Configuration::where('plant_id', $plantId) - ->where('c_name', 'INSULATION_RESISTANCE_TYPE') - ->orderBy('created_at') - ->pluck('c_value', 'c_value') - ->toArray(); - } - else - { + ->where('c_name', 'INSULATION_RESISTANCE_TYPE') + ->orderBy('created_at') + ->pluck('c_value', 'c_value') + ->toArray(); + } else { return Configuration::where('c_name', 'INSULATION_RESISTANCE_TYPE') ->orderBy('created_at') ->pluck('c_value', 'c_value') @@ -317,6 +307,7 @@ class MotorTestingMasterResource 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') @@ -474,6 +465,7 @@ class MotorTestingMasterResource extends Resource // }) ->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(); }) ->reactive() @@ -488,6 +480,7 @@ class MotorTestingMasterResource extends Resource ->nullable() ->options(function (callable $get) { $pId = $get('Plant'); + return Item::whereHas('motorTestingMasters', function ($query) use ($pId) { if ($pId) { $query->where('plant_id', $pId); @@ -505,7 +498,7 @@ class MotorTestingMasterResource extends Resource ->options([ 'All' => 'All', 'Y' => 'Y', - 'N' => 'N' + 'N' => 'N', ]) ->default(null) ->inlineLabel(false) @@ -516,16 +509,13 @@ class MotorTestingMasterResource extends Resource ->options(function (callable $get) { $plantId = $get('plant_id'); - if ($plantId) - { + if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); - } - else - { + } else { return Configuration::where('c_name', 'MOTOR_PHASE') ->orderBy('created_at') ->pluck('c_value', 'c_value') @@ -544,16 +534,13 @@ class MotorTestingMasterResource extends Resource ->nullable() ->options(function (callable $get) { $plantId = $get('plant_id'); - if ($plantId) - { + if ($plantId) { return Configuration::where('plant_id', $plantId) ->where('c_name', 'MOTOR_CONNECTION') ->orderBy('created_at') ->pluck('c_value', 'c_value') ->toArray(); - } - else - { + } else { return Configuration::where('c_name', 'MOTOR_CONNECTION') ->orderBy('created_at') ->pluck('c_value', 'c_value') @@ -571,12 +558,9 @@ class MotorTestingMasterResource extends Resource ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId) - { + if (! $plantId) { return MotorTestingMaster::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); - } - else - { + } else { return MotorTestingMaster::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by'); } }) @@ -597,12 +581,9 @@ class MotorTestingMasterResource extends Resource ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId) - { + if (! $plantId) { return MotorTestingMaster::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); - } - else - { + } else { return MotorTestingMaster::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); } }) @@ -625,127 +606,139 @@ class MotorTestingMasterResource extends Resource return $query->whereRaw('1 = 0'); } - if (!empty($data['Plant'])) { + if (! empty($data['Plant'])) { $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['Item'])) { + if (! empty($data['Item'])) { $itemIds = Item::where('id', $data['Item']) ->pluck('id') ->toArray(); - if (!empty($itemIds)) { + if (! empty($itemIds)) { $query->whereIn('item_id', $itemIds); } } - if (!empty($data['description'])) { + 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(); + $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)) { + if (! empty($descIds)) { $query->whereIn('item_id', $descIds); } } if ($data['isi_type'] == 'Y') { $query->where('isi_model', true); - } - else if ($data['isi_type'] == 'N') { + } elseif ($data['isi_type'] == 'N') { $query->where('isi_model', false); } - if (!empty($data['phase_type'])) { + if (! empty($data['phase_type'])) { $query->where('phase', $data['phase_type']); } - if (!empty($data['connection_type'])) { + if (! empty($data['connection_type'])) { $query->where('connection', $data['connection_type']); } - if (!empty($data['created_by'])) { + if (! empty($data['created_by'])) { $query->where('created_by', $data['created_by']); } - if (!empty($data['created_from'])) { + if (! empty($data['created_from'])) { $query->where('created_at', '>=', $data['created_from']); } - if (!empty($data['created_to'])) { + if (! empty($data['created_to'])) { $query->where('created_at', '<=', $data['created_to']); } - if (!empty($data['updated_by'])) { + if (! empty($data['updated_by'])) { $query->where('updated_by', $data['updated_by']); } - if (!empty($data['updated_from'])) { + if (! empty($data['updated_from'])) { $query->where('updated_at', '>=', $data['updated_from']); } - if (!empty($data['updated_to'])) { + 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['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['Item'])) { + if (! empty($data['Item'])) { $itemCode = Item::find($data['Item'])->code ?? 'Unknown'; - $indicators[] = 'Item Codes: ' . $itemCode; + $indicators[] = 'Item Codes: '.$itemCode; } - if (!empty($data['description'])) { - $indicators[] = 'Description: ' . $data['description']; + if (! empty($data['description'])) { + $indicators[] = 'Description: '.$data['description']; } if ($data['isi_type'] == 'Y') { $indicators[] = 'ISI Model: Yes'; - } - else if ($data['isi_type'] == 'N') { + } elseif ($data['isi_type'] == 'N') { $indicators[] = 'ISI Model: No'; } - if (!empty($data['phase_type'])) { - $indicators[] = 'Phase: ' . $data['phase_type']; + if (! empty($data['phase_type'])) { + $indicators[] = 'Phase: '.$data['phase_type']; } - if (!empty($data['connection_type'])) { - $indicators[] = 'Connection: ' . $data['connection_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_by'])) { + $indicators[] = 'Created By: '.$data['created_by']; } - if (!empty($data['created_from'])) { - $indicators[] = 'Created From: ' . $data['created_from']; + 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['created_to'])) { + $indicators[] = 'Created To: '.$data['created_to']; } - if (!empty($data['updated_by'])) { - $indicators[] = 'Updated By: ' . $data['updated_by']; + 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_from'])) { + $indicators[] = 'Updated From: '.$data['updated_from']; } - if (!empty($data['updated_to'])) { - $indicators[] = 'Updated To: ' . $data['updated_to']; + if (! empty($data['updated_to'])) { + $indicators[] = 'Updated To: '.$data['updated_to']; } return $indicators; - }) + }), ]) ->filtersFormMaxHeight('280px') ->actions([ @@ -764,14 +757,14 @@ class MotorTestingMasterResource extends Resource ->label('Import Motor Testing Masters') ->color('warning') ->importer(MotorTestingMasterImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import motor testing master'); }), ExportAction::make() ->label('Export Motor Testing Masters') ->color('warning') ->exporter(MotorTestingMasterExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export motor testing master'); }), ]);