diff --git a/app/Filament/Resources/ProductionQuantityResource.php b/app/Filament/Resources/ProductionQuantityResource.php index 284ac724c..7a4ef8724 100644 --- a/app/Filament/Resources/ProductionQuantityResource.php +++ b/app/Filament/Resources/ProductionQuantityResource.php @@ -22,6 +22,7 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Forms\Components\Section; use Filament\Notifications\Notification; use Filament\Tables\Actions\ExportAction; +//use Livewire\Livewire; class ProductionQuantityResource extends Resource { @@ -33,6 +34,7 @@ class ProductionQuantityResource extends Resource protected static ?string $navigationGroup = 'Production'; + public static function form(Form $form): Form { return $form @@ -44,6 +46,7 @@ class ProductionQuantityResource extends Resource ->required() // ->nullable() ->reactive() + // ->default(fn () => optional(ProductionQuantity::latest()->first())->plant_id) ->default(function () { return optional(ProductionQuantity::latest()->first())->plant_id; }) @@ -247,10 +250,11 @@ class ProductionQuantityResource extends Resource // ->required() ->reactive() ->autofocus(true) - ->debounce(1000) + ->debounce(100) // ->submitOnEnter() ->afterStateUpdated(function ($state, callable $get, callable $set) { - // **Check if input is empty before processing** + $set('success_msg', null); + if (empty($state)) { $set('item_id', 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 (strpos($state, '|') === false) { - $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); + $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); + $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); Notification::make() ->title('Invalid QR') @@ -586,10 +591,12 @@ class ProductionQuantityResource extends Resource $sNumber = isset($splits[1]) ? trim($splits[1]) : null; if (!ctype_alnum($iCode)) { - $set('validationError', 'Item code must contain alpha-numeric values.'); + $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); + $set('validationError', 'Item code must contain alpha-numeric values.'); + Notification::make() ->title('Invalid Item Code') ->body("Item code must contain alpha-numeric values only.") @@ -598,10 +605,12 @@ class ProductionQuantityResource extends Resource return; } 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_description', null); $set('serial_number', null); + $set('validationError', 'Item code must be at least 6 digits.'); + Notification::make() ->title('Invalid Item Code') ->body("Item code must be at least 6 digits.") @@ -610,10 +619,12 @@ class ProductionQuantityResource extends Resource return; } else if (!ctype_alnum($sNumber)) { - $set('validationError', 'Serial Number must contain alpha-numeric values.'); + $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); + $set('validationError', 'Serial Number must contain alpha-numeric values.'); + Notification::make() ->title('Invalid Serial Number') ->body("Serial Number must contain alpha-numeric values only.") @@ -622,10 +633,12 @@ class ProductionQuantityResource extends Resource return; } 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_description', null); $set('serial_number', null); + $set('validationError', 'Serial Number must be at least 9 digits.'); + Notification::make() ->title('Invalid Serial Number') ->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_description', null); $set('serial_number', null); + $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); Notification::make() ->title('Invalid QR') @@ -672,14 +685,33 @@ class ProductionQuantityResource extends Resource ->exists(); if (!$sNo) { - $set('serial_number', $serialNumber); - $set('item_id', $item->id); - $set('item_code', $itemCode); - // $set('item_description', $item->description); + $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_code', $itemCode); + // } + // 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 { $set('validationError', 'Serial number already exist in database.'); + ////$set('item_code', null); //246118|651616516155667 $set('item_id', null); // $set('item_description', null); $set('serial_number', null); @@ -693,10 +725,11 @@ class ProductionQuantityResource extends Resource } else { - $set('validationError', 'Item code does not exist in master data.'); + $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); + $set('validationError', 'Item code does not exist in master data.'); Notification::make() ->title('Unknown Item Code') @@ -710,20 +743,28 @@ class ProductionQuantityResource extends Resource 'class' => $get('validationError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('validationError') ? $get('validationError') : null) - ->hintColor('danger'), - // ->extraAttributes([ - // 'x-data' => '{ value: "" }', - // 'x-model' => 'value', - // 'x-on:keydown.enter.prevent' => '$wire.processQr(value)', - // ]), + ->hintColor('danger') + ->extraAttributes([ + 'x-data' => '{}', + // 'x-data' => '{ value: "" }', + // 'x-model' => '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') ->required(), - Forms\Components\TextInput::make('serial_number') - ->required() - ->unique(ignoreRecord: true) - ->readOnly(true) - ->autocapitalize('characters'), - //->columnSpanFull(), + Forms\Components\Hidden::make('success_msg') + ->required(), + Forms\Components\Hidden::make('serial_number') + ->required(), + //->unique(ignoreRecord: true), + // ->autocapitalize('characters'), + // ->columnSpanFull(), Forms\Components\TextInput::make('recent_qr') //item_description ->label('Last scanned QR') ->reactive() diff --git a/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php b/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php index 2b83185f5..874a8dfac 100644 --- a/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php +++ b/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php @@ -3,9 +3,13 @@ namespace App\Filament\Resources\ProductionQuantityResource\Pages; use App\Filament\Resources\ProductionQuantityResource; +use App\Models\ProductionQuantity; +use App\Models\Shift; use Filament\Actions; +use Filament\Facades\Filament; use Filament\Notifications\Notification; use Filament\Resources\Pages\CreateRecord; +use Livewire\Livewire; class CreateProductionQuantity extends CreateRecord { @@ -16,29 +20,118 @@ class CreateProductionQuantity extends CreateRecord 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) -// { -// $this->qr_data = $itemCode; + // protected static bool $canCreateAnother = false; -// // Check if the file is uploaded -// if (!$this->qr_data) { -// Notification::make() -// ->title('No QR Found') -// ->body("Please, scan the QR code.") -// ->danger() -// // ->persistent() -// ->send(); -// return; -// } -// else { -// Notification::make() -// ->title('QR Found') -// ->body("Valid QR code scanned: {$this->qr_data}.") -// ->success() -// // ->persistent() -// ->send(); -// } -// } + public function getFormActions(): array + { + // return parent::getFormActions(); //return []; + return [ + $this->getCancelFormAction(), + ]; + } + + public function processAllValues($formData) + { + //$formValues = []; + $formValues = request()->all(); // This will get all form data from the request + //dd($formValues); + $this->validateAndProcessForm($formValues); // Process the form values + } + + 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); + } }