Added duplicate document number logic in invoice out validation screen
This commit is contained in:
@@ -184,22 +184,23 @@ class InvoiceOutValidationResource extends Resource
|
||||
$invalidScannedBy = [];
|
||||
$invalidUser = [];
|
||||
$userNotFound = [];
|
||||
$seenPlantQr = [];
|
||||
$duplicateQrExcel = [];
|
||||
$duplicateQrDb = [];
|
||||
|
||||
foreach ($rows as $index => $row)
|
||||
{
|
||||
if ($index == 0) continue; // Skip header
|
||||
if ($index == 0) continue;
|
||||
|
||||
$qrCode = trim($row[1]);
|
||||
$plantCode = trim($row[2]);
|
||||
$scannedAt = trim($row[3]);
|
||||
$scannedby = trim($row[4]);
|
||||
//$createdBy = trim($row[4]);
|
||||
|
||||
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
||||
if (empty($qrCode)) $invalidqrCode[] = "Row {$index}";
|
||||
if (empty($scannedAt)) $invalidScannedAt[] = "Row {$index}";
|
||||
if (empty($scannedby)) $invalidScannedBy[] = "Row {$index}";
|
||||
//if (empty($createdBy)) $invalidUser[] = "Row {$index}";
|
||||
|
||||
if (strlen($plantCode) < 4) {
|
||||
$invalidPlantCode[] = $plantCode;
|
||||
@@ -208,11 +209,26 @@ class InvoiceOutValidationResource extends Resource
|
||||
{
|
||||
$invalidPlaCoFound[] = $plantCode;
|
||||
}
|
||||
// else if(!User::where('name', $createdBy)->first())
|
||||
// {
|
||||
// $userNotFound[] = $createdBy;
|
||||
// }
|
||||
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
|
||||
$plantId = $plant->id;
|
||||
|
||||
$uniqueKey = $plantCode . '_' . $qrCode;
|
||||
|
||||
if (in_array($uniqueKey, $seenPlantQr)) {
|
||||
$duplicateQrExcel[] = "Duplicate in file at Row {$index}: QR Code '{$qrCode}' already exists for Plant Code {$plantCode}";
|
||||
}
|
||||
|
||||
$seenPlantQr[] = $uniqueKey;
|
||||
|
||||
$existsInDb = InvoiceOutValidation::where('plant_id', $plantId)
|
||||
->where('qr_code', $qrCode)
|
||||
->first();
|
||||
|
||||
if ($existsInDb) {
|
||||
$duplicateQrDb[] = "QR Code '{$qrCode}' already exists in DB for Plant Code {$plantCode}";
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($invalidqrCode) || !empty($invalidScannedAt) || !empty($invalidScannedBy) || !empty($invalidUser))
|
||||
@@ -222,7 +238,6 @@ class InvoiceOutValidationResource extends Resource
|
||||
if (!empty($invalidqrCode)) $errorMsg .= 'Missing Qr code in rows: '.implode(', ', $invalidqrCode) . '<br>';
|
||||
if (!empty($invalidScannedAt)) $errorMsg .= 'Missing Scanned At in rows: '.implode(', ', $invalidScannedAt) . '<br>';
|
||||
if (!empty($invalidScannedBy)) $errorMsg .= 'Missing Scanned By in rows: '.implode(', ', $invalidScannedBy) . '<br>';
|
||||
//if (!empty($invalidUser)) $errorMsg .= 'Missing User in rows: '.implode(', ', $invalidUser) . '<br>';
|
||||
|
||||
Notification::make()
|
||||
->title('Missing Mandatory Fields')
|
||||
@@ -272,83 +287,97 @@ class InvoiceOutValidationResource extends Resource
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!empty($duplicateQrExcel))
|
||||
{
|
||||
$duplicateGroupedByPlantQr = [];
|
||||
|
||||
// $successCount = 0;
|
||||
// $failCount = 0;
|
||||
foreach ($duplicateQrExcel as $message) {
|
||||
if (preg_match("/Document Numbers '([^']+)' already exists for Plant Code (\S+)/", $message, $matches)) {
|
||||
$qrCode = $matches[1];
|
||||
$plantCode = $matches[2];
|
||||
$duplicateGroupedByPlantQr[$plantCode][] = $qrCode;
|
||||
}
|
||||
}
|
||||
|
||||
// $lastErrorMessage = null;
|
||||
$errorMsg = 'Duplicate Document Number found in Uploaded File:<br>';
|
||||
|
||||
// foreach ($rows as $index => $row) {
|
||||
// if ($index == 0) continue;
|
||||
foreach ($duplicateGroupedByPlantQr as $plantCode => $qrCodes) {
|
||||
$uniqueQrCodes = array_unique($qrCodes);
|
||||
$count = count($uniqueQrCodes);
|
||||
|
||||
if ($count > 10) {
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: {$count} Document Numbers already exist in uploaded file<br>";
|
||||
} else {
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: "
|
||||
. implode(', ', $uniqueQrCodes)
|
||||
. " already exist<br>";
|
||||
}
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
//->title('Duplicate Document Number in Uploaded File')
|
||||
->body($errorMsg)
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
if (!empty($duplicateQrDb)) {
|
||||
$duplicateGroupedByPlantDb = [];
|
||||
|
||||
foreach ($duplicateQrDb as $message) {
|
||||
if (preg_match("/Document Numbers '([^']+)' already exists in DB for Plant Code (\S+)/", $message, $matches)) {
|
||||
$qrCode = $matches[1];
|
||||
$plantCode = $matches[2];
|
||||
$duplicateGroupedByPlantDb[$plantCode][] = $qrCode;
|
||||
}
|
||||
}
|
||||
|
||||
$errorMsg = 'Duplicate Document Numbers found in Database:<br>';
|
||||
|
||||
foreach ($duplicateGroupedByPlantDb as $plantCode => $qrCodes) {
|
||||
$uniqueQrCodes = array_unique($qrCodes);
|
||||
$count = count($uniqueQrCodes);
|
||||
|
||||
if ($count > 10) {
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: {$count} Document Numbers already exist in DB<br>";
|
||||
} else {
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: "
|
||||
. implode(', ', $uniqueQrCodes)
|
||||
. " already exist in DB<br>";
|
||||
}
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
// ->title('Duplicate Document Numbers in Database')
|
||||
->body($errorMsg)
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// $qrcode = trim($row[1]);
|
||||
// $plantCode = trim($row[2]);
|
||||
// $scannedAt = trim($row[3]);
|
||||
// $scannedBy = trim($row[4]);
|
||||
|
||||
// try
|
||||
// {
|
||||
// $plant = Plant::where('code', $plantCode)->first();
|
||||
|
||||
// $formattedDate = null;
|
||||
// if (!empty($scannedAt)) {
|
||||
// try {
|
||||
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
||||
// ->format('Y-m-d H:i:s');
|
||||
// } catch (\Exception $e) {
|
||||
// throw new \Exception("Invalid date format: {$scannedAt}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// $inserted = InvoiceOutValidation::create([
|
||||
// 'plant_id' => $plant->id,
|
||||
// 'qr_code' => $qrcode,
|
||||
// 'scanned_at' => $formattedDate,
|
||||
// 'scanned_by' => $scannedBy,
|
||||
// 'created_by' => $operatorName,
|
||||
// ]);
|
||||
|
||||
// if ($inserted) {
|
||||
// $successCount++;
|
||||
// } else {
|
||||
// $failCount++;
|
||||
// }
|
||||
// } catch (\Exception $e) {
|
||||
// //$failCount++;
|
||||
// $lastErrorMessage = $e->getMessage();
|
||||
// }
|
||||
// }
|
||||
// if($successCount > 0)
|
||||
// {
|
||||
// Notification::make()
|
||||
// ->title('Import Success')
|
||||
// ->body("Successfully inserted: {$successCount} records")
|
||||
// ->success()
|
||||
// ->send();
|
||||
// }
|
||||
// else {
|
||||
// $errorText = $lastErrorMessage
|
||||
// ? "Failed to insert any records. Error: {$lastErrorMessage}"
|
||||
// : "Failed to insert any records. Unknown error.";
|
||||
|
||||
// Notification::make()
|
||||
// ->title('Import Failed')
|
||||
// ->body($errorText)
|
||||
// ->danger()
|
||||
// ->send();
|
||||
// }
|
||||
|
||||
$successCount = 0;
|
||||
$failedRecords = [];
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
foreach ($rows as $index => $row) {
|
||||
if ($index == 0) continue; // skip header
|
||||
if ($index == 0) continue;
|
||||
|
||||
$rowNumber = $index + 1; // For Excel-style numbering
|
||||
$rowNumber = $index + 1;
|
||||
|
||||
try {
|
||||
$qrcode = trim($row[1]);
|
||||
@@ -408,7 +437,6 @@ class InvoiceOutValidationResource extends Resource
|
||||
|
||||
DB::commit();
|
||||
|
||||
// ✅ After loop, show result summary
|
||||
if (count($failedRecords) > 0) {
|
||||
$failedSummary = collect($failedRecords)
|
||||
->map(fn($f) => "Row {$f['row']} ({$f['qrcode']}): {$f['error']}")
|
||||
@@ -428,9 +456,10 @@ class InvoiceOutValidationResource extends Resource
|
||||
->send();
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
DB::rollBack();
|
||||
|
||||
Notification::make()
|
||||
->title('Import Failed')
|
||||
->body("No records were inserted. Error: {$e->getMessage()}")
|
||||
|
||||
Reference in New Issue
Block a user