ranjith-dev #1379

Merged
jothi merged 3 commits from ranjith-dev into master 2026-11-03 10:44:20 +00:00
3 changed files with 198 additions and 62 deletions

View File

@@ -0,0 +1,150 @@
<?php
namespace App\Filament\Pages;
use App\Models\LeakTestReading;
use App\Models\Plant;
use Filament\Facades\Filament;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Auth;
class LeakTestValidation extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static string $view = 'filament.pages.leak-test-validation';
protected static ?string $navigationGroup = 'Motor Testing Panel';
protected static ?int $navigationSort = 4;
use InteractsWithForms;
public $plantId;
// public $state = [];
public array $filters = [];
public $leakTestQr;
public function mount()
{
$this->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<br>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');
}
}

View File

@@ -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<br>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<br>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 [];

View File

@@ -0,0 +1,10 @@
<x-filament-panels::page>
<div class="space-y-4">
{{-- Render the Select form fields --}}
<div class="space-y-4">
{{ $this->form }}
</div>
</div>
</x-filament-panels::page>