Create function on enter key press and enable cancel action button only
This commit is contained in:
@@ -22,6 +22,7 @@ use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|||||||
use Filament\Forms\Components\Section;
|
use Filament\Forms\Components\Section;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Tables\Actions\ExportAction;
|
use Filament\Tables\Actions\ExportAction;
|
||||||
|
//use Livewire\Livewire;
|
||||||
|
|
||||||
class ProductionQuantityResource extends Resource
|
class ProductionQuantityResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -33,6 +34,7 @@ class ProductionQuantityResource extends Resource
|
|||||||
|
|
||||||
protected static ?string $navigationGroup = 'Production';
|
protected static ?string $navigationGroup = 'Production';
|
||||||
|
|
||||||
|
|
||||||
public static function form(Form $form): Form
|
public static function form(Form $form): Form
|
||||||
{
|
{
|
||||||
return $form
|
return $form
|
||||||
@@ -44,6 +46,7 @@ class ProductionQuantityResource extends Resource
|
|||||||
->required()
|
->required()
|
||||||
// ->nullable()
|
// ->nullable()
|
||||||
->reactive()
|
->reactive()
|
||||||
|
// ->default(fn () => optional(ProductionQuantity::latest()->first())->plant_id)
|
||||||
->default(function () {
|
->default(function () {
|
||||||
return optional(ProductionQuantity::latest()->first())->plant_id;
|
return optional(ProductionQuantity::latest()->first())->plant_id;
|
||||||
})
|
})
|
||||||
@@ -247,10 +250,11 @@ class ProductionQuantityResource extends Resource
|
|||||||
// ->required()
|
// ->required()
|
||||||
->reactive()
|
->reactive()
|
||||||
->autofocus(true)
|
->autofocus(true)
|
||||||
->debounce(1000)
|
->debounce(100)
|
||||||
// ->submitOnEnter()
|
// ->submitOnEnter()
|
||||||
->afterStateUpdated(function ($state, callable $get, callable $set) {
|
->afterStateUpdated(function ($state, callable $get, callable $set) {
|
||||||
// **Check if input is empty before processing**
|
$set('success_msg', null);
|
||||||
|
|
||||||
if (empty($state)) {
|
if (empty($state)) {
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
@@ -567,10 +571,11 @@ class ProductionQuantityResource extends Resource
|
|||||||
|
|
||||||
if (!preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $state)) {
|
if (!preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $state)) {
|
||||||
if (strpos($state, '|') === false) {
|
if (strpos($state, '|') === false) {
|
||||||
$set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )');
|
$set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid QR')
|
->title('Invalid QR')
|
||||||
@@ -586,10 +591,12 @@ class ProductionQuantityResource extends Resource
|
|||||||
$sNumber = isset($splits[1]) ? trim($splits[1]) : null;
|
$sNumber = isset($splits[1]) ? trim($splits[1]) : null;
|
||||||
|
|
||||||
if (!ctype_alnum($iCode)) {
|
if (!ctype_alnum($iCode)) {
|
||||||
$set('validationError', 'Item code must contain alpha-numeric values.');
|
$set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Item code must contain alpha-numeric values.');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid Item Code')
|
->title('Invalid Item Code')
|
||||||
->body("Item code must contain alpha-numeric values only.")
|
->body("Item code must contain alpha-numeric values only.")
|
||||||
@@ -598,10 +605,12 @@ class ProductionQuantityResource extends Resource
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (strlen($iCode) < 6) {
|
else if (strlen($iCode) < 6) {
|
||||||
$set('validationError', 'Item code must be at least 6 digits.');
|
$set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Item code must be at least 6 digits.');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid Item Code')
|
->title('Invalid Item Code')
|
||||||
->body("Item code must be at least 6 digits.")
|
->body("Item code must be at least 6 digits.")
|
||||||
@@ -610,10 +619,12 @@ class ProductionQuantityResource extends Resource
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (!ctype_alnum($sNumber)) {
|
else if (!ctype_alnum($sNumber)) {
|
||||||
$set('validationError', 'Serial Number must contain alpha-numeric values.');
|
$set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Serial Number must contain alpha-numeric values.');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid Serial Number')
|
->title('Invalid Serial Number')
|
||||||
->body("Serial Number must contain alpha-numeric values only.")
|
->body("Serial Number must contain alpha-numeric values only.")
|
||||||
@@ -622,10 +633,12 @@ class ProductionQuantityResource extends Resource
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (strlen($sNumber) < 9) {
|
else if (strlen($sNumber) < 9) {
|
||||||
$set('validationError', 'Serial Number must be at least 9 digits.');
|
// $set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Serial Number must be at least 9 digits.');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid Serial Number')
|
->title('Invalid Serial Number')
|
||||||
->body("Serial Number must be at least 9 digits.")
|
->body("Serial Number must be at least 9 digits.")
|
||||||
@@ -635,10 +648,10 @@ class ProductionQuantityResource extends Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )');
|
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Invalid QR')
|
->title('Invalid QR')
|
||||||
@@ -672,14 +685,33 @@ class ProductionQuantityResource extends Resource
|
|||||||
->exists();
|
->exists();
|
||||||
if (!$sNo)
|
if (!$sNo)
|
||||||
{
|
{
|
||||||
$set('serial_number', $serialNumber);
|
$set('success_msg', 'Y');
|
||||||
|
// if (preg_match('/\n/', $state)) {
|
||||||
|
// dd($state.': Enter key pressed');
|
||||||
|
|
||||||
|
//$set('serial_number', $serialNumber);
|
||||||
$set('item_id', $item->id);
|
$set('item_id', $item->id);
|
||||||
$set('item_code', $itemCode);
|
//$set('item_code', $itemCode);
|
||||||
// $set('item_description', $item->description);
|
// }
|
||||||
|
// if (str_ends_with($state, "\n")) {
|
||||||
|
// // Enter key was pressed (newline character detected)
|
||||||
|
// //$state = trim($state); // Remove the newline
|
||||||
|
|
||||||
|
// dd($state.': Enter key pressed');
|
||||||
|
// // Perform your custom logic here
|
||||||
|
// // For example, you could trigger a form submission:
|
||||||
|
// // $this->submit();
|
||||||
|
|
||||||
|
// $set('serial_number', $serialNumber);
|
||||||
|
// $set('item_id', $item->id);
|
||||||
|
// $set('item_code', $itemCode);
|
||||||
|
// // $set('item_description', $item->description);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$set('validationError', 'Serial number already exist in database.');
|
$set('validationError', 'Serial number already exist in database.');
|
||||||
|
////$set('item_code', null); //246118|651616516155667
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
@@ -693,10 +725,11 @@ class ProductionQuantityResource extends Resource
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$set('validationError', 'Item code does not exist in master data.');
|
$set('item_code', null);
|
||||||
$set('item_id', null);
|
$set('item_id', null);
|
||||||
// $set('item_description', null);
|
// $set('item_description', null);
|
||||||
$set('serial_number', null);
|
$set('serial_number', null);
|
||||||
|
$set('validationError', 'Item code does not exist in master data.');
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Unknown Item Code')
|
->title('Unknown Item Code')
|
||||||
@@ -710,19 +743,27 @@ class ProductionQuantityResource extends Resource
|
|||||||
'class' => $get('validationError') ? 'border-red-500' : '',
|
'class' => $get('validationError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
->hint(fn ($get) => $get('validationError') ? $get('validationError') : null)
|
->hint(fn ($get) => $get('validationError') ? $get('validationError') : null)
|
||||||
->hintColor('danger'),
|
->hintColor('danger')
|
||||||
// ->extraAttributes([
|
->extraAttributes([
|
||||||
|
'x-data' => '{}',
|
||||||
// 'x-data' => '{ value: "" }',
|
// 'x-data' => '{ value: "" }',
|
||||||
// 'x-model' => 'value',
|
// 'x-model' => 'value',
|
||||||
// 'x-on:keydown.enter.prevent' => '$wire.processQr(value)',
|
// 'x-on:keydown.enter.prevent' => '$wire.processQr(value)',
|
||||||
// ]),
|
//'wire:keydown.enter.prevent' => 'processQr(value)',
|
||||||
|
'x-on:keydown.enter.prevent' => '
|
||||||
|
const formData = new FormData($event.target.form);
|
||||||
|
const data = Object.fromEntries(formData.entries());
|
||||||
|
$wire.processAllValues(data)
|
||||||
|
',
|
||||||
|
]),
|
||||||
Forms\Components\Hidden::make('item_id')
|
Forms\Components\Hidden::make('item_id')
|
||||||
->required(),
|
->required(),
|
||||||
Forms\Components\TextInput::make('serial_number')
|
Forms\Components\Hidden::make('success_msg')
|
||||||
->required()
|
->required(),
|
||||||
->unique(ignoreRecord: true)
|
Forms\Components\Hidden::make('serial_number')
|
||||||
->readOnly(true)
|
->required(),
|
||||||
->autocapitalize('characters'),
|
//->unique(ignoreRecord: true),
|
||||||
|
// ->autocapitalize('characters'),
|
||||||
// ->columnSpanFull(),
|
// ->columnSpanFull(),
|
||||||
Forms\Components\TextInput::make('recent_qr') //item_description
|
Forms\Components\TextInput::make('recent_qr') //item_description
|
||||||
->label('Last scanned QR')
|
->label('Last scanned QR')
|
||||||
|
|||||||
@@ -3,9 +3,13 @@
|
|||||||
namespace App\Filament\Resources\ProductionQuantityResource\Pages;
|
namespace App\Filament\Resources\ProductionQuantityResource\Pages;
|
||||||
|
|
||||||
use App\Filament\Resources\ProductionQuantityResource;
|
use App\Filament\Resources\ProductionQuantityResource;
|
||||||
|
use App\Models\ProductionQuantity;
|
||||||
|
use App\Models\Shift;
|
||||||
use Filament\Actions;
|
use Filament\Actions;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
use Filament\Resources\Pages\CreateRecord;
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
|
||||||
class CreateProductionQuantity extends CreateRecord
|
class CreateProductionQuantity extends CreateRecord
|
||||||
{
|
{
|
||||||
@@ -16,29 +20,118 @@ class CreateProductionQuantity extends CreateRecord
|
|||||||
return $this->getResource()::getUrl('create'); // Stay on Create Page after saving
|
return $this->getResource()::getUrl('create'); // Stay on Create Page after saving
|
||||||
}
|
}
|
||||||
|
|
||||||
// public $qr_data;
|
public $qrData, $pId, $bId, $sId, $lId, $iId, $succId, $sNoId, $succStat, $recQr;
|
||||||
|
|
||||||
// public function processQr($itemCode)
|
// protected static bool $canCreateAnother = false;
|
||||||
// {
|
|
||||||
// $this->qr_data = $itemCode;
|
|
||||||
|
|
||||||
// // Check if the file is uploaded
|
public function getFormActions(): array
|
||||||
// if (!$this->qr_data) {
|
{
|
||||||
// Notification::make()
|
// return parent::getFormActions(); //return [];
|
||||||
// ->title('No QR Found')
|
return [
|
||||||
// ->body("Please, scan the QR code.")
|
$this->getCancelFormAction(),
|
||||||
// ->danger()
|
];
|
||||||
// // ->persistent()
|
}
|
||||||
// ->send();
|
|
||||||
// return;
|
public function processAllValues($formData)
|
||||||
// }
|
{
|
||||||
// else {
|
//$formValues = [];
|
||||||
// Notification::make()
|
$formValues = request()->all(); // This will get all form data from the request
|
||||||
// ->title('QR Found')
|
//dd($formValues);
|
||||||
// ->body("Valid QR code scanned: {$this->qr_data}.")
|
$this->validateAndProcessForm($formValues); // Process the form values
|
||||||
// ->success()
|
}
|
||||||
// // ->persistent()
|
|
||||||
// ->send();
|
public function validateAndProcessForm($formValues)
|
||||||
// }
|
{
|
||||||
// }
|
$user = Filament::auth()->user();
|
||||||
|
$operatorName = $user->name;
|
||||||
|
// $this->validate();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Access the nested form data
|
||||||
|
$componentData = json_decode($formValues['components'][0]['snapshot'], true);
|
||||||
|
$formData = $componentData['data']['data'][0]; // Access first item in data array
|
||||||
|
|
||||||
|
// Extract specific values
|
||||||
|
$this->qrData = $formData['item_code'] ?? null;
|
||||||
|
$this->pId = $formData['plant_id'] ?? null;
|
||||||
|
$this->bId = $formData['block_name'] ?? null;
|
||||||
|
$this->sId = $formData['shift_id'] ?? null;
|
||||||
|
$this->lId = $formData['line_id'] ?? null;
|
||||||
|
$this->iId = $formData['item_id'] ?? null;
|
||||||
|
$this->succId = $formData['success_msg'] ?? null;
|
||||||
|
// $this->sNoId = $formData['serial_number'] ?? null;
|
||||||
|
$this->recQr = $formData['recent_qr'] ?? null;
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
dd('Error parsing form data:', $e->getMessage(), $formValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->succId === null) {
|
||||||
|
$this->form->fill([
|
||||||
|
'plant_id'=> $this->pId,
|
||||||
|
'block_name'=> $this->bId,
|
||||||
|
'shift_id'=> $this->sId,
|
||||||
|
'line_id'=> $this->lId,
|
||||||
|
'item_id'=> null,
|
||||||
|
'serial_number'=> null,
|
||||||
|
'success_msg'=> null,
|
||||||
|
'recent_qr' => $this->recQr,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title("Invalid QR Found") // {$operatorName}
|
||||||
|
->body("Please, scan the valid QR code.")
|
||||||
|
->danger()
|
||||||
|
// ->persistent()
|
||||||
|
->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// // Perform any additional processing or database operations
|
||||||
|
// $this->saveFormData($formValues);
|
||||||
|
|
||||||
|
$parts = explode('|', $this->qrData);
|
||||||
|
$itemCode = trim($parts[0]);
|
||||||
|
$this->sNoId = isset($parts[1]) ? trim($parts[1]) : null;
|
||||||
|
|
||||||
|
ProductionQuantity::create([
|
||||||
|
'plant_id'=> $this->pId,
|
||||||
|
'shift_id'=> $this->sId,
|
||||||
|
'line_id' => $this->lId,
|
||||||
|
'item_id'=> $this->iId,
|
||||||
|
'serial_number' => $this->sNoId,
|
||||||
|
// 'operator_id'=> $operatorName,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// after success insertion
|
||||||
|
$this->form->fill([
|
||||||
|
'plant_id'=> $this->pId,
|
||||||
|
'block_name'=> $this->bId,
|
||||||
|
'shift_id'=> $this->sId,
|
||||||
|
'line_id'=> $this->lId,
|
||||||
|
'item_id'=> null,
|
||||||
|
'serial_number'=> null,
|
||||||
|
'success_msg'=> null,
|
||||||
|
'recent_qr' => $itemCode.' | '.$this->sNoId,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title("Valid QR Found") // {$operatorName}
|
||||||
|
->body("Valid QR code scanned: {$this->qrData}.")
|
||||||
|
->success()
|
||||||
|
// ->persistent()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function saveFormData($formValues)
|
||||||
|
{
|
||||||
|
// Save the form data to the database or perform other operations
|
||||||
|
// For example:
|
||||||
|
$model = ProductionQuantity::create($formValues);
|
||||||
|
|
||||||
|
// // Optionally, you can emit an event or perform a redirect after saving
|
||||||
|
// $this->emit('formSaved', $model->id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user