diff --git a/app/Filament/Pages/LeakTestValidation.php b/app/Filament/Pages/LeakTestValidation.php new file mode 100644 index 00000000..d953cc79 --- /dev/null +++ b/app/Filament/Pages/LeakTestValidation.php @@ -0,0 +1,150 @@ +form->fill([ + 'plant_id' => $this->plantId, + 'leak_test_qr' => null, + ]); + } + + public function form(Form $form): Form + { + return $form + ->statePath('filters') + ->schema([ + Section::make('') + ->schema([ + Select::make('plant_id') + ->label('Plant') + ->reactive() + // ->options(Plant::pluck('name', 'id')) + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); + }) + ->afterStateUpdated(function ($state, $set, callable $get, $livewire) { + $plantId = $get('plant_id'); + $this->plantId = $plantId; + }) + ->required(), + TextInput::make('leak_test_qr') + ->label('Serial Number') + ->placeholder('Scan Serial Number') + ->reactive() + ->minLength(16) + ->afterStateUpdated(function ($state, callable $get, callable $set) { + $this->plantId = $get('plant_id'); + $this->leakTestQr = $state; + }) + ->required() + ->extraAttributes([ + 'wire:keydown.enter' => 'processQr($event.target.value)', + ]), + ]) + ->columns(2), + ]); + } + + public function processQr($value) + { + $plantId = $this->plantId; + $this->leakTestQr = $value; + $parts = explode('|', trim($value)); + + if (! preg_match('/^[^|]+\|[^|]+\|?$/', trim($value))) { + Notification::make() + ->title('Invalid QR Format') + ->body("Expected format: ItemCode|SerialNumber
Received Format '$value'") + ->danger() + ->duration(5000) + ->send(); + } else { + $iCode = trim($parts[0]); + $sNumber = isset($parts[1]) ? trim($parts[1]) : null; + if (strlen($iCode) < 6) { + Notification::make() + ->title('Invalid Item Code') + ->body("Item code '$iCode' should contain minimum 6 digits!") + ->danger() + ->duration(5000) + ->send(); + } elseif (! preg_match('/^[a-zA-Z0-9]+$/', $iCode)) { + Notification::make() + ->title('Invalid Item Code') + ->body("Item code '$iCode' should contain alpha numeric values!") + ->danger() + ->duration(5000) + ->send(); + } elseif (strlen($sNumber) < 9) { + Notification::make() + ->title('Invalid Serial Number') + ->body("Serial number '$sNumber' should contain minimum 9 digits!") + ->danger() + ->duration(5000) + ->send(); + } else { + $leakTestQrExists = LeakTestReading::where('plant_id', $plantId)->where('item_code', $iCode)->where('serial_number', $sNumber)->first(); + + if (! $leakTestQrExists) { + Notification::make() + ->title('Leak Test QR Not Found') + ->danger() + ->duration(5000) + ->send(); + } else { + Notification::make() + ->title('Leak Test QR Found') + ->success() + ->duration(5000) + ->send(); + } + } + } + + $this->leakTestQr = null; + $this->form->fill([ + 'plant_id' => $this->plantId, + 'leak_test_qr' => null, + ]); + } + + public static function canAccess(): bool + { + return Auth::check() && Auth::user()->can('view leak test validation page'); + } +} diff --git a/app/Filament/Resources/LeakTestReadingResource.php b/app/Filament/Resources/LeakTestReadingResource.php index 1e50b3f4..9c3dc6b4 100644 --- a/app/Filament/Resources/LeakTestReadingResource.php +++ b/app/Filament/Resources/LeakTestReadingResource.php @@ -14,6 +14,8 @@ use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; use Filament\Forms\Get; +use Filament\Forms\Set; +use Filament\Notifications\Notification; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Actions\ExportAction; @@ -21,8 +23,6 @@ use Filament\Tables\Filters\Filter; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; -use Filament\Forms\Set; -use Filament\Notifications\Notification; class LeakTestReadingResource extends Resource { @@ -85,77 +85,53 @@ class LeakTestReadingResource extends Resource // ->minLength(6) ->reactive() ->disabled(fn (Get $get) => ! empty($get('id')) && ! Filament::auth()->user()->hasRole('Super Admin')) - // ->afterStateUpdated(function ($state, callable $set, callable $get) { - // $code = $get('item_code'); - // $set('lsrCodeError', null); - // if (! $code) { - // $set('lsrCodeError', 'Scan the valid item code.'); - - // return; - // } else { - // if (strlen($code) < 6) { - // $set('lsrCodeError', 'Item code must be at least 6 digits.'); - - // return; - // } elseif (! ctype_alnum($code)) { - // $set('item_code', null); - // $set('lsrCodeError', 'Item code must contain only alpha-numeric characters!'); - - // return; - // } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $code)) { - // $set('item_code', null); - // $set('lsrCodeError', "Item code should not begin with '0' or letter!"); - - // return; - // } - // $set('lsrCodeError', null); - // } - // }) ->afterStateUpdated(function ($state, Set $set, Get $get, $livewire) { - if (blank($state)) { - $set('item_code', null); - $set('serial_number', null); - return; - } + if (blank($state)) { + $set('item_code', null); + $set('serial_number', null); - $parts = explode('|', trim($state)); + return; + } + + $parts = explode('|', trim($state)); + + if (! preg_match('/^[^|]+\|[^|]+\|?$/', trim($state))) { + Notification::make() + ->title('Invalid QR Format') + ->body("Expected format: ItemCode|SerialNumber
Received Format '$state'") + ->danger() + ->send(); + $set('item_code', null); + + return; + } else { + $iCode = $parts[0]; + if (strlen($parts[0]) < 6) { - if (! preg_match('/^[^|]+\|[^|]+\|?$/', trim($state))) { Notification::make() - ->title('Invalid QR Format') - ->body("Expected format: ItemCode|SerialNumber
Received Format '$state'") + ->title('Invalid Item Code') + ->body("Item code '$iCode' should contain minimum 6 digits!") ->danger() ->send(); $set('item_code', null); - return; - } - else{ - $iCode = $parts[0]; - if (strlen($parts[0]) > 6) { - Notification::make() - ->title('Invalid Item Code') - ->body("Item code '$iCode' should contain minimum 6 digits!") - ->danger() - ->send(); - $set('item_code', null); - return; - } - elseif(!preg_match('/^[a-zA-Z0-9]+$/', $iCode)){ - Notification::make() - ->title('Invalid Item Code') - ->body("Item code '$iCode' should contain alpha numeric values!") - ->danger() - ->send(); - $set('item_code', null); - return; - } + return; + } elseif (! preg_match('/^[a-zA-Z0-9]+$/', $iCode)) { + Notification::make() + ->title('Invalid Item Code') + ->body("Item code '$iCode' should contain alpha numeric values!") + ->danger() + ->send(); + $set('item_code', null); + + return; + } else { $set('item_code', trim($parts[0])); $set('serial_number', isset($parts[1]) ? trim($parts[1]) : null); - // $set('test_status', 'P'); $livewire->dispatch('submit-leak-test-form'); } - }) + } + }) ->extraAttributes(fn ($get) => [ 'class' => $get('lsrCodeError') ? 'border-red-500' : '', ]) @@ -212,7 +188,7 @@ class LeakTestReadingResource extends Resource // $itemCode = $get('item_code'); // $serialNumber = $get('serial_number'); - if (! $plantId) { //|| ! $itemCode || ! $serialNumber + if (! $plantId) { // || ! $itemCode || ! $serialNumber $set('test_status', null); return []; diff --git a/resources/views/filament/pages/leak-test-validation.blade.php b/resources/views/filament/pages/leak-test-validation.blade.php new file mode 100644 index 00000000..7014a78d --- /dev/null +++ b/resources/views/filament/pages/leak-test-validation.blade.php @@ -0,0 +1,10 @@ + + +
+ {{-- Render the Select form fields --}} +
+ {{ $this->form }} +
+
+ +