From 2a17b020b4e3f9fb0d127e77a01198d1fe59c1b4 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 25 Apr 2025 13:10:35 +0530 Subject: [PATCH] Added QR validation --- .../Pages/CreateProductionQuantity.php | 760 +++++++++++++++++- 1 file changed, 753 insertions(+), 7 deletions(-) diff --git a/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php b/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php index 52d0046..17a379c 100644 --- a/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php +++ b/app/Filament/Resources/ProductionQuantityResource/Pages/CreateProductionQuantity.php @@ -6,6 +6,8 @@ use App\Filament\Resources\ProductionQuantityResource; use App\Filament\Widgets\ItemOverview; use App\Models\ProductionQuantity; use App\Models\Shift; +use App\Models\Item; +use Carbon\Carbon; use Filament\Actions; use Filament\Facades\Filament; use Filament\Notifications\Notification; @@ -111,15 +113,20 @@ class CreateProductionQuantity extends CreateRecord return []; } - public function processAllValues($formData) + public function processAllValues($formQRData) { //$formValues = []; - $formValues = request()->all(); // This will get all form data from the request - //dd($formValues); - $this->validateAndProcessForm($formValues); // Process the form values + // This will get all form data from the request + $this->qrData = null; + $this->iId = null; + $this->succId = null; + $this->sNoId = null; + $this->succStat = null; + $formValues = request()->all(); + $this->validateAndProcessForm($formValues, $formQRData); // Process the form values } - public function validateAndProcessForm($formValues) + public function validateAndProcessForm($formValues, $formQRData) { $user = Filament::auth()->user(); //->name $operatorName = $user->name; @@ -131,7 +138,7 @@ class CreateProductionQuantity extends CreateRecord $formData = $componentData['data']['data'][0]; // Access first item in data array // Extract specific values - $this->qrData = $formData['item_code'] ?? null; + $this->qrData = $formQRData ?? $formData['item_code']; $this->pId = $formData['plant_id'] ?? null; $this->bId = $formData['block_name'] ?? null; $this->sId = $formData['shift_id'] ?? null; @@ -153,6 +160,745 @@ class CreateProductionQuantity extends CreateRecord return; } + // dd($formData, $formQRData); + // dd($this->form->getState()['serial_number']); + + if (empty($formQRData)) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Invalid QR') + ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") + ->danger() + ->send(); + return; + } + else + { + if (!$this->pId) { + $this->form->fill([ + 'plant_id'=> null, + 'block_name'=> null, + 'shift_id'=> null, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Plant') + ->body("Please select a plant first.") + ->danger() + ->send(); + return; + } + else if (!$this->bId) { + $this->form->fill([ + 'plant_id'=> $this->pId, + 'block_name'=> null, + 'shift_id'=> null, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Block') + ->body("Please select a block first.") + ->danger() + ->send(); + return; + } + else if (!$this->sId) { + $this->form->fill([ + 'plant_id'=> $this->pId, + 'block_name'=> $this->bId, + 'shift_id'=> null, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Shift') + ->body("Please select a shift first.") + ->danger() + ->send(); + return; + } + else if (!$this->lId) { + $this->form->fill([ + 'plant_id'=> $this->pId, + 'block_name'=> $this->bId, + 'shift_id'=> $this->sId, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Line') + ->body("Please select a line first.") + ->danger() + ->send(); + return; + } + else if (!$this->prodOrder) { + $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, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Please scan the production order first.') + ->danger() + ->send(); + return; + } + + if(!is_numeric($this->prodOrder)) + { + $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, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Production Order') + ->body("Must contain numeric values only.") + ->danger() + ->send(); + return; + } + else if (!preg_match('/^[1-9][0-9]{6,}$/', $this->prodOrder)) + { + $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, + 'production_order'=> null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Production Order') + ->body("Must contain at least 7 digits.
Must start with a non-zero digit.") + ->danger() + ->send(); + return; + } + + // ******************************** + + $exists = \App\Models\ProductionPlan::where('plant_id', $this->pId) + ->where('shift_id', $this->sId) + ->where('line_id', $this->lId) + ->whereDate('created_at', today()) + ->latest() + ->exists(); + + if ($exists) + { + $currentDate = date('Y-m-d'); + + $shiftId = Shift::where('id', $this->sId) + ->first(); + + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + //$miNs = (int) $miNs;-*/ + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $currentDate . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if (!($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Invalid Shift') + ->body("Please select a valid shift.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + else + { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + } + } + else + { + $existShifts = \App\Models\ProductionPlan::where('plant_id', $this->pId) + ->where('shift_id', $this->sId) + ->where('line_id', $this->lId) + ->whereDate('created_at', Carbon::yesterday()) + ->latest() + ->exists(); + + if ($existShifts) //if ($existShifts->count() > 0) + { // record exist on yesterday + //$currentDate = date('Y-m-d'); + $yesterday = date('Y-m-d', strtotime('-1 days')); + + $shiftId = Shift::where('id', $this->sId) + ->first(); + + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + // $miNs = (int) $miNs;-*/ + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $yesterday . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + } + else + { + $currentDate = date('Y-m-d'); + + $shiftId = Shift::where('id', $this->sId) + ->first(); + + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + // $miNs = (int) $miNs;-*/ + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $currentDate . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Plan Not Found') + ->body("Please set production plan first.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + else + { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Invalid Shift') + ->body("Please select a valid shift.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + } + } + else + { // record not exist on yesterday + + //$currentDate = date('Y-m-d'); + $yesterday = date('Y-m-d', strtotime('-1 days')); + + $shiftId = Shift::where('id', $this->sId) + ->first(); + + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + // $miNs = (int) $miNs;-*/ + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $yesterday . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Plan Not Found') + ->body("Please set production plan first.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + else + { + $currentDate = date('Y-m-d'); + + $shiftId = Shift::where('id', $this->sId) + ->first(); + + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + // $miNs = (int) $miNs;-*/ + + $totalMinutes = $hRs * 60 + $miNs; + + $from_dt = $currentDate . ' ' . $shiftId->start_time; + + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + + $currentDateTime = date('Y-m-d H:i:s'); + + // Check if current date time is within the range + if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Plan Not Found') + ->body("Please set production plan first.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + else + { + //echo "Choosed a valid shift..."; + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Invalid Shift') + ->body("Please select a valid shift.") + ->danger() + ->send(); + //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + } + } + } + + // ******************************** + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + } + + if (!preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $formQRData)) { + if (strpos($formQRData, '|') === false) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid QR') + ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") + ->danger() + ->send(); + return; + } + else + { + $splits = explode('|', $formQRData); + $iCode = trim($splits[0]); + $sNumber = isset($splits[1]) ? trim($splits[1]) : null; + + if (!ctype_alnum($iCode)) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Item Code') + ->body("Item code must contain alpha-numeric values only.") + ->danger() + ->send(); + return; + } + else if (strlen($iCode) < 6) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Item Code') + ->body("Item code must be at least 6 digits.") + ->danger() + ->send(); + return; + } + else if (!ctype_alnum($sNumber)) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Serial Number') + ->body("Serial Number must contain alpha-numeric values only.") + ->danger() + ->duration(800) + ->send(); + return; + } + else if (strlen($sNumber) < 9) { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid Serial Number') + ->body("Serial Number must be at least 9 digits.") + ->danger() + ->duration(800) + ->send(); + return; + } + } + + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Invalid QR') + ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") + ->danger() + ->send(); + return; + } + else + { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + } + + // Only search when all parent IDs are selected + $parts = explode('|', $formQRData); + $itemCode = trim($parts[0]); + $serialNumber = isset($parts[1]) ? trim($parts[1]) : null; + + // Fetch item using item code and plant_id + $masItem = Item::where('code', $itemCode)->first(); + if (!$masItem) + { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Unknown Item Code') + ->body("Item code does not exist in master data.") + ->danger() + ->send(); + return; + } + + $item = Item::where('code', $itemCode) + ->where('plant_id', $this->pId) + ->first(); + + if ($item) + { + $sNo = ProductionQuantity::where('serial_number', $serialNumber)->where('plant_id', $this->pId)->exists(); + if (!$sNo) + { + $this->form->fill([ + 'plant_id'=> $this->pId, + 'block_name'=> $this->bId, + 'shift_id'=> $this->sId, + 'line_id'=> $this->lId, + 'item_id'=> $item->id, + 'serial_number'=> null, + 'success_msg'=> 'Y', + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + $this->succId = 'Y'; + $this->iId = $item->id; + } + else + { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Duplicate Serial Number') + ->body("Serial number already exist in database for choosed plant.") + ->danger() + ->send(); + return; + } + } + else + { + $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, + 'production_order'=> $this->prodOrder, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + + Notification::make() + ->title('Unknown Item Code') + ->body("Item code does not exist in master data for choosed plant.") + ->danger() + ->send(); + return; + } + if ($this->succId === null) { $this->form->fill([ 'plant_id'=> $this->pId, @@ -162,7 +908,7 @@ class CreateProductionQuantity extends CreateRecord 'item_id'=> null, 'serial_number'=> null, 'success_msg'=> null, - 'production_order'=> null, + 'production_order'=> $this->prodOrder, 'operator_id'=> $operatorName, 'recent_qr' => $this->recQr, ]);