All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
151 lines
5.3 KiB
PHP
151 lines
5.3 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('Leak Test QR')
|
|
->placeholder('Scan Leak Test QR')
|
|
->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');
|
|
}
|
|
}
|