From adb61638c929e5e8e5b1b1c1b78a7a60538125b4 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Wed, 14 Jan 2026 10:33:59 +0530 Subject: [PATCH] Added view rights against plant on view report and Updated alignments on resource --- .../Resources/ProductionPlanResource.php | 274 +++++++++--------- 1 file changed, 136 insertions(+), 138 deletions(-) diff --git a/app/Filament/Resources/ProductionPlanResource.php b/app/Filament/Resources/ProductionPlanResource.php index f1e45f7..d4845ca 100644 --- a/app/Filament/Resources/ProductionPlanResource.php +++ b/app/Filament/Resources/ProductionPlanResource.php @@ -6,7 +6,6 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction; use App\Filament\Exports\ProductionPlanExporter; use App\Filament\Imports\ProductionPlanImporter; use App\Filament\Resources\ProductionPlanResource\Pages; -use App\Filament\Resources\ProductionPlanResource\RelationManagers; use App\Models\Block; use App\Models\Line; use App\Models\Plant; @@ -16,19 +15,18 @@ 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 Filament\Forms\Components\Section; -use Filament\Forms\Components\Select; -use Filament\Tables\Actions\ExportAction; -use Filament\Tables\Filters\Filter; -use Illuminate\Support\Facades\Request; class ProductionPlanResource extends Resource { @@ -55,22 +53,22 @@ class ProductionPlanResource 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(ProductionPlan::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('ppPlantError', 'Please select a plant first.'); + return; - } - else - { + } else { $set('ppPlantError', null); } }) @@ -84,7 +82,7 @@ class ProductionPlanResource extends Resource // ->nullable() ->label('Block') ->options(function (callable $get) { - if (!$get('plant_id')) { + if (! $get('plant_id')) { return []; } @@ -95,16 +93,15 @@ class ProductionPlanResource extends Resource ->reactive() ->default(function () { $latestShiftId = optional(ProductionPlan::latest()->first())->shift_id; + return optional(Shift::where('id', $latestShiftId)->first())->block_id; }) - //->afterStateUpdated(fn ($set) => $set('shift_id', null)) + // ->afterStateUpdated(fn ($set) => $set('shift_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionPlan::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('ppBlockError', null); } @@ -113,12 +110,11 @@ class ProductionPlanResource extends Resource $blockId = $get('block_name'); $set('shift_id', null); - if (!$blockId) { + if (! $blockId) { $set('ppBlockError', 'Please select a block first.'); + return; - } - else - { + } else { $set('ppBlockError', null); } }) @@ -133,7 +129,7 @@ class ProductionPlanResource extends Resource // ->nullable() ->autofocus(true) ->options(function (callable $get) { - if (!$get('plant_id') || !$get('block_name')) { + if (! $get('plant_id') || ! $get('block_name')) { return []; } @@ -148,11 +144,9 @@ class ProductionPlanResource extends Resource }) // ->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionPlan::where('id', $get('id'))->first(); - if($getShift->shift_id) - { + if ($getShift->shift_id) { $set('shift_id', $getShift->shift_id); $set('ppShiftError', null); } @@ -161,12 +155,11 @@ class ProductionPlanResource extends Resource $curShiftId = $get('shift_id'); $set('line_id', null); - if (!$curShiftId) { + if (! $curShiftId) { $set('ppShiftError', 'Please select a shift first.'); + return; - } - else - { + } else { $set('ppShiftError', null); } }) @@ -185,7 +178,7 @@ class ProductionPlanResource 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 []; } @@ -198,17 +191,13 @@ class ProductionPlanResource extends Resource // return optional(ProductionPlan::latest()->first())->line_id; // }) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if($get('id')) - { + if ($get('id')) { $getShift = ProductionPlan::where('id', $get('id'))->first(); - if($getShift->line_id) - { + if ($getShift->line_id) { $set('line_id', $getShift->line_id); $set('ppLineError', null); } - } - else - { + } else { $currentDT = Carbon::now()->toDateTimeString(); $set('created_at', $currentDT); $set('update_date', null); @@ -217,44 +206,39 @@ class ProductionPlanResource extends Resource $lineId = $get('line_id'); // $set('plan_quantity', null); - if (!$lineId) { + if (! $lineId) { $set('ppLineError', 'Please select a line first.'); - return; - } - else - { - $isUpdate = !empty($get('id')); - if (!$isUpdate) - { - $exists = ProductionPlan::where('plant_id', $get('plant_id')) - ->where('shift_id', $get('shift_id')) - ->where('line_id', $get('line_id')) - ->whereDate('created_at', today()) - ->latest() - ->exists(); - if ($exists) - { - $set('line_id', null); - $set('ppLineError', 'Production plan already updated.'); - return; - } - else - { - $existShifts = ProductionPlan::where('plant_id', $get('plant_id')) + return; + } else { + $isUpdate = ! empty($get('id')); + if (! $isUpdate) { + $exists = ProductionPlan::where('plant_id', $get('plant_id')) ->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) - ->whereDate('created_at', Carbon::yesterday()) + ->whereDate('created_at', today()) ->latest() ->exists(); - if ($existShifts) //if ($existShifts->count() > 0) - { - //$currentDate = date('Y-m-d'); + if ($exists) { + $set('line_id', null); + $set('ppLineError', 'Production plan already updated.'); + + return; + } else { + $existShifts = ProductionPlan::where('plant_id', $get('plant_id')) + ->where('shift_id', $get('shift_id')) + ->where('line_id', $get('line_id')) + ->whereDate('created_at', Carbon::yesterday()) + ->latest() + ->exists(); + + if ($existShifts) { // if ($existShifts->count() > 0) + // $currentDate = date('Y-m-d'); $yesterday = date('Y-m-d', strtotime('-1 days')); $shiftId = Shift::where('id', $get('shift_id')) - ->first(); + ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; @@ -262,27 +246,26 @@ class ProductionPlanResource extends Resource $totalMinutes = $hRs * 60 + $miNs; - $from_dt = $yesterday . ' ' . $shiftId->start_time; + $from_dt = $yesterday.' '.$shiftId->start_time; - $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt." + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { - //echo "Choosed a valid shift..."; + // echo "Choosed a valid shift..."; $set('line_id', null); $set('ppLineError', 'Production plan already updated.'); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); return; - } - else - { + } else { $currentDate = date('Y-m-d'); $shiftId = Shift::where('id', $get('shift_id')) - ->first(); + ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; @@ -290,33 +273,33 @@ class ProductionPlanResource extends Resource $totalMinutes = $hRs * 60 + $miNs; - $from_dt = $currentDate . ' ' . $shiftId->start_time; + $from_dt = $currentDate.' '.$shiftId->start_time; - $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt." + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range - if (!($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { - //echo "Choosed a valid shift..."; + if (! ($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { + // echo "Choosed a valid shift..."; $set('line_id', null); $set('ppLineError', 'Choosed a invalid shift.'); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); return; } } $set('ppLineError', null); + return; - } - else - { - //$currentDate = date('Y-m-d'); + } else { + // $currentDate = date('Y-m-d'); $yesterday = date('Y-m-d', strtotime('-1 days')); $shiftId = Shift::where('id', $get('shift_id')) - ->first(); + ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; @@ -324,30 +307,29 @@ class ProductionPlanResource extends Resource $totalMinutes = $hRs * 60 + $miNs; - $from_dt = $yesterday . ' ' . $shiftId->start_time; + $from_dt = $yesterday.' '.$shiftId->start_time; - $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt." + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { - //echo "Choosed a valid shift..."; + // echo "Choosed a valid shift..."; // here i'm updating created as yesterday $set('created_at', $from_dt); $set('update_date', '1'); $set('ppLineError', null); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); return; - } - else - { + } else { $currentDate = date('Y-m-d'); $shiftId = Shift::where('id', $get('shift_id')) - ->first(); + ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; @@ -355,24 +337,26 @@ class ProductionPlanResource extends Resource $totalMinutes = $hRs * 60 + $miNs; - $from_dt = $currentDate . ' ' . $shiftId->start_time; + $from_dt = $currentDate.' '.$shiftId->start_time; - $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt." + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range - if (!($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { - //echo "Choosed a valid shift..."; + if (! ($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { + // echo "Choosed a valid shift..."; $set('line_id', null); $set('ppLineError', 'Choosed a invalid shift.'); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); return; } } $set('ppLineError', null); + return; } @@ -449,21 +433,18 @@ class ProductionPlanResource extends Resource ->afterStateUpdated(function ($state, callable $set, callable $get) { $planQuan = $get('plan_quantity'); - if(!$get('update_date') ) - { - if(!$get('id')) - { + if (! $get('update_date')) { + if (! $get('id')) { $currentDT = Carbon::now()->toDateTimeString(); $set('created_at', $currentDT); } } - if (!$planQuan) { + if (! $planQuan) { $set('ppPlanQuanError', 'Scan the valid plan quantity.'); + return; - } - else - { + } else { $set('ppPlanQuanError', null); } }) @@ -476,7 +457,7 @@ class ProductionPlanResource extends Resource ->required() ->integer() ->label('Production Quantity') - ->readOnly(fn (callable $get) => !$get('id')) + ->readOnly(fn (callable $get) => ! $get('id')) ->default(0), Forms\Components\TextInput::make('id') ->hidden() @@ -493,7 +474,7 @@ class ProductionPlanResource extends Resource ->readOnly(), Forms\Components\Hidden::make('operator_id') ->default(Filament::auth()->user()->name), - ]) + ]) ->columns(2), ]); } @@ -529,6 +510,7 @@ class ProductionPlanResource 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('plan_quantity') @@ -544,7 +526,7 @@ class ProductionPlanResource extends Resource Tables\Columns\TextColumn::make('line.name') ->label('Line') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('shift.block.name') ->label('Block') ->alignCenter() @@ -552,11 +534,11 @@ class ProductionPlanResource extends Resource Tables\Columns\TextColumn::make('shift.name') ->label('Shift') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('plant.name') ->label('Plant') ->alignCenter() - ->sortable(),// ->searchable(), + ->sortable(), // ->searchable(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->dateTime() @@ -584,7 +566,7 @@ class ProductionPlanResource extends Resource Filter::make('advanced_filters') ->label('Advanced Filters') ->form([ - //plant + // plant Select::make('Plant') ->label('Select Plant') ->nullable() @@ -593,6 +575,7 @@ class ProductionPlanResource 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() @@ -602,31 +585,33 @@ class ProductionPlanResource extends Resource $set('Shift', 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() @@ -634,7 +619,7 @@ class ProductionPlanResource extends Resource $set('Shift', null); }), - //shift + // shift Select::make('Shift') ->label('Select Shift') ->nullable() @@ -642,7 +627,7 @@ class ProductionPlanResource extends Resource $plantId = $get('Plant'); $blockId = $get('Block'); - if (!$plantId || !$blockId) { + if (! $plantId || ! $blockId) { return []; // Return empty if plant or block is not selected } @@ -669,52 +654,65 @@ class ProductionPlanResource 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 ($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']; } return $indicators; - }) + }), ]) ->filtersFormMaxHeight('280px') ->actions([ @@ -726,7 +724,7 @@ class ProductionPlanResource extends Resource Tables\Actions\DeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), - FilamentExportBulkAction::make('export') + FilamentExportBulkAction::make('export'), ]), ]) ->headerActions([ @@ -734,14 +732,14 @@ class ProductionPlanResource extends Resource ->label('Import Production Plans') ->color('warning') ->importer(ProductionPlanImporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view import production plan'); }), ExportAction::make() ->label('Export Production Plans') ->color('warning') ->exporter(ProductionPlanExporter::class) - ->visible(function() { + ->visible(function () { return Filament::auth()->user()->can('view export production plan'); }), ]);