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,6 +54,7 @@ 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 () {
@@ -68,10 +67,9 @@ class ProductionLineStopResource extends Resource
$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);
} }
}) })
@@ -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);
} }
@@ -118,10 +115,9 @@ class ProductionLineStopResource extends Resource
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);
} }
}) })
@@ -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);
} }
@@ -165,10 +159,9 @@ class ProductionLineStopResource extends Resource
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);
} }
}) })
@@ -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);
} }
@@ -212,10 +203,9 @@ class ProductionLineStopResource extends Resource
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);
} }
}) })
@@ -253,6 +243,7 @@ class ProductionLineStopResource extends Resource
// 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' : '',
@@ -328,14 +317,11 @@ class ProductionLineStopResource extends Resource
$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,7 +420,6 @@ 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')
@@ -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()
@@ -463,6 +450,7 @@ class ProductionLineStopResource extends Resource
if (! $plantId) { if (! $plantId) {
return []; return [];
} }
return Line::where('plant_id', $plantId) return Line::where('plant_id', $plantId)
->pluck('name', 'id'); ->pluck('name', 'id');
}) })
@@ -478,6 +466,7 @@ class ProductionLineStopResource extends Resource
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()
@@ -509,6 +498,7 @@ class ProductionLineStopResource extends Resource
// ->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,15 +554,15 @@ 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;
}) })
@@ -575,6 +571,12 @@ class ProductionLineStopResource extends Resource
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'])) {
@@ -599,7 +601,7 @@ class ProductionLineStopResource extends Resource
} }
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([