ranjith-dev #1302
@@ -21,6 +21,8 @@ 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
|
||||||
{
|
{
|
||||||
@@ -76,39 +78,84 @@ class LeakTestReadingResource extends Resource
|
|||||||
->hintColor('danger'),
|
->hintColor('danger'),
|
||||||
Forms\Components\TextInput::make('item_code')
|
Forms\Components\TextInput::make('item_code')
|
||||||
->required()
|
->required()
|
||||||
->label('Item Code')
|
->label('Scan QR Code')
|
||||||
->placeholder('Scan the valid item code')
|
->placeholder('Scan the valid QR Code')
|
||||||
->autofocus(true)
|
->autofocus(true)
|
||||||
->alphaNum()
|
// ->alphaNum()
|
||||||
->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) {
|
// ->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$code = $get('item_code');
|
// $code = $get('item_code');
|
||||||
$set('lsrCodeError', null);
|
// $set('lsrCodeError', null);
|
||||||
if (! $code) {
|
// if (! $code) {
|
||||||
$set('lsrCodeError', 'Scan the valid item code.');
|
// $set('lsrCodeError', 'Scan the valid item code.');
|
||||||
|
|
||||||
return;
|
// return;
|
||||||
} else {
|
// } else {
|
||||||
if (strlen($code) < 6) {
|
// if (strlen($code) < 6) {
|
||||||
$set('lsrCodeError', 'Item code must be at least 6 digits.');
|
// $set('lsrCodeError', 'Item code must be at least 6 digits.');
|
||||||
|
|
||||||
return;
|
// return;
|
||||||
} elseif (! ctype_alnum($code)) {
|
// } 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('item_code', null);
|
||||||
$set('lsrCodeError', 'Item code must contain only alpha-numeric characters!');
|
$set('serial_number', null);
|
||||||
|
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
$set('lsrCodeError', null);
|
|
||||||
}
|
$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) {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
$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) => [
|
->extraAttributes(fn ($get) => [
|
||||||
'class' => $get('lsrCodeError') ? 'border-red-500' : '',
|
'class' => $get('lsrCodeError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
@@ -158,13 +205,14 @@ class LeakTestReadingResource extends Resource
|
|||||||
->label('Test Status')
|
->label('Test Status')
|
||||||
->searchable()
|
->searchable()
|
||||||
->reactive()
|
->reactive()
|
||||||
|
->default('P')
|
||||||
// ->disabled(fn (Get $get) => ! empty($get('id')))
|
// ->disabled(fn (Get $get) => ! empty($get('id')))
|
||||||
->options(function (callable $set, callable $get) {
|
->options(function (callable $set, callable $get) {
|
||||||
$plantId = $get('plant_id');
|
$plantId = $get('plant_id');
|
||||||
$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 [];
|
||||||
|
|||||||
@@ -4,9 +4,55 @@ namespace App\Filament\Resources\LeakTestReadingResource\Pages;
|
|||||||
|
|
||||||
use App\Filament\Resources\LeakTestReadingResource;
|
use App\Filament\Resources\LeakTestReadingResource;
|
||||||
use Filament\Actions;
|
use Filament\Actions;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Resources\Pages\CreateRecord;
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Livewire\Attributes\On;
|
||||||
|
|
||||||
class CreateLeakTestReading extends CreateRecord
|
class CreateLeakTestReading extends CreateRecord
|
||||||
{
|
{
|
||||||
protected static string $resource = LeakTestReadingResource::class;
|
protected static string $resource = LeakTestReadingResource::class;
|
||||||
|
|
||||||
|
|
||||||
|
public function processQR($value){
|
||||||
|
$qrCode = $value;
|
||||||
|
|
||||||
|
$parts = explode('|', $qrCode);
|
||||||
|
$plantId = $this->form->getState()['plant_id'];
|
||||||
|
|
||||||
|
if (! preg_match('/^[^|]+(?:\|[^|]+)?$/', $value)) {
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid QR Format')
|
||||||
|
->body('Expected format: ItemCode|SerialNumber')
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$itemCode = $parts[0] ?? null;
|
||||||
|
$serialNumber = $parts[1] ?? null;
|
||||||
|
|
||||||
|
$this->form->fill([
|
||||||
|
'plant_id' => $plantId,
|
||||||
|
'item_code' => $itemCode,
|
||||||
|
'serial_number' => $serialNumber,
|
||||||
|
'test_status' => 'PASS'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[On('submit-leak-test-form')]
|
||||||
|
public function submitLeakTestForm(): void
|
||||||
|
{
|
||||||
|
$this->create();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRedirectUrl(): string
|
||||||
|
{
|
||||||
|
//return $this->getResource()::getUrl('create'); // Stay on Create Page after savin
|
||||||
|
|
||||||
|
return $this->getResource()::getUrl('create', [
|
||||||
|
'plant_id' => $this->data['plant_id'] ?? null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user