Updated label and validation logics
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled

This commit is contained in:
dhanabalan
2026-04-21 18:11:43 +05:30
parent 3a0c728bb2
commit 117ea6c6ec
4 changed files with 179 additions and 161 deletions

View File

@@ -11,6 +11,7 @@ use App\Models\Plant;
use App\Models\WorkGroupMaster;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
@@ -37,147 +38,156 @@ class MachineResource extends Resource
{
return $form
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant')
->relationship('plant', 'name')
->required()
->reactive()
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
Section::make('')
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant Name')
->relationship('plant', 'name')
->required()
->columnSpan(1)
->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::orderBy('code')->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
if (! $plantId) {
$set('mPlantError', 'Please select a plant first.');
$set('line_id', null);
$set('work_group_master_id', null);
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
if (! $plantId) {
$set('mPlantError', 'Please select a plant first.');
$set('line_id', null);
$set('work_group_master_id', null);
return;
} else {
$set('mPlantError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mPlantError') ? 'border-red-500' : '',
return;
} else {
$set('mPlantError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mPlantError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mPlantError') ? $get('mPlantError') : null)
->hintColor('danger'),
Forms\Components\Select::make('line_id')
->label('Line Name')
->relationship('line', 'name')
->required()
->columnSpan(1)
->reactive()
->options(function (callable $get) {
if (! $get('plant_id')) {
return [];
}
return Line::where('plant_id', $get('plant_id'))->where('no_of_operation', '>', 0)->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->line_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (! $lineId) {
$set('mLineError', 'Please select a line first.');
$set('work_group_master_id', null);
return;
} else {
// $grpWrkCnr = Line::find($lineId)->group_work_center;
// if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
// {
// $set('mLineError', 'Please select a group work center line.');
// $set('line_id', null);
// return;
// }
$set('mLineError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mLineError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mLineError') ? $get('mLineError') : null)
->hintColor('danger'),
Forms\Components\Select::make('work_group_master_id')
->label('Group Work Center')
->relationship('workGroupMaster', 'name')
->required()
->columnSpan(1)
->reactive()
->options(function (callable $get) {
if (! $get('plant_id') || ! $get('line_id')) {
return [];
}
$line = Line::find($get('line_id'));
$workGroupIds = [];
for ($i = 1; $i <= $line->no_of_operation; $i++) {
$column = "work_group{$i}_id";
if (! empty($line->$column)) {
$workGroupIds[] = $line->$column;
}
}
return WorkGroupMaster::where('plant_id', $get('plant_id'))->whereIn('id', $workGroupIds)->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->work_group_master_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (! $lineId) {
$set('mGroupWorkError', 'Please select a line first.');
return;
} else {
// $grpWrkCnr = Line::find($lineId)->group_work_center;
// if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
// {
// $set('mGroupWorkError', 'Please select a group work center line.');
// $set('line_id', null);
// return;
// }
$set('mGroupWorkError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mGroupWorkError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mGroupWorkError') ? $get('mGroupWorkError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('work_center')
->label('Work Center')
->minLength(6)
->placeholder('Scan the valid Work Center')
->columnSpan(1)
->required()
->rule(function (callable $get) {
return Rule::unique('machines', 'work_center')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('name')
->label('Description')
->minLength(5)
->placeholder('Scan the valid Description')
->columnSpan(['default' => 1, 'sm' => 2])
->required()
->rule(function (callable $get) {
return Rule::unique('machines', 'name')
// ->where('line_id', $get('line_id'))
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
])
->hint(fn ($get) => $get('mPlantError') ? $get('mPlantError') : null)
->hintColor('danger'),
Forms\Components\Select::make('line_id')
->label('Line')
->relationship('line', 'name')
->required()
->reactive()
->options(function (callable $get) {
if (! $get('plant_id')) {
return [];
}
return Line::where('plant_id', $get('plant_id'))->where('no_of_operation', '>', 0)->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->line_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (! $lineId) {
$set('mLineError', 'Please select a line first.');
$set('work_group_master_id', null);
return;
} else {
// $grpWrkCnr = Line::find($lineId)->group_work_center;
// if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
// {
// $set('mLineError', 'Please select a group work center line.');
// $set('line_id', null);
// return;
// }
$set('mLineError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mLineError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mLineError') ? $get('mLineError') : null)
->hintColor('danger'),
Forms\Components\Select::make('work_group_master_id')
->label('Group Work Center')
->relationship('workGroupMaster', 'name')
->required()
->reactive()
->options(function (callable $get) {
if (! $get('plant_id') || ! $get('line_id')) {
return [];
}
$line = Line::find($get('line_id'));
$workGroupIds = [];
for ($i = 1; $i <= $line->no_of_operation; $i++) {
$column = "work_group{$i}_id";
if (! empty($line->$column)) {
$workGroupIds[] = $line->$column;
}
}
return WorkGroupMaster::where('plant_id', $get('plant_id'))->whereIn('id', $workGroupIds)->pluck('name', 'id')->toArray();
})
->default(function () {
return optional(Machine::latest()->first())->work_group_master_id;
})
->disabled(fn (Get $get) => ! empty($get('id')))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$lineId = $get('line_id');
if (! $lineId) {
$set('mGroupWorkError', 'Please select a line first.');
return;
} else {
// $grpWrkCnr = Line::find($lineId)->group_work_center;
// if (!$grpWrkCnr || Str::length($grpWrkCnr) < 1)
// {
// $set('mGroupWorkError', 'Please select a group work center line.');
// $set('line_id', null);
// return;
// }
$set('mGroupWorkError', null);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('mGroupWorkError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('mGroupWorkError') ? $get('mGroupWorkError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('name')
->label('Name')
->minLength(5)
->placeholder('Scan the valid Machine Name')
->required()
->rule(function (callable $get) {
return Rule::unique('machines', 'name')
// ->where('line_id', $get('line_id'))
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('work_center')
->label('Work Center')
->minLength(6)
->placeholder('Scan the valid Work Center')
->required()
->rule(function (callable $get) {
return Rule::unique('machines', 'work_center')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
->columns(['default' => 1, 'sm' => 2]),
]);
}
@@ -195,15 +205,15 @@ class MachineResource extends Resource
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.name')
->label('Plant')
->label('Plant Name')
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('line.name')
->label('Line')
->label('Line Name')
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('name')
->label('Name')
->label('Description')
->alignCenter()
->searchable(),
Tables\Columns\TextColumn::make('work_center')
@@ -212,7 +222,7 @@ class MachineResource extends Resource
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('workGroupMaster.name')
->label('Work Group Center')
->label('Group Work Center')
->alignCenter()
->searchable()
->sortable(),