From f611a81da0f15d2deb179f653f28dcc3b3483b47 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 14 Jan 2026 10:43:14 +0530 Subject: [PATCH] Added view rights against plant on view report and Updated alignments and filter query logic on resource --- .../Resources/ProductionQuantityResource.php | 286 +++++++++--------- 1 file changed, 137 insertions(+), 149 deletions(-) diff --git a/app/Filament/Resources/ProductionQuantityResource.php b/app/Filament/Resources/ProductionQuantityResource.php index 2da4d1b..1cada28 100644 --- a/app/Filament/Resources/ProductionQuantityResource.php +++ b/app/Filament/Resources/ProductionQuantityResource.php @@ -6,8 +6,6 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction as ActionsFilam use App\Filament\Exports\ProductionQuantityExporter; use App\Filament\Imports\ProductionQuantityImporter; use App\Filament\Resources\ProductionQuantityResource\Pages; -use App\Filament\Resources\ProductionQuantityResource\RelationManagers; -use App\Forms\Components\PlantSelect; use App\Models\Block; use App\Models\Item; use App\Models\Line; @@ -18,41 +16,36 @@ use Carbon\Carbon; use Filament\Facades\Filament; use Filament\Forms; use Filament\Forms\Components\DateTimePicker; -use Filament\Forms\Components\Hidden; -use Filament\Forms\Form; -use Filament\Forms\Get; -use Filament\Resources\Resource; -use Filament\Tables; -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\Forms\Components\Select; use Filament\Forms\Components\TextInput; -use Filament\Forms\Concerns\InteractsWithForms; +use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Notifications\Notification; +use Filament\Resources\Resource; +use Filament\Tables; use Filament\Tables\Actions\ExportAction; -use Livewire\Livewire; -// use Filament\Forms\Components\View; -use Filament\Tables\Actions\FilamentExportBulkAction; +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\Forms\Components\View; +use Livewire\Livewire; class ProductionQuantityResource extends Resource { - protected static ?string $model = ProductionQuantity::class; protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; // protected static ?string $navigationParentItem = 'Display Transactions'; - // protected static string $view = 'filament.pages.hourly-production'; + // protected static string $view = 'filament.pages.hourly-production'; protected static ?string $navigationGroup = 'Production'; protected static ?int $navigationSort = 1; - // public $plant_id; public static function form(Form $form): Form { @@ -64,30 +57,29 @@ class ProductionQuantityResource extends Resource ->relationship('plant', 'name') ->required() // ->nullable() - ->reactive() + ->reactive() // ->statePath('filters') - ->columnSpan(2) //1 - ->options(function (callable $get) { + ->columnSpan(2) // 1 + ->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(fn () => optional(ProductionQuantity::latest()->first())->plant_id) ->default(function () { return optional(ProductionQuantity::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, $set, callable $get,$livewire) { + ->afterStateUpdated(function ($state, $set, callable $get, $livewire) { $plantId = $get('plant_id'); $set('block_name', null); - if (!$plantId) - { + if (! $plantId) { $set('pqPlantError', 'Please select a plant first.'); + return; - } - else - { + } else { $set('validationError', null); $set('pqPlantError', null); } @@ -127,7 +119,7 @@ class ProductionQuantityResource extends Resource // ->nullable() ->columnSpan(1) ->options(function (callable $get) { - if (!$get('plant_id')) { + if (! $get('plant_id')) { return []; } @@ -138,15 +130,14 @@ class ProductionQuantityResource extends Resource ->reactive() ->default(function () { $latestShiftId = optional(ProductionQuantity::latest()->first())->shift_id; + return optional(Shift::where('id', $latestShiftId)->first())->block_id; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionQuantity::where('id', $get('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('pqBlockError', null); } @@ -158,12 +149,11 @@ class ProductionQuantityResource extends Resource // session(['select_plant' => $get('plant_id')]); // session()->forget('select_line'); - if (!$blockId) { + if (! $blockId) { $set('pqBlockError', 'Please select a block first.'); + return; - } - else - { + } else { $set('validationError', null); $set('pqBlockError', null); } @@ -179,7 +169,7 @@ class ProductionQuantityResource extends Resource ->columnSpan(1) // ->nullable() ->options(function (callable $get) { - if (!$get('plant_id') || !$get('block_name')) { + if (! $get('plant_id') || ! $get('block_name')) { return []; } @@ -192,13 +182,11 @@ class ProductionQuantityResource extends Resource ->default(function () { return optional(ProductionQuantity::latest()->first())->shift_id; }) - //->afterStateUpdated(fn ($set) => $set('line_id', null)) + // ->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionQuantity::where('id', $get('id'))->first(); - if($getShift->shift_id) - { + if ($getShift->shift_id) { $set('shift_id', $getShift->shift_id); $set('pqShiftError', null); } @@ -207,12 +195,11 @@ class ProductionQuantityResource extends Resource $curShiftId = $get('shift_id'); $set('line_id', null); - if (!$curShiftId) { + if (! $curShiftId) { $set('pqShiftError', 'Please select a shift first.'); + return; - } - else - { + } else { $set('validationError', null); $set('pqShiftError', null); } @@ -233,7 +220,7 @@ class ProductionQuantityResource extends Resource // ->toArray() // Convert collection to array // ) ->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 []; } @@ -246,11 +233,9 @@ class ProductionQuantityResource extends Resource return optional(ProductionQuantity::latest()->first())->line_id; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionQuantity::where('id', $get('id'))->first(); - if($getShift->line_id) - { + if ($getShift->line_id) { $set('line_id', $getShift->line_id); $set('pqLineError', null); } @@ -259,16 +244,15 @@ class ProductionQuantityResource extends Resource $lineId = $get('line_id'); $set('item_code', null); - // session(['select_line' => $get('line_id')]); + // session(['select_line' => $get('line_id')]); session(['select_line' => $state]); - if (!$lineId) { + if (! $lineId) { $set('pqLineError', 'Please select a line first.'); + return; - } - else - { + } else { $set('validationError', null); $set('pqLineError', null); $set('item_id', null); @@ -276,7 +260,7 @@ class ProductionQuantityResource extends Resource $set('serial_number', null); $exists = ProductionQuantity::where('plant_id', $get('plant_id')) - //->where('shift_id', $get('shift_id')) + // ->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) ->latest() // Orders by created_at DESC ->first(); @@ -296,7 +280,7 @@ class ProductionQuantityResource extends Resource // 'x-on:change' => "\$wire.dispatch('filtersUpdated')", // Dispatch Livewire event from Alpine.js // ]) ->extraAttributes([ - 'x-on:change' => "\$wire.dispatch('filtersUpdated', { lineId: \$event.target.value })" + 'x-on:change' => "\$wire.dispatch('filtersUpdated', { lineId: \$event.target.value })", ]) ->extraAttributes(fn ($get) => [ 'class' => $get('pqLineError') ? 'border-red-500' : '', @@ -319,7 +303,7 @@ class ProductionQuantityResource extends Resource ->minLength(7) ->maxLength(14) ->columnSpan(2) - //->rules(['regex:/^[1-9][0-9]{6,}$/']) + // ->rules(['regex:/^[1-9][0-9]{6,}$/']) // ->disabled(function ($get) { // return $get('item_code'); // }) @@ -328,21 +312,20 @@ class ProductionQuantityResource extends Resource // $latestProductionOrder = ProductionQuantity::latest()->first()->production_order; // return $latestProductionOrder ?? null; $latestProduction = ProductionQuantity::latest()->first(); + return $latestProduction ? $latestProduction->production_order : null; }) ->afterStateUpdated(function ($state, callable $get, callable $set): void { - if(!is_numeric($get('production_order')) || !preg_match('/^[1-9][0-9]{6,13}$/', $get('production_order'))) - { + if (! is_numeric($get('production_order')) || ! preg_match('/^[1-9][0-9]{6,13}$/', $get('production_order'))) { $set('item_code', null); $set('item_id', null); $set('serial_number', null); $set('production_order', null); - $set('prodOrdError', "Must be a numeric value with 7 to 14 digits."); + $set('prodOrdError', 'Must be a numeric value with 7 to 14 digits.'); $set('validationError', null); + return; - } - else - { + } else { $set('item_code', null); $set('item_id', null); // $set('item_description', null); @@ -350,6 +333,7 @@ class ProductionQuantityResource extends Resource $set('production_order', $state); $set('prodOrdError', null); $set('validationError', null); + return; // if (empty($state)) { // } @@ -923,17 +907,17 @@ class ProductionQuantityResource extends Resource ->required(), Forms\Components\Hidden::make('sap_msg_status'), Forms\Components\Hidden::make('sap_msg_description'), - //->unique(ignoreRecord: true), - // ->autocapitalize('characters'), - // ->columnSpanFull(), - Forms\Components\TextInput::make('recent_qr') //item_description + // ->unique(ignoreRecord: true), + // ->autocapitalize('characters'), + // ->columnSpanFull(), + Forms\Components\TextInput::make('recent_qr') // item_description ->label('Last scanned QR') ->reactive() ->columnSpan(2) ->default(function () { // Get the latest 'item_id' foreign key from 'production_quantities' table $latestProductionQuantity = ProductionQuantity::latest()->first(); - if (!$latestProductionQuantity) { + if (! $latestProductionQuantity) { return null; // Return null if no production quantities exist } @@ -953,7 +937,7 @@ class ProductionQuantityResource extends Resource Forms\Components\Hidden::make('operator_id') ->default(Filament::auth()->user()->name), ]) - ->columns(12), //6 + ->columns(12), // 6 ]); } @@ -971,20 +955,21 @@ class ProductionQuantityResource 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('production_order') ->label('Production Order') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('serial_number') ->label('Serial Number') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('item.code') ->label('Item Code') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('item.uom') ->label('Unit of Measure') ->alignCenter() @@ -1040,7 +1025,7 @@ class ProductionQuantityResource extends Resource Filter::make('advanced_filters') ->label('Advanced Filters') ->form([ - //plant + // plant Select::make('Plant') ->label('Select Plant') ->nullable() @@ -1049,6 +1034,7 @@ class ProductionQuantityResource 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() @@ -1061,34 +1047,36 @@ class ProductionQuantityResource extends Resource $set('operator_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() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('operator_id', null); }), - //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() @@ -1097,7 +1085,7 @@ class ProductionQuantityResource extends Resource $set('operator_id', null); }), - //shift + // shift Select::make('Shift') ->label('Select Shift') ->nullable() @@ -1105,7 +1093,7 @@ class ProductionQuantityResource extends Resource $plantId = $get('Plant'); $blockId = $get('Block'); - if (!$plantId || !$blockId) { + if (! $plantId || ! $blockId) { return []; // Return empty if plant or block is not selected } @@ -1132,11 +1120,10 @@ class ProductionQuantityResource extends Resource ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId ) { - return Item::distinct()->whereHas('productionQuantities')->pluck('code', 'id'); - } - else { - return Item::where('plant_id', $plantId)->whereHas('productionQuantities')->distinct()->pluck('code', 'id'); + if (! $plantId) { + return Item::whereHas('productionQuantities')->distinct()->pluck('code', 'id'); + } else { + return Item::whereHas('productionQuantities')->where('plant_id', $plantId)->distinct()->pluck('code', 'id'); } // return Item::whereHas('stickerMasters', function ($query) use ($pId) { // if ($pId) { @@ -1154,10 +1141,9 @@ class ProductionQuantityResource extends Resource ->options(function (callable $get) { $plantId = $get('Plant'); - if (!$plantId ) { + if (! $plantId) { return ProductionQuantity::whereNotNull('sap_msg_status')->select('sap_msg_status')->distinct()->pluck('sap_msg_status', 'sap_msg_status'); - } - else { + } else { return ProductionQuantity::where('plant_id', $plantId)->whereNotNull('sap_msg_status')->select('sap_msg_status')->distinct()->pluck('sap_msg_status', 'sap_msg_status'); } }) @@ -1171,24 +1157,15 @@ class ProductionQuantityResource extends Resource $plantId = $get('Plant'); $lineId = $get('Line'); $shiftId = $get('Shift'); - if (!$plantId && !$lineId && !$shiftId) - { + if (! $plantId && ! $lineId && ! $shiftId) { return ProductionQuantity::whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); - } - else if ($plantId && !$lineId && !$shiftId) - { + } elseif ($plantId && ! $lineId && ! $shiftId) { return ProductionQuantity::where('plant_id', $plantId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); - } - else if ($plantId && $lineId && !$shiftId) - { + } elseif ($plantId && $lineId && ! $shiftId) { return ProductionQuantity::where('plant_id', $plantId)->where('line_id', $lineId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); - } - else if ($plantId && !$lineId && $shiftId) - { + } elseif ($plantId && ! $lineId && $shiftId) { return ProductionQuantity::where('plant_id', $plantId)->where('shift_id', $shiftId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); - } - else// if ($plantId && $lineId && $shiftId) - { + } else { // if ($plantId && $lineId && $shiftId) return ProductionQuantity::where('plant_id', $plantId)->where('line_id', $lineId)->where('shift_id', $shiftId)->whereNotNull('operator_id')->select('operator_id')->distinct()->pluck('operator_id', 'operator_id'); } }) @@ -1212,44 +1189,50 @@ class ProductionQuantityResource 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 (!empty($data['production_order'])) { - $query->where('production_order', 'like', '%' . $data['production_order'] . '%'); + if (! empty($data['production_order'])) { + $query->where('production_order', 'like', '%'.$data['production_order'].'%'); } - if (!empty($data['serial_number'])) { - $query->where('serial_number', 'like', '%' . $data['serial_number'] . '%'); + if (! empty($data['serial_number'])) { + $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); } - if (!empty($data['Item'])) { + if (! empty($data['Item'])) { $query->where('item_id', $data['Item']); } - if (!empty($data['sap_msg_status'])) { + if (! empty($data['sap_msg_status'])) { $query->where('sap_msg_status', $data['sap_msg_status']); } - if (!empty($data['operator_id'])) { + if (! empty($data['operator_id'])) { $query->where('operator_id', $data['operator_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; @@ -1257,48 +1240,54 @@ class ProductionQuantityResource extends Resource ->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['production_order'])) { - $indicators[] = 'Production Order: ' . $data['production_order']; + if (! empty($data['production_order'])) { + $indicators[] = 'Production Order: '.$data['production_order']; } - if (!empty($data['serial_number'])) { - $indicators[] = 'Serial Number: ' . $data['serial_number']; + if (! empty($data['serial_number'])) { + $indicators[] = 'Serial Number: '.$data['serial_number']; } - if (!empty($data['Item'])) { - $indicators[] = 'Item Code: ' . Item::where('id', $data['Item'])->value('code'); + if (! empty($data['Item'])) { + $indicators[] = 'Item Code: '.Item::where('id', $data['Item'])->value('code'); } - if (!empty($data['sap_msg_status'])) { - $indicators[] = 'SAP Message Status: ' . $data['sap_msg_status']; + if (! empty($data['sap_msg_status'])) { + $indicators[] = 'SAP Message Status: '.$data['sap_msg_status']; } - if (!empty($data['operator_id'])) { - $indicators[] = 'Created By: ' . $data['operator_id']; + if (! empty($data['operator_id'])) { + $indicators[] = 'Created By: '.$data['operator_id']; } - 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']; } return $indicators; - }) + }), ]) ->filtersFormMaxHeight('280px') ->actions([ @@ -1311,7 +1300,7 @@ class ProductionQuantityResource extends Resource Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), ActionsFilamentExportBulkAction::make('export') - ->defaultPageOrientation('landscape'), + ->defaultPageOrientation('landscape'), ]), ]) ->headerActions([ @@ -1319,16 +1308,16 @@ class ProductionQuantityResource extends Resource ->label('Import Production Quantities') ->color('warning') ->importer(ProductionQuantityImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import production quantities'); }), - // ->chunkSize(250), - // ->maxRows(100000), + // ->chunkSize(250), + // ->maxRows(100000), ExportAction::make() ->label('Export Production Quantities') ->color('warning') ->exporter(ProductionQuantityExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export production quantities'); }), ]); @@ -1370,7 +1359,6 @@ class ProductionQuantityResource extends Resource return 'Production Reports'; } - // public function triggerChartUpdate(): void // { // if (session()->has('select_plant') && session()->has('select_line')) {