Add duplicate entry checks in InvoiceDataValidationResource for database and file uploads

This commit is contained in:
dhanabalan
2025-11-07 22:37:46 +05:30
parent be2b02c1c9
commit e27504d46d

View File

@@ -212,6 +212,10 @@ class InvoiceDataValidationResource extends Resource
$userNotFound = []; $userNotFound = [];
$invalidPlantType = []; $invalidPlantType = [];
$seenPlantDoc = [];
$duplicateEntries = [];
$duplicateEntriesExcel = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row)
{ {
if ($index == 0) continue; // Skip header if ($index == 0) continue; // Skip header
@@ -226,11 +230,24 @@ class InvoiceDataValidationResource extends Resource
$CusLocation = trim($row[10]); $CusLocation = trim($row[10]);
// if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}"; // if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
if (empty($DisChaDesc)) $invalidDisChaDesc[] = "Row {$index}"; if (empty($DisChaDesc)){
if (empty($CustomerCode)) $invalidCustomerCode[] = "Row {$index}"; $invalidDisChaDesc[] = "Row {$index}";
if (empty($DocNo)) $invalidDocNo[] = "Row {$index}"; }
if (empty($CusTradeName)) $invalidCusTradeName[] = "Row {$index}"; if (empty($CustomerCode)){
if (empty($CusLocation)) $invalidCusLocation[] = "Row {$index}"; $invalidCustomerCode[] = "Row {$index}";
}
if (empty($DocNo))
{
$invalidDocNo[] = "Row {$index}";
}
if (empty($CusTradeName))
{
$invalidCusTradeName[] = "Row {$index}";
}
if (empty($CusLocation))
{
$invalidCusLocation[] = "Row {$index}";
}
// if (empty($createdBy)) $invalidUser[] = "Row {$index}"; // if (empty($createdBy)) $invalidUser[] = "Row {$index}";
if (strlen($plantCode) < 4) { if (strlen($plantCode) < 4) {
@@ -243,16 +260,29 @@ class InvoiceDataValidationResource extends Resource
{ {
$invalidPlaCoFound[] = $plantCode; $invalidPlaCoFound[] = $plantCode;
} }
// else if(!User::where('name', $createdBy)->first())
// { // --- Find Plant by code ---
// $userNotFound[] = $createdBy; $plant = Plant::where('code', $plantCode)->first();
// }
//Duplicate Check in DB ---
$exists = InvoiceDataValidation::where('plant_id', $plant->id)
->where('document_number', $DocNo)
->first();
if ($exists)
{
$duplicateEntries[] = "Duplicate found at Row {$index}: Document {$DocNo} already exists for Plant {$plant->name}";
}
//Also check duplicates within the same file ---
$uniqueKey = $plantCode . '_' . $DocNo;
if (in_array($uniqueKey, $seenPlantDoc)) {
$duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document {$DocNo} already processed for Plant {$plant->name}";
}
$seenPlantDoc[] = $uniqueKey;
} }
//!empty($invalidDisChaDesc) ||
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation)) if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation))
{ {
$errorMsg = ''; $errorMsg = '';
@@ -260,7 +290,7 @@ class InvoiceDataValidationResource extends Resource
//if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>'; //if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>';
if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '<br>'; if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '<br>';
if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>'; if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>';
if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>'; if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>'; if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>'; if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
@@ -276,29 +306,6 @@ class InvoiceDataValidationResource extends Resource
return; return;
} }
// if (!empty($invalidDocNo) || !empty($invalidUser))
// {
// $errorMsg = '';
// //if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>';
// //if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '<br>';
// if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>';
// //if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
// //if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
// //if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
// //if (!empty($invalidUser)) $errorMsg .= 'Missing User in rows: ' . implode(', ', $invalidUser) . '<br>';
// Notification::make()
// ->title('Missing Mandatory Fields')
// ->body($errorMsg)
// ->danger()
// ->send();
// if ($disk->exists($path)) {
// $disk->delete($path);
// }
// return;
// }
if (!empty($invalidPlantCode)) { if (!empty($invalidPlantCode)) {
$invalidPlantCode = array_unique($invalidPlantCode); $invalidPlantCode = array_unique($invalidPlantCode);
@@ -337,13 +344,37 @@ class InvoiceDataValidationResource extends Resource
} }
return; return;
} }
if (!empty($userNotFound)) { // if (!empty($plantCode) && !empty($DocNo)) {
$userNotFound = array_unique($userNotFound); // $key = $plantCode . '|' . $DocNo;
// if (isset($seenPlantDoc[$key])) {
// // 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);
Notification::make() Notification::make()
->title('Invalid User') ->title('Duplicate Entries in Database')
->body('The following user not found:<br>' . implode(', ', $userNotFound)) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
if(!empty($duplicateEntriesExcel))
{
$errorMsg = 'Duplicate Entries found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
Notification::make()
->title('Duplicate Entries in Uploaded File')
->body($errorMsg)
->danger()
->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }