Refactor duplicate entry handling and improve error notifications in InvoiceDataValidationResource

This commit is contained in:
dhanabalan
2025-11-08 18:27:23 +05:30
parent 3291c47d2c
commit af0db6275f

View File

@@ -23,6 +23,7 @@ use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use App\Models\StickerMaster; use App\Models\StickerMaster;
use App\Models\User; use App\Models\User;
use DB;
use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ExportAction;
class InvoiceDataValidationResource extends Resource class InvoiceDataValidationResource extends Resource
@@ -213,16 +214,13 @@ class InvoiceDataValidationResource extends Resource
$invalidPlantType = []; $invalidPlantType = [];
$seenPlantDoc = []; $seenPlantDoc = [];
$duplicateEntries = []; //$duplicateEntries = [];
$duplicateEntriesExcel = []; $duplicateEntriesExcel = [];
$duplicateGroupedByPlant = [];
$duplicateGroupedByPlantExcel = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row)
{ {
if ($index == 0) continue; // Skip header if ($index == 0) continue; // Skip header
$DisChaDesc = trim($row[3]); $DisChaDesc = trim($row[3]);
$plantCode = trim($row[4]); $plantCode = trim($row[4]);
$CustomerCode = trim($row[5]); $CustomerCode = trim($row[5]);
@@ -266,23 +264,22 @@ class InvoiceDataValidationResource extends Resource
// --- Find Plant by code --- // --- Find Plant by code ---
$plant = Plant::where('code', $plantCode)->first(); $plant = Plant::where('code', $plantCode)->first();
//Duplicate Check in DB --- // //Duplicate Check in DB ---
$exists = InvoiceDataValidation::where('plant_id', $plant->id) // $exists = InvoiceDataValidation::where('plant_id', $plant->id)
->where('document_number', $DocNo) // ->where('document_number', $DocNo)
->first(); // ->first();
if ($exists) // if ($exists)
{ // {
$duplicateEntries[] = "Duplicate record: Document Number '{$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 Number '{$DocNo}' already exist for Plant {$plant->name}"; $duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document Number '{$DocNo}' already exists for Plant '{$plant->name}'";
} }
$seenPlantDoc[] = $uniqueKey; $seenPlantDoc[] = $uniqueKey;
} }
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation)) if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation))
@@ -307,9 +304,8 @@ class InvoiceDataValidationResource extends Resource
} }
return; return;
} }
else if (!empty($invalidPlantCode))
{
if (!empty($invalidPlantCode)) {
$invalidPlantCode = array_unique($invalidPlantCode); $invalidPlantCode = array_unique($invalidPlantCode);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
@@ -334,7 +330,8 @@ class InvoiceDataValidationResource extends Resource
} }
return; return;
} }
if (!empty($invalidPlaCoFound)) { else if (!empty($invalidPlaCoFound))
{
$invalidPlaCoFound = array_unique($invalidPlaCoFound); $invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
@@ -347,56 +344,61 @@ class InvoiceDataValidationResource extends Resource
return; return;
} }
foreach ($duplicateEntries as $message) // if (!empty($duplicateEntries))
{ // {
if (preg_match("/Document Number '([^']+)' already exists for Plant (.+)/", $message, $matches)) { // $duplicateGroupedByPlant = [];
$docNo = $matches[1];
$plantName = trim($matches[2]);
$duplicateGroupedByPlant[$plantName][] = $docNo;
}
}
foreach ($duplicateEntriesExcel as $message) { // foreach ($duplicateEntries as $message)
if (preg_match("/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/", $message, $matches)) { // {
// if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) {
// $docNo = $matches[1];
// $plantName = trim($matches[2]);
// $duplicateGroupedByPlant[$plantName][] = $docNo;
// }
// }
// $errorMsg = 'Duplicate Document Number found 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()
// //->title('Duplicate Entries in Database')
// ->body($errorMsg)
// ->danger()
// ->send();
// if ($disk->exists($path)) {
// $disk->delete($path);
// }
// return;
// }
if (!empty($duplicateEntriesExcel))
{
$duplicateGroupedByPlantExcel = [];
foreach ($duplicateEntriesExcel as $message) {//"/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/"
if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) {
$docNo = $matches[1]; $docNo = $matches[1];
$plantName = $matches[2] ?? 'Unknown'; $plantName = $matches[2] ?? 'Unknown';
$duplicateGroupedByPlantExcel[$plantName][] = $docNo; $duplicateGroupedByPlantExcel[$plantName][] = $docNo;
} }
} }
if (!empty($duplicateGroupedByPlant)) { $errorMsg = 'Duplicate Document Number found in Uploaded File :<br>';
$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()
//->title('Duplicate Entries in Database')
->body($errorMsg)
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
if (!empty($duplicateGroupedByPlantExcel))
{
$errorMsg = 'Duplicate Entries found in Uploaded File:<br>';
foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers) foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers)
{ {
@@ -405,16 +407,16 @@ class InvoiceDataValidationResource extends Resource
$count = count($uniqueDocNumbers); $count = count($uniqueDocNumbers);
if ($count > 10) { if ($count > 10) {
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: {$count} document numbers already exist in uploaded file<br>"; $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : {$count} Document Numbers already exist in uploaded file<br>";
} else { } else {
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: " $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : '"
. implode(', ', $uniqueDocNumbers) . implode(', ', $uniqueDocNumbers)
. " already exist<br>"; . "' already exist in uploaded file<br>";
} }
} }
Notification::make() Notification::make()
//->title('Duplicate Entries in Uploaded File') //->title('Duplicate Document Number in Uploaded File')
->body($errorMsg) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
@@ -427,19 +429,25 @@ class InvoiceDataValidationResource extends Resource
// if (!empty($duplicateEntriesExcel)) // if (!empty($duplicateEntriesExcel))
// { // {
// //$errorMsg = 'Duplicate Entries found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel); // //$errorMsg = 'Duplicate Document Number found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
// $errorMsg = buildDuplicateMessage($duplicateEntriesExcel, 'Duplicate Entries found in Uploaded File'); // $errorMsg = buildDuplicateMessage($duplicateEntriesExcel, 'Duplicate Document Number found in Uploaded File');
// Notification::make() // Notification::make()
// ->title('Duplicate Entries in Uploaded File') // ->title('Duplicate Document Number in Uploaded File')
// ->body($errorMsg) // ->body($errorMsg)
$successCount = 0; $successCount = 0;
$failCount = 0; $failedRecords = [];
DB::beginTransaction();
try
{
foreach ($rows as $index => $row) { foreach ($rows as $index => $row) {
if ($index == 0) continue; if ($index == 0) continue;
$rowNumber = $index + 1;
try {
$DisChaDesc = trim($row[3]); $DisChaDesc = trim($row[3]);
$plantCode = trim($row[4]); $plantCode = trim($row[4]);
$CustomerCode = trim($row[5]); $CustomerCode = trim($row[5]);
@@ -448,9 +456,14 @@ class InvoiceDataValidationResource extends Resource
$CusTradeName = trim($row[9]); $CusTradeName = trim($row[9]);
$CusLocation = trim($row[10]); $CusLocation = trim($row[10]);
try if (empty($DocNo)) {
{ throw new \Exception("Row '{$rowNumber}' Missing QR Code");
}
$plant = Plant::where('code', $plantCode)->first(); $plant = Plant::where('code', $plantCode)->first();
if (!$plant) {
throw new \Exception("Invalid plant code : '{$plantCode}'");
}
if (!empty($DocDate)) { if (!empty($DocDate)) {
if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) { if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) {
@@ -465,39 +478,87 @@ class InvoiceDataValidationResource extends Resource
$formattedDate = null; $formattedDate = null;
} }
$inserted = InvoiceDataValidation::create([ $record = InvoiceDataValidation::where('plant_id', $plant->id)
'plant_id' => $plant->id, ->where('document_number', $DocNo)
->first();
$curStat = $record ? 'Updation' : 'Insertion';
if ($record) {
$record->update([
'distribution_channel_desc' => $DisChaDesc, 'distribution_channel_desc' => $DisChaDesc,
'customer_code' => $CustomerCode, 'customer_code' => $CustomerCode,
'document_number' => $DocNo,
'document_date' => $formattedDate, 'document_date' => $formattedDate,
'customer_trade_name' => $CusTradeName, 'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation, 'customer_location' => $CusLocation,
'created_by' => $operatorName, 'updated_by' => $operatorName
]); ]);
$inserted = $record;
if ($inserted) {
$successCount++;
} else { } else {
$failCount++; // Record does not exist, create with 'created_by'
$inserted = InvoiceDataValidation::create([
'plant_id' => $plant->id,
'document_number' => $DocNo,
'distribution_channel_desc' => $DisChaDesc,
'customer_code' => $CustomerCode,
'document_date' => $formattedDate,
'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation,
'created_by' => $operatorName
]);
} }
// $inserted = InvoiceDataValidation::create([
// 'plant_id' => $plant->id,
// 'document_number' => $DocNo,
// 'distribution_channel_desc' => $DisChaDesc,
// 'customer_code' => $CustomerCode,
// 'document_date' => $formattedDate,
// 'customer_trade_name' => $CusTradeName,
// 'customer_location' => $CusLocation,
// 'created_by' => $operatorName
// ]);
if (!$inserted) {
throw new \Exception("{$curStat} failed for Document Number : {$DocNo}");
}
$successCount++;
} catch (\Exception $e) { } catch (\Exception $e) {
$failCount++; $failedRecords[] = [
'row' => $rowNumber,
'document_number' => $DocNo ?? null,
'error' => $e->getMessage()
];
} }
} }
if($successCount > 0)
{ DB::commit();
if (count($failedRecords) > 0) {
$failedSummary = collect($failedRecords)
->map(fn($f) => "Row {$f['row']} ({$f['document_number']}) : {$f['error']}")
->take(5) // limit preview to first 5 errors
->implode("\n");
Notification::make() Notification::make()
->title('Import Summary') ->title('Partial Import Warning')
->body("Successfully inserted: {$successCount} records") ->body("'{$successCount}' records inserted. " . count($failedRecords) . " failed.\n\n{$failedSummary}")
->warning()
->send();
} else {
Notification::make()
->title('Import Success')
->body("Successfully imported '{$successCount}' records.")
->success() ->success()
->send(); ->send();
} }
else }
catch (\Exception $e)
{ {
DB::rollBack();
Notification::make() Notification::make()
->title('Import Summary') ->title('Import Failed')
->body("Failed to insert any records.") ->body("No records were inserted. Error : {$e->getMessage()}")
->danger() ->danger()
->send(); ->send();
} }