Refactored validations and design in resource file
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 14s

This commit is contained in:
dhanabalan
2026-10-03 17:59:26 +05:30
parent c410f37d3d
commit 693a97cf78

View File

@@ -11,6 +11,7 @@ use App\Models\Line;
use App\Models\Machine;
use App\Models\MotorTestingMaster;
use App\Models\Plant;
use Closure;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
@@ -26,6 +27,7 @@ use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
class BeforeTestReadingResource extends Resource
{
@@ -47,33 +49,29 @@ class BeforeTestReadingResource extends Resource
->label('Plant Name')
->relationship('plant', 'name')
->columnSpan(1) // (['default' => 1, 'sm' => 2])
->searchable()
->required()
->reactive()
->searchable()
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(BeforeTestReading::latest()->first())->plant_id ?? null;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? $userHas : optional(BeforeTestReading::latest()->first())->plant_id ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
if (! $plantId) {
$set('line_id', null);
$set('motor_testing_master_id', null);
$set('machine_id', null);
$set('tPrError', 'Please select a plant first.');
return;
} else {
$set('line_id', null);
$set('motor_testing_master_id', null);
$set('machine_id', null);
$set('tPrError', null);
}
$set('line_id', null);
$set('motor_testing_master_id', null);
$set('machine_id', null);
$set('updated_by', Filament::auth()->user()?->name);
})
@@ -81,10 +79,12 @@ class BeforeTestReadingResource extends Resource
'class' => $get('tPrError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('tPrError') ? $get('tPrError') : null)
->hintColor('danger'),
->hintColor('danger')
->required(),
Forms\Components\Select::make('line_id')
->label('Line Name')
// ->relationship('line', 'name')
// ->relationship('line', 'name')
->columnSpan(1)
->searchable()
->options(function (callable $get) {
$plantId = $get('plant_id');
@@ -92,24 +92,29 @@ class BeforeTestReadingResource extends Resource
return [];
}
return Line::where('plant_id', $plantId)
->pluck('name', 'id')
->toArray();
})
->default(function () {
return optional(BeforeTestReading::latest()->first())->line_id ?? null;
return Line::where('plant_id', $plantId)->pluck('name', 'id')->toArray();
})
->disabled(fn (Get $get) => ! empty($get('id')))
->required()
->default(function (callable $get) {
$plantId = $get('plant_id');
if (empty($plantId)) {
return null;
}
return BeforeTestReading::where('plant_id', $plantId)->latest()->first()->line_id ?? null;
})
->reactive()
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('motor_testing_master_id', null);
$set('machine_id', null);
$set('updated_by', Filament::auth()->user()?->name);
}),
})
->required(),
Forms\Components\Select::make('machine_id')
->label('Work Center')
// ->relationship('machine', 'work_center')
// ->relationship('machine', 'work_center')
->columnSpan(1)
->reactive()
->searchable()
->options(function (callable $get) {
$lineId = $get('line_id');
@@ -118,34 +123,39 @@ class BeforeTestReadingResource extends Resource
}
// Only show machines for the selected line
return Machine::where('line_id', $lineId)
->pluck('work_center', 'id')
->toArray();
})
->default(function () {
return optional(BeforeTestReading::latest()->first())->machine_id ?? null;
return Machine::where('line_id', $lineId)->pluck('work_center', 'id')->toArray();
})
->disabled(fn (Get $get) => ! empty($get('id')))
->required()
->reactive()
->default(function (callable $get) {
$plantId = $get('plant_id');
if (empty($plantId)) {
return null;
}
return BeforeTestReading::where('plant_id', $plantId)->latest()->first()->machine_id ?? null;
})
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
})
->required(),
Forms\Components\Select::make('motor_testing_master_id')
->label('Item Code')
// ->relationship('motorTestingMaster', 'item.code')
// ->options(function (callable $get) {
// $plantId = $get('plant_id');
// if (!$plantId) {
// return [];
// }
// return MotorTestingMaster::with('item')
// ->where('plant_id', $plantId)
// ->get()
// //->filter(fn ($mtm) => $mtm->item)
// ->pluck('item.code', 'id')
// ->toArray();
// })
// ->relationship('motorTestingMaster', 'item.code')
->columnSpan(1)
// ->options(function (callable $get) {
// $plantId = $get('plant_id');
// if (!$plantId) {
// return [];
// }
// return MotorTestingMaster::with('item')
// ->where('plant_id', $plantId)
// ->get()
// //->filter(fn ($mtm) => $mtm->item)
// ->pluck('item.code', 'id')
// ->toArray();
// })
->reactive()
->searchable()
->options(function (callable $get) {
$plantId = $get('plant_id');
if (! $plantId) {
@@ -159,42 +169,83 @@ class BeforeTestReadingResource extends Resource
->pluck('items.code', 'motor_testing_masters.id')
->toArray();
})
// ->getOptionLabelUsing(fn ($value) =>
// MotorTestingMaster::with('item')->find($value)?->item?->code
// )
->required()
->searchable()
->reactive()
->disabled(fn (Get $get) => ! empty($get('id')))
// ->getOptionLabelUsing(fn ($value) =>
// MotorTestingMaster::with('item')->find($value)?->item?->code
// )
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
})
->required(),
Forms\Components\TextInput::make('serial_number')
->label('Serial Number')
->required()
->columnSpan(1)
->reactive()
->readOnly(fn (Get $get) => $get('id'))
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
})
->rule(function (callable $get) {
return function (string $attribute, $value, Closure $fail) use ($get) {
$plantId = $get('plant_id');
$lineId = $get('line_id');
$machineId = $get('machine_id');
$itemId = $get('motor_testing_master_id');
$serialNumber = trim($value);
// $currentId = $get('id'); // current editing record id
if (! $plantId || ! $lineId || ! $machineId || ! $itemId) {
return;
}
if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) {
$fail("Serial number can't be empty!");
} elseif (Str::length($serialNumber) < 9) {
$fail('Serial number should contain minimum 9 digits!');
} elseif (! ctype_alnum($serialNumber)) {
$fail('Serial number should contain only alpha-numeric values!');
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) {
$fail("Serial number should not begin with '0' or letter!");
}
};
})
->required(),
Forms\Components\TextInput::make('before_fr_res_ry')
->label('Before FR Resistance RY')
->columnSpan(1)
->reactive()
->readOnly(fn (Get $get) => $get('id'))
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\TextInput::make('before_fr_res_yb')
->label('Before FR Resistance YB')
->columnSpan(1)
->reactive()
->readOnly(fn (Get $get) => $get('id'))
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\TextInput::make('before_fr_res_br')
->label('Before FR Resistance BR')
->columnSpan(1)
->reactive()
->readOnly(fn (Get $get) => $get('id'))
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\TextInput::make('before_fr_ir')
->label('Before FR IR')
->columnSpan(1)
->reactive()
->readOnly(fn (Get $get) => $get('id'))
->afterStateUpdated(function (callable $set, callable $get, ?string $state) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\TextInput::make('tested_by')
->label('Tested By')
->columnSpan(1)
->default(fn () => Filament::auth()->user()?->name)
->readOnly()
->required(),
@@ -232,6 +283,9 @@ class BeforeTestReadingResource extends Resource
Tables\Columns\TextColumn::make('line.name')
->label('Line Name')
->alignCenter(),
Tables\Columns\TextColumn::make('machine.workGroupMaster.name')
->label('Group Work Center')
->alignCenter(),
Tables\Columns\TextColumn::make('machine.work_center')
->label('Work Center')
->alignCenter(),
@@ -244,9 +298,20 @@ class BeforeTestReadingResource extends Resource
Tables\Columns\TextColumn::make('motorTestingMaster.item.description')
->label('Model')
->alignCenter(),
Tables\Columns\TextColumn::make('motorTestingMaster.item.category')
->label('Category')
->default('-')
->alignCenter()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('motorTestingMaster.item.uom')
->label('Unit of Measure')
->default('-')
->alignCenter()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('serial_number')
->label('Serial Number')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('motorTestingMaster.kw')
->label('KW')
@@ -288,8 +353,7 @@ class BeforeTestReadingResource extends Resource
->sortable(),
Tables\Columns\TextColumn::make('tested_by')
->label('Tested By')
->alignCenter()
->numeric(),
->alignCenter(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->alignCenter()
@@ -297,8 +361,7 @@ class BeforeTestReadingResource extends Resource
->dateTime(),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated By')
->alignCenter()
->numeric(),
->alignCenter(),
Tables\Columns\TextColumn::make('deleted_at')
->label('Deleted At')
->alignCenter()
@@ -306,6 +369,7 @@ class BeforeTestReadingResource extends Resource
->dateTime()
->toggleable(isToggledHiddenByDefault: true),
])
->searchPlaceholder('Search by Serial Number')
->filters([
Tables\Filters\TrashedFilter::make(),
Filter::make('advanced_filters')
@@ -313,8 +377,9 @@ class BeforeTestReadingResource extends Resource
->form([
Select::make('Plant')
->label('Search by Plant Name')
->searchable()
->nullable()
->searchable()
->reactive()
->options(function () {
// return Plant::pluck('name', 'id');
$userHas = Filament::auth()->user()->plant_id;
@@ -329,7 +394,6 @@ class BeforeTestReadingResource extends Resource
// return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('Line', null);
$set('item_code', null);
@@ -337,26 +401,25 @@ class BeforeTestReadingResource extends Resource
}),
Select::make('Line')
->label('Search by Line Name')
->searchable()
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if (! $plantId) {
return [];
}
return Line::where('plant_id', $plantId)
->pluck('name', 'id')
->toArray();
return Line::where('plant_id', $plantId)->pluck('name', 'id')->toArray();
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('item_code', null);
}),
Select::make('machine_name')
->label('Search by Work Center')
->searchable()
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
$lineId = $get('Line');
@@ -365,31 +428,26 @@ class BeforeTestReadingResource extends Resource
return [];
}
return Machine::where('plant_id', $plantId)
->where('line_id', $lineId)
->pluck('work_center', 'id')
->toArray();
})
->reactive(),
return Machine::where('plant_id', $plantId)->where('line_id', $lineId)->pluck('work_center', 'id')->toArray();
}),
Select::make('item_code')
->label('Search by Item Code')
->searchable()
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return Item::where('plant_id', $plantId)
->whereHas('motorTestingMasters')
->pluck('code', 'id')
->toArray();
} else {
if (empty($plantId)) {
return [];
// return Item::whereHas('motorTestingMasters')
// ->pluck('code', 'id')
// ->toArray();
}
return Item::whereHas('motorTestingMasters', function ($query) use ($plantId) {
if ($plantId) {
$query->where('plant_id', $plantId);
}
})->pluck('code', 'id')->toArray();
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('item_description', null);
}),
@@ -399,57 +457,52 @@ class BeforeTestReadingResource extends Resource
->placeholder('Enter Serial Number'),
Select::make('subassembly_code')
->label('Search by Subassembly Code')
->searchable()
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return MotorTestingMaster::whereHas('beforeTestReadings', function ($query) {
$query->whereNotNull('id');
})->whereNotNull('subassembly_code')->orderBy('subassembly_code')->pluck('subassembly_code', 'id');
} else {
if (empty($plantId)) {
return [];
// return Item::whereHas('motorTestingMasters')
// ->pluck('code', 'id')
// ->toArray();
}
return MotorTestingMaster::whereHas('beforeTestReadings', function ($query) use ($plantId) {
if ($plantId) {
$query->where('plant_id', $plantId);
}
$query->whereNotNull('id');
})->whereNotNull('subassembly_code')->orderBy('subassembly_code')->pluck('subassembly_code', 'id');
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('item_description', null);
}),
Select::make('item_description')
->label('Search by Model')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
// $query = Item::query();
// if ($plantId) {
// $query->where('plant_id', $plantId);
// }
$plantId = $get('Plant');
if ($plantId) {
return Item::where('plant_id', $plantId)
->whereHas('motorTestingMasters')
->pluck('description', 'id')
->toArray();
} else {
if (empty($plantId)) {
return [];
// return Item::whereHas('motorTestingMasters')
// ->pluck('description', 'id')
// ->toArray();
}
// return $query->pluck('description', 'description')->toArray();
return Item::whereHas('motorTestingMasters', function ($query) use ($plantId) {
if ($plantId) {
$query->where('plant_id', $plantId);
}
})->pluck('description', 'id')->toArray();
})
->reactive()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('item_code', null);
}),
Select::make('connection')
->label('Connection')
->required()
->nullable()
->reactive()
->selectablePlaceholder(false)
->default('Star')
->options(function (callable $get) {
$plantId = $get('Plant');
@@ -466,11 +519,12 @@ class BeforeTestReadingResource extends Resource
->toArray();
}
})
->selectablePlaceholder(false)
->reactive(),
->required(),
Select::make('tested_by')
->label('Tested By')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if (! $plantId) {
@@ -478,22 +532,22 @@ class BeforeTestReadingResource extends Resource
} else {
return BeforeTestReading::where('plant_id', $plantId)->whereNotNull('tested_by')->select('tested_by')->distinct()->pluck('tested_by', 'tested_by');
}
})
->searchable()
->reactive(),
DateTimePicker::make(name: 'created_from')
}),
DateTimePicker::make('created_from')
->label('Created From')
->placeholder(placeholder: 'Select From DateTime')
->placeholder('Select From DateTime')
->reactive()
->native(false),
DateTimePicker::make('created_to')
->label('Created To')
->placeholder(placeholder: 'Select To DateTime')
->placeholder('Select To DateTime')
->reactive()
->native(false),
Select::make('updated_by')
->label('Updated By')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$plantId = $get('Plant');
if (! $plantId) {
@@ -501,17 +555,15 @@ class BeforeTestReadingResource extends Resource
} else {
return BeforeTestReading::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by');
}
})
->searchable()
->reactive(),
DateTimePicker::make(name: 'updated_from')
}),
DateTimePicker::make('updated_from')
->label('Updated From')
->placeholder(placeholder: 'Select From DateTime')
->placeholder('Select From DateTime')
->reactive()
->native(false),
DateTimePicker::make('updated_to')
->label('Updated To')
->placeholder(placeholder: 'Select To DateTime')
->placeholder('Select To DateTime')
->reactive()
->native(false),
])