diff --git a/app/Filament/Resources/CharacteristicValueResource.php b/app/Filament/Resources/CharacteristicValueResource.php index 7b53ae3..3ff0538 100644 --- a/app/Filament/Resources/CharacteristicValueResource.php +++ b/app/Filament/Resources/CharacteristicValueResource.php @@ -25,6 +25,7 @@ use Filament\Tables\Filters\Filter; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; +use Str; class CharacteristicValueResource extends Resource { @@ -187,14 +188,71 @@ class CharacteristicValueResource extends Resource Forms\Components\TextInput::make('observed_value') ->label('Observed Value') ->reactive() + ->afterStateUpdated(function ($state, $set, callable $get) { + $plantId = $get('plant_id'); + $itemId = $get('item_id'); + $lineId = $get('line_id'); + $machineId = $get('machine_id'); + + if (! $plantId || ! $itemId || ! $lineId || ! $machineId) { + $set('status', 'NotOk'); + + return; + } + + if (Str::length($state) <= 0 || ! is_numeric($state) || ! preg_match('/^\d+(\.\d+)?$/', $state) + ) { + $set('status', 'NotOk'); + + return; + } + + $specVal = ProductCharacteristicsMaster::where('plant_id', $plantId)->where('item_id', $itemId)->where('line_id', $lineId)->where('machine_id', $machineId)->first(); + if (! $specVal) { + $set('status', 'NotOk'); + + return; + } + + $lowLimit = $specVal?->lower ?? 0; + $uppLimit = $specVal?->upper ?? 0; + + if (Str::length($lowLimit) <= 0 || ! is_numeric($lowLimit) || ! preg_match('/^\d+(\.\d+)?$/', $lowLimit) + ) { + $set('status', 'NotOk'); + + return; + } elseif (Str::length($uppLimit) <= 0 || ! is_numeric($uppLimit) || ! preg_match('/^\d+(\.\d+)?$/', $uppLimit) + ) { + $set('status', 'NotOk'); + + return; + } + + if (($lowLimit == 0 && $uppLimit == 0) || ($uppLimit == 0)) { + $set('status', 'NotOk'); + + return; + } + + if ($lowLimit > $state || $uppLimit < $state) { + $set('status', 'NotOk'); + + return; + } + + $set('status', 'Ok'); + }) ->required(), - Forms\Components\Select::make('status') + Forms\Components\TextInput::make('status')// Select ->label('Status') - ->options([ - 'Ok' => 'OK', - 'NotOk' => 'Not Ok', - ]) + // ->options([ + // 'Ok' => 'OK', + // 'NotOk' => 'Not Ok', + // ]) ->reactive() + ->default('NotOk') + ->readOnly() ->required(), Forms\Components\Hidden::make('created_by') ->label('Created By')