ranjith-dev #1379
150
app/Filament/Pages/LeakTestValidation.php
Normal file
150
app/Filament/Pages/LeakTestValidation.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ use Filament\Forms\Components\Select;
|
|||||||
use Filament\Forms\Components\TextInput;
|
use Filament\Forms\Components\TextInput;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
use Filament\Forms\Get;
|
use Filament\Forms\Get;
|
||||||
|
use Filament\Forms\Set;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Resources\Resource;
|
use Filament\Resources\Resource;
|
||||||
use Filament\Tables;
|
use Filament\Tables;
|
||||||
use Filament\Tables\Actions\ExportAction;
|
use Filament\Tables\Actions\ExportAction;
|
||||||
@@ -21,8 +23,6 @@ use Filament\Tables\Filters\Filter;
|
|||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
use Filament\Forms\Set;
|
|
||||||
use Filament\Notifications\Notification;
|
|
||||||
|
|
||||||
class LeakTestReadingResource extends Resource
|
class LeakTestReadingResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -85,77 +85,53 @@ class LeakTestReadingResource extends Resource
|
|||||||
// ->minLength(6)
|
// ->minLength(6)
|
||||||
->reactive()
|
->reactive()
|
||||||
->disabled(fn (Get $get) => ! empty($get('id')) && ! Filament::auth()->user()->hasRole('Super Admin'))
|
->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) {
|
->afterStateUpdated(function ($state, Set $set, Get $get, $livewire) {
|
||||||
if (blank($state)) {
|
if (blank($state)) {
|
||||||
$set('item_code', null);
|
$set('item_code', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$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()
|
Notification::make()
|
||||||
->title('Invalid QR Format')
|
->title('Invalid Item Code')
|
||||||
->body("Expected format: ItemCode|SerialNumber<br>Received Format '$state'")
|
->body("Item code '$iCode' should contain minimum 6 digits!")
|
||||||
->danger()
|
->danger()
|
||||||
->send();
|
->send();
|
||||||
$set('item_code', null);
|
$set('item_code', null);
|
||||||
return;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$iCode = $parts[0];
|
|
||||||
if (strlen($parts[0]) > 6) {
|
|
||||||
|
|
||||||
Notification::make()
|
return;
|
||||||
->title('Invalid Item Code')
|
} elseif (! preg_match('/^[a-zA-Z0-9]+$/', $iCode)) {
|
||||||
->body("Item code '$iCode' should contain minimum 6 digits!")
|
Notification::make()
|
||||||
->danger()
|
->title('Invalid Item Code')
|
||||||
->send();
|
->body("Item code '$iCode' should contain alpha numeric values!")
|
||||||
$set('item_code', null);
|
->danger()
|
||||||
return;
|
->send();
|
||||||
}
|
$set('item_code', null);
|
||||||
elseif(!preg_match('/^[a-zA-Z0-9]+$/', $iCode)){
|
|
||||||
Notification::make()
|
return;
|
||||||
->title('Invalid Item Code')
|
} else {
|
||||||
->body("Item code '$iCode' should contain alpha numeric values!")
|
|
||||||
->danger()
|
|
||||||
->send();
|
|
||||||
$set('item_code', null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$set('item_code', trim($parts[0]));
|
$set('item_code', trim($parts[0]));
|
||||||
$set('serial_number', isset($parts[1]) ? trim($parts[1]) : null);
|
$set('serial_number', isset($parts[1]) ? trim($parts[1]) : null);
|
||||||
// $set('test_status', 'P');
|
|
||||||
$livewire->dispatch('submit-leak-test-form');
|
$livewire->dispatch('submit-leak-test-form');
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
})
|
||||||
->extraAttributes(fn ($get) => [
|
->extraAttributes(fn ($get) => [
|
||||||
'class' => $get('lsrCodeError') ? 'border-red-500' : '',
|
'class' => $get('lsrCodeError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
@@ -212,7 +188,7 @@ class LeakTestReadingResource extends Resource
|
|||||||
// $itemCode = $get('item_code');
|
// $itemCode = $get('item_code');
|
||||||
// $serialNumber = $get('serial_number');
|
// $serialNumber = $get('serial_number');
|
||||||
|
|
||||||
if (! $plantId) { //|| ! $itemCode || ! $serialNumber
|
if (! $plantId) { // || ! $itemCode || ! $serialNumber
|
||||||
$set('test_status', null);
|
$set('test_status', null);
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
@@ -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>
|
||||||
Reference in New Issue
Block a user