Add duplicate entry checks in InvoiceDataValidationResource for database and file uploads
This commit is contained in:
@@ -212,6 +212,10 @@ class InvoiceDataValidationResource extends Resource
|
||||
$userNotFound = [];
|
||||
$invalidPlantType = [];
|
||||
|
||||
$seenPlantDoc = [];
|
||||
$duplicateEntries = [];
|
||||
$duplicateEntriesExcel = [];
|
||||
|
||||
foreach ($rows as $index => $row)
|
||||
{
|
||||
if ($index == 0) continue; // Skip header
|
||||
@@ -226,11 +230,24 @@ class InvoiceDataValidationResource extends Resource
|
||||
$CusLocation = trim($row[10]);
|
||||
|
||||
// if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
||||
if (empty($DisChaDesc)) $invalidDisChaDesc[] = "Row {$index}";
|
||||
if (empty($CustomerCode)) $invalidCustomerCode[] = "Row {$index}";
|
||||
if (empty($DocNo)) $invalidDocNo[] = "Row {$index}";
|
||||
if (empty($CusTradeName)) $invalidCusTradeName[] = "Row {$index}";
|
||||
if (empty($CusLocation)) $invalidCusLocation[] = "Row {$index}";
|
||||
if (empty($DisChaDesc)){
|
||||
$invalidDisChaDesc[] = "Row {$index}";
|
||||
}
|
||||
if (empty($CustomerCode)){
|
||||
$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 (strlen($plantCode) < 4) {
|
||||
@@ -243,15 +260,28 @@ class InvoiceDataValidationResource extends Resource
|
||||
{
|
||||
$invalidPlaCoFound[] = $plantCode;
|
||||
}
|
||||
// else if(!User::where('name', $createdBy)->first())
|
||||
// {
|
||||
// $userNotFound[] = $createdBy;
|
||||
// }
|
||||
|
||||
// --- Find Plant by code ---
|
||||
$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))
|
||||
{
|
||||
@@ -276,29 +306,6 @@ class InvoiceDataValidationResource extends Resource
|
||||
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)) {
|
||||
$invalidPlantCode = array_unique($invalidPlantCode);
|
||||
@@ -337,11 +344,35 @@ class InvoiceDataValidationResource extends Resource
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!empty($userNotFound)) {
|
||||
$userNotFound = array_unique($userNotFound);
|
||||
// if (!empty($plantCode) && !empty($DocNo)) {
|
||||
// $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()
|
||||
->title('Invalid User')
|
||||
->body('The following user not found:<br>' . implode(', ', $userNotFound))
|
||||
->title('Duplicate Entries in Database')
|
||||
->body($errorMsg)
|
||||
->danger()
|
||||
->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)) {
|
||||
|
||||
Reference in New Issue
Block a user