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]);
@@ -231,7 +229,7 @@ class InvoiceDataValidationResource extends Resource
$CusTradeName = trim($row[9]); $CusTradeName = trim($row[9]);
$CusLocation = trim($row[10]); $CusLocation = trim($row[10]);
// if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}"; // if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
if (empty($DisChaDesc)){ if (empty($DisChaDesc)){
$invalidDisChaDesc[] = "Row {$index}"; $invalidDisChaDesc[] = "Row {$index}";
} }
@@ -250,7 +248,7 @@ class InvoiceDataValidationResource extends Resource
{ {
$invalidCusLocation[] = "Row {$index}"; $invalidCusLocation[] = "Row {$index}";
} }
// if (empty($createdBy)) $invalidUser[] = "Row {$index}"; // if (empty($createdBy)) $invalidUser[] = "Row {$index}";
if (strlen($plantCode) < 4) { if (strlen($plantCode) < 4) {
$invalidPlantCode[] = $plantCode; $invalidPlantCode[] = $plantCode;
@@ -258,31 +256,30 @@ class InvoiceDataValidationResource extends Resource
if (!is_numeric($plantCode)) { if (!is_numeric($plantCode)) {
$invalidPlantType[] = $plantCode; $invalidPlantType[] = $plantCode;
} }
else if(!Plant::where('code', $plantCode)->first()) else if (!Plant::where('code', $plantCode)->first())
{ {
$invalidPlaCoFound[] = $plantCode; $invalidPlaCoFound[] = $plantCode;
} }
// --- 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,96 +304,101 @@ 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')
->body('The following plant codes should contain minimum 4 digits:<br>' . implode(', ', $invalidPlantCode)) ->body('The following plant codes should contain minimum 4 digits:<br>' . implode(', ', $invalidPlantCode))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
else if(!empty($invalidPlantType)) else if (!empty($invalidPlantType))
{ {
$invalidPlantType = array_unique($invalidPlantType); $invalidPlantType = array_unique($invalidPlantType);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes should contain numeric values:<br>' . implode(', ', $invalidPlantType)) ->body('The following plant codes should contain numeric values:<br>' . implode(', ', $invalidPlantType))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
if (!empty($invalidPlaCoFound)) {
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make()
->title('Invalid Plant Codes')
->body('The following plant codes not found in plants:<br>' . implode(', ', $invalidPlaCoFound))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
foreach ($duplicateEntries as $message)
{
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()
//->title('Duplicate Entries in Database')
->body($errorMsg)
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if (!empty($duplicateGroupedByPlantExcel)) else if (!empty($invalidPlaCoFound))
{ {
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make()
->title('Invalid Plant Codes')
->body('The following plant codes not found in plants:<br>' . implode(', ', $invalidPlaCoFound))
->danger()
->send();
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
$errorMsg = 'Duplicate Entries found in Uploaded File:<br>'; // if (!empty($duplicateEntries))
// {
// $duplicateGroupedByPlant = [];
// foreach ($duplicateEntries as $message)
// {
// 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];
$plantName = $matches[2] ?? 'Unknown';
$duplicateGroupedByPlantExcel[$plantName][] = $docNo;
}
}
$errorMsg = 'Duplicate Document Number 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();
@@ -425,79 +427,138 @@ class InvoiceDataValidationResource extends Resource
return; return;
} }
// 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 = [];
foreach ($rows as $index => $row) { DB::beginTransaction();
if ($index == 0) continue;
$DisChaDesc = trim($row[3]); try
$plantCode = trim($row[4]); {
$CustomerCode = trim($row[5]); foreach ($rows as $index => $row) {
$DocNo = trim($row[6]); if ($index == 0) continue;
$DocDate = trim($row[7]);
$CusTradeName = trim($row[9]);
$CusLocation = trim($row[10]);
try $rowNumber = $index + 1;
{
$plant = Plant::where('code', $plantCode)->first();
if (!empty($DocDate)) { try {
if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) { $DisChaDesc = trim($row[3]);
[$day, $month, $year] = preg_split('/[-\/]/', $DocDate); $plantCode = trim($row[4]);
$formattedDate = "{$year}-{$month}-{$day}"; $CustomerCode = trim($row[5]);
} elseif (is_numeric($DocDate)) { $DocNo = trim($row[6]);
$formattedDate = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($DocDate)->format('Y-m-d'); $DocDate = trim($row[7]);
} else { $CusTradeName = trim($row[9]);
$formattedDate = date('Y-m-d', strtotime($DocDate)); $CusLocation = trim($row[10]);
if (empty($DocNo)) {
throw new \Exception("Row '{$rowNumber}' Missing QR Code");
} }
} else {
$formattedDate = null;
}
$inserted = InvoiceDataValidation::create([ $plant = Plant::where('code', $plantCode)->first();
'plant_id' => $plant->id, if (!$plant) {
'distribution_channel_desc' => $DisChaDesc, throw new \Exception("Invalid plant code : '{$plantCode}'");
'customer_code' => $CustomerCode, }
'document_number' => $DocNo,
'document_date' => $formattedDate, if (!empty($DocDate)) {
'customer_trade_name' => $CusTradeName, if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) {
'customer_location' => $CusLocation, [$day, $month, $year] = preg_split('/[-\/]/', $DocDate);
'created_by' => $operatorName, $formattedDate = "{$year}-{$month}-{$day}";
]); } elseif (is_numeric($DocDate)) {
$formattedDate = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($DocDate)->format('Y-m-d');
} else {
$formattedDate = date('Y-m-d', strtotime($DocDate));
}
} else {
$formattedDate = null;
}
$record = InvoiceDataValidation::where('plant_id', $plant->id)
->where('document_number', $DocNo)
->first();
$curStat = $record ? 'Updation' : 'Insertion';
if ($record) {
$record->update([
'distribution_channel_desc' => $DisChaDesc,
'customer_code' => $CustomerCode,
'document_date' => $formattedDate,
'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation,
'updated_by' => $operatorName
]);
$inserted = $record;
} else {
// 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}");
}
if ($inserted) {
$successCount++; $successCount++;
} else { } catch (\Exception $e) {
$failCount++; $failedRecords[] = [
'row' => $rowNumber,
'document_number' => $DocNo ?? null,
'error' => $e->getMessage()
];
} }
} catch (\Exception $e) { }
$failCount++;
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()
->title('Partial Import Warning')
->body("'{$successCount}' records inserted. " . count($failedRecords) . " failed.\n\n{$failedSummary}")
->warning()
->send();
} else {
Notification::make()
->title('Import Success')
->body("Successfully imported '{$successCount}' records.")
->success()
->send();
} }
} }
if($successCount > 0) catch (\Exception $e)
{ {
DB::rollBack();
Notification::make() Notification::make()
->title('Import Summary') ->title('Import Failed')
->body("Successfully inserted: {$successCount} records") ->body("No records were inserted. Error : {$e->getMessage()}")
->success()
->send();
}
else
{
Notification::make()
->title('Import Summary')
->body("Failed to insert any records.")
->danger() ->danger()
->send(); ->send();
} }
@@ -506,10 +567,10 @@ class InvoiceDataValidationResource extends Resource
->visible(function() { ->visible(function() {
return Filament::auth()->user()->can('view import invoice data validation'); return Filament::auth()->user()->can('view import invoice data validation');
}), }),
ExportAction::make() ExportAction::make()
->exporter(InvoiceDataValidationExporter::class) ->exporter(InvoiceDataValidationExporter::class)
->visible(function() { ->visible(function() {
return Filament::auth()->user()->can('view export invoice data validation'); return Filament::auth()->user()->can('view export invoice data validation');
}), }),
]); ]);
} }