Added view rights against plant on view report and Updated alignments and filter query logic on resource
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-14 10:29:43 +05:30
parent f9cb090a25
commit 8c0676e9b8

View File

@@ -6,30 +6,28 @@ use AlperenErsoy\FilamentExport\Actions\FilamentExportBulkAction;
use App\Filament\Exports\ProductionLineStopExporter; use App\Filament\Exports\ProductionLineStopExporter;
use App\Filament\Imports\ProductionLineStopImporter; use App\Filament\Imports\ProductionLineStopImporter;
use App\Filament\Resources\ProductionLineStopResource\Pages; use App\Filament\Resources\ProductionLineStopResource\Pages;
use App\Filament\Resources\ProductionLineStopResource\RelationManagers;
use App\Models\Block; use App\Models\Block;
use App\Models\Line; use App\Models\Line;
use App\Models\LineStop; use App\Models\LineStop;
use App\Models\Plant; use App\Models\Plant;
use App\Models\ProductionLineStop; use App\Models\ProductionLineStop;
use App\Models\Shift; use App\Models\Shift;
use Carbon\Carbon;
use Filament\Facades\Filament;
use Filament\Forms; 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\Form;
use Filament\Forms\Get; use Filament\Forms\Get;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction; use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; 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 class ProductionLineStopResource extends Resource
{ {
@@ -56,22 +54,22 @@ class ProductionLineStopResource extends Resource
->reactive() ->reactive()
->options(function (callable $get) { ->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id; $userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
}) })
->default(function () { ->default(function () {
return optional(ProductionLineStop::latest()->first())->plant_id; 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(fn ($set) => $set('block_name', null))
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id'); $plantId = $get('plant_id');
$set('block_name', null); $set('block_name', null);
if (!$plantId) { if (! $plantId) {
$set('plsPlantError', 'Please select a plant first.'); $set('plsPlantError', 'Please select a plant first.');
return; return;
} } else {
else
{
$set('plsPlantError', null); $set('plsPlantError', null);
} }
}) })
@@ -85,7 +83,7 @@ class ProductionLineStopResource extends Resource
// ->nullable() // ->nullable()
->label('Block') ->label('Block')
->options(function (callable $get) { ->options(function (callable $get) {
if (!$get('plant_id')) { if (! $get('plant_id')) {
return []; return [];
} }
@@ -97,17 +95,16 @@ class ProductionLineStopResource extends Resource
->reactive() ->reactive()
->default(function () { ->default(function () {
$latestShiftId = optional(ProductionLineStop::latest()->first())->shift_id; $latestShiftId = optional(ProductionLineStop::latest()->first())->shift_id;
return optional(Shift::where('id', $latestShiftId)->first())->block_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) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
if($get('id')) if ($get('id')) {
{
$getShift = ProductionLineStop::where('id', $get('id'))->first(); $getShift = ProductionLineStop::where('id', $get('id'))->first();
// $getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first(); // $getBlock = \App\Models\Shift::where('id', $getShift->shift_id)->first();
$getBlock = 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('block_name', $getBlock->block_id);
$set('plsBlockError', null); $set('plsBlockError', null);
} }
@@ -116,12 +113,11 @@ class ProductionLineStopResource extends Resource
$blockId = $get('block_name'); $blockId = $get('block_name');
$set('shift_id', null); $set('shift_id', null);
if (!$blockId) { if (! $blockId) {
$set('plsBlockError', 'Please select a block first.'); $set('plsBlockError', 'Please select a block first.');
return; return;
} } else {
else
{
$set('plsBlockError', null); $set('plsBlockError', null);
} }
}) })
@@ -135,7 +131,7 @@ class ProductionLineStopResource extends Resource
->required() ->required()
// ->nullable() // ->nullable()
->options(function (callable $get) { ->options(function (callable $get) {
if (!$get('plant_id') || !$get('block_name')) { if (! $get('plant_id') || ! $get('block_name')) {
return []; return [];
} }
@@ -150,11 +146,9 @@ class ProductionLineStopResource extends Resource
}) })
// ->afterStateUpdated(fn ($set) => $set('line_id', null)) // ->afterStateUpdated(fn ($set) => $set('line_id', null))
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
if($get('id')) if ($get('id')) {
{
$getShift = ProductionLineStop::where('id', $get('id'))->first(); $getShift = ProductionLineStop::where('id', $get('id'))->first();
if($getShift->shift_id) if ($getShift->shift_id) {
{
$set('shift_id', $getShift->shift_id); $set('shift_id', $getShift->shift_id);
$set('plsShiftError', null); $set('plsShiftError', null);
} }
@@ -163,12 +157,11 @@ class ProductionLineStopResource extends Resource
$shiftId = $get('shift_id'); $shiftId = $get('shift_id');
$set('line_id', null); $set('line_id', null);
if (!$shiftId) { if (! $shiftId) {
$set('plsShiftError', 'Please select a shift first.'); $set('plsShiftError', 'Please select a shift first.');
return; return;
} } else {
else
{
$set('plsShiftError', null); $set('plsShiftError', null);
} }
}) })
@@ -182,7 +175,7 @@ class ProductionLineStopResource extends Resource
->required() ->required()
// ->nullable() // ->nullable()
->options(function (callable $get) { ->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 [];
} }
@@ -196,11 +189,9 @@ class ProductionLineStopResource extends Resource
return optional(ProductionLineStop::latest()->first())->line_id; return optional(ProductionLineStop::latest()->first())->line_id;
}) })
->afterStateUpdated(function ($state, callable $set, callable $get) { ->afterStateUpdated(function ($state, callable $set, callable $get) {
if($get('id')) if ($get('id')) {
{
$getShift = ProductionLineStop::where('id', $get('id'))->first(); $getShift = ProductionLineStop::where('id', $get('id'))->first();
if($getShift->line_id) if ($getShift->line_id) {
{
$set('line_id', $getShift->line_id); $set('line_id', $getShift->line_id);
$set('plsLineError', null); $set('plsLineError', null);
} }
@@ -210,12 +201,11 @@ class ProductionLineStopResource extends Resource
$set('linestop_id', null); $set('linestop_id', null);
$set('lineStop_reason', null); $set('lineStop_reason', null);
if (!$lineId) { if (! $lineId) {
$set('plsLineError', 'Please select a line first.'); $set('plsLineError', 'Please select a line first.');
return; return;
} } else {
else
{
$set('plsLineError', null); $set('plsLineError', null);
} }
}) })
@@ -251,8 +241,9 @@ class ProductionLineStopResource extends Resource
$lineStopId = $get('linestop_id'); // Get entered linestop_id $lineStopId = $get('linestop_id'); // Get entered linestop_id
// Ensure `linestop_id` is not cleared // Ensure `linestop_id` is not cleared
if (!$lineStopId) { if (! $lineStopId) {
$set('lineStop_reason', null); $set('lineStop_reason', null);
return; return;
} }
@@ -275,16 +266,14 @@ class ProductionLineStopResource extends Resource
->before('to_datetime') ->before('to_datetime')
->reactive() ->reactive()
// ->closeOnDateSelection() // ->closeOnDateSelection()
->afterStateUpdated(fn ($state, callable $set, callable $get) => ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set)
self::updateStopDuration($get, $set)
), ),
Forms\Components\DateTimePicker::make('to_datetime') Forms\Components\DateTimePicker::make('to_datetime')
->label('To DateTime') ->label('To DateTime')
->required() ->required()
->after('from_datetime') ->after('from_datetime')
->reactive() ->reactive()
->afterStateUpdated(fn ($state, callable $set, callable $get) => ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) // self means it calling the function within the class
self::updateStopDuration($get, $set) //self means it calling the function within the class
) )
->extraAttributes(fn ($get) => [ ->extraAttributes(fn ($get) => [
'class' => $get('plsToDateError') ? 'border-red-500' : '', 'class' => $get('plsToDateError') ? 'border-red-500' : '',
@@ -293,7 +282,7 @@ class ProductionLineStopResource extends Resource
->hintColor('danger'), ->hintColor('danger'),
Forms\Components\TextInput::make('stop_hour') Forms\Components\TextInput::make('stop_hour')
->required() ->required()
->label( 'Stop Hour') ->label('Stop Hour')
// ->dehydrated(false) // Don't send to backend // ->dehydrated(false) // Don't send to backend
->readOnly(true) ->readOnly(true)
->numeric(), ->numeric(),
@@ -321,21 +310,18 @@ class ProductionLineStopResource extends Resource
// Carbon is a PHP date and time library. // Carbon is a PHP date and time library.
if ($from && $to) { 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); $toTime = Carbon::parse($to);
if ($fromTime->lt($toTime)) { if ($fromTime->lt($toTime)) {
$diffInMinutes = $fromTime->diffInMinutes($toTime); $diffInMinutes = $fromTime->diffInMinutes($toTime);
// $set('stop_hour', floor($diffInMinutes / 60)); // $set('stop_hour', floor($diffInMinutes / 60));
// $set('stop_min', $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_hour', null);
$set('stop_min', null); $set('stop_min', null);
$set('plsToDateError', 'Time difference must be atlease a minute.'); $set('plsToDateError', 'Time difference must be atlease a minute.');
} } else {
else
{
$set('stop_hour', floor($diffInMinutes / 60)); $set('stop_hour', floor($diffInMinutes / 60));
$set('stop_min', $diffInMinutes % 60); $set('stop_min', $diffInMinutes % 60);
$set('plsToDateError', null); $set('plsToDateError', null);
@@ -363,6 +349,7 @@ class ProductionLineStopResource extends Resource
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
Tables\Columns\TextColumn::make('linestop.code') Tables\Columns\TextColumn::make('linestop.code')
@@ -433,8 +420,7 @@ class ProductionLineStopResource extends Resource
Filter::make('advanced_filters') Filter::make('advanced_filters')
->label('Advanced Filters') ->label('Advanced Filters')
->form([ ->form([
// plant
//plant
Select::make('Plant') Select::make('Plant')
->label('Select Plant') ->label('Select Plant')
->nullable() ->nullable()
@@ -443,6 +429,7 @@ class ProductionLineStopResource extends Resource
// }) // })
->options(function (callable $get) { ->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id; $userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
}) })
->reactive() ->reactive()
@@ -453,31 +440,33 @@ class ProductionLineStopResource extends Resource
$set('line_stop_id', null); $set('line_stop_id', null);
}), }),
//line // line
Select::make('Line') Select::make('Line')
->label('Select line') ->label('Select line')
->nullable() ->nullable()
->options(function (callable $get) { ->options(function (callable $get) {
$plantId = $get('Plant'); $plantId = $get('Plant');
if (!$plantId ) { if (! $plantId) {
return []; return [];
} }
return Line::where('plant_id', $plantId) return Line::where('plant_id', $plantId)
->pluck('name', 'id'); ->pluck('name', 'id');
}) })
->reactive(), ->reactive(),
//block // block
Select::make('Block') Select::make('Block')
->label('Select Block') ->label('Select Block')
->nullable() ->nullable()
->options(function (callable $get) { ->options(function (callable $get) {
$plantId = $get('Plant'); $plantId = $get('Plant');
if (!$plantId ) { if (! $plantId) {
return []; return [];
} }
return Block::where('plant_id', $get('Plant'))->pluck('name', 'id'); return Block::where('plant_id', $get('Plant'))->pluck('name', 'id');
}) })
->reactive() ->reactive()
@@ -485,7 +474,7 @@ class ProductionLineStopResource extends Resource
$set('Shift', null); $set('Shift', null);
}), }),
//shift // shift
Select::make('Shift') Select::make('Shift')
->label('Select Shift') ->label('Select Shift')
->nullable() ->nullable()
@@ -493,7 +482,7 @@ class ProductionLineStopResource extends Resource
$plantId = $get('Plant'); $plantId = $get('Plant');
$blockId = $get('Block'); $blockId = $get('Block');
if (!$plantId || !$blockId) { if (! $plantId || ! $blockId) {
return []; // Return empty if plant or block is not selected return []; // Return empty if plant or block is not selected
} }
@@ -503,12 +492,13 @@ class ProductionLineStopResource extends Resource
}) })
->reactive(), ->reactive(),
Select::make('line_stop_id') //linestop_id Select::make('line_stop_id') // linestop_id
->label('Search by Line Stop Code') ->label('Search by Line Stop Code')
->nullable() ->nullable()
// ->options(fn () => LineStop::orderBy('code')->whereHas('productionLineStops')->pluck('code', 'id')) // ->options(fn () => LineStop::orderBy('code')->whereHas('productionLineStops')->pluck('code', 'id'))
->options(function (callable $get) { ->options(function (callable $get) {
$pId = $get('Plant'); $pId = $get('Plant');
return LineStop::orderBy('code')->whereHas('productionLineStops', function ($query) use ($pId) { return LineStop::orderBy('code')->whereHas('productionLineStops', function ($query) use ($pId) {
if ($pId) { if ($pId) {
$query->where('plant_id', $pId); $query->where('plant_id', $pId);
@@ -535,19 +525,25 @@ class ProductionLineStopResource extends Resource
return $query->whereRaw('1 = 0'); return $query->whereRaw('1 = 0');
} }
if ($plant = $data['Plant'] ?? null) { if (! empty($data['Plant'])) {// if ($plant = $data['Plant'] ?? null) {
$query->where('plant_id', $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 ($shift = $data['Shift'] ?? null) { if (! empty($data['Shift'])) {// if ($shift = $data['Shift'] ?? null) {
$query->where('shift_id', $shift); $query->where('shift_id', $data['Shift']);
} }
if ($line = $data['Line'] ?? null) { if (! empty($data['Line'])) {// if ($line = $data['Line'] ?? null) {
$query->where('line_id', $line); $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 // Find the linestop_id by code entered
// $lineStop = \App\Models\LineStop::where('code', 'like', "%{$code}%")->first(); // $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 // // 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', 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) { if (! empty($data['created_from'])) {// if ($from = $data['created_from'] ?? null) {
$query->where('created_at', '>=', $from); $query->where('created_at', '>=', $data['created_from']);
} }
if ($to = $data['created_to'] ?? null) { if (! empty($data['created_to'])) {// if ($to = $data['created_to'] ?? null) {
$query->where('created_at', '<=', $to); $query->where('created_at', '<=', $data['created_to']);
} }
// return $query; // return $query;
}) })
->indicateUsing(function (array $data) { ->indicateUsing(function (array $data) {
$indicators = []; $indicators = [];
if (!empty($data['Plant'])) { if (! empty($data['Plant'])) {
$indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); $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'])) { if (! empty($data['Shift'])) {
$indicators[] = 'Shift: ' . Shift::where('id', $data['Shift'])->value('name'); $indicators[] = 'Shift: '.Shift::where('id', $data['Shift'])->value('name');
} }
if (!empty($data['Line'])) { if (! empty($data['Line'])) {
$indicators[] = 'Line: ' . Line::where('id', $data['Line'])->value('name'); $indicators[] = 'Line: '.Line::where('id', $data['Line'])->value('name');
} }
if (!empty($data['created_from'])) { if (! empty($data['created_from'])) {
$indicators[] = 'From: ' . $data['created_from']; $indicators[] = 'From: '.$data['created_from'];
} }
if (!empty($data['created_to'])) { if (! empty($data['created_to'])) {
$indicators[] = 'To: ' . $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'; $lineStopCod = LineStop::find($data['line_stop_id'])->code ?? 'Unknown';
$indicators[] = 'Line Stop Code: ' . $lineStopCod; $indicators[] = 'Line Stop Code: '.$lineStopCod;
} }
return $indicators; return $indicators;
}) }),
]) ])
->filtersFormMaxHeight('280px') ->filtersFormMaxHeight('280px')
->actions([ ->actions([
@@ -611,7 +613,7 @@ class ProductionLineStopResource extends Resource
Tables\Actions\DeleteBulkAction::make(), Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(), Tables\Actions\RestoreBulkAction::make(),
FilamentExportBulkAction::make('export') FilamentExportBulkAction::make('export'),
]), ]),
]) ])
->headerActions([ ->headerActions([
@@ -619,14 +621,14 @@ class ProductionLineStopResource extends Resource
->label('Import Production Line Stops') ->label('Import Production Line Stops')
->color('warning') ->color('warning')
->importer(ProductionLineStopImporter::class) ->importer(ProductionLineStopImporter::class)
->visible(function() { ->visible(function () {
return Filament::auth()->user()->can('view import production line stop'); return Filament::auth()->user()->can('view import production line stop');
}), }),
ExportAction::make() ExportAction::make()
->label('Export Production Line Stops') ->label('Export Production Line Stops')
->color('warning') ->color('warning')
->exporter(ProductionLineStopExporter::class) ->exporter(ProductionLineStopExporter::class)
->visible(function() { ->visible(function () {
return Filament::auth()->user()->can('view export production line stop'); return Filament::auth()->user()->can('view export production line stop');
}), }),
]); ]);