From 8c0676e9b88531dc80adde26e23de7ff965e065d Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 14 Jan 2026 10:29:43 +0530 Subject: [PATCH] Added view rights against plant on view report and Updated alignments and filter query logic on resource --- .../Resources/ProductionLineStopResource.php | 200 +++++++++--------- 1 file changed, 101 insertions(+), 99 deletions(-) diff --git a/app/Filament/Resources/ProductionLineStopResource.php b/app/Filament/Resources/ProductionLineStopResource.php index d064fd18..60cb6c16 100644 --- a/app/Filament/Resources/ProductionLineStopResource.php +++ b/app/Filament/Resources/ProductionLineStopResource.php @@ -6,30 +6,28 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction; use App\Filament\Exports\ProductionLineStopExporter; use App\Filament\Imports\ProductionLineStopImporter; use App\Filament\Resources\ProductionLineStopResource\Pages; -use App\Filament\Resources\ProductionLineStopResource\RelationManagers; use App\Models\Block; use App\Models\Line; use App\Models\LineStop; use App\Models\Plant; use App\Models\ProductionLineStop; use App\Models\Shift; +use Carbon\Carbon; +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\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 Carbon\Carbon; -use Filament\Facades\Filament; -use Filament\Forms\Components\DateTimePicker; -use Filament\Forms\Components\Section; -use Filament\Forms\Components\Select; -use Filament\Forms\Components\TextInput; -use Filament\Tables\Actions\ExportAction; -use Filament\Tables\Filters\Filter; class ProductionLineStopResource extends Resource { @@ -56,22 +54,22 @@ class ProductionLineStopResource 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(ProductionLineStop::latest()->first())->plant_id; }) - ->disabled(fn (Get $get) => !empty($get('id'))) + ->disabled(fn (Get $get) => ! empty($get('id'))) // ->afterStateUpdated(fn ($set) => $set('block_name', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); $set('block_name', null); - if (!$plantId) { + if (! $plantId) { $set('plsPlantError', 'Please select a plant first.'); + return; - } - else - { + } else { $set('plsPlantError', null); } }) @@ -85,29 +83,28 @@ class ProductionLineStopResource extends Resource // ->nullable() ->label('Block') ->options(function (callable $get) { - if (!$get('plant_id')) { + if (! $get('plant_id')) { return []; } - // return \App\Models\Block::where('plant_id', $get('plant_id')) - return Block::where('plant_id', $get('plant_id')) + // return \App\Models\Block::where('plant_id', $get('plant_id')) + return Block::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->default(function () { $latestShiftId = optional(ProductionLineStop::latest()->first())->shift_id; + return optional(Shift::where('id', $latestShiftId)->first())->block_id; }) // ->afterStateUpdated(fn ($set) => $set('shift_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionLineStop::where('id', $get('id'))->first(); // $getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first(); $getBlock = Shift::where('id', $getShift->shift_id)->first(); - if($getBlock->block_id) - { + if ($getBlock->block_id) { $set('block_name', $getBlock->block_id); $set('plsBlockError', null); } @@ -116,12 +113,11 @@ class ProductionLineStopResource extends Resource $blockId = $get('block_name'); $set('shift_id', null); - if (!$blockId) { + if (! $blockId) { $set('plsBlockError', 'Please select a block first.'); + return; - } - else - { + } else { $set('plsBlockError', null); } }) @@ -135,12 +131,12 @@ class ProductionLineStopResource extends Resource ->required() // ->nullable() ->options(function (callable $get) { - if (!$get('plant_id') || !$get('block_name')) { + if (! $get('plant_id') || ! $get('block_name')) { return []; } - return Shift::where('plant_id', $get('plant_id')) - ->where('block_id', $get('block_name')) + return Shift::where('plant_id', $get('plant_id')) + ->where('block_id', $get('block_name')) ->pluck('name', 'id') ->toArray(); }) @@ -150,11 +146,9 @@ class ProductionLineStopResource extends Resource }) // ->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionLineStop::where('id', $get('id'))->first(); - if($getShift->shift_id) - { + if ($getShift->shift_id) { $set('shift_id', $getShift->shift_id); $set('plsShiftError', null); } @@ -163,12 +157,11 @@ class ProductionLineStopResource extends Resource $shiftId = $get('shift_id'); $set('line_id', null); - if (!$shiftId) { + if (! $shiftId) { $set('plsShiftError', 'Please select a shift first.'); + return; - } - else - { + } else { $set('plsShiftError', null); } }) @@ -182,12 +175,12 @@ class ProductionLineStopResource extends Resource ->required() // ->nullable() ->options(function (callable $get) { - if (!$get('plant_id') || !$get('block_name') || !$get('shift_id')) { + if (! $get('plant_id') || ! $get('block_name') || ! $get('shift_id')) { return []; } - // return \App\Models\Line::where('plant_id', $get('plant_id')) - return Line::where('plant_id', $get('plant_id')) + // return \App\Models\Line::where('plant_id', $get('plant_id')) + return Line::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) @@ -196,11 +189,9 @@ class ProductionLineStopResource extends Resource return optional(ProductionLineStop::latest()->first())->line_id; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionLineStop::where('id', $get('id'))->first(); - if($getShift->line_id) - { + if ($getShift->line_id) { $set('line_id', $getShift->line_id); $set('plsLineError', null); } @@ -210,12 +201,11 @@ class ProductionLineStopResource extends Resource $set('linestop_id', null); $set('lineStop_reason', null); - if (!$lineId) { + if (! $lineId) { $set('plsLineError', 'Please select a line first.'); + return; - } - else - { + } else { $set('plsLineError', null); } }) @@ -251,8 +241,9 @@ class ProductionLineStopResource extends Resource $lineStopId = $get('linestop_id'); // Get entered linestop_id // Ensure `linestop_id` is not cleared - if (!$lineStopId) { + if (! $lineStopId) { $set('lineStop_reason', null); + return; } @@ -275,16 +266,14 @@ class ProductionLineStopResource extends Resource ->before('to_datetime') ->reactive() // ->closeOnDateSelection() - ->afterStateUpdated(fn ($state, callable $set, callable $get) => - self::updateStopDuration($get, $set) - ), + ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) + ), Forms\Components\DateTimePicker::make('to_datetime') ->label('To DateTime') ->required() ->after('from_datetime') ->reactive() - ->afterStateUpdated(fn ($state, callable $set, callable $get) => - self::updateStopDuration($get, $set) //self means it calling the function within the class + ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) // self means it calling the function within the class ) ->extraAttributes(fn ($get) => [ 'class' => $get('plsToDateError') ? 'border-red-500' : '', @@ -293,7 +282,7 @@ class ProductionLineStopResource extends Resource ->hintColor('danger'), Forms\Components\TextInput::make('stop_hour') ->required() - ->label( 'Stop Hour') + ->label('Stop Hour') // ->dehydrated(false) // Don't send to backend ->readOnly(true) ->numeric(), @@ -318,24 +307,21 @@ class ProductionLineStopResource extends Resource $from = $get('from_datetime'); $to = $get('to_datetime'); - // Carbon is a PHP date and time library. + // Carbon is a PHP date and time library. if ($from && $to) { - $fromTime = Carbon::parse($from); //Carbon::parse($from) converts the from datetime string into a Carbon objec + $fromTime = Carbon::parse($from); // Carbon::parse($from) converts the from datetime string into a Carbon objec $toTime = Carbon::parse($to); if ($fromTime->lt($toTime)) { $diffInMinutes = $fromTime->diffInMinutes($toTime); // $set('stop_hour', floor($diffInMinutes / 60)); // $set('stop_min', $diffInMinutes % 60); - if((floor($diffInMinutes / 60) === 0.0) && ($diffInMinutes % 60 === 0)) - { + if ((floor($diffInMinutes / 60) === 0.0) && ($diffInMinutes % 60 === 0)) { $set('stop_hour', null); $set('stop_min', null); $set('plsToDateError', 'Time difference must be atlease a minute.'); - } - else - { + } else { $set('stop_hour', floor($diffInMinutes / 60)); $set('stop_min', $diffInMinutes % 60); $set('plsToDateError', null); @@ -363,6 +349,7 @@ class ProductionLineStopResource 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('linestop.code') @@ -433,8 +420,7 @@ class ProductionLineStopResource extends Resource Filter::make('advanced_filters') ->label('Advanced Filters') ->form([ - - //plant + // plant Select::make('Plant') ->label('Select Plant') ->nullable() @@ -443,6 +429,7 @@ class ProductionLineStopResource 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() @@ -453,31 +440,33 @@ class ProductionLineStopResource extends Resource $set('line_stop_id', null); }), - //line + // line Select::make('Line') ->label('Select line') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId ) { + if (! $plantId) { return []; } + return Line::where('plant_id', $plantId) - ->pluck('name', 'id'); + ->pluck('name', 'id'); }) ->reactive(), - //block + // block Select::make('Block') ->label('Select Block') ->nullable() ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId ) { + if (! $plantId) { return []; } + return Block::where('plant_id', $get('Plant'))->pluck('name', 'id'); }) ->reactive() @@ -485,7 +474,7 @@ class ProductionLineStopResource extends Resource $set('Shift', null); }), - //shift + // shift Select::make('Shift') ->label('Select Shift') ->nullable() @@ -493,7 +482,7 @@ class ProductionLineStopResource extends Resource $plantId = $get('Plant'); $blockId = $get('Block'); - if (!$plantId || !$blockId) { + if (! $plantId || ! $blockId) { return []; // Return empty if plant or block is not selected } @@ -503,12 +492,13 @@ class ProductionLineStopResource extends Resource }) ->reactive(), - Select::make('line_stop_id') //linestop_id + Select::make('line_stop_id') // linestop_id ->label('Search by Line Stop Code') ->nullable() // ->options(fn () => LineStop::orderBy('code')->whereHas('productionLineStops')->pluck('code', 'id')) ->options(function (callable $get) { $pId = $get('Plant'); + return LineStop::orderBy('code')->whereHas('productionLineStops', function ($query) use ($pId) { if ($pId) { $query->where('plant_id', $pId); @@ -535,19 +525,25 @@ class ProductionLineStopResource extends Resource return $query->whereRaw('1 = 0'); } - if ($plant = $data['Plant'] ?? null) { - $query->where('plant_id', $plant); + if (! empty($data['Plant'])) {// if ($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 ($shift = $data['Shift'] ?? null) { - $query->where('shift_id', $shift); + if (! empty($data['Shift'])) {// if ($shift = $data['Shift'] ?? null) { + $query->where('shift_id', $data['Shift']); } - if ($line = $data['Line'] ?? null) { - $query->where('line_id', $line); + if (! empty($data['Line'])) {// if ($line = $data['Line'] ?? null) { + $query->where('line_id', $data['Line']); } - if ($code = $data['line_stop_id'] ?? null) { + if (! empty($data['line_stop_id'])) { // if ($code = $data['line_stop_id'] ?? null) { // Find the linestop_id by code entered // $lineStop = \App\Models\LineStop::where('code', 'like', "%{$code}%")->first(); @@ -558,48 +554,54 @@ class ProductionLineStopResource extends Resource // // If no match found, you can either handle it as an error or return no results // $query->where('linestop_id', null); // This will return no results if no match // } - $query->where('linestop_id', $code); + $query->where('linestop_id', $data['line_stop_id']); } - if ($from = $data['created_from'] ?? null) { - $query->where('created_at', '>=', $from); + if (! empty($data['created_from'])) {// if ($from = $data['created_from'] ?? null) { + $query->where('created_at', '>=', $data['created_from']); } - if ($to = $data['created_to'] ?? null) { - $query->where('created_at', '<=', $to); + if (! empty($data['created_to'])) {// if ($to = $data['created_to'] ?? null) { + $query->where('created_at', '<=', $data['created_to']); } // return $query; }) ->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['Shift'])) { - $indicators[] = 'Shift: ' . Shift::where('id', $data['Shift'])->value('name'); + if (! empty($data['Shift'])) { + $indicators[] = 'Shift: '.Shift::where('id', $data['Shift'])->value('name'); } - if (!empty($data['Line'])) { - $indicators[] = 'Line: ' . Line::where('id', $data['Line'])->value('name'); + if (! empty($data['Line'])) { + $indicators[] = 'Line: '.Line::where('id', $data['Line'])->value('name'); } - if (!empty($data['created_from'])) { - $indicators[] = 'From: ' . $data['created_from']; + if (! empty($data['created_from'])) { + $indicators[] = 'From: '.$data['created_from']; } - if (!empty($data['created_to'])) { - $indicators[] = 'To: ' . $data['created_to']; + if (! empty($data['created_to'])) { + $indicators[] = 'To: '.$data['created_to']; } - if (!empty($data['line_stop_id'])) { + if (! empty($data['line_stop_id'])) { $lineStopCod = LineStop::find($data['line_stop_id'])->code ?? 'Unknown'; - $indicators[] = 'Line Stop Code: ' . $lineStopCod; + $indicators[] = 'Line Stop Code: '.$lineStopCod; } return $indicators; - }) + }), ]) ->filtersFormMaxHeight('280px') ->actions([ @@ -611,7 +613,7 @@ class ProductionLineStopResource extends Resource Tables\Actions\DeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), - FilamentExportBulkAction::make('export') + FilamentExportBulkAction::make('export'), ]), ]) ->headerActions([ @@ -619,14 +621,14 @@ class ProductionLineStopResource extends Resource ->label('Import Production Line Stops') ->color('warning') ->importer(ProductionLineStopImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import production line stop'); }), ExportAction::make() ->label('Export Production Line Stops') ->color('warning') ->exporter(ProductionLineStopExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export production line stop'); }), ]);