1
0
forked from poc/pds

Added locator validation functionality on resource page and importer file

This commit is contained in:
dhanabalan
2025-07-04 17:39:30 +05:30
parent 10b439f8e3
commit af3ae0803c
2 changed files with 140 additions and 14 deletions

View File

@@ -7,6 +7,8 @@ use App\Filament\Imports\LocatorImporter;
use App\Filament\Resources\LocatorResource\Pages;
use App\Filament\Resources\LocatorResource\RelationManagers;
use App\Models\Locator;
use App\Models\PalletValidation;
use Filament\Forms\Get;
use Filament\Tables\Actions\ImportAction;
use Filament\Facades\Filament;
use Filament\Forms;
@@ -17,6 +19,8 @@ use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Validation\Rule;
use Str;
class LocatorResource extends Resource
{
@@ -34,19 +38,87 @@ class LocatorResource extends Resource
->schema([
Forms\Components\Select::make('plant_id')
->relationship('plant', 'name')
->required(),
->required()
// ->nullable(),
->reactive()
->default(function () {
return optional(Locator::latest()->first())->plant_id;
})
->disabled(fn (Get $get) => !empty($get('id')))
// ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null))
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
// Ensure `linestop_id` is not cleared
if (!$plantId) {
$set('locPlantError', 'Please select a plant first.');
$set('locator_number', null);
$set('locator_quantity', 0);
$set('operator_id', Filament::auth()->user()?->name);
return;
}
else
{
$set('locPlantError', null);
$set('locator_number', null);
$set('locator_quantity', 0);
$set('operator_id', Filament::auth()->user()?->name);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('locPlantError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('locPlantError') ? $get('locPlantError') : null)
->hintColor('danger'),
Forms\Components\TextInput::make('locator_number')
->label('Locator Number')
->required(),
Forms\Components\TextInput::make('locator_quantity')
->minLength(7)
->reactive()
->required()
->numeric()
->afterStateUpdated(function ($state, callable $set, callable $get) {
$plantId = $get('plant_id');
$locator = $get('locator_number');
if (!$plantId) {
$set('locNameError', 'Please select a plant first.');
$set('locator_number', null);
$set('locator_quantity', 0);
$set('operator_id', Filament::auth()->user()?->name);
return;
}
else if (!$locator || Str::length($locator) < 7) {
$set('locNameError', 'Please scan the valid locator number.');
$set('locator_quantity', 0);
$set('operator_id', Filament::auth()->user()?->name);
return;
}
else
{
$set('locNameError', null);
$set('locator_quantity', PalletValidation::where('locator_number', $locator)->where('plant_id', $plantId)->distinct('pallet_number')->count('pallet_number'));
$set('operator_id', Filament::auth()->user()?->name);
}
})
->extraAttributes(fn ($get) => [
'class' => $get('locNameError') ? 'border-red-500' : '',
])
->hint(fn ($get) => $get('locNameError') ? $get('locNameError') : null)
->hintColor('danger')
->rule(function (callable $get) {
return Rule::unique('locators', 'locator_number')
->where('plant_id', $get('plant_id'))
->ignore($get('id')); // Ignore current record during updates
}),
Forms\Components\TextInput::make('locator_quantity')
->label('Locator Quantity')
->required()
->readOnly()
->numeric()
->default(0),
Forms\Components\Hidden::make('operator_id')
->default(Filament::auth()->user()?->name)
->required(),
->default(Filament::auth()->user()?->name)
->required(),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
]);
}