Added logic for checking document number against plant in invoice data validations screen

This commit is contained in:
dhanabalan
2025-11-08 01:12:53 +05:30
parent afb45a2d5b
commit b4a2cf132c

View File

@@ -215,6 +215,8 @@ class InvoiceDataValidationResource extends Resource
$seenPlantDoc = []; $seenPlantDoc = [];
$duplicateEntries = []; $duplicateEntries = [];
$duplicateEntriesExcel = []; $duplicateEntriesExcel = [];
$duplicateGroupedByPlant = [];
$duplicateGroupedByPlantExcel = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row)
{ {
@@ -271,13 +273,13 @@ class InvoiceDataValidationResource extends Resource
if ($exists) if ($exists)
{ {
$duplicateEntries[] = "Duplicate found at Row {$index}: Document {$DocNo} already exists for Plant {$plant->name}"; $duplicateEntries[] = "Duplicate record: Document Number '{$DocNo}' already exists for Plant {$plant->name}";
} }
//Also check duplicates within the same file --- //Also check duplicates within the same file ---
$uniqueKey = $plantCode . '_' . $DocNo; $uniqueKey = $plantCode . '_' . $DocNo;
if (in_array($uniqueKey, $seenPlantDoc)) { if (in_array($uniqueKey, $seenPlantDoc)) {
$duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document {$DocNo} already processed for Plant {$plant->name}"; $duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document Number '{$DocNo}' already exist for Plant {$plant->name}";
} }
$seenPlantDoc[] = $uniqueKey; $seenPlantDoc[] = $uniqueKey;
@@ -344,43 +346,94 @@ class InvoiceDataValidationResource extends Resource
} }
return; return;
} }
// if (!empty($plantCode) && !empty($DocNo)) {
// $key = $plantCode . '|' . $DocNo;
// if (isset($seenPlantDoc[$key])) { foreach ($duplicateEntries as $message)
// // Duplicate found
// $duplicateEntries[] = "Row {$index}: Duplicate document number '{$DocNo}' found in plant '{$plantCode}' (also in Row {$seenPlantDoc[$key]})";
// } else {
// $seenPlantDoc[$key] = $index; // store first occurrence
// }
// }
if(!empty($duplicateEntries))
{ {
$errorMsg = 'Duplicate Entries found:<br>' . implode('<br>', $duplicateEntries); if (preg_match("/Document Number '([^']+)' already exists for Plant (.+)/", $message, $matches)) {
$docNo = $matches[1];
$plantName = trim($matches[2]);
$duplicateGroupedByPlant[$plantName][] = $docNo;
}
}
foreach ($duplicateEntriesExcel as $message) {
if (preg_match("/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/", $message, $matches)) {
$docNo = $matches[1];
$plantName = $matches[2] ?? 'Unknown';
$duplicateGroupedByPlantExcel[$plantName][] = $docNo;
}
}
if (!empty($duplicateGroupedByPlant)) {
$errorMsg = 'Duplicate Entries in Database:<br>';
foreach ($duplicateGroupedByPlant as $plant => $docNumbers) {
$count = count($docNumbers);
if ($count > 10)
{
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: {$count} document numbers already exist in DB<br>";
}
else
{
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: "
. implode(', ', $docNumbers)
. " already exist<br>";
}
}
Notification::make() Notification::make()
->title('Duplicate Entries in Database') //->title('Duplicate Entries in Database')
->body($errorMsg) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if(!empty($duplicateEntriesExcel)) if (!empty($duplicateGroupedByPlantExcel))
{ {
$errorMsg = 'Duplicate Entries found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
$errorMsg = 'Duplicate Entries found in Uploaded File:<br>';
foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers)
{
// Remove duplicate document numbers for each plant
$uniqueDocNumbers = array_unique($docNumbers);
$count = count($uniqueDocNumbers);
if ($count > 10) {
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: {$count} document numbers already exist in uploaded file<br>";
} else {
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: "
. implode(', ', $uniqueDocNumbers)
. " already exist<br>";
}
}
Notification::make() Notification::make()
->title('Duplicate Entries in Uploaded File') //->title('Duplicate Entries in Uploaded File')
->body($errorMsg) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
// if(!empty($duplicateEntriesExcel))
// {
// //$errorMsg = 'Duplicate Entries found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
// $errorMsg = buildDuplicateMessage($duplicateEntriesExcel, 'Duplicate Entries found in Uploaded File');
// Notification::make()
// ->title('Duplicate Entries in Uploaded File')
// ->body($errorMsg)
$successCount = 0; $successCount = 0;
$failCount = 0; $failCount = 0;