From dc0eed1bac4faeb1349965891c76524f303ec1a9 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 4 Jun 2026 12:33:30 +0530 Subject: [PATCH] Changed logic of invoice number in serial validation --- .../Pages/CreateSerialValidation.php | 3723 +++++++++++++---- 1 file changed, 2967 insertions(+), 756 deletions(-) diff --git a/app/Filament/Resources/SerialValidationResource/Pages/CreateSerialValidation.php b/app/Filament/Resources/SerialValidationResource/Pages/CreateSerialValidation.php index 8a2f3eb..f4e8661 100644 --- a/app/Filament/Resources/SerialValidationResource/Pages/CreateSerialValidation.php +++ b/app/Filament/Resources/SerialValidationResource/Pages/CreateSerialValidation.php @@ -16,7 +16,6 @@ use Maatwebsite\Excel\Facades\Excel; use Livewire\Livewire; use Str; use Livewire\Attributes\On; -use App\Services\SmbService; use Illuminate\Support\Facades\Log; class CreateSerialValidation extends CreateRecord @@ -65,11 +64,2416 @@ class CreateSerialValidation extends CreateRecord } + // public function processInvoice($invoiceNumber) + // { + // $invoiceNumber = trim($invoiceNumber); + + // $fileName = $invoiceNumber . '.txt'; + + // $this->showCapacitorInput = false; + + // $user = Filament::auth()->user(); + + // $operatorName = $user->name; + + // $plantId = $this->form->getState()['plant_id']; + + // $this->plantId = $plantId; + + // $plant = Plant::find($plantId); + + // if ($plant) + // { + // $plantCode = $plant->code; + // } + // else + // { + // $plantCode = null; + // } + + // //..GET SERIAL INVOICE API + + // // $content = SmbService::readTextFile($fileName); + + // // if ($content == '') { + // // Notification::make() + // // ->title('File Not Found') + // // ->body("Unable to locate file: {$fileName}") + // // ->danger() + // // ->send(); + + // // return; + // // } + + // // $lines = preg_split("/\r\n|\n|\r/", trim($content)); + + // // $insertData = []; + // // $missingItemCodes = []; + // // $InvalidLenSno = []; + // // $InvalidSno = []; + // // $InvalidLenItem = []; + // // $InvalidItem = []; + + // // foreach ($lines as $line) + // // { + // // $line = trim($line); + // // if ($line == '') { + // // continue; + // // } + + // // $parts = array_map('trim', explode(',', $line)); + + // // if (count($parts) != 2) { + // // Notification::make() + // // ->title("Invalid data found inside the file.") + // // ->danger() + // // ->seconds(1) + // // ->send(); + // // return; + // // } + + // // [$itemCode, $serialNumber] = $parts; + + // // $sticker = StickerMaster::where('plant_id', $plantId) + // // ->whereHas('item', function ($query) use ($itemCode, $plantId) { + // // $query->where('plant_id', $plantId) + // // ->where('code', $itemCode); + // // }) + // // ->first(); + + // // if (Str::length($itemCode) < 6) + // // { + // // $InvalidLenItem [] = $itemCode; + // // continue; + // // } + // // else if(!is_numeric($itemCode)){ + // // $InvalidItem [] = $itemCode; + // // continue; + // // } + // // if (Str::length($serialNumber) < 9) + // // { + // // $InvalidLenSno [] = $serialNumber; + // // continue; + // // } + // // else if(!ctype_alnum($serialNumber)){ + // // $InvalidSno [] = $serialNumber; + // // continue; + // // } + + // // if (!$sticker) { + // // $missingItemCodes[] = $itemCode; + // // continue; + // // } + + // // $insertData[] = [ + // // 'plant_id' => $plantId, + // // 'sticker_master_id' => $sticker->id, + // // 'invoice_number' => $invoiceNumber, + // // 'serial_number' => $serialNumber, + // // 'created_at' => now(), + // // 'operator_id' => $operatorName, + // // 'updated_at' => now(), + // // ]; + // // } + + // if (!empty($InvalidLenItem)) + // { + // $count = count($InvalidLenItem); + + // if ($count <= 10) { + // $body = 'Item Code should contain minimum 6 digits: ' . implode(', ', $InvalidLenItem); + // } else { + // $body = "{$count} item codes contain minimum 6 digits."; + // } + // Notification::make() + // ->title("Invalid Item Code.") + // ->body("$body") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + // else if (!empty($InvalidItem)) + // { + // $count = count($InvalidItem); + + // if ($count <= 10) { + // $body = 'Item code must be in numeric values: ' . implode(', ', $InvalidSno); + // } else { + // $body = "{$count} item codes must be in numeric values."; + // } + // Notification::make() + // ->title("Invalid Item Code.") + // ->body("$body") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + // else if (!empty($InvalidLenSno)) + // { + // $count = count($InvalidLenSno); + + // if ($count <= 10) { + // $body = 'Serial number should be minimum 9 digits: ' . implode(', ', $InvalidLenSno); + // } else { + // $body = "{$count} serial number should be minimum 9 digits."; + // } + // Notification::make() + // ->title("Invalid Serial Number.") + // ->body("$body") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + // else if (!empty($InvalidSno)) + // { + // $count = count($InvalidSno); + + // if ($count <= 10) { + // $body = 'Serial number should be conatin alpha numeric values: ' . implode(', ', $InvalidSno); + // } else { + // $body = "{$count} serial number should be conatin alpha numeric values."; + // } + // Notification::make() + // ->title("Invalid Serial Number.") + // ->body("$body") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + // else if (!empty($missingItemCodes)) + // { + // $count = count($missingItemCodes); + + // if ($count <= 10) { + // $body = 'Item codes not found in sticker master: ' . implode(', ', $missingItemCodes); + // } else { + // $body = "{$count} item codes not found in sticker master table."; + // } + // Notification::make() + // ->title("Unknown Item Code.") + // ->body("$body") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + + // if (!empty($insertData)) { + // SerialValidation::insert($insertData); + // } + // else{ + // Notification::make() + // ->title("Insert Failed.") + // ->body("Data insertion failed") + // ->danger() + // ->seconds(1) + // ->send(); + // return; + // } + + // //.. + + // $updateStatus = $this->form->getState()['update_invoice'] ?? null; + + // $this->invoiceNumber = trim($this->form->getState()['invoice_number']) ?? $invoiceNumber; + + // $invoiceType = null; + // //$this->invoiceNumber = $this->invoiceNumber ?? $invoiceNumber; + + // $invoiceExist = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->exists(); + + // if (!$invoiceExist) { + // Notification::make() + // ->title("New invoice detected.") + // ->info() + // ->seconds(1) + // ->send(); + // $this->dispatch('playNotificationSound'); + + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => 0, + // 'scanned_quantity'=> 0, + // ]); + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // } + // else + // { + // $totQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scanSQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + // $totMQuan = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); //->where('quantity', '!=', '') + // $scanMQuan = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + + // if ($totMQuan > 0) + // { + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totQuan, + // 'scanned_quantity'=> $scanMQuan, + // ]); + + // if ($totQuan == $scanMQuan) + // { + // Notification::make() + // ->title("Completed: Material Invoice") + // ->body("Material invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Material Invoice' to proceed!") + // ->warning() + // ->seconds(3) + // ->send(); + // $this->dispatch('playNotificationSound'); + + // $filename = $invoiceNumber . '.xlsx'; + // $directory = 'uploads/temp'; + // $disk = Storage::disk('local'); + // $filePath = $directory . '/' . $filename; + // //$fullPath = null; + // if ($disk->exists($filePath)) { + // //$fullPath = $disk->path($filePath); + // $disk->delete($filePath); + // } + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); + // } + // else + // { + // Notification::make() + // ->title("Start the scanning process!") + // ->info() + // ->seconds(1) + // ->send(); + + // $this->dispatch('playNotificationSound'); + // // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + + // if ($updateStatus == '1') + // { + // //'Material invoice update in progress...'; + // $filename = $invoiceNumber . '.xlsx'; + // $directory = 'uploads/temp'; + // $disk = Storage::disk('local'); + // $filePath = $directory . '/' . $filename; + // $fullPath = $disk->path($filePath); + + // if ($fullPath && file_exists($fullPath)) + // { + // // Now you can read/process the file here + // $rows = Excel::toArray(null, $fullPath)[0]; + + // if ((count($rows) - 1) <= 0) + // { + // Notification::make() + // ->title('Records Not Found') + // ->body("Import the valid updated 'Material Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $invalidMatCodes = []; + // $materialCodes = []; + // $missingQuantities = []; + // $invalidMatQuan = []; + // $hasQuanTypIds = []; + // $validRowsFound = false; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $materialCode = trim($row[0]); + // $materialQuantity = trim($row[1]); + + // if (empty($materialCode) && empty($materialQuantity)) { + // continue; + // } + + // if (!empty($materialCode)) { + // if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) + // { + // $invalidMatCodes[] = $materialCode; + // } + // else + // { + // if (empty($materialQuantity)) { + // $missingQuantities[] = $materialCode; + // } + // else if (!is_numeric($materialQuantity)) + // { + // $invalidMatQuan[] = $materialCode; + // } + // else if ($materialQuantity == 0) + // { + // $invalidMatQuan[] = $materialCode; + // } + // else + // { + // $materialCodes[] = $materialCode; + // $validRowsFound = true; + // } + // } + // } + // else + // { + // continue; + // } + // } + + // $uniqueInvalidCodes = array_unique($invalidMatCodes); + + // if (!empty($uniqueInvalidCodes)) { + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueMissingQuanCodes = array_unique($missingQuantities); + + // if (!empty($uniqueMissingQuanCodes)) { + // Notification::make() + // ->title('Missing Material Quantity') + // ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueMissingQuanCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueInvalidMatQuan = array_unique($invalidMatQuan); + + // if (!empty($uniqueInvalidMatQuan)) { + // Notification::make() + // ->title('Invalid Material Quantity') + // ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueInvalidMatQuan)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // if (!$validRowsFound) { + // Notification::make() + // ->title('Invalid Material Invoice') + // ->danger() // This makes the notification red to indicate an error + // ->body('Uploaded Excel sheet is empty or
contains no valid data.') + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $uniqueCodes = array_unique($materialCodes); + + // //itemNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes) { + // $query->whereIn('code', $uniqueCodes); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // //plantNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + // $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $invalidCodes = $matchedItems + // ->filter(fn ($sticker) => empty($sticker->material_type)) //filter invalid + // ->pluck('item.code') + // ->toArray(); + + + // if (!empty($invalidCodes)) + // { + // $missingCount = count($invalidCodes); + + // $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
" . implode(', ', $invalidCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $nonNumericQtyCodes = []; + // $zeroQtyCodes = []; + // $notDivisibleCodes = []; + // $hasQuanTyp = false; + + // foreach ($matchedItems as $sticker) + // { + // $code = $sticker->item->code; + // $materialType = $sticker->material_type; + + // if ($materialType == 2) + // { + // $bundleQty = $sticker->bundle_quantity ?? 0; + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // if ($totalExcelQty == 0) { + // $zeroQtyCodes[] = $code; + // } else if (!is_numeric($totalExcelQty)) { + // $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too + // } else if ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { + // $notDivisibleCodes[] = $code; + // } + // } + // else if ($materialType == 3) + // { + // $hasQuanTyp = true; + // $hasQuanTypIds[] = $sticker->id; + // } + // } + + // $showValidationNotification = function(array $codes, string $message) { + // if (count($codes) == 0) return; + + // $uniqueCodes = array_unique($codes); + // $codeList = implode(', ', $uniqueCodes); + + // Notification::make() + // ->title('Invalid Bundle Quantity') + // ->body("$message
$codeList") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + // }; + + // $showValidationNotification($nonNumericQtyCodes, "The following item codes contains invalid bundle quantity:"); + // $showValidationNotification($zeroQtyCodes, "The following item codes quantity should be greater than '0':"); + // $showValidationNotification($notDivisibleCodes, "The following item codes quantity is not divisible by bundle quantity."); + + + // if ($nonNumericQtyCodes || $zeroQtyCodes || $notDivisibleCodes) { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + // else + // { + // $oldQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $newQuan = -1; + + // $inserted = 0; + // foreach ($matchedItems as $sticker) + // { + // if ($newQuan == -1 && !$hasQuanTyp) + // { + // SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + // $query->whereNull('serial_number')->orWhere('serial_number', ''); + // }) + // ->forceDelete(); //->delete(); + + // $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // } + // else if ($newQuan == -1 && $hasQuanTyp) + // { + // SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + // $query->whereNull('serial_number')->orWhere('serial_number', ''); + // })->whereNotIn('sticker_master_id', $hasQuanTypIds) + // ->forceDelete(); //->delete(); + + // $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // } + + // $code = $sticker->item->code; + // $materialType = $sticker->material_type; + // // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($code) { $query->where('plant_id', $this->plantId)->where('code', $code); })->first(); + + // if ($materialType == 1) + // { + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // $existQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where('sticker_master_id', $sticker->id)->count(); + + // if ($existQty < $totalExcelQty) + // { + // $newQty = $totalExcelQty - $existQty; + // for ($i = 0; $i < $newQty; $i++) + // { + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => 1, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // } + // } + // else if ($materialType == 2) + // { + // $bundleQty = $sticker->bundle_quantity; + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // $existQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where('sticker_master_id', $sticker->id)->count(); + + // //for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) + // $newTotQty = $totalExcelQty/$bundleQty; + // if ($existQty < $newTotQty) + // { + // $newQty = $newTotQty - $existQty; + // for ($i = 0; $i < $newQty; $i++) + // { + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => $bundleQty, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // } + // } + // else if ($materialType == 3) + // { + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; + // } + // } + + // $existEmpRecQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + // $query->whereNull('serial_number')->orWhere('serial_number', ''); + // })->where('sticker_master_id', $sticker->id)->first(); + // $existEmpQty = $existEmpRecQty ? $existEmpRecQty->quantity : 0; + + // $existComQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + // $query->whereNotNull('serial_number')->where('serial_number', '!=', ''); + // }) + // ->where('sticker_master_id', $sticker->id) + // ->sum(column: 'quantity'); + // $existComQty = $existComQty ? $existComQty : 0; + + // // 8 = 3 + 5 // 8 = 5 + 3 // 8 = 0 + 8 // 8 = 8 + 0 + // // 8 = 3 + 5 // 8 = 5 + 3 // 8 = 0 + 8 // 8 = 8 + 0 + // // 0 = 0 + 0 + // $existQty = $existEmpQty + $existComQty; + + // // 8 <= 11 // 8 <= 8 // 8 <= 11 // 8 <= 9 + // // 8 <= 7 // 8 <= 7 // 8 <= 7 // 8 <= 7 + // // 0 <= 5 + + // if ($existQty <= $totalExcelQty) + // { + // // 6 = 11 - 5 // 5 = 8 - 3 // 3 = 11 - 8 // 9 = 9 - 0 + // // 5 = 5 - 0 + // $newInsQty = $totalExcelQty - $existComQty; + + // // 3 > 0 // 5 > 0 // 0 > 0 // 8 > 0 + // // 0 > 0 + // if ($existEmpQty > 0) // update + // { + // // 3 = 6 // 5 = 5 // 0 = 3 // 8 = 9 + // // 0 = 5 + // if ($existEmpQty == $newInsQty) { continue; } + // // $existEmpRecQty->delete(); + // $existEmpRecQty->update([ + // 'quantity' => $newInsQty, + // 'operator_id'=> $operatorName, + // 'updated_at' => now(), + // ]); + // $newQuan--; + // $inserted++; + // } + // else if ($newInsQty > 0) // if ($sticker) // create + // { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => $newInsQty, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // // 8 > 7 // 8 > 7 // 8 > 7 // 8 > 7 + // else + // { + // // 2 = 7 - 5 // 4 = 7 - 3 // -1 = 7 - 8 // 7 = 7 - 0 + // $newInsQty = $totalExcelQty - $existComQty; + + // // 3 > 0 // 5 > 0 // 0 > 0 // 8 > 0 + // if ($existEmpQty > 0) // update + // { + // // 3 = 2 // 5 = 4 // 0 = -1 // 8 = 7 + // if ($existEmpQty == $newInsQty) { continue; } + // // $existEmpRecQty->delete(); + // $existEmpRecQty->update([ + // 'quantity' => $newInsQty, + // 'operator_id'=> $operatorName, + // 'updated_at' => now(), + // ]); + // $newQuan--; + // $inserted++; + // } + // else if ($newInsQty > 0) // create + // { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => $newInsQty, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + + // } + // } + + // if ($inserted > 0 || $oldQuan != $newQuan) + // { + // Notification::make() + // ->title("Material invoice successfully updatad.") + // ->success() + // ->seconds(1) + // ->send(); + + // Notification::make() + // ->title("Start the scanning process!") + // ->body("'$inserted' new material invoice records were inserted.") + // ->info() + // // ->success() + // ->seconds(1) + // ->send(); + + // $this->dispatch('playNotificationSound'); + + // // Update total quantity in the form + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // if ($totalQuantity == $scannedQuantity) + // { + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); + // } + // else + // { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // } + // } + // else + // { + // Notification::make() + // ->title("Update Failed: Material Invoice") + // ->body("No new records were inserted for Material Invoice : '$invoiceNumber'.") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // } + // else + // { + // Notification::make() + // ->title('Updated Invoice Not Found') + // ->body("Import the updated 'Material Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => 0, + // 'scanned_quantity'=> 0, + // ]); + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // } + // return; + // } + // else + // { + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totQuan, + // 'scanned_quantity'=> $scanSQuan, + // ]); + // //$hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + + // if ($totQuan == $scanSQuan) + // { + // Notification::make() + // ->title("Completed: Serial Invoice") + // ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed.!") + // ->warning() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playNotificationSound'); + + // $filename = $invoiceNumber . '.xlsx'; + // $directory = 'uploads/temp'; + // $disk = Storage::disk('local'); + // $filePath = $directory . '/' . $filename; + // //$fullPath = null; + // if ($disk->exists($filePath)) { + // //$fullPath = $disk->path($filePath); + // $disk->delete($filePath); + // } + // $this->dispatch('playNotificationSound'); + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); + // } + // else + // { + // Notification::make() + // ->title("Start the scanning process!") + // ->info() + // ->seconds(1) + // ->send(); + + // $this->dispatch('playNotificationSound'); + // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + + // if ($updateStatus == '1') + // { + // $filename = $invoiceNumber . '.xlsx'; + // $directory = 'uploads/temp'; + // $disk = Storage::disk('local'); + // $filePath = $directory . '/' . $filename; + // $fullPath = $disk->path($filePath); + + // // Check if file exists //$disk->exists($filePath) + // if ($fullPath && file_exists($fullPath)) + // { + // // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx + // // 'Serial invoice update in progress...' + + // // Now you can read/process the file here + // $rows = Excel::toArray(null, $fullPath)[0]; + + // if ((count($rows) - 1) <= 0) + // { + // Notification::make() + // ->title('Records Not Found') + // ->body("Import the valid updated 'Serial Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $invalidMatCodes = []; + // $materialCodes = []; + // $missingSerials = []; + // $invalidSerCodes = []; + // $duplicateSerials = []; + // $serialNumbers = []; + // $validRowsFound = false; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $materialCode = trim($row[0]); + // $serialNumber = trim($row[1]); + + // if (empty($materialCode) && empty($serialNumber)) + // { + // continue; + // } + + // if (!empty($materialCode)) + // { + // if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) + // { + // $invalidMatCodes[] = $materialCode; + // continue; + // } + // else + // { + // if (empty($serialNumber)) { + // $missingSerials[] = $materialCode; + // } + // else if (Str::length($serialNumber) < 9 || !ctype_alnum($serialNumber)) + // { + // $invalidSerCodes[] = $serialNumber; + // } + // else + // { + // if (in_array($serialNumber, $serialNumbers)) + // { + // $duplicateSerials[] = $serialNumber; + // } + // else + // { + // $serialNumbers[] = $serialNumber; + // $materialCodes[] = $materialCode; + // $validRowsFound = true; + // } + // } + // } + // } + // else + // { + // continue; + // } + // } + + // $uniqueInvalidCodes = array_unique($invalidMatCodes); + + // if (!empty($uniqueInvalidCodes)) { + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueMissingSerials = array_unique($missingSerials); + + // if (!empty($uniqueMissingSerials)) { + // Notification::make() + // ->title('Missing Serial Numbers') + // ->body("The following item codes doesn't have valid serial number:
" . implode(', ', $uniqueMissingSerials)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueInvalidSerCodes = array_unique($invalidSerCodes); + + // if (!empty($uniqueInvalidSerCodes)) { + // Notification::make() + // ->title('Invalid Serial Numbers') + // ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
' . implode(', ', $uniqueInvalidSerCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueDupSerCodes = array_unique($duplicateSerials); + + // if (!empty($uniqueDupSerCodes)) { + // Notification::make() + // ->title('Duplicate Serial Numbers') + // ->body('The following serial numbers are already exist in invoice excel:
' . implode(', ', $uniqueDupSerCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // if (!$validRowsFound) { + // Notification::make() + // ->title('Invalid Serial Invoice') + // ->danger() // This makes the notification red to indicate an error + // ->body('Uploaded excel sheet is empty or
contains no valid data.') + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $uniqueCodes = array_unique($materialCodes); + + // //itemNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes) { + // $query->whereIn('code', $uniqueCodes); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); //->where('plant_id', $plantId) + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // //plantNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + // $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $invalidCodes = $matchedItems + // ->filter(fn ($sticker) => !empty($sticker->material_type)) //filter invalid + // ->pluck('item.code') + // ->toArray(); + + // if (!empty($invalidCodes)) + // { + // $missingCount = count($invalidCodes); + + // $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
" . implode(', ', $invalidCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $missPackCodes = $matchedItems + // ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) //filter invalid + // ->pluck('item.code') + // ->toArray(); + + // if (!empty($missPackCodes)) + // { + // $missingCount = count($missPackCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
" . implode(', ', $missPackCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $oldQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $newQuan = 0; + + // $inserted = 0; + // foreach ($rows as $index => $row) + // { + // if ($index == 0) // Skip header; + // { + // SerialValidation::where('invoice_number', $invoiceNumber) + // ->where(function($query) { + // $query->whereNull('motor_scanned_status')->orWhere('motor_scanned_status', ''); + // }) + // ->where(function($query) { + // $query->whereNull('pump_scanned_status')->orWhere('pump_scanned_status', ''); + // }) + // ->where(function($query) { + // $query->whereNull('capacitor_scanned_status')->orWhere('capacitor_scanned_status', ''); + // }) + // ->where(function($query) { + // $query->whereNull('scanned_status_set')->orWhere('scanned_status_set', ''); + // }) + // ->where(function($query) { + // $query->whereNull('scanned_status')->orWhere('scanned_status', ''); + // }) + // ->forceDelete(); //->delete(); + + // $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // continue; + // } + + // $materialCode = trim($row[0]); + // $serialNumber = trim($row[1]); + + // if (empty($materialCode) || empty($serialNumber)) { + // continue; + // } + + // $sNoExist = SerialValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->exists(); //->pluck('serial_number') + + // if ($sNoExist) { continue; } + + // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { + // $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode + // })->first(); + + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => $serialNumber, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + + // if ($inserted > 0 || $oldQuan != $newQuan) + // { + // Notification::make() + // ->title("Serial invoice successfully updated.") + // ->success() + // ->seconds(1) + // ->send(); + + // Notification::make() + // ->title("Start the scanning process!") + // ->body("'$inserted' new serial invoice records were inserted.") + // ->info() + // // ->success() + // ->seconds(1) + // ->send(); + + // $this->dispatch('playNotificationSound'); + + // // Update total quantity in the form + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // if ($totalQuantity == $scannedQuantity) + // { + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); + // } + // else + // { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // } + // } + // else + // { + // Notification::make() + // ->title("Update Failed: Serial Invoice") + // ->body("No new records were inserted for Serial Invoice : '$invoiceNumber'.") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // else + // { + // Notification::make() + // ->title('Updated Invoice Not Found') + // ->body("Import the updated 'Serial Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => 0, + // 'scanned_quantity'=> 0, + // ]); + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // } + // return; + // } + // } + + // $filename = $invoiceNumber . '.xlsx'; + // $directory = 'uploads/temp'; + // $disk = Storage::disk('local'); + // $filePath = $directory . '/' . $filename; + // $fullPath = null; //$fullPath = $disk->path($filePath); + + // // Check if file exists + // if ($disk->exists($filePath)) { + // $fullPath = $disk->path($filePath); + // //$fullPath = session('uploaded_invoice_path'); + // // Notification::make() + // // ->title('File exists.') + // // ->success() + // // ->seconds(3) + // // ->send(); + // } else { + // Notification::make() + // ->title('Invoice Not Found') + // ->body("Import the scanned 'Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => 0, + // 'scanned_quantity'=> 0, + // ]); + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + + // if (!file_exists($fullPath)) + // { + // Notification::make() + // ->title('Invoice file doesn\'t exist.') + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + // return; + + // } + + // // Extract filename without extension (e.g., "3RA0018732") + // $uploadedFilename = pathinfo($fullPath, PATHINFO_FILENAME); + + // // Compare with invoice number + // if ($uploadedFilename != $invoiceNumber) { + // Notification::make() + // ->title("Uploaded file name does not match the invoice number.") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + // return; + // } + + // if ($fullPath && file_exists($fullPath)) + // { + // // Now you can read/process the file here + // $rows = Excel::toArray(null, $fullPath)[0]; + // // $collection = Excel::toCollection(null, $fullPath); + // // $rows = $collection[0]->toArray(); + + // // $collection = Excel::toCollection(new ExcelImport, $fullPath); + // // // Convert the collection to an array + // // $rows = $collection->toArray(); + + // // $excelImport = new ExcelImport(); + // // // Import the Excel file + // // Excel::import($excelImport, $fullPath); + // // // Get the rows + // // $rows = $excelImport->getRows(); + + // if ((count($rows) - 1) <= 0) + // { + // Notification::make() + // ->title('Records Not Found') + // ->body("Import the valid 'Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; + + // $materialCode = trim($row[0]); + + // if (!empty($materialCode)) { + // if (Str::length($materialCode) < 6) + // { + // continue; + // } + // else + // { + // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { + // $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode + // }); + // if ($sticker->exists()) + // { + // if ($sticker->first()->material_type && !empty($sticker->first()->material_type)) + // { + // $invoiceType = "M"; + // break; + // } + // else + // { + // $invoiceType = "S"; + // break; + // } + // } + // else + // { + // continue; + // } + // } + // } + // else + // { + // continue; + // } + // } + + // if ($invoiceType == 'M') + // { + // $invalidMatCodes = []; + // $materialCodes = []; + // $missingQuantities = []; + // $invalidMatQuan = []; + // $validRowsFound = false; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $materialCode = trim($row[0]); + // $materialQuantity = trim($row[1]); + + // if (empty($materialCode) && empty($materialQuantity)) { + // continue; + // } + + // if (!empty($materialCode)) { + // if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) + // { + // $invalidMatCodes[] = $materialCode; + // } + // else + // { + // if (empty($materialQuantity)) { + // $missingQuantities[] = $materialCode; + // } + // else if (!is_numeric($materialQuantity)) + // { + // $invalidMatQuan[] = $materialCode; + // } + // else if ($materialQuantity == 0) + // { + // $invalidMatQuan[] = $materialCode; + // } + // else + // { + // $materialCodes[] = $materialCode; + // $validRowsFound = true; + // } + // } + // } + // else + // { + // continue; + // } + // } + + // $uniqueInvalidCodes = array_unique($invalidMatCodes); + + // if (!empty($uniqueInvalidCodes)) { + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueMissingQuanCodes = array_unique($missingQuantities); + + // if (!empty($uniqueMissingQuanCodes)) { + // Notification::make() + // ->title('Missing Material Quantity') + // ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueMissingQuanCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueInvalidMatQuan = array_unique($invalidMatQuan); + + // if (!empty($uniqueInvalidMatQuan)) { + // Notification::make() + // ->title('Invalid Material Quantity') + // ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueInvalidMatQuan)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // if (!$validRowsFound) { + // Notification::make() + // ->title('Invalid Material Invoice') + // ->danger() // This makes the notification red to indicate an error + // ->body('Uploaded Excel sheet is empty or
contains no valid data.') + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $uniqueCodes = array_unique($materialCodes); + + // //itemNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes) { + // $query->whereIn('code', $uniqueCodes); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // //plantNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + // $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $invalidCodes = $matchedItems + // ->filter(fn ($sticker) => empty($sticker->material_type)) //filter invalid + // ->pluck('item.code') + // ->toArray(); + + // if (!empty($invalidCodes)) + // { + // $missingCount = count($invalidCodes); + + // $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
" . implode(', ', $invalidCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $nonNumericQtyCodes = []; + // $zeroQtyCodes = []; + // $notDivisibleCodes = []; + + // foreach ($matchedItems as $sticker) + // { + // $code = $sticker->item->code; + // $materialType = $sticker->material_type; + + // if ($materialType == 2) + // { + // $bundleQty = $sticker->bundle_quantity ?? 0; + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // if ($totalExcelQty == 0) { + // $zeroQtyCodes[] = $code; + // } else if (!is_numeric($totalExcelQty)) { + // $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too + // } else if ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { + // $notDivisibleCodes[] = $code; + // } + // } + // } + + // $showValidationNotification = function(array $codes, string $message) { + // if (count($codes) == 0) return; + + // $uniqueCodes = array_unique($codes); + // $codeList = implode(', ', $uniqueCodes); + + // Notification::make() + // ->title('Invalid Bundle Quantity') + // ->body("$message
$codeList") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // }; + + // $showValidationNotification($nonNumericQtyCodes, "The following item codes contains invalid bundle quantity:"); + // $showValidationNotification($zeroQtyCodes, "The following item codes quantity should be greater than '0':"); + // $showValidationNotification($notDivisibleCodes, "The following item codes quantity is not divisible by bundle quantity."); + + + // if ($nonNumericQtyCodes || $zeroQtyCodes || $notDivisibleCodes) { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + // else + // { + // $inserted = 0; + // foreach ($matchedItems as $sticker) + // { + // $code = $sticker->item->code; + // $materialType = $sticker->material_type; + // // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($code) { $query->where('plant_id', $this->plantId)->where('code', $code); })->first(); + + // if ($materialType == 1) + // { + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // for ($i = 0; $i < $totalExcelQty; $i++) + // { + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => 1, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // } + // else if ($materialType == 2) + // { + // $bundleQty = $sticker->bundle_quantity; + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) + // { + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => $bundleQty, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // } + // else if ($materialType == 3) + // { + // $totalExcelQty = 0; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $excelCode = trim($row[0]); + // $excelMatQty = trim($row[1]); + + // if (empty($excelCode) || empty($excelMatQty)) { + // continue; + // } + + // if ($excelCode == $code && is_numeric($excelMatQty)) { + // $totalExcelQty += $excelMatQty; // Sum up the quantities + // } + // } + + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'quantity' => $totalExcelQty, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + // } + + // if ($inserted > 0) + // { + // Notification::make() + // ->title("Start the scanning process!") + // ->body("'$inserted' material invoice records were inserted.") + // ->info() + // // ->success() + // ->seconds(1) + // ->send(); + + // // Update total quantity in the form + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // if ($totalQuantity == $scannedQuantity) + // { + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); + // } + // else + // { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // } + // } + // else + // { + // Notification::make() + // ->title("Import Failed: Material Invoice") + // ->body("No new records were inserted for Material Invoice: '$invoiceNumber'.") + // ->danger() + // ->seconds(3) + // ->send(); + + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // } + // else if ($invoiceType == 'S') + // { + // $invalidMatCodes = []; + // $materialCodes = []; + // $missingSerials = []; + // $invalidSerCodes = []; + // $duplicateSerials = []; + // $serialNumbers = []; + // $validRowsFound = false; + + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; // Skip header + + // $materialCode = trim($row[0]); + // $serialNumber = trim($row[1]); + + // if (empty($materialCode) && empty($serialNumber)) { + // continue; + // } + + // if (!empty($materialCode)) { + // if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) + // { + // $invalidMatCodes[] = $materialCode; + // continue; + // } + // else + // { + // if (empty($serialNumber)) { + // $missingSerials[] = $materialCode; + // } + // else if (Str::length($serialNumber) < 9 || !ctype_alnum($serialNumber)) + // { + // $invalidSerCodes[] = $serialNumber; + // } + // else + // { + // if (in_array($serialNumber, $serialNumbers)) { + // $duplicateSerials[] = $serialNumber; + // } else { + // $serialNumbers[] = $serialNumber; + // $materialCodes[] = $materialCode; + // $validRowsFound = true; + // } + // } + // } + // } + // else + // { + // continue; + // } + // } + + // $uniqueInvalidCodes = array_unique($invalidMatCodes); + + // if (!empty($uniqueInvalidCodes)) { + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueMissingSerials = array_unique($missingSerials); + + // if (!empty($uniqueMissingSerials)) { + // Notification::make() + // ->title('Missing Serial Numbers') + // ->body("The following item codes doesn't have valid serial number:
" . implode(', ', $uniqueMissingSerials)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueInvalidSerCodes = array_unique($invalidSerCodes); + + // if (!empty($uniqueInvalidSerCodes)) { + // Notification::make() + // ->title('Invalid Serial Numbers') + // ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
' . implode(', ', $uniqueInvalidSerCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $uniqueDupSerCodes = array_unique($duplicateSerials); + + // if (!empty($uniqueDupSerCodes)) { + // Notification::make() + // ->title('Duplicate Serial Numbers') + // ->body('The following serial numbers are already exist in invoice excel:
' . implode(', ', $uniqueDupSerCodes)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // if (!$validRowsFound) { + // Notification::make() + // ->title('Invalid Serial Invoice') + // ->danger() // This makes the notification red to indicate an error + // ->body('Uploaded excel sheet is empty or
contains no valid data.') + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + // return; + // } + + // $uniqueCodes = array_unique($materialCodes); + + // //itemNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes) { + // $query->whereIn('code', $uniqueCodes); + // }) + // ->get(); + + // // // Get all codes that exist in the database for the given plant_id + // // $existingCodes = StickerMaster::where('plant_id', $plantId) + // // ->whereHas('item', function ($query) use ($uniqueCodes) { + // // $query->whereIn('code', $uniqueCodes); + // // }) + // // ->with('item') // Eager load for performance + // // ->get() + // // ->pluck('item.code') + // // ->toArray(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // //plantNotFound + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + // $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (!empty($missingCodes)) + // { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $invalidCodes = $matchedItems + // ->filter(fn ($sticker) => !empty($sticker->material_type)) //filter invalid + // ->pluck('item.code') + // ->toArray(); + + // if (!empty($invalidCodes)) + // { + // $missingCount = count($invalidCodes); + + // $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
" . implode(', ', $invalidCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + + // // $missPackCodes = $matchedItems + // // ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) + // // ->pluck('item.code') + // // ->toArray(); + + // $missPackCodes = $matchedItems + // ->filter(fn ($sticker) => empty($sticker->pack_slip_motor) && empty($sticker->pack_slip_pump) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) + // ->pluck('item.code') + // ->toArray(); + + // if (!empty($missPackCodes)) + // { + // $missingCount = count($missPackCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
" . implode(', ', $missPackCodes); + + // Notification::make() + // ->title('Invalid: Item Codes') + // ->body($message) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $existingSerialNumbers = SerialValidation::whereIn('serial_number', $serialNumbers)->where('plant_id', $plantId)->pluck('serial_number')->toArray(); + + // // If there are duplicates, notify and stop the process + // if (!empty($existingSerialNumbers)) { + // Notification::make() + // ->title('Duplicate Serial Numbers') + // ->body('The following serial numbers are already exist in database:
' . implode(', ', $existingSerialNumbers)) + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + + // $inserted = 0; + // foreach ($rows as $index => $row) + // { + // if ($index == 0) continue; + + // $materialCode = trim($row[0]); + // $serialNumber = trim($row[1]); + + // if (empty($materialCode) || empty($serialNumber)) { + // continue; + // } + + // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { + // $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode + // })->first(); + + // if ($sticker) { + // SerialValidation::create([ + // 'sticker_master_id' => $sticker->id, + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => $serialNumber, + // 'operator_id'=> $operatorName, + // ]); + // $inserted++; + // } + // } + + // if ($inserted > 0) + // { + // Notification::make() + // ->title("Start the scanning process!") + // ->body("'$inserted' serial invoice records were inserted.") + // ->info() + // // ->success() + // ->seconds(1) + // ->send(); + + // $this->dispatch('playNotificationSound'); + + // // Update total quantity in the form + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // if ($totalQuantity == $scannedQuantity) + // { + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + + // $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); + // } + // else + // { + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // } + // } + // else + // { + // Notification::make() + // ->title("Import Failed: Serial Invoice") + // ->body("No new records were inserted for Serial Invoice : '$invoiceNumber'.") + // ->danger() + // ->seconds(3) + // ->send(); + + // $this->dispatch('playWarnSound'); + + // $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + // $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + // $this->form->fill([ + // 'plant_id' => $plantId, + // 'invoice_number' => $invoiceNumber, + // 'serial_number' => null, + // 'total_quantity' => $totalQuantity, + // 'scanned_quantity'=> $scannedQuantity, + // ]); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + + // $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + // return; + // } + // } + // else + // { + // Notification::make() + // ->title('Invoice Type Not Found') + // ->body("Import the valid 'Invoice' file to proceed..!") + // ->danger() + // ->seconds(3) + // ->send(); + // $this->dispatch('playWarnSound'); + + // // if ($disk->exists($filePath)) { + // // $disk->delete($filePath); + // // } + // return; + // } + // } + // } + + public function processInvoice($invoiceNumber) { $invoiceNumber = trim($invoiceNumber); - $fileName = $invoiceNumber . '.txt'; + $parts = explode('.', $invoiceNumber); $this->showCapacitorInput = false; @@ -82,214 +2486,80 @@ class CreateSerialValidation extends CreateRecord $this->plantId = $plantId; $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; - if ($plant) - { - $plantCode = $plant->code; - } - else - { - $plantCode = null; - } + // ..GET SERIAL INVOICE API - //..GET SERIAL INVOICE API + if (strlen($invoiceNumber) > 15) { - $content = SmbService::readTextFile($fileName); + $payloadJson = base64_decode(strtr($parts[1], '-_', '+/')); - if ($content == '') { - Notification::make() - ->title('File Not Found') - ->body("Unable to locate file: {$fileName}") - ->danger() - ->send(); - - return; - } - - $lines = preg_split("/\r\n|\n|\r/", trim($content)); - - $insertData = []; - $missingItemCodes = []; - $InvalidLenSno = []; - $InvalidSno = []; - $InvalidLenItem = []; - $InvalidItem = []; - - foreach ($lines as $line) - { - $line = trim($line); - if ($line == '') { - continue; - } - - $parts = array_map('trim', explode(',', $line)); - - if (count($parts) != 2) { + if (empty($payloadJson)) { Notification::make() - ->title("Invalid data found inside the file.") - ->danger() - ->seconds(1) - ->send(); + ->title('Invalid payload for scanned qr code.') + ->danger() + ->seconds(1) + ->send(); + return; } - [$itemCode, $serialNumber] = $parts; + $payload = json_decode($payloadJson, true); - $sticker = StickerMaster::where('plant_id', $plantId) - ->whereHas('item', function ($query) use ($itemCode, $plantId) { - $query->where('plant_id', $plantId) - ->where('code', $itemCode); - }) - ->first(); + if (! isset($payload['data'])) { + Notification::make() + ->title('Invalid payload for scanned qr code.') + ->info() + ->seconds(1) + ->send(); - if (Str::length($itemCode) < 6) - { - $InvalidLenItem [] = $itemCode; - continue; - } - else if(!is_numeric($itemCode)){ - $InvalidItem [] = $itemCode; - continue; - } - if (Str::length($serialNumber) < 9) - { - $InvalidLenSno [] = $serialNumber; - continue; - } - else if(!ctype_alnum($serialNumber)){ - $InvalidSno [] = $serialNumber; - continue; - } - - if (!$sticker) { - $missingItemCodes[] = $itemCode; - continue; - } - - $insertData[] = [ - 'plant_id' => $plantId, - 'sticker_master_id' => $sticker->id, - 'invoice_number' => $invoiceNumber, - 'serial_number' => $serialNumber, - 'created_at' => now(), - 'operator_id' => $operatorName, - 'updated_at' => now(), - ]; - } - - if (!empty($InvalidLenItem)) - { - $count = count($InvalidLenItem); - - if ($count <= 10) { - $body = 'Item Code should contain minimum 6 digits: ' . implode(', ', $InvalidLenItem); - } else { - $body = "{$count} item codes contain minimum 6 digits."; - } - Notification::make() - ->title("Invalid Item Code.") - ->body("$body") - ->danger() - ->seconds(1) - ->send(); return; - } - else if (!empty($InvalidItem)) - { - $count = count($InvalidItem); - - if ($count <= 10) { - $body = 'Item code must be in numeric values: ' . implode(', ', $InvalidSno); - } else { - $body = "{$count} item codes must be in numeric values."; } - Notification::make() - ->title("Invalid Item Code.") - ->body("$body") - ->danger() - ->seconds(1) - ->send(); - return; - } - else if (!empty($InvalidLenSno)) - { - $count = count($InvalidLenSno); - if ($count <= 10) { - $body = 'Serial number should be minimum 9 digits: ' . implode(', ', $InvalidLenSno); - } else { - $body = "{$count} serial number should be minimum 9 digits."; - } - Notification::make() - ->title("Invalid Serial Number.") - ->body("$body") - ->danger() - ->seconds(1) - ->send(); + $documentData = $payload['data']; + + if ($documentData == '' || $documentData == '') { + Notification::make() + ->title('Invalid payload for scanned qr code.') + ->info() + ->seconds(1) + ->send(); + return; - } - else if (!empty($InvalidSno)) - { - $count = count($InvalidSno); - - if ($count <= 10) { - $body = 'Serial number should be conatin alpha numeric values: ' . implode(', ', $InvalidSno); - } else { - $body = "{$count} serial number should be conatin alpha numeric values."; } - Notification::make() - ->title("Invalid Serial Number.") - ->body("$body") - ->danger() - ->seconds(1) - ->send(); - return; - } - else if (!empty($missingItemCodes)) - { - $count = count($missingItemCodes); - if ($count <= 10) { - $body = 'Item codes not found in sticker master: ' . implode(', ', $missingItemCodes); - } else { - $body = "{$count} item codes not found in sticker master table."; + // Extract DocNo + preg_match('/"DocNo"\s*:\s*"([^"]+)"/', $documentData, $matches); + + if (! isset($matches[1])) { + Notification::make() + ->title('Invoice number not found.') + ->info() + ->seconds(1) + ->send(); + + return; } - Notification::make() - ->title("Unknown Item Code.") - ->body("$body") - ->danger() - ->seconds(1) - ->send(); - return; - } - if (!empty($insertData)) { - SerialValidation::insert($insertData); + if (isset($matches[1])) { + $invoiceNumber = strtoupper($matches[1]); + } + } else { + $invoiceNumber = strtoupper($invoiceNumber); } - else{ - Notification::make() - ->title("Insert Failed.") - ->body("Data insertion failed") - ->danger() - ->seconds(1) - ->send(); - return; - } - - //.. $updateStatus = $this->form->getState()['update_invoice'] ?? null; - $this->invoiceNumber = trim($this->form->getState()['invoice_number']) ?? $invoiceNumber; + $this->invoiceNumber = strtoupper(trim($this->form->getState()['invoice_number'])) ?? $invoiceNumber; $invoiceType = null; - //$this->invoiceNumber = $this->invoiceNumber ?? $invoiceNumber; + // $this->invoiceNumber = $this->invoiceNumber ?? $invoiceNumber; $invoiceExist = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->exists(); - if (!$invoiceExist) { + if (! $invoiceExist) { Notification::make() - ->title("New invoice detected.") + ->title('New invoice detected.') ->info() ->seconds(1) ->send(); @@ -300,88 +2570,90 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => 0, - 'scanned_quantity'=> 0, + 'scanned_quantity' => 0, ]); + $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); - } - else - { + } else { $totQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); $scanSQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); - $totMQuan = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); //->where('quantity', '!=', '') + $totMQuan = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); // ->where('quantity', '!=', '') $scanMQuan = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); - if ($totMQuan > 0) - { + if ($totMQuan > 0) { $this->form->fill([ 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totQuan, - 'scanned_quantity'=> $scanMQuan, + 'scanned_quantity' => $scanMQuan, ]); - if ($totQuan == $scanMQuan) - { + if ($totQuan == $scanMQuan) { Notification::make() - ->title("Completed: Material Invoice") + ->title('Completed: Material Invoice') ->body("Material invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Material Invoice' to proceed!") ->warning() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playNotificationSound'); - $filename = $invoiceNumber . '.xlsx'; - $directory = 'uploads/temp'; + $filename = $invoiceNumber.'.xlsx'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); - $filePath = $directory . '/' . $filename; - //$fullPath = null; + $filePath = $directory.'/'.$filename; + // $fullPath = null; if ($disk->exists($filePath)) { - //$fullPath = $disk->path($filePath); + // $fullPath = $disk->path($filePath); $disk->delete($filePath); } + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => null, + 'serial_number' => null, + 'total_quantity' => 0, + 'update_invoice' => false, + 'scanned_quantity' => 0, + ]); + $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); - } - else - { + } else { Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->info() ->seconds(1) ->send(); - $this->dispatch('playNotificationSound'); - // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; - // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $hasRecords = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); //$this->invoiceNumber $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); - if ($updateStatus == '1') - { - //'Material invoice update in progress...'; - $filename = $invoiceNumber . '.xlsx'; - $directory = 'uploads/temp'; + if ($updateStatus == '1') { + // 'Material invoice update in progress...'; + $filename = $invoiceNumber.'.xlsx'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); - $filePath = $directory . '/' . $filename; + $filePath = $directory.'/'.$filename; $fullPath = $disk->path($filePath); - if ($fullPath && file_exists($fullPath)) - { + if ($fullPath && file_exists($fullPath)) { // Now you can read/process the file here $rows = Excel::toArray(null, $fullPath)[0]; - if ((count($rows) - 1) <= 0) - { + if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid updated 'Material Invoice' file to proceed..!") ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } @@ -392,9 +2664,10 @@ class CreateSerialValidation extends CreateRecord $hasQuanTypIds = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $materialQuantity = trim($row[1]); @@ -403,48 +2676,38 @@ class CreateSerialValidation extends CreateRecord continue; } - if (!empty($materialCode)) { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; - } - else - { + } else { if (empty($materialQuantity)) { $missingQuantities[] = $materialCode; - } - else if (!is_numeric($materialQuantity)) - { + } elseif (! is_numeric($materialQuantity)) { $invalidMatQuan[] = $materialCode; - } - else if ($materialQuantity == 0) - { + } elseif ($materialQuantity == 0) { $invalidMatQuan[] = $materialCode; - } - else - { + } else { $materialCodes[] = $materialCode; $validRowsFound = true; } } - } - else - { + } else { continue; } } $uniqueInvalidCodes = array_unique($invalidMatCodes); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid: Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -453,15 +2716,16 @@ class CreateSerialValidation extends CreateRecord $uniqueMissingQuanCodes = array_unique($missingQuantities); - if (!empty($uniqueMissingQuanCodes)) { + if (! empty($uniqueMissingQuanCodes)) { Notification::make() ->title('Missing Material Quantity') - ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueMissingQuanCodes)) + ->body("The following item codes doesn't have valid material quantity:
".implode(', ', $uniqueMissingQuanCodes)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -470,39 +2734,41 @@ class CreateSerialValidation extends CreateRecord $uniqueInvalidMatQuan = array_unique($invalidMatQuan); - if (!empty($uniqueInvalidMatQuan)) { + if (! empty($uniqueInvalidMatQuan)) { Notification::make() ->title('Invalid Material Quantity') - ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueInvalidMatQuan)) + ->body("The following item codes doesn't have valid material quantity:
".implode(', ', $uniqueInvalidMatQuan)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Material Invoice') ->danger() // This makes the notification red to indicate an error ->body('Uploaded Excel sheet is empty or
contains no valid data.') - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } $uniqueCodes = array_unique($materialCodes); - //itemNotFound + // itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -513,27 +2779,26 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - //plantNotFound + // plantNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); @@ -544,20 +2809,19 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -565,25 +2829,23 @@ class CreateSerialValidation extends CreateRecord } $invalidCodes = $matchedItems - ->filter(fn ($sticker) => empty($sticker->material_type)) //filter invalid + ->filter(fn ($sticker) => empty($sticker->material_type)) // filter invalid ->pluck('item.code') ->toArray(); - - if (!empty($invalidCodes)) - { + if (! empty($invalidCodes)) { $missingCount = count($invalidCodes); - $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
" . implode(', ', $invalidCodes); + $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
".implode(', ', $invalidCodes); Notification::make() ->title('Invalid: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -595,19 +2857,18 @@ class CreateSerialValidation extends CreateRecord $notDivisibleCodes = []; $hasQuanTyp = false; - foreach ($matchedItems as $sticker) - { + foreach ($matchedItems as $sticker) { $code = $sticker->item->code; $materialType = $sticker->material_type; - if ($materialType == 2) - { + if ($materialType == 2) { $bundleQty = $sticker->bundle_quantity ?? 0; $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -623,21 +2884,21 @@ class CreateSerialValidation extends CreateRecord if ($totalExcelQty == 0) { $zeroQtyCodes[] = $code; - } else if (!is_numeric($totalExcelQty)) { + } elseif (! is_numeric($totalExcelQty)) { $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too - } else if ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { + } elseif ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { $notDivisibleCodes[] = $code; } - } - else if ($materialType == 3) - { + } elseif ($materialType == 3) { $hasQuanTyp = true; $hasQuanTypIds[] = $sticker->id; } } - $showValidationNotification = function(array $codes, string $message) { - if (count($codes) == 0) return; + $showValidationNotification = function (array $codes, string $message) { + if (count($codes) == 0) { + return; + } $uniqueCodes = array_unique($codes); $codeList = implode(', ', $uniqueCodes); @@ -646,60 +2907,53 @@ class CreateSerialValidation extends CreateRecord ->title('Invalid Bundle Quantity') ->body("$message
$codeList") ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); }; - $showValidationNotification($nonNumericQtyCodes, "The following item codes contains invalid bundle quantity:"); + $showValidationNotification($nonNumericQtyCodes, 'The following item codes contains invalid bundle quantity:'); $showValidationNotification($zeroQtyCodes, "The following item codes quantity should be greater than '0':"); - $showValidationNotification($notDivisibleCodes, "The following item codes quantity is not divisible by bundle quantity."); - + $showValidationNotification($notDivisibleCodes, 'The following item codes quantity is not divisible by bundle quantity.'); if ($nonNumericQtyCodes || $zeroQtyCodes || $notDivisibleCodes) { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; - } - else - { - $oldQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + } else { + $oldQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); $newQuan = -1; $inserted = 0; - foreach ($matchedItems as $sticker) - { - if ($newQuan == -1 && !$hasQuanTyp) - { - SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + foreach ($matchedItems as $sticker) { + if ($newQuan == -1 && ! $hasQuanTyp) { + InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function ($query) { $query->whereNull('serial_number')->orWhere('serial_number', ''); }) - ->forceDelete(); //->delete(); + ->forceDelete(); // ->delete(); - $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); - } - else if ($newQuan == -1 && $hasQuanTyp) - { - SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + $newQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + } elseif ($newQuan == -1 && $hasQuanTyp) { + InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function ($query) { $query->whereNull('serial_number')->orWhere('serial_number', ''); })->whereNotIn('sticker_master_id', $hasQuanTypIds) - ->forceDelete(); //->delete(); + ->forceDelete(); // ->delete(); - $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $newQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); } $code = $sticker->item->code; $materialType = $sticker->material_type; // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($code) { $query->where('plant_id', $this->plantId)->where('code', $code); })->first(); - if ($materialType == 1) - { + if ($materialType == 1 || $materialType == 4) { $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -715,32 +2969,31 @@ class CreateSerialValidation extends CreateRecord $existQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where('sticker_master_id', $sticker->id)->count(); - if ($existQty < $totalExcelQty) - { + if ($existQty < $totalExcelQty) { $newQty = $totalExcelQty - $existQty; - for ($i = 0; $i < $newQty; $i++) - { + for ($i = 0; $i < $newQty; $i++) { if ($sticker) { SerialValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => 1, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } } - } - else if ($materialType == 2) - { + } elseif ($materialType == 2) { $bundleQty = $sticker->bundle_quantity; $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -756,33 +3009,32 @@ class CreateSerialValidation extends CreateRecord $existQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where('sticker_master_id', $sticker->id)->count(); - //for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) - $newTotQty = $totalExcelQty/$bundleQty; - if ($existQty < $newTotQty) - { + // for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) + $newTotQty = $totalExcelQty / $bundleQty; + if ($existQty < $newTotQty) { $newQty = $newTotQty - $existQty; - for ($i = 0; $i < $newQty; $i++) - { + for ($i = 0; $i < $newQty; $i++) { if ($sticker) { SerialValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => $bundleQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } } - } - else if ($materialType == 3) - { + } elseif ($materialType == 3) { $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -796,16 +3048,16 @@ class CreateSerialValidation extends CreateRecord } } - $existEmpRecQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + $existEmpRecQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function ($query) { $query->whereNull('serial_number')->orWhere('serial_number', ''); })->where('sticker_master_id', $sticker->id)->first(); $existEmpQty = $existEmpRecQty ? $existEmpRecQty->quantity : 0; - $existComQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function($query) { + $existComQty = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->where(function ($query) { $query->whereNotNull('serial_number')->where('serial_number', '!=', ''); }) - ->where('sticker_master_id', $sticker->id) - ->sum(column: 'quantity'); + ->where('sticker_master_id', $sticker->id) + ->sum(column: 'quantity'); $existComQty = $existComQty ? $existComQty : 0; // 8 = 3 + 5 // 8 = 5 + 3 // 8 = 0 + 8 // 8 = 8 + 0 @@ -817,94 +3069,95 @@ class CreateSerialValidation extends CreateRecord // 8 <= 7 // 8 <= 7 // 8 <= 7 // 8 <= 7 // 0 <= 5 - if ($existQty <= $totalExcelQty) - { + if ($existQty <= $totalExcelQty) { // 6 = 11 - 5 // 5 = 8 - 3 // 3 = 11 - 8 // 9 = 9 - 0 // 5 = 5 - 0 $newInsQty = $totalExcelQty - $existComQty; // 3 > 0 // 5 > 0 // 0 > 0 // 8 > 0 // 0 > 0 - if ($existEmpQty > 0) // update - { + if ($existEmpQty > 0) { // update // 3 = 6 // 5 = 5 // 0 = 3 // 8 = 9 // 0 = 5 - if ($existEmpQty == $newInsQty) { continue; } + if ($existEmpQty == $newInsQty) { + continue; + } // $existEmpRecQty->delete(); $existEmpRecQty->update([ 'quantity' => $newInsQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'updated_by' => $operatorName, 'updated_at' => now(), ]); $newQuan--; $inserted++; - } - else if ($newInsQty > 0) // if ($sticker) // create - { + } elseif ($newInsQty > 0) { // if ($sticker) // create SerialValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => $newInsQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } // 8 > 7 // 8 > 7 // 8 > 7 // 8 > 7 - else - { + else { // 2 = 7 - 5 // 4 = 7 - 3 // -1 = 7 - 8 // 7 = 7 - 0 $newInsQty = $totalExcelQty - $existComQty; // 3 > 0 // 5 > 0 // 0 > 0 // 8 > 0 - if ($existEmpQty > 0) // update - { + if ($existEmpQty > 0) { // update // 3 = 2 // 5 = 4 // 0 = -1 // 8 = 7 - if ($existEmpQty == $newInsQty) { continue; } + if ($existEmpQty == $newInsQty) { + continue; + } // $existEmpRecQty->delete(); $existEmpRecQty->update([ 'quantity' => $newInsQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'updated_by' => $operatorName, 'updated_at' => now(), ]); $newQuan--; $inserted++; - } - else if ($newInsQty > 0) // create - { + } elseif ($newInsQty > 0) { // create SerialValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => $newInsQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } - } } - if ($inserted > 0 || $oldQuan != $newQuan) - { + if ($inserted > 0 || $oldQuan != $newQuan) { Notification::make() - ->title("Material invoice successfully updatad.") + ->title('Material invoice successfully updated.') ->success() ->seconds(1) ->send(); Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->body("'$inserted' new material invoice records were inserted.") ->info() // ->success() ->seconds(1) ->send(); - $this->dispatch('playNotificationSound'); + SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->update(['updated_by' => $operatorName]); // 'updated_at' => now(), + // Update total quantity in the form $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); @@ -913,33 +3166,28 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); - if ($totalQuantity == $scannedQuantity) - { + if ($totalQuantity == $scannedQuantity) { if ($disk->exists($filePath)) { $disk->delete($filePath); } $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); - } - else - { + } else { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); } - } - else - { + } else { Notification::make() - ->title("Update Failed: Material Invoice") + ->title('Update Failed: Material Invoice') ->body("No new records were inserted for Material Invoice : '$invoiceNumber'.") ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); @@ -950,7 +3198,7 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); // if ($disk->exists($filePath)) { @@ -958,17 +3206,16 @@ class CreateSerialValidation extends CreateRecord // } $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } } - } - else - { + } else { Notification::make() ->title('Updated Invoice Not Found') ->body("Import the updated 'Material Invoice' file to proceed..!") ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); @@ -977,80 +3224,80 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => 0, - 'scanned_quantity'=> 0, + 'scanned_quantity' => 0, ]); $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } } } + return; - } - else - { + } else { $this->form->fill([ 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totQuan, - 'scanned_quantity'=> $scanSQuan, + 'scanned_quantity' => $scanSQuan, ]); - //$hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; - // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $hasRecords = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); - if ($totQuan == $scanSQuan) - { + if ($totQuan == $scanSQuan) { Notification::make() - ->title("Completed: Serial Invoice") - ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed.!") + ->title('Completed: Serial Invoice') + ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed..!") ->warning() ->seconds(3) ->send(); - $this->dispatch('playNotificationSound'); - $filename = $invoiceNumber . '.xlsx'; - $directory = 'uploads/temp'; + $filename = $invoiceNumber.'.xlsx'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); - $filePath = $directory . '/' . $filename; - //$fullPath = null; + $filePath = $directory.'/'.$filename; + // $fullPath = null; if ($disk->exists($filePath)) { - //$fullPath = $disk->path($filePath); + // $fullPath = $disk->path($filePath); $disk->delete($filePath); } - $this->dispatch('playNotificationSound'); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => null, + 'serial_number' => null, + 'total_quantity' => 0, + 'update_invoice' => false, + 'scanned_quantity' => 0, + ]); + $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); - } - else - { + } else { Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->info() ->seconds(1) ->send(); - $this->dispatch('playNotificationSound'); - $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); - if ($updateStatus == '1') - { - $filename = $invoiceNumber . '.xlsx'; - $directory = 'uploads/temp'; + if ($updateStatus == '1') { + $filename = $invoiceNumber.'.xlsx'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); - $filePath = $directory . '/' . $filename; + $filePath = $directory.'/'.$filename; $fullPath = $disk->path($filePath); - // Check if file exists //$disk->exists($filePath) - if ($fullPath && file_exists($fullPath)) - { - // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx + // Check if file exists //if ($disk->exists($filePath)) + if ($fullPath && file_exists($fullPath)) { + // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx // 'Serial invoice update in progress...' - // Now you can read/process the file here $rows = Excel::toArray(null, $fullPath)[0]; - if ((count($rows) - 1) <= 0) - { + if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid updated 'Serial Invoice' file to proceed..!") @@ -1062,6 +3309,7 @@ class CreateSerialValidation extends CreateRecord if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } @@ -1073,61 +3321,49 @@ class CreateSerialValidation extends CreateRecord $serialNumbers = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $serialNumber = trim($row[1]); - if (empty($materialCode) && empty($serialNumber)) - { + if (empty($materialCode) && empty($serialNumber)) { continue; } - if (!empty($materialCode)) - { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; + continue; - } - else - { + } else { if (empty($serialNumber)) { $missingSerials[] = $materialCode; - } - else if (Str::length($serialNumber) < 9 || !ctype_alnum($serialNumber)) - { + } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { $invalidSerCodes[] = $serialNumber; - } - else - { - if (in_array($serialNumber, $serialNumbers)) - { + } else { + if (in_array($serialNumber, $serialNumbers)) { $duplicateSerials[] = $serialNumber; - } - else - { + } else { $serialNumbers[] = $serialNumber; $materialCodes[] = $materialCode; $validRowsFound = true; } } } - } - else - { + } else { continue; } } $uniqueInvalidCodes = array_unique($invalidMatCodes); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid: Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() ->seconds(3) ->send(); @@ -1142,10 +3378,10 @@ class CreateSerialValidation extends CreateRecord $uniqueMissingSerials = array_unique($missingSerials); - if (!empty($uniqueMissingSerials)) { + if (! empty($uniqueMissingSerials)) { Notification::make() ->title('Missing Serial Numbers') - ->body("The following item codes doesn't have valid serial number:
" . implode(', ', $uniqueMissingSerials)) + ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) ->danger() ->seconds(3) ->send(); @@ -1160,13 +3396,14 @@ class CreateSerialValidation extends CreateRecord $uniqueInvalidSerCodes = array_unique($invalidSerCodes); - if (!empty($uniqueInvalidSerCodes)) { + if (! empty($uniqueInvalidSerCodes)) { Notification::make() ->title('Invalid Serial Numbers') - ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
' . implode(', ', $uniqueInvalidSerCodes)) + ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
'.implode(', ', $uniqueInvalidSerCodes)) ->danger() ->seconds(3) ->send(); + $this->dispatch('playWarnSound'); // if ($disk->exists($filePath)) { @@ -1177,22 +3414,23 @@ class CreateSerialValidation extends CreateRecord $uniqueDupSerCodes = array_unique($duplicateSerials); - if (!empty($uniqueDupSerCodes)) { + if (! empty($uniqueDupSerCodes)) { Notification::make() ->title('Duplicate Serial Numbers') - ->body('The following serial numbers are already exist in invoice excel:
' . implode(', ', $uniqueDupSerCodes)) + ->body('The following serial numbers are already exist in invoice excel:
'.implode(', ', $uniqueDupSerCodes)) ->danger() ->seconds(3) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Serial Invoice') ->danger() // This makes the notification red to indicate an error @@ -1204,12 +3442,13 @@ class CreateSerialValidation extends CreateRecord if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } $uniqueCodes = array_unique($materialCodes); - //itemNotFound + // itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -1220,11 +3459,10 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); //->where('plant_id', $plantId) + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); // ->where('plant_id', $plantId) Notification::make() ->title('Unknown: Item Codes') @@ -1241,7 +3479,7 @@ class CreateSerialValidation extends CreateRecord return; } - //plantNotFound + // plantNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); @@ -1252,11 +3490,10 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') @@ -1264,8 +3501,8 @@ class CreateSerialValidation extends CreateRecord ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1273,15 +3510,14 @@ class CreateSerialValidation extends CreateRecord } $invalidCodes = $matchedItems - ->filter(fn ($sticker) => !empty($sticker->material_type)) //filter invalid - ->pluck('item.code') - ->toArray(); + ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid + ->pluck('item.code') + ->toArray(); - if (!empty($invalidCodes)) - { + if (! empty($invalidCodes)) { $missingCount = count($invalidCodes); - $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
" . implode(', ', $invalidCodes); + $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
".implode(', ', $invalidCodes); Notification::make() ->title('Invalid: Item Codes') @@ -1289,8 +3525,8 @@ class CreateSerialValidation extends CreateRecord ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1298,15 +3534,14 @@ class CreateSerialValidation extends CreateRecord } $missPackCodes = $matchedItems - ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) //filter invalid + ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) // filter invalid ->pluck('item.code') ->toArray(); - if (!empty($missPackCodes)) - { + if (! empty($missPackCodes)) { $missingCount = count($missPackCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
" . implode(', ', $missPackCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
".implode(', ', $missPackCodes); Notification::make() ->title('Invalid: Item Codes') @@ -1314,8 +3549,8 @@ class CreateSerialValidation extends CreateRecord ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1326,29 +3561,28 @@ class CreateSerialValidation extends CreateRecord $newQuan = 0; $inserted = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) // Skip header; - { + foreach ($rows as $index => $row) { + if ($index == 0) { // Skip header; SerialValidation::where('invoice_number', $invoiceNumber) - ->where(function($query) { + ->where(function ($query) { $query->whereNull('motor_scanned_status')->orWhere('motor_scanned_status', ''); }) - ->where(function($query) { + ->where(function ($query) { $query->whereNull('pump_scanned_status')->orWhere('pump_scanned_status', ''); }) - ->where(function($query) { + ->where(function ($query) { $query->whereNull('capacitor_scanned_status')->orWhere('capacitor_scanned_status', ''); }) - ->where(function($query) { + ->where(function ($query) { $query->whereNull('scanned_status_set')->orWhere('scanned_status_set', ''); }) - ->where(function($query) { + ->where(function ($query) { $query->whereNull('scanned_status')->orWhere('scanned_status', ''); }) - ->forceDelete(); //->delete(); + ->forceDelete(); // ->delete(); $newQuan = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + continue; } @@ -1359,13 +3593,15 @@ class CreateSerialValidation extends CreateRecord continue; } - $sNoExist = SerialValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->exists(); //->pluck('serial_number') + $sNoExist = SerialValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->exists(); // ->pluck('serial_number') - if ($sNoExist) { continue; } + if ($sNoExist) { + continue; + } $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { - $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode - })->first(); + $query->where('plant_id', $this->plantId)->where('code', $materialCode); // Check if item.code matches Excel's materialCode + })->first(); if ($sticker) { SerialValidation::create([ @@ -1373,22 +3609,23 @@ class CreateSerialValidation extends CreateRecord 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'serial_number' => $serialNumber, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } - if ($inserted > 0 || $oldQuan != $newQuan) - { + if ($inserted > 0 || $oldQuan != $newQuan) { Notification::make() - ->title("Serial invoice successfully updated.") + ->title('Serial invoice successfully updated.') ->success() ->seconds(1) ->send(); Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->body("'$inserted' new serial invoice records were inserted.") ->info() // ->success() @@ -1397,6 +3634,8 @@ class CreateSerialValidation extends CreateRecord $this->dispatch('playNotificationSound'); + SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->update(['updated_by' => $operatorName]); // 'updated_at' => now(), + // Update total quantity in the form $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); $scannedQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); @@ -1405,32 +3644,27 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); - if ($totalQuantity == $scannedQuantity) - { + if ($totalQuantity == $scannedQuantity) { if ($disk->exists($filePath)) { $disk->delete($filePath); } $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); - } - else - { + } else { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } - // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; - // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber - $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $hasRecords = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); //$this->invoiceNumber + $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); } - } - else - { + } else { Notification::make() - ->title("Update Failed: Serial Invoice") + ->title('Update Failed: Serial Invoice') ->body("No new records were inserted for Serial Invoice : '$invoiceNumber'.") ->danger() ->seconds(3) @@ -1444,7 +3678,7 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); // if ($disk->exists($filePath)) { @@ -1452,11 +3686,10 @@ class CreateSerialValidation extends CreateRecord // } $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } - } - else - { + } else { Notification::make() ->title('Updated Invoice Not Found') ->body("Import the updated 'Serial Invoice' file to proceed..!") @@ -1471,27 +3704,29 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => 0, - 'scanned_quantity'=> 0, + 'scanned_quantity' => 0, ]); $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } } } + return; } } - $filename = $invoiceNumber . '.xlsx'; - $directory = 'uploads/temp'; + $filename = $invoiceNumber.'.xlsx'; + $directory = "uploads/temp/{$plantCode}"; $disk = Storage::disk('local'); - $filePath = $directory . '/' . $filename; - $fullPath = null; //$fullPath = $disk->path($filePath); + $filePath = $directory.'/'.$filename; + $fullPath = null; // $fullPath = $disk->path($filePath); // Check if file exists if ($disk->exists($filePath)) { $fullPath = $disk->path($filePath); - //$fullPath = session('uploaded_invoice_path'); + // $fullPath = session('uploaded_invoice_path'); // Notification::make() // ->title('File exists.') // ->success() @@ -1510,20 +3745,21 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => 0, - 'scanned_quantity'=> 0, + 'scanned_quantity' => 0, ]); $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } - if (!file_exists($fullPath)) - { + if (! file_exists($fullPath)) { Notification::make() ->title('Invoice file doesn\'t exist.') ->danger() ->seconds(3) ->send(); $this->dispatch('playWarnSound'); + return; } @@ -1534,16 +3770,16 @@ class CreateSerialValidation extends CreateRecord // Compare with invoice number if ($uploadedFilename != $invoiceNumber) { Notification::make() - ->title("Uploaded file name does not match the invoice number.") + ->title('Uploaded file name does not match the invoice number.') ->danger() ->seconds(3) ->send(); $this->dispatch('playWarnSound'); + return; } - if ($fullPath && file_exists($fullPath)) - { + if ($fullPath && file_exists($fullPath)) { // Now you can read/process the file here $rows = Excel::toArray(null, $fullPath)[0]; // $collection = Excel::toCollection(null, $fullPath); @@ -1559,8 +3795,7 @@ class CreateSerialValidation extends CreateRecord // // Get the rows // $rows = $excelImport->getRows(); - if ((count($rows) - 1) <= 0) - { + if ((count($rows) - 1) <= 0) { Notification::make() ->title('Records Not Found') ->body("Import the valid 'Invoice' file to proceed..!") @@ -1572,61 +3807,74 @@ class CreateSerialValidation extends CreateRecord if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } - foreach ($rows as $index => $row) - { - if ($index == 0) continue; + $missingCodes = []; + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } $materialCode = trim($row[0]); - if (!empty($materialCode)) { - if (Str::length($materialCode) < 6) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6) { continue; - } - else - { - $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { - $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode - }); - if ($sticker->exists()) - { - if ($sticker->first()->material_type && !empty($sticker->first()->material_type)) - { - $invoiceType = "M"; + } else { + $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($plantId, $materialCode) { + $query->where('plant_id', $plantId)->where('code', $materialCode); // $this->plantId >> Check if item.code matches Excel's materialCode + }); + if ($sticker->exists()) { + if ($sticker->first()->material_type && ! empty($sticker->first()->material_type)) { + $invoiceType = 'M'; + break; + } else { + $invoiceType = 'S'; break; } - else - { - $invoiceType = "S"; - break; - } - } - else - { + } else { + $missingCodes[] = $materialCode; + continue; } } - } - else - { + } else { continue; } } - if ($invoiceType == 'M') - { + $uniqueCodes = array_unique($missingCodes); + + if (! empty($uniqueCodes)) { + $missingCount = count($uniqueCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
'.implode(', ', $uniqueCodes); + + Notification::make() + ->title('Unknown: Item Codes') + ->body($message) + ->danger() + ->seconds(3) + ->send(); + $this->dispatch('playWarnSound'); + + // if ($disk->exists($filePath)) { + // $disk->delete($filePath); + // } + return; + } elseif ($invoiceType == 'M') { $invalidMatCodes = []; $materialCodes = []; $missingQuantities = []; $invalidMatQuan = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $materialQuantity = trim($row[1]); @@ -1635,48 +3883,38 @@ class CreateSerialValidation extends CreateRecord continue; } - if (!empty($materialCode)) { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; - } - else - { + } else { if (empty($materialQuantity)) { $missingQuantities[] = $materialCode; - } - else if (!is_numeric($materialQuantity)) - { + } elseif (! is_numeric($materialQuantity)) { $invalidMatQuan[] = $materialCode; - } - else if ($materialQuantity == 0) - { + } elseif ($materialQuantity == 0) { $invalidMatQuan[] = $materialCode; - } - else - { + } else { $materialCodes[] = $materialCode; $validRowsFound = true; } } - } - else - { + } else { continue; } } $uniqueInvalidCodes = array_unique($invalidMatCodes); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid: Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1685,15 +3923,16 @@ class CreateSerialValidation extends CreateRecord $uniqueMissingQuanCodes = array_unique($missingQuantities); - if (!empty($uniqueMissingQuanCodes)) { + if (! empty($uniqueMissingQuanCodes)) { Notification::make() ->title('Missing Material Quantity') - ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueMissingQuanCodes)) + ->body("The following item codes doesn't have valid material quantity:
".implode(', ', $uniqueMissingQuanCodes)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1702,39 +3941,41 @@ class CreateSerialValidation extends CreateRecord $uniqueInvalidMatQuan = array_unique($invalidMatQuan); - if (!empty($uniqueInvalidMatQuan)) { + if (! empty($uniqueInvalidMatQuan)) { Notification::make() ->title('Invalid Material Quantity') - ->body("The following item codes doesn't have valid material quantity:
" . implode(', ', $uniqueInvalidMatQuan)) + ->body("The following item codes doesn't have valid material quantity:
".implode(', ', $uniqueInvalidMatQuan)) ->danger() - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Material Invoice') ->danger() // This makes the notification red to indicate an error ->body('Uploaded Excel sheet is empty or
contains no valid data.') - ->seconds(3) + ->seconds(2) ->send(); $this->dispatch('playWarnSound'); if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } $uniqueCodes = array_unique($materialCodes); - //itemNotFound + // itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -1745,27 +3986,26 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - //plantNotFound + // plantNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); @@ -1776,20 +4016,19 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1797,24 +4036,23 @@ class CreateSerialValidation extends CreateRecord } $invalidCodes = $matchedItems - ->filter(fn ($sticker) => empty($sticker->material_type)) //filter invalid + ->filter(fn ($sticker) => empty($sticker->material_type)) // filter invalid ->pluck('item.code') ->toArray(); - if (!empty($invalidCodes)) - { + if (! empty($invalidCodes)) { $missingCount = count($invalidCodes); - $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
" . implode(', ', $invalidCodes); + $message = $missingCount > 10 ? "'$missingCount' Serial Invoice item codes found." : "'Serial Invoice' item codes found:
".implode(', ', $invalidCodes); Notification::make() ->title('Invalid: Item Codes') ->body($message) ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -1825,19 +4063,18 @@ class CreateSerialValidation extends CreateRecord $zeroQtyCodes = []; $notDivisibleCodes = []; - foreach ($matchedItems as $sticker) - { + foreach ($matchedItems as $sticker) { $code = $sticker->item->code; $materialType = $sticker->material_type; - if ($materialType == 2) - { + if ($materialType == 2) { $bundleQty = $sticker->bundle_quantity ?? 0; $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -1853,16 +4090,18 @@ class CreateSerialValidation extends CreateRecord if ($totalExcelQty == 0) { $zeroQtyCodes[] = $code; - } else if (!is_numeric($totalExcelQty)) { + } elseif (! is_numeric($totalExcelQty)) { $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too - } else if ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { + } elseif ($bundleQty != 0 && $totalExcelQty % $bundleQty != 0) { $notDivisibleCodes[] = $code; } } } - $showValidationNotification = function(array $codes, string $message) { - if (count($codes) == 0) return; + $showValidationNotification = function (array $codes, string $message) { + if (count($codes) == 0) { + return; + } $uniqueCodes = array_unique($codes); $codeList = implode(', ', $uniqueCodes); @@ -1871,39 +4110,34 @@ class CreateSerialValidation extends CreateRecord ->title('Invalid Bundle Quantity') ->body("$message
$codeList") ->danger() - ->seconds(3) + ->seconds(2) ->send(); - $this->dispatch('playWarnSound'); }; - $showValidationNotification($nonNumericQtyCodes, "The following item codes contains invalid bundle quantity:"); + $showValidationNotification($nonNumericQtyCodes, 'The following item codes contains invalid bundle quantity:'); $showValidationNotification($zeroQtyCodes, "The following item codes quantity should be greater than '0':"); - $showValidationNotification($notDivisibleCodes, "The following item codes quantity is not divisible by bundle quantity."); - + $showValidationNotification($notDivisibleCodes, 'The following item codes quantity is not divisible by bundle quantity.'); if ($nonNumericQtyCodes || $zeroQtyCodes || $notDivisibleCodes) { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; - } - else - { + } else { $inserted = 0; - foreach ($matchedItems as $sticker) - { + foreach ($matchedItems as $sticker) { $code = $sticker->item->code; $materialType = $sticker->material_type; // $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($code) { $query->where('plant_id', $this->plantId)->where('code', $code); })->first(); - if ($materialType == 1) - { + if ($materialType == 1 || $materialType == 4) { $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -1917,28 +4151,28 @@ class CreateSerialValidation extends CreateRecord } } - for ($i = 0; $i < $totalExcelQty; $i++) - { + for ($i = 0; $i < $totalExcelQty; $i++) { if ($sticker) { - SerialValidation::create([ + InvoiceValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => 1, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } - } - else if ($materialType == 2) - { + } elseif ($materialType == 2) { $bundleQty = $sticker->bundle_quantity; $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -1952,27 +4186,27 @@ class CreateSerialValidation extends CreateRecord } } - for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) - { + for ($i = 0; $i < ($totalExcelQty / $bundleQty); $i++) { if ($sticker) { - SerialValidation::create([ + InvoiceValidation::create([ 'sticker_master_id' => $sticker->id, 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => $bundleQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } - } - else if ($materialType == 3) - { + } elseif ($materialType == 3) { $totalExcelQty = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $excelCode = trim($row[0]); $excelMatQty = trim($row[1]); @@ -1992,17 +4226,18 @@ class CreateSerialValidation extends CreateRecord 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'quantity' => $totalExcelQty, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } } - if ($inserted > 0) - { + if ($inserted > 0) { Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->body("'$inserted' material invoice records were inserted.") ->info() // ->success() @@ -2017,35 +4252,30 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); - if ($totalQuantity == $scannedQuantity) - { + if ($totalQuantity == $scannedQuantity) { if ($disk->exists($filePath)) { $disk->delete($filePath); } $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: false); - } - else - { + } else { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; - // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber + // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); //$this->invoiceNumber $this->dispatch('refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); } - } - else - { + } else { Notification::make() - ->title("Import Failed: Material Invoice") + ->title('Import Failed: Material Invoice') ->body("No new records were inserted for Material Invoice: '$invoiceNumber'.") ->danger() - ->seconds(3) + ->seconds(2) ->send(); $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); @@ -2055,7 +4285,7 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); // if ($disk->exists($filePath)) { @@ -2063,12 +4293,11 @@ class CreateSerialValidation extends CreateRecord // } $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } } - } - else if ($invoiceType == 'S') - { + } elseif ($invoiceType == 'S') { $invalidMatCodes = []; $materialCodes = []; $missingSerials = []; @@ -2077,9 +4306,10 @@ class CreateSerialValidation extends CreateRecord $serialNumbers = []; $validRowsFound = false; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; // Skip header + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header $materialCode = trim($row[0]); $serialNumber = trim($row[1]); @@ -2088,23 +4318,17 @@ class CreateSerialValidation extends CreateRecord continue; } - if (!empty($materialCode)) { - if (Str::length($materialCode) < 6 || !ctype_alnum($materialCode)) - { + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { $invalidMatCodes[] = $materialCode; + continue; - } - else - { + } else { if (empty($serialNumber)) { $missingSerials[] = $materialCode; - } - else if (Str::length($serialNumber) < 9 || !ctype_alnum($serialNumber)) - { + } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { $invalidSerCodes[] = $serialNumber; - } - else - { + } else { if (in_array($serialNumber, $serialNumbers)) { $duplicateSerials[] = $serialNumber; } else { @@ -2114,24 +4338,22 @@ class CreateSerialValidation extends CreateRecord } } } - } - else - { + } else { continue; } } $uniqueInvalidCodes = array_unique($invalidMatCodes); - if (!empty($uniqueInvalidCodes)) { + if (! empty($uniqueInvalidCodes)) { Notification::make() ->title('Invalid: Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
' . implode(', ', $uniqueInvalidCodes)) + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2140,15 +4362,15 @@ class CreateSerialValidation extends CreateRecord $uniqueMissingSerials = array_unique($missingSerials); - if (!empty($uniqueMissingSerials)) { + if (! empty($uniqueMissingSerials)) { Notification::make() ->title('Missing Serial Numbers') - ->body("The following item codes doesn't have valid serial number:
" . implode(', ', $uniqueMissingSerials)) + ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2157,15 +4379,15 @@ class CreateSerialValidation extends CreateRecord $uniqueInvalidSerCodes = array_unique($invalidSerCodes); - if (!empty($uniqueInvalidSerCodes)) { + if (! empty($uniqueInvalidSerCodes)) { Notification::make() ->title('Invalid Serial Numbers') - ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
' . implode(', ', $uniqueInvalidSerCodes)) + ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
'.implode(', ', $uniqueInvalidSerCodes)) ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2174,22 +4396,22 @@ class CreateSerialValidation extends CreateRecord $uniqueDupSerCodes = array_unique($duplicateSerials); - if (!empty($uniqueDupSerCodes)) { + if (! empty($uniqueDupSerCodes)) { Notification::make() ->title('Duplicate Serial Numbers') - ->body('The following serial numbers are already exist in invoice excel:
' . implode(', ', $uniqueDupSerCodes)) + ->body('The following serial numbers are already exist in invoice excel:
'.implode(', ', $uniqueDupSerCodes)) ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - if (!$validRowsFound) { + if (! $validRowsFound) { Notification::make() ->title('Invalid Serial Invoice') ->danger() // This makes the notification red to indicate an error @@ -2201,12 +4423,13 @@ class CreateSerialValidation extends CreateRecord if ($disk->exists($filePath)) { $disk->delete($filePath); } + return; } $uniqueCodes = array_unique($materialCodes); - //itemNotFound + // itemNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes) { $query->whereIn('code', $uniqueCodes); @@ -2227,11 +4450,10 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') @@ -2239,15 +4461,15 @@ class CreateSerialValidation extends CreateRecord ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } - //plantNotFound + // plantNotFound $matchedItems = StickerMaster::with('item') ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); @@ -2258,11 +4480,10 @@ class CreateSerialValidation extends CreateRecord $missingCodes = array_diff($uniqueCodes, $matchedCodes); - if (!empty($missingCodes)) - { + if (! empty($missingCodes)) { $missingCount = count($missingCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
' . implode(', ', $missingCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database for choosed plant." : 'The following item codes are not found in database for choosed plant:
'.implode(', ', $missingCodes); Notification::make() ->title('Unknown: Item Codes') @@ -2272,6 +4493,7 @@ class CreateSerialValidation extends CreateRecord ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2279,15 +4501,14 @@ class CreateSerialValidation extends CreateRecord } $invalidCodes = $matchedItems - ->filter(fn ($sticker) => !empty($sticker->material_type)) //filter invalid - ->pluck('item.code') - ->toArray(); + ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid + ->pluck('item.code') + ->toArray(); - if (!empty($invalidCodes)) - { + if (! empty($invalidCodes)) { $missingCount = count($invalidCodes); - $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
" . implode(', ', $invalidCodes); + $message = $missingCount > 10 ? "'$missingCount' Material Invoice item codes found." : "'Material Invoice' item codes found:
".implode(', ', $invalidCodes); Notification::make() ->title('Invalid: Item Codes') @@ -2297,28 +4518,22 @@ class CreateSerialValidation extends CreateRecord ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } return; } + $missPackCodes = $matchedItems + ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) + ->pluck('item.code') + ->toArray(); - // $missPackCodes = $matchedItems - // ->filter(fn ($sticker) => empty($sticker->tube_sticker_motor) && empty($sticker->pack_slip_motor) && empty($sticker->tube_sticker_pump) && empty($sticker->pack_slip_pump) && empty($sticker->tube_sticker_pumpset) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) - // ->pluck('item.code') - // ->toArray(); - - $missPackCodes = $matchedItems - ->filter(fn ($sticker) => empty($sticker->pack_slip_motor) && empty($sticker->pack_slip_pump) && empty($sticker->pack_slip_pumpset) && empty($sticker->panel_box_code)) - ->pluck('item.code') - ->toArray(); - - if (!empty($missPackCodes)) - { + if (! empty($missPackCodes)) { $missingCount = count($missPackCodes); - $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
" . implode(', ', $missPackCodes); + $message = $missingCount > 10 ? "'$missingCount' item codes doesn't have valid package type to proceed!" : "The following 'Item Code' doesn't have valid package type:
".implode(', ', $missPackCodes); Notification::make() ->title('Invalid: Item Codes') @@ -2328,6 +4543,7 @@ class CreateSerialValidation extends CreateRecord ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2337,15 +4553,16 @@ class CreateSerialValidation extends CreateRecord $existingSerialNumbers = SerialValidation::whereIn('serial_number', $serialNumbers)->where('plant_id', $plantId)->pluck('serial_number')->toArray(); // If there are duplicates, notify and stop the process - if (!empty($existingSerialNumbers)) { + if (! empty($existingSerialNumbers)) { Notification::make() ->title('Duplicate Serial Numbers') - ->body('The following serial numbers are already exist in database:
' . implode(', ', $existingSerialNumbers)) + ->body('The following serial numbers are already exist in database:
'.implode(', ', $existingSerialNumbers)) ->danger() ->seconds(3) ->send(); $this->dispatch('playWarnSound'); + // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } @@ -2353,9 +4570,10 @@ class CreateSerialValidation extends CreateRecord } $inserted = 0; - foreach ($rows as $index => $row) - { - if ($index == 0) continue; + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } $materialCode = trim($row[0]); $serialNumber = trim($row[1]); @@ -2365,8 +4583,8 @@ class CreateSerialValidation extends CreateRecord } $sticker = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) use ($materialCode) { - $query->where('plant_id', $this->plantId)->where('code', $materialCode); //Check if item.code matches Excel's materialCode - })->first(); + $query->where('plant_id', $this->plantId)->where('code', $materialCode); // Check if item.code matches Excel's materialCode + })->first(); if ($sticker) { SerialValidation::create([ @@ -2374,22 +4592,22 @@ class CreateSerialValidation extends CreateRecord 'plant_id' => $plantId, 'invoice_number' => $invoiceNumber, 'serial_number' => $serialNumber, - 'operator_id'=> $operatorName, + 'operator_id' => $operatorName, + 'created_by' => $operatorName, + 'updated_by' => $operatorName, ]); $inserted++; } } - if ($inserted > 0) - { + if ($inserted > 0) { Notification::make() - ->title("Start the scanning process!") + ->title('Start the scanning process!') ->body("'$inserted' serial invoice records were inserted.") ->info() // ->success() ->seconds(1) ->send(); - $this->dispatch('playNotificationSound'); // Update total quantity in the form @@ -2400,37 +4618,31 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); - if ($totalQuantity == $scannedQuantity) - { + if ($totalQuantity == $scannedQuantity) { if ($disk->exists($filePath)) { $disk->delete($filePath); } $this->dispatch('refreshCompletedInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId, isSerial: true); - } - else - { + } else { // if ($disk->exists($filePath)) { // $disk->delete($filePath); // } // $hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; - // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); //$this->invoiceNumber - $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); //$this->invoiceNumber + $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId, onCapFocus: false); } - } - else - { + } else { Notification::make() - ->title("Import Failed: Serial Invoice") + ->title('Import Failed: Serial Invoice') ->body("No new records were inserted for Serial Invoice : '$invoiceNumber'.") ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); $totalQuantity = SerialValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); @@ -2440,7 +4652,7 @@ class CreateSerialValidation extends CreateRecord 'invoice_number' => $invoiceNumber, 'serial_number' => null, 'total_quantity' => $totalQuantity, - 'scanned_quantity'=> $scannedQuantity, + 'scanned_quantity' => $scannedQuantity, ]); // if ($disk->exists($filePath)) { @@ -2448,18 +4660,17 @@ class CreateSerialValidation extends CreateRecord // } $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); + return; } - } - else - { + } else { Notification::make() ->title('Invoice Type Not Found') ->body("Import the valid 'Invoice' file to proceed..!") ->danger() ->seconds(3) ->send(); - $this->dispatch('playWarnSound'); + $this->dispatch('playWarnSound'); // if ($disk->exists($filePath)) { // $disk->delete($filePath);