Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 18s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 18s
Laravel Pint / pint (pull_request) Successful in 6m27s
Laravel Larastan / larastan (pull_request) Failing after 7m22s
217 lines
8.2 KiB
PHP
217 lines
8.2 KiB
PHP
<?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 = trim($value);
|
|
$parts = explode('|', trim($value));
|
|
$validQr = false;
|
|
|
|
if (! $plantId) {
|
|
Notification::make()
|
|
->title('Please select a plant first!')
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! $value) {
|
|
Notification::make()
|
|
->title('Please scan the serial number!')
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! preg_match('/^[^|]+\|[^|]+\|?$/', trim($value)) && count($parts) > 1) {
|
|
Notification::make()
|
|
->title('Invalid QR Format')
|
|
->body("Expected format: ItemCode|SerialNumber<br>Received Format '$value'")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (count($parts) == 1) {
|
|
$sNumber = trim($value);
|
|
if (strlen($sNumber) < 9) {
|
|
Notification::make()
|
|
->title('Invalid Serial Number')
|
|
->body("Serial number '$sNumber' should contain minimum 9 digits!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! ctype_alnum($sNumber)) {
|
|
Notification::make()
|
|
->title('Invalid Serial Number')
|
|
->body("Serial number '$sNumber' should contain only alpha numeric values!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $sNumber)) {
|
|
Notification::make()
|
|
->title('Invalid Serial Number')
|
|
->body("Serial number '{$sNumber}' should not begin with '0' or letter!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} else {
|
|
$validQr = true;
|
|
}
|
|
} 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 (! ctype_alnum($iCode)) {
|
|
Notification::make()
|
|
->title('Invalid Item Code')
|
|
->body("Item code '$iCode' should contain only alpha numeric values!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $iCode)) {
|
|
Notification::make()
|
|
->title('Invalid Item Code')
|
|
->body("Item code '{$iCode}' should not begin with '0'!")
|
|
->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();
|
|
} elseif (! ctype_alnum($sNumber)) {
|
|
Notification::make()
|
|
->title('Invalid Serial Number')
|
|
->body("Serial number '$sNumber' should contain only alpha numeric values!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $sNumber)) {
|
|
Notification::make()
|
|
->title('Invalid Serial Number')
|
|
->body("Serial number '{$sNumber}' should not begin with '0' or letter!")
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} else {
|
|
$validQr = true;
|
|
}
|
|
}
|
|
|
|
if ($validQr) {
|
|
$leakTestQrExists = LeakTestReading::where('plant_id', $plantId)->where('serial_number', $sNumber)->latest()->first();
|
|
|
|
if (! $leakTestQrExists) {
|
|
Notification::make()
|
|
->title('No leak test readings found!')
|
|
->danger()
|
|
->duration(5000)
|
|
->send();
|
|
} else {
|
|
$leakStat = ($leakTestQrExists->test_status == 'P') ? 'Pass' : 'Fail';
|
|
Notification::make()
|
|
->title('Leak Test Readings Found')
|
|
->body("Item Code : {$leakTestQrExists->item_code} <br> Serial Number : {$leakTestQrExists->serial_number} <br> Test Status : {$leakStat} <br> Created At : {$leakTestQrExists->created_at->format('d-m-Y H:i:s')}")
|
|
->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');
|
|
}
|
|
}
|