diff --git a/app/Filament/Resources/InvoiceDataValidationResource.php b/app/Filament/Resources/InvoiceDataValidationResource.php
index 27e452c..3b1a9b3 100644
--- a/app/Filament/Resources/InvoiceDataValidationResource.php
+++ b/app/Filament/Resources/InvoiceDataValidationResource.php
@@ -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,16 +260,29 @@ 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))
{
$errorMsg = '';
@@ -260,7 +290,7 @@ class InvoiceDataValidationResource extends Resource
//if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '
';
if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '
';
if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '
';
- if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '
';
+ if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '
';
if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '
';
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $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) . '
';
- // //if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '
';
- // if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '
';
- // //if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '
';
- // //if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '
';
- // //if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '
';
- // //if (!empty($invalidUser)) $errorMsg .= 'Missing User in rows: ' . implode(', ', $invalidUser) . '
';
-
- // 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,13 +344,37 @@ 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:
' . implode('
', $duplicateEntries);
Notification::make()
- ->title('Invalid User')
- ->body('The following user not found:
' . implode(', ', $userNotFound))
- ->danger()
- ->send();
+ ->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:
' . implode('
', $duplicateEntriesExcel);
+ Notification::make()
+ ->title('Duplicate Entries in Uploaded File')
+ ->body($errorMsg)
+ ->danger()
+ ->send();
if ($disk->exists($path)) {
$disk->delete($path);
}