user(); $operatorName = $user->name; $plantId = $this->form->getState()['plant_id']; $filePath = session('uploaded_invoice_path'); if (!file_exists($filePath)) { Notification::make() ->title('Excel File not found.') ->danger() ->send(); return; } // Extract filename without extension (e.g., "3RA0018732") $uploadedFilename = pathinfo($filePath, 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)) { // Now you can read/process the file here $rows = Excel::toArray(null, $filePath)[0]; //0 means excel first sheet $materialCodes = []; $serialNumbers = []; foreach ($rows as $index => $row) { 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)) ->danger() ->send(); 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 } $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++; } } if ($inserted > 0) { Notification::make() ->title("Import Successful") ->body("$inserted records were inserted.") ->success() ->send(); $this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber); } else { Notification::make() ->title("Import Failed") ->body("No new records were inserted.") ->danger() ->send(); return; } } } protected function refreshInvoiceTable() { if (empty($this->invoiceNumber)) { $this->invoiceNumber = $this->form->getState()['invoice_number'] ?? ''; } if (!empty($this->invoiceNumber)) { $this->dispatch('refreshInvoiceData', invoiceNumber: $this->invoiceNumber); } } public function processSerialNumber($serialNumber) { if (!preg_match('/^([a-zA-Z0-9]{6,})\|([a-zA-Z0-9]{8,})(?:\/[MmPpCc])?$/', $serialNumber, $matches)) { Notification::make() ->danger() ->title('Invalid format') ->body('Please enter serial in correct format: ITEM123|123456789/M') ->send(); 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); $serialNumber = preg_replace('/\/[MmPpCc]$/', '', $serialNumber); $record = InvoiceValidation::where('serial_number', $serialNumber) ->whereHas('stickerMasterRelation.item', function ($query) use ($itemCode) { $query->where('code', $itemCode); }) ->first(); if (!$record) { Notification::make() ->danger() ->title('Serial not found') ->body("Item code '$itemCode' with serial '$serialNumber' not found.") ->send(); return; } if ($isMarkM) { $record->motor_scanned_status = 1; $record->save(); Notification::make() ->success() ->title('Updated') ->body("Motor scanned status marked as updated.") ->send(); $this->refreshInvoiceTable(); } else if ($isMarkP) { $record->pump_scanned_status = 1; $record->save(); Notification::make() ->success() ->title('Updated') ->body("Pump scanned status marked as updated.") ->send(); $this->refreshInvoiceTable(); } } } public function getHeading(): string { return 'Scan Invoice Validation'; } }