diff --git a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php index f6c9577a5..4bfc814c0 100644 --- a/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php +++ b/app/Filament/Resources/InvoiceValidationResource/Pages/CreateInvoiceValidation.php @@ -3,18 +3,26 @@ namespace App\Filament\Resources\InvoiceValidationResource\Pages; use App\Filament\Resources\InvoiceValidationResource; +use App\Filament\Widgets\ItemOverview; +use App\Imports\ExcelImport; use App\Livewire\InvoiceDataTable; use App\Models\InvoiceValidation; +use App\Models\Item; use App\Models\StickerMaster; use Filament\Facades\Filament; +use Filament\Pages\Concerns\ExposesTableToWidgets; use Illuminate\Contracts\View\View; use Filament\Resources\Pages\CreateRecord; use Filament\Notifications\Notification; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Collection; use Maatwebsite\Excel\Facades\Excel; use Livewire\Livewire; +use Str; class CreateInvoiceValidation extends CreateRecord { + // use ExposesTableToWidgets; protected static string $resource = InvoiceValidationResource::class; protected static string $view = 'filament.resources.invoice-validation-resource.pages.create-invoice-validation'; @@ -29,234 +37,1852 @@ class CreateInvoiceValidation extends CreateRecord public $total_quantity; - public $invoiceNumber; + public string $currentItemCode = ''; + + public string $currentSerialNumber = ''; + + public $plantId; + + public $invoiceNumber; + public bool $hasSearched = false; + public bool $showCapacitorInput = false; public $excel_file; public function getFormActions(): array { - return parent::getFormActions(); + // return parent::getFormActions(); //return []; + return [ + $this->getCancelFormAction(), + ]; } public function processInvoice($invoiceNumber) { + $this->showCapacitorInput = false; + $user = Filament::auth()->user(); $operatorName = $user->name; $plantId = $this->form->getState()['plant_id']; - $filePath = session('uploaded_invoice_path'); + $this->plantId = $plantId; - if (!file_exists($filePath)) + $updateStatus = $this->form->getState()['update_invoice'] ?? null; + + $this->invoiceNumber = $this->form->getState()['invoice_number'] ?? $invoiceNumber; + + $invoiceType = null; + //$this->invoiceNumber = $this->invoiceNumber ?? $invoiceNumber; + + $invoiceExist = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->exists(); + + if (!$invoiceExist) { + Notification::make() + ->title("New invoice detected.") + ->info() + ->send(); + + $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 = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scanSQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + $totMQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); //->where('quantity', '!=', '') + $scanMQuan = InvoiceValidation::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() + ->send(); + + $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); + } + else + { + Notification::make() + ->title("Start the scanning process!") + ->info() + ->send(); + // $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') + { + dd('Material invoice update in progress...'); + } + } + 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() + ->send(); + + $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); + } + else + { + Notification::make() + ->title("Start the scanning process!") + ->info() + ->send(); + $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + + if($updateStatus === '1') + { + dd('Serial invoice update in progress...'); + } + } + 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() + // ->send(); + } else { + Notification::make() + ->title('Invoice Not Found') + ->body("Import the scanned 'Invoice' file to proceed..!") + ->danger() + ->send(); + + $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('Excel File not found.') + ->title('Invoice file doesn\'t exist.') ->danger() ->send(); return; } // Extract filename without extension (e.g., "3RA0018732") - $uploadedFilename = pathinfo($filePath, PATHINFO_FILENAME); + $uploadedFilename = pathinfo($fullPath, PATHINFO_FILENAME); // Compare with invoice number if ($uploadedFilename !== $invoiceNumber) { Notification::make() ->title("Uploaded file name does not match the invoice number.") - // ->body("Expected: {$invoiceNumber}.xlsx, but got: {$uploadedFilename}.xlsx") ->danger() ->send(); return; } - if ($filePath && file_exists($filePath)) + if ($fullPath && file_exists($fullPath)) { // Now you can read/process the file here - $rows = Excel::toArray(null, $filePath)[0]; //0 means excel first sheet + $rows = Excel::toArray(null, $fullPath)[0]; + // $collection = Excel::toCollection(null, $fullPath); + // $rows = $collection[0]->toArray(); - $materialCodes = []; - $serialNumbers = []; + // $collection = Excel::toCollection(new ExcelImport, $fullPath); + // // Convert the collection to an array + // $rows = $collection->toArray(); - foreach ($rows as $index => $row) + // $excelImport = new ExcelImport(); + // // Import the Excel file + // Excel::import($excelImport, $fullPath); + // // Get the rows + // $rows = $excelImport->getRows(); + + if((count($rows) - 1) <= 0) { - if ($index === 0) continue; // Skip header - - $materialCode = trim($row[0]); - $serialNumber = trim($row[1]); - - if (!empty($materialCode)) { - $materialCodes[] = $materialCode; - } - if (!empty($serialNumber)) { - $serialNumbers[] = $serialNumber; - } - } - - $existingSerialNumbers = InvoiceValidation::whereIn('serial_number', $serialNumbers)->pluck('serial_number')->toArray(); - - // If there are duplicates, notify and stop the process - if (!empty($existingSerialNumbers)) { Notification::make() - ->title('Duplicate Serial Numbers Found') - ->body('The following serial numbers already exist: ' . implode(', ', $existingSerialNumbers)) + ->title('Records Not Found') + ->body("Import the valid 'Invoice' file to proceed..!") ->danger() ->send(); - return; + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; } - $uniqueCodes = array_unique($materialCodes); - - $existingCodes = StickerMaster::with('item') - ->get() - ->pluck('item.code') - ->toArray(); - - $missingCodes = array_diff($uniqueCodes, $existingCodes); - - if (!empty($missingCodes)) { - Notification::make() - ->title('Material codes do not exist in Sticker Master') - ->body('Missing: ' . implode(', ', $missingCodes)) - ->danger() - ->send(); - return; - } - - $inserted = 0; foreach ($rows as $index => $row) { if ($index === 0) continue; $materialCode = trim($row[0]); - $serialNumber = trim($row[1]); - if (in_array($serialNumber, $existingSerialNumbers)) { - continue; // here duplicate serial numbers are skipped and new serial numbers are inserted + 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; + } + } } - - $sticker = StickerMaster::whereHas('item', function ($query) use ($materialCode) { - $query->where('code', $materialCode); //Check if item.code matches Excel's materialCode - })->first(); - - if ($sticker) { - InvoiceValidation::create([ - 'sticker_master_id' => $sticker->id, - 'serial_number' => $serialNumber, - 'plant_id' => $plantId, - 'invoice_number' => $invoiceNumber, - 'operator_id'=> $operatorName, - ]); - $inserted++; + else + { + continue; } } - if ($inserted > 0) + if($invoiceType === 'M') { - Notification::make() - ->title("Import Successful") - ->body("$inserted records were inserted.") - ->success() + $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; + } + } + + 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.') + ->send(); + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $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() ->send(); - $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber); + 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() + ->send(); + + 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() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $uniqueCodes = array_unique($materialCodes); + + $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() + ->send(); + + 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() + ->send(); + + 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 ($excelCode === $code && is_numeric($excelMatQty)) { + $totalExcelQty += $excelMatQty; // Sum up the quantities + } + } + + if ($totalExcelQty === 0) { + $zeroQtyCodes[] = $code; + } elseif (!is_numeric($totalExcelQty)) { + $nonNumericQtyCodes[] = $code; // Here you may want to check divisibility condition too + } elseif ($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() + ->send(); + }; + + $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 ($excelCode === $code && is_numeric($excelMatQty)) { + $totalExcelQty += $excelMatQty; // Sum up the quantities + } + } + + for ($i = 0; $i < $totalExcelQty; $i++) + { + if ($sticker) { + InvoiceValidation::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 ($excelCode === $code && is_numeric($excelMatQty)) { + $totalExcelQty += $excelMatQty; // Sum up the quantities + } + } + + for ($i = 0; $i < ($totalExcelQty/$bundleQty); $i++) + { + if ($sticker) { + InvoiceValidation::create([ + 'sticker_master_id' => $sticker->id, + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'quantity' => $bundleQty, + 'operator_id'=> $operatorName, + ]); + $inserted++; + } + } + } + } + + if ($inserted > 0) + { + Notification::make() + ->title("Start the scanning process!") + ->body("'$inserted' material invoice records were inserted.") + ->info() + // ->success() + ->send(); + + // Update total quantity in the form + $totalQuantity = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scannedQuantity = InvoiceValidation::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); + } + 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() + ->send(); + + $totalQuantity = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scannedQuantity = InvoiceValidation::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; + } + } + + 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.') + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $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() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $uniqueCodes = array_unique($materialCodes); + + $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() + ->send(); + + 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() + ->send(); + + 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() + ->send(); + + 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() + ->send(); + + 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() + ->send(); + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; + } + + $existingSerialNumbers = InvoiceValidation::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() + ->send(); + + 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) { + InvoiceValidation::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() + ->send(); + + // Update total quantity in the form + $totalQuantity = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scannedQuantity = InvoiceValidation::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); + } + 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() + ->send(); + + $totalQuantity = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scannedQuantity = InvoiceValidation::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("Import Failed") - ->body("No new records were inserted.") + ->title('Invoice Type Not Found') + ->body("Import the valid 'Invoice' file to proceed..!") ->danger() ->send(); - return; + + if ($disk->exists($filePath)) { + $disk->delete($filePath); + } + return; } } - } + } protected function refreshInvoiceTable() { - if (empty($this->invoiceNumber)) { - $this->invoiceNumber = $this->form->getState()['invoice_number'] ?? ''; - } + $this->plantId = $this->form->getState()['plant_id'] ?? ''; + $this->invoiceNumber = $this->form->getState()['invoice_number'] ?? ''; if (!empty($this->invoiceNumber)) { - $this->dispatch('refreshInvoiceData', invoiceNumber: $this->invoiceNumber); + $hasInvoice = InvoiceValidation::where('invoice_number', $this->invoiceNumber)->where('plant_id', $this->plantId)->first(); + + if (empty($hasInvoice) || !$hasInvoice) + { + $this->dispatch('refreshEmptyInvoice', invoiceNumber: $this->invoiceNumber, plantId: $this->plantId); + } + else + { + $totalQuantity = InvoiceValidation::where('invoice_number', $this->invoiceNumber)->where('plant_id', $this->plantId)->count(); + $scannedQuantity = InvoiceValidation::where('invoice_number', $this->invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $this->plantId)->count(); + if ($totalQuantity === $scannedQuantity) + { + $this->dispatch('refreshCompletedInvoice', invoiceNumber: $this->invoiceNumber, plantId: $this->plantId); + } + else + { + $hasRecords = $hasInvoice->stickerMasterRelation->material_type ?? null; + $this->dispatch( (!empty($hasRecords) && $hasRecords) ? 'refreshMaterialInvoiceData' : 'refreshInvoiceData', invoiceNumber: $this->invoiceNumber, plantId: $this->plantId); + } + } } } public function processSerialNumber($serialNumber) { + $plantId = $this->form->getState()['plant_id']; + $this->plantId = $plantId; + $invoiceNumber = $this->form->getState()['invoice_number']; + $this->invoiceNumber = $invoiceNumber; - if (!preg_match('/^([a-zA-Z0-9]{6,})\|([a-zA-Z0-9]{8,})(?:\/[MmPpCc])?$/', $serialNumber, $matches)) { + $totQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->count(); + $scanSQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + $totMQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->whereNotNull('quantity')->where('plant_id', $plantId)->count(); //->where('quantity', '!=', '') + $scanMQuan = InvoiceValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + + if($totQuan <= 0) + { Notification::make() + ->title('Invoice Not Found') + ->body("Invoice file '$invoiceNumber' doesn't exist.
Scan the valid 'Invoice' file to proceed!") ->danger() - ->title('Invalid format') - ->body('Please enter serial in correct format: ITEM123|123456789/M') ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => 0, //$totQuan + 'scanned_quantity'=> 0, //($totMQuan > 0) ? $scanMQuan : $scanSQuan + ]); + $this->dispatch('refreshEmptyInvoice', invoiceNumber: $invoiceNumber, plantId: $plantId); return; } - - if (preg_match('/^([a-zA-Z0-9]+)\|([a-zA-Z0-9]+(?:\/[MmPpCc]?)?)$/', $serialNumber, $matches)) + else { + if ($totMQuan > 0) + { + 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() + ->send(); - $itemCode = $matches[1]; - $serialNumber = $matches[2]; + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); - // Check if it ends with /M, /P, /C etc. - $isMarkM = preg_match('/\/[Mm]$/', $serialNumber); - $isMarkP = preg_match('/\/[Pp]$/', $serialNumber); + $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); + return; + } + else + { + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + //$hasRecords = InvoiceValidation::where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first()->stickerMasterRelation->material_type ?? null; + $this->dispatch( 'refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + } - $serialNumber = preg_replace('/\/[MmPpCc]$/', '', $serialNumber); + $pattern1 = '/^(?[^|]+)\|(?[^|]+)\|(?[^|]+)\|(?.+)$/i'; + $pattern2 = '/^(?[^|]+)\|(?[^|]+)-(?.+)$/i'; + $itemCode = ''; + $batchNumber = ''; - $record = InvoiceValidation::where('serial_number', $serialNumber) - ->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) { - $query->where('code', $itemCode); - }) - ->first(); + if (preg_match($pattern1, $serialNumber, $matches)) { + $itemCode = $matches['item_code']; + $this->currentItemCode = $itemCode; + $batchNumber = $matches['batch_number']; + $serialNumber = $matches['batch_id'] . '-' . $matches['batch_count']; - if (!$record) { - Notification::make() + if(empty($matches['batch_id']) || !$matches['batch_id']) + { + Notification::make() + ->danger() + ->title('Invalid Material QR Format') + ->body('Scan valid Material QR code proceed!
Sample formats are:
123456|12345|12345678|1 (or)
123456|12345678-1') + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + elseif (empty($matches['batch_count']) || !$matches['batch_count']) + { + Notification::make() + ->danger() + ->title('Invalid Material QR Format') + ->body('Scan valid Material QR code proceed!
Sample formats are:
123456|12345|12345678|1 (or)
123456|12345678-1') + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + } elseif (preg_match($pattern2, $serialNumber, $matches)) { + $itemCode = $matches['item_code']; + $this->currentItemCode = $itemCode; + $batchNumber = null; // batch_number not present in this pattern + $serialNumber = $matches['batch_id'] . '-' . $matches['batch_count']; + + if(empty($matches['batch_id']) || !$matches['batch_id']) + { + Notification::make() + ->danger() + ->title('Invalid Material QR Format') + ->body('Scan valid Material QR code proceed!
Sample formats are:
123456|12345|12345678|1 (or)
123456|12345678-1') + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + elseif (empty($matches['batch_count']) || !$matches['batch_count']) + { + Notification::make() + ->danger() + ->title('Invalid Material QR Format') + ->body('Scan valid Material QR code proceed!
Sample formats are:
123456|12345|12345678|1 (or)
123456|12345678-1') + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + } else { + $itemCode = null; + $batchNumber = null; + $serialNumber = null; + + Notification::make() + ->danger() + ->title('Invalid Material QR Format') + ->body('Scan valid Material QR code proceed!
Sample formats are:
123456|12345|12345678|1 (or)
123456|12345678-1') + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + // $record = StickerMaster::where('plant_id', $plantId)->with('item')->where('item.code', $itemCode); + $record = StickerMaster::where('plant_id', $plantId)->whereHas('item', function ($query) { + $query->where('code', $this->currentItemCode); + }); + if ($record->count() <= 0) + { + Notification::make() + ->title('Unknown: Item Code') + ->body("Item code '$itemCode' not found in database.") ->danger() - ->title('Serial not found') - ->body("Item code '$itemCode' with serial '$serialNumber' not found.") ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + $record = StickerMaster::where('plant_id', $plantId)->where('item_id', Item::where('plant_id', $plantId)->where('code', $itemCode)->first()->id)->first(); + + if (empty($record->material_type) || !$record->material_type) + { + Notification::make() + ->title('Invalid: Item Code') + ->body("Item code '$itemCode' doesn't have a valid material type.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + $record = InvoiceValidation::where('invoice_number', $invoiceNumber) + ->where('plant_id', $plantId) + ->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) { + $query->where('plant_id', $this->plantId)->where('code', $itemCode); + }) + ->first(); + if (!$record) { + Notification::make() + ->title('Unknown: Item Code') + ->body("Item code '$itemCode' doesn't exist in invoice.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + //->where('serial_number', '!=', '') + $record = InvoiceValidation::where('invoice_number', $invoiceNumber) + ->where('serial_number', null) + ->where('plant_id', $plantId) + ->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) { + $query->where('plant_id', $this->plantId)->where('code', $itemCode); + }) + ->first(); + if (!$record) { + Notification::make() + ->title('Item Code Limit Exceeds') + ->body("Scanned item code '$itemCode' already reached its scanning quantity for the invoice '$invoiceNumber'.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + $dupRecord = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first(); + if ($dupRecord) + { + Notification::make() + ->title('Duplicate: Material QR') + ->body("Scanned 'Material QR' already completed the scanning process.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanMQuan, + ]); + return; + } + + $record->serial_number = $serialNumber; + if($batchNumber && !empty($batchNumber)) + { + $record->batch_number = $batchNumber; + } + $record->save(); + + Notification::make() + ->title('Success: Material QR') + ->body("'Material QR' scanned status updated, Scan next QR.") + ->success() + ->send(); + + $scannedMQuantity = InvoiceValidation::where('invoice_number', $invoiceNumber)->whereNotNull('serial_number')->where('serial_number', '!=', '')->where('plant_id', $plantId)->count(); + + if($totQuan === $scannedMQuantity) + { + Notification::make() + ->title('Completed: Material Invoice') + ->body("Material invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Material Invoice' to proceed!") + ->success() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scannedMQuantity, + ]); + + $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); + } + else + { + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scannedMQuantity, + ]); + $this->dispatch( 'refreshMaterialInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + } return; } + else + { + 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() + ->send(); - if ($isMarkM) { - $record->motor_scanned_status = 1; - $record->save(); + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); - Notification::make() - ->success() - ->title('Updated') - ->body("Motor scanned status marked as updated.") - ->send(); - $this->refreshInvoiceTable(); + $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); + 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); + } - } - else if ($isMarkP) { - $record->pump_scanned_status = 1; - $record->save(); + if (!preg_match('/^([a-zA-Z0-9]{6,})\|([a-zA-Z0-9]{8,})(?:\/[MmPpCc])?$/', $serialNumber, $matches)) { + Notification::make() + ->danger() + ->title('Invalid Serial QR Format') + ->body('Scan valid Serial QR code proceed!
Sample formats are:
123456|1234567890123/M (or)
123456|1234567890123/P (or)
123456|1234567890123/C (or)
123456|1234567890123') + ->send(); - Notification::make() - ->success() - ->title('Updated') - ->body("Pump scanned status marked as updated.") - ->send(); - $this->refreshInvoiceTable(); + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + if (preg_match('/^([a-zA-Z0-9]+)\|([a-zA-Z0-9]+(?:\/[MmPpCc]?)?)$/', $serialNumber, $matches)) + { + $itemCode = $matches[1]; + $serialNumber = $matches[2]; + + // Check if it ends with /M, /P, /C etc. + $isMarkM = preg_match('/\/[Mm]$/', $serialNumber); + $isMarkP = preg_match('/\/[Pp]$/', $serialNumber); + $isMarkC = preg_match('/\/[Cc]$/', $serialNumber); + $isMarkPs = (!$isMarkM && !$isMarkP && !$isMarkC) ? true : false; + + $serialNumber = preg_replace('/\/[MmPpCc]$/', '', $serialNumber); + + $record = InvoiceValidation::where('serial_number', $serialNumber)->where('plant_id', $plantId)->first(); + + if (!$record) { + Notification::make() + ->title('Serial Number Not Found') + ->body("Serial '$serialNumber' not found in database.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $record = InvoiceValidation::where('serial_number', $serialNumber)->where('invoice_number', $invoiceNumber)->where('plant_id', $plantId)->first(); + //$invoiceNumber = $this->form->getState()['invoice_number']; + + if (!$record) { + Notification::make() + ->title('Unknown: Serial Number') + ->body("Serial '$serialNumber' not found in invoice.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $record = InvoiceValidation::where('serial_number', $serialNumber) + ->where('plant_id', $plantId) + ->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) { + $query->where('plant_id', $this->plantId)->where('code', $itemCode); + }) + ->first(); + + if (!$record) { + Notification::make() + ->title('Unknown: Item Code') + ->body("Item code '$itemCode' with serial number '$serialNumber' not found.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $hasMotorQr = $record->stickerMasterRelation->tube_sticker_motor ?? null; + $hasPumpQr = $record->stickerMasterRelation->tube_sticker_pump ?? null; + $hasPumpSetQr = $record->stickerMasterRelation->tube_sticker_pumpset ?? null; + $hasCapacitorQr = $record->stickerMasterRelation->panel_box_code ?? null; + + $hadMotorQr = $record->motor_scanned_status ?? null; + $hadPumpQr = $record->pump_scanned_status ?? null; + $hadPumpSetQr = $record->scanned_status_set ?? null; + $hadCapacitorQr = $record->capacitor_scanned_status ?? null; + + if ($isMarkM) { + if (!$hasMotorQr) + { + Notification::make() + ->title('Unknown: Motor QR') + ->body("Scanned 'Item Code' doesn't have 'Motor' QR to proceed!") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + else if($hadMotorQr === $hasMotorQr) + { + Notification::make() + ->title('Duplicate: Motor QR') + ->body("Scanned 'Motor' serial number already completed the scanning process.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $packCnt = 1; + $scanCnt = 1; + $record->motor_scanned_status = 1; + //if($hadPumpQr === $hasPumpQr && $hadPumpSetQr === $hasPumpSetQr) + if($hasPumpQr || $hasPumpSetQr || $hasCapacitorQr) + { + $packCnt = $hasPumpQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasPumpSetQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasCapacitorQr ? $packCnt + 1 : $packCnt; + + $scanCnt = $hadPumpQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadPumpSetQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadCapacitorQr ? $scanCnt + 1: $scanCnt; + + if($packCnt === $scanCnt) + { + $record->scanned_status = 'Scanned'; + } + } + else + { + $record->scanned_status = 'Scanned'; + } + $record->save(); + + Notification::make() + ->title('Success: Motor QR') + ->body("'Motor' QR scanned status updated, Scan next QR.") + ->success() + ->send(); + + $scannedQuantity = InvoiceValidation::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' => $totQuan, + 'scanned_quantity'=> $scannedQuantity, + ]); + + if($totQuan === $scannedQuantity) + { + Notification::make() + ->title('Completed: Serial Invoice') + ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed!") + ->success() + ->send(); + + $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); + } + else + { + $this->dispatch( 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $this->refreshInvoiceTable(); + } + return; + } + else if ($isMarkP) { + if (!$hasPumpQr) + { + Notification::make() + ->title('Unknown: Pump QR') + ->body("Scanned 'Item Code' doesn't have 'Pump' QR to proceed!") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + else if($hadPumpQr === $hasPumpQr) + { + Notification::make() + ->title('Duplicate: Pump QR') + ->body("Scanned 'Pump' serial number already completed the scanning process.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $packCnt = 1; + $scanCnt = 1; + $record->pump_scanned_status = 1; + // if($hadMotorQr === $hasMotorQr && $hadPumpSetQr === $hasPumpSetQr && ($hadCapacitorQr === '1' && $hasCapacitorQr)) + if($hasMotorQr || $hasPumpSetQr || $hasCapacitorQr) + { + $packCnt = $hasMotorQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasPumpSetQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasCapacitorQr ? $packCnt + 1 : $packCnt; + + $scanCnt = $hadMotorQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadPumpSetQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadCapacitorQr ? $scanCnt + 1: $scanCnt; + + if($packCnt === $scanCnt) + { + $record->scanned_status = 'Scanned'; + } + } + else + { + $record->scanned_status = 'Scanned'; + } + $record->save(); + + Notification::make() + ->title('Success: Pump QR') + ->body("'Pump' QR scanned status updated, Scan next QR.") + ->success() + ->send(); + + $scannedQuantity = InvoiceValidation::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' => $totQuan, + 'scanned_quantity'=> $scannedQuantity, + ]); + + if($totQuan === $scannedQuantity) + { + Notification::make() + ->title('Completed: Serial Invoice') + ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed!") + ->success() + ->send(); + + $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); + } + else + { + $this->dispatch( 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $this->refreshInvoiceTable(); + } + return; + } + elseif ($isMarkC) { + if (!$hasCapacitorQr) + { + Notification::make() + // ->title('Unknown: Capacitor QR') + ->title('Missing: Panel Box Code') + // ->body("Panel Box Code is not available for Item Code : '$itemCode'.") + ->body("Scanned 'Item Code' doesn't have 'Panel Box Code' to proceed!") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + else if($hadCapacitorQr === '1' && $hasCapacitorQr) + { + Notification::make() + ->title('Duplicate: Capacitor QR') + ->body("Scanned 'Capacitor' serial number already completed the scanning process.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + //$this->dispatch('openCapacitorModal'); + $this->dispatch('openCapacitorModal', itemCode: $itemCode, serialNumber: $serialNumber, plantId: $plantId); + + $scannedQuantity = InvoiceValidation::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' => $totQuan, + 'scanned_quantity'=> $scannedQuantity, + ]); + $this->dispatch( 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $this->refreshInvoiceTable(); + return; + } + elseif ($isMarkPs) + { + if (!$hasPumpSetQr) + { + Notification::make() + ->title('Unknown: Pump Set QR') + ->body("Scanned 'Item Code' doesn't have 'Pump Set' QR to proceed!") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + else if($hadPumpSetQr === $hasPumpSetQr) + { + Notification::make() + ->title('Duplicate: Pump Set QR') + ->body("Scanned 'Pump Set' serial number already completed the scanning process.") + ->danger() + ->send(); + + $this->form->fill([ + 'plant_id' => $plantId, + 'invoice_number' => $invoiceNumber, + 'serial_number' => null, + 'total_quantity' => $totQuan, + 'scanned_quantity'=> $scanSQuan, + ]); + return; + } + + $packCnt = 1; + $scanCnt = 1; + $record->scanned_status_set = 1; + // if($hadMotorQr === $hasMotorQr && $hadPumpQr === $hasPumpQr && ($hadCapacitorQr === '1' && $hasCapacitorQr)) + if($hasMotorQr || $hasPumpQr || $hasCapacitorQr) + { + $packCnt = $hasMotorQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasPumpQr ? $packCnt + 1 : $packCnt; + $packCnt = $hasCapacitorQr ? $packCnt + 1 : $packCnt; + + $scanCnt = $hadMotorQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadPumpQr ? $scanCnt + 1: $scanCnt; + $scanCnt = $hadCapacitorQr ? $scanCnt + 1: $scanCnt; + + if($packCnt === $scanCnt) + { + $record->scanned_status = 'Scanned'; + } + } + else + { + $record->scanned_status = 'Scanned'; + } + $record->save(); + + Notification::make() + ->title('Success: Pump Set QR') + ->body("'Pump Set' QR scanned status updated, Scan next QR.") + ->success() + ->send(); + + $scannedQuantity = InvoiceValidation::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' => $totQuan, + 'scanned_quantity'=> $scannedQuantity, + ]); + + if($totQuan === $scannedQuantity) + { + Notification::make() + ->title('Completed: Serial Invoice') + ->body("Serial invoice '$invoiceNumber' completed the scanning process.
Scan the next 'Serial Invoice' to proceed!") + ->success() + ->send(); + + $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); + } + else + { + $this->dispatch( 'refreshInvoiceData', invoiceNumber: $invoiceNumber, plantId: $plantId); + // $this->refreshInvoiceTable(); + } + return; + } + } } } - } public function getHeading(): string { return 'Scan Invoice Validation'; } - - }