Refactor duplicate entry handling and improve error notifications in InvoiceDataValidationResource
This commit is contained in:
@@ -23,6 +23,7 @@ use Illuminate\Support\Facades\Storage;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Models\StickerMaster;
|
||||
use App\Models\User;
|
||||
use DB;
|
||||
use Filament\Tables\Actions\ExportAction;
|
||||
|
||||
class InvoiceDataValidationResource extends Resource
|
||||
@@ -213,16 +214,13 @@ class InvoiceDataValidationResource extends Resource
|
||||
$invalidPlantType = [];
|
||||
|
||||
$seenPlantDoc = [];
|
||||
$duplicateEntries = [];
|
||||
//$duplicateEntries = [];
|
||||
$duplicateEntriesExcel = [];
|
||||
$duplicateGroupedByPlant = [];
|
||||
$duplicateGroupedByPlantExcel = [];
|
||||
|
||||
foreach ($rows as $index => $row)
|
||||
{
|
||||
if ($index == 0) continue; // Skip header
|
||||
|
||||
|
||||
$DisChaDesc = trim($row[3]);
|
||||
$plantCode = trim($row[4]);
|
||||
$CustomerCode = trim($row[5]);
|
||||
@@ -266,23 +264,22 @@ class InvoiceDataValidationResource extends Resource
|
||||
// --- 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();
|
||||
// //Duplicate Check in DB ---
|
||||
// $exists = InvoiceDataValidation::where('plant_id', $plant->id)
|
||||
// ->where('document_number', $DocNo)
|
||||
// ->first();
|
||||
|
||||
if ($exists)
|
||||
{
|
||||
$duplicateEntries[] = "Duplicate record: Document Number '{$DocNo}' already exists for Plant {$plant->name}";
|
||||
}
|
||||
// if ($exists)
|
||||
// {
|
||||
// $duplicateEntries[] = "Duplicate record: Document Number '{$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 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;
|
||||
|
||||
}
|
||||
|
||||
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation))
|
||||
@@ -307,9 +304,8 @@ class InvoiceDataValidationResource extends Resource
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!empty($invalidPlantCode)) {
|
||||
else if (!empty($invalidPlantCode))
|
||||
{
|
||||
$invalidPlantCode = array_unique($invalidPlantCode);
|
||||
Notification::make()
|
||||
->title('Invalid Plant Codes')
|
||||
@@ -334,7 +330,8 @@ class InvoiceDataValidationResource extends Resource
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!empty($invalidPlaCoFound)) {
|
||||
else if (!empty($invalidPlaCoFound))
|
||||
{
|
||||
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
|
||||
Notification::make()
|
||||
->title('Invalid Plant Codes')
|
||||
@@ -347,56 +344,61 @@ class InvoiceDataValidationResource extends Resource
|
||||
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;
|
||||
}
|
||||
}
|
||||
// if (!empty($duplicateEntries))
|
||||
// {
|
||||
// $duplicateGroupedByPlant = [];
|
||||
|
||||
foreach ($duplicateEntriesExcel as $message) {
|
||||
if (preg_match("/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/", $message, $matches)) {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
->send();
|
||||
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!empty($duplicateGroupedByPlantExcel))
|
||||
{
|
||||
|
||||
$errorMsg = 'Duplicate Entries found in Uploaded File:<br>';
|
||||
$errorMsg = 'Duplicate Document Number found in Uploaded File :<br>';
|
||||
|
||||
foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers)
|
||||
{
|
||||
@@ -405,16 +407,16 @@ class InvoiceDataValidationResource extends Resource
|
||||
$count = count($uniqueDocNumbers);
|
||||
|
||||
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 {
|
||||
$errorMsg .= "Duplicate record(s) for Plant <b>{$plant}</b>: "
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : '"
|
||||
. implode(', ', $uniqueDocNumbers)
|
||||
. " already exist<br>";
|
||||
. "' already exist in uploaded file<br>";
|
||||
}
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
//->title('Duplicate Entries in Uploaded File')
|
||||
//->title('Duplicate Document Number in Uploaded File')
|
||||
->body($errorMsg)
|
||||
->danger()
|
||||
->send();
|
||||
@@ -427,19 +429,25 @@ class InvoiceDataValidationResource extends Resource
|
||||
|
||||
// if (!empty($duplicateEntriesExcel))
|
||||
// {
|
||||
// //$errorMsg = 'Duplicate Entries found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
|
||||
// $errorMsg = buildDuplicateMessage($duplicateEntriesExcel, 'Duplicate Entries found in Uploaded File');
|
||||
// //$errorMsg = 'Duplicate Document Number found in the uploaded file:<br>' . implode('<br>', $duplicateEntriesExcel);
|
||||
// $errorMsg = buildDuplicateMessage($duplicateEntriesExcel, 'Duplicate Document Number found in Uploaded File');
|
||||
// Notification::make()
|
||||
// ->title('Duplicate Entries in Uploaded File')
|
||||
// ->title('Duplicate Document Number in Uploaded File')
|
||||
// ->body($errorMsg)
|
||||
|
||||
|
||||
$successCount = 0;
|
||||
$failCount = 0;
|
||||
$failedRecords = [];
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
foreach ($rows as $index => $row) {
|
||||
if ($index == 0) continue;
|
||||
|
||||
$rowNumber = $index + 1;
|
||||
|
||||
try {
|
||||
$DisChaDesc = trim($row[3]);
|
||||
$plantCode = trim($row[4]);
|
||||
$CustomerCode = trim($row[5]);
|
||||
@@ -448,9 +456,14 @@ class InvoiceDataValidationResource extends Resource
|
||||
$CusTradeName = trim($row[9]);
|
||||
$CusLocation = trim($row[10]);
|
||||
|
||||
try
|
||||
{
|
||||
if (empty($DocNo)) {
|
||||
throw new \Exception("Row '{$rowNumber}' Missing QR Code");
|
||||
}
|
||||
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
if (!$plant) {
|
||||
throw new \Exception("Invalid plant code : '{$plantCode}'");
|
||||
}
|
||||
|
||||
if (!empty($DocDate)) {
|
||||
if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) {
|
||||
@@ -465,39 +478,87 @@ class InvoiceDataValidationResource extends Resource
|
||||
$formattedDate = null;
|
||||
}
|
||||
|
||||
$inserted = InvoiceDataValidation::create([
|
||||
'plant_id' => $plant->id,
|
||||
$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_number' => $DocNo,
|
||||
'document_date' => $formattedDate,
|
||||
'customer_trade_name' => $CusTradeName,
|
||||
'customer_location' => $CusLocation,
|
||||
'created_by' => $operatorName,
|
||||
'updated_by' => $operatorName
|
||||
]);
|
||||
|
||||
if ($inserted) {
|
||||
$successCount++;
|
||||
$inserted = $record;
|
||||
} 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) {
|
||||
$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()
|
||||
->title('Import Summary')
|
||||
->body("Successfully inserted: {$successCount} records")
|
||||
->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();
|
||||
}
|
||||
else
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
DB::rollBack();
|
||||
Notification::make()
|
||||
->title('Import Summary')
|
||||
->body("Failed to insert any records.")
|
||||
->title('Import Failed')
|
||||
->body("No records were inserted. Error : {$e->getMessage()}")
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user