Enhance invoice out validation table with searchable 'updated_by' column and improved duplicate handling logic on import

This commit is contained in:
dhanabalan
2025-11-08 12:21:24 +05:30
parent d8a66e7678
commit b7a8c933ff

View File

@@ -104,8 +104,14 @@ class InvoiceOutValidationResource extends Resource
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('updated_at') Tables\Columns\TextColumn::make('updated_at')
->dateTime() ->dateTime()
->sortable() ->searchable()
->toggleable(isToggledHiddenByDefault: true), ->sortable(),
// ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated By')
->searchable()
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('deleted_at') Tables\Columns\TextColumn::make('deleted_at')
->dateTime() ->dateTime()
->sortable() ->sortable()
@@ -186,7 +192,7 @@ class InvoiceOutValidationResource extends Resource
$userNotFound = []; $userNotFound = [];
$seenPlantQr = []; $seenPlantQr = [];
$duplicateQrExcel = []; $duplicateQrExcel = [];
$duplicateQrDb = []; //$duplicateQrDb = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row)
{ {
@@ -217,18 +223,18 @@ class InvoiceOutValidationResource extends Resource
$uniqueKey = $plantCode . '_' . $qrCode; $uniqueKey = $plantCode . '_' . $qrCode;
if (in_array($uniqueKey, $seenPlantQr)) { if (in_array($uniqueKey, $seenPlantQr)) {
$duplicateQrExcel[] = "Duplicate in file at Row {$index}: Document Numbers '{$qrCode}' already exists for Plant Code {$plant->name}"; $duplicateQrExcel[] = "Duplicate in file at Row {$index}: Document Numbers '{$qrCode}' already exists for Plant Code '{$plant->name}'";
} }
$seenPlantQr[] = $uniqueKey; $seenPlantQr[] = $uniqueKey;
$existsInDb = InvoiceOutValidation::where('plant_id', $plantId) // $existsInDb = InvoiceOutValidation::where('plant_id', $plantId)
->where('qr_code', $qrCode) // ->where('qr_code', $qrCode)
->first(); // ->first();
if ($existsInDb) { // if ($existsInDb) {
$duplicateQrDb[] = "Document Numbers '{$qrCode}' already exists in DB for Plant Code {$plant->name}"; // $duplicateQrDb[] = "Document Numbers '{$qrCode}' already exists in DB for Plant Code {$plant->name}";
} // }
} }
if (!empty($invalidqrCode) || !empty($invalidScannedAt) || !empty($invalidScannedBy) || !empty($invalidUser)) if (!empty($invalidqrCode) || !empty($invalidScannedAt) || !empty($invalidScannedBy) || !empty($invalidUser))
@@ -263,6 +269,7 @@ class InvoiceOutValidationResource extends Resource
} }
return; return;
} }
if (!empty($invalidPlaCoFound)) { if (!empty($invalidPlaCoFound)) {
$invalidPlaCoFound = array_unique($invalidPlaCoFound); $invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make() Notification::make()
@@ -275,6 +282,7 @@ class InvoiceOutValidationResource extends Resource
} }
return; return;
} }
if (!empty($userNotFound)) { if (!empty($userNotFound)) {
$userNotFound = array_unique($userNotFound); $userNotFound = array_unique($userNotFound);
Notification::make() Notification::make()
@@ -287,12 +295,13 @@ class InvoiceOutValidationResource extends Resource
} }
return; return;
} }
if (!empty($duplicateQrExcel)) if (!empty($duplicateQrExcel))
{ {
$duplicateGroupedByPlantQr = []; $duplicateGroupedByPlantQr = [];
foreach ($duplicateQrExcel as $message) { foreach ($duplicateQrExcel as $message) {//"/Document Numbers '([^']+)' already exists for Plant Code (\S+)/"
if (preg_match("/Document Numbers '([^']+)' already exists for Plant Code (\S+)/", $message, $matches)) { if (preg_match("/Document Numbers '([^']+)' already exists for Plant Code '([^']+)'/", $message, $matches)) {
$qrCode = $matches[1]; $qrCode = $matches[1];
$plantCode = $matches[2]; $plantCode = $matches[2];
$duplicateGroupedByPlantQr[$plantCode][] = $qrCode; $duplicateGroupedByPlantQr[$plantCode][] = $qrCode;
@@ -308,9 +317,9 @@ class InvoiceOutValidationResource extends Resource
if ($count > 10) { if ($count > 10) {
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : {$count} Document Numbers already exist in uploaded file<br>"; $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : {$count} Document Numbers already exist in uploaded file<br>";
} else { } else {
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: " $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : '"
. implode(', ', $uniqueQrCodes) . implode(', ', $uniqueQrCodes)
. " already exist<br>"; . "' already exist in uploaded file<br>";
} }
} }
@@ -326,46 +335,45 @@ class InvoiceOutValidationResource extends Resource
return; return;
} }
if (!empty($duplicateQrDb)) {
$duplicateGroupedByPlantDb = [];
foreach ($duplicateQrDb as $message) { // if (!empty($duplicateQrDb)) {
if (preg_match("/Document Numbers '([^']+)' already exists in DB for Plant Code (\S+)/", $message, $matches)) { // $duplicateGroupedByPlantDb = [];
$qrCode = $matches[1];
$plantCode = $matches[2];
$duplicateGroupedByPlantDb[$plantCode][] = $qrCode;
}
}
$errorMsg = 'Duplicate Document Numbers found in Database:<br>'; // foreach ($duplicateQrDb as $message) {
// if (preg_match("/Document Numbers '([^']+)' already exists in DB for Plant Code (\S+)/", $message, $matches)) {
// $qrCode = $matches[1];
// $plantCode = $matches[2];
// $duplicateGroupedByPlantDb[$plantCode][] = $qrCode;
// }
// }
foreach ($duplicateGroupedByPlantDb as $plantCode => $qrCodes) { // $errorMsg = 'Duplicate Document Numbers found in Database:<br>';
$uniqueQrCodes = array_unique($qrCodes);
$count = count($uniqueQrCodes);
if ($count > 10) { // foreach ($duplicateGroupedByPlantDb as $plantCode => $qrCodes) {
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: {$count} Document Numbers already exist in DB<br>"; // $uniqueQrCodes = array_unique($qrCodes);
} else { // $count = count($uniqueQrCodes);
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: "
. implode(', ', $uniqueQrCodes)
. " already exist in DB<br>";
}
}
Notification::make() // if ($count > 10) {
// ->title('Duplicate Document Numbers in Database') // $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: {$count} Document Numbers already exist in DB<br>";
->body($errorMsg) // } else {
->danger() // $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b>: "
->send(); // . implode(', ', $uniqueQrCodes)
// . " already exist in DB<br>";
// }
// }
if ($disk->exists($path)) { // Notification::make()
$disk->delete($path); // // ->title('Duplicate Document Numbers in Database')
} // ->body($errorMsg)
// ->danger()
return; // ->send();
}
// if ($disk->exists($path)) {
// $disk->delete($path);
// }
// return;
// }
$successCount = 0; $successCount = 0;
$failedRecords = []; $failedRecords = [];
@@ -412,16 +420,39 @@ class InvoiceOutValidationResource extends Resource
} }
} }
$record = InvoiceOutValidation::where('plant_id', $plant->id)
->where('qr_code', $qrcode)
->first();
$curStat = $record ? 'Updation' : 'Insertion';
if ($record) {
$record->update([
'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy,
'updated_by' => $operatorName
]);
$inserted = $record;
} else {
// Record does not exist, create with 'created_by'
$inserted = InvoiceOutValidation::create([ $inserted = InvoiceOutValidation::create([
'plant_id' => $plant->id, 'plant_id' => $plant->id,
'qr_code' => $qrcode, 'qr_code' => $qrcode,
'scanned_at' => $formattedDate, 'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy, 'scanned_by' => $scannedBy,
'created_by' => $operatorName, 'created_by' => $operatorName
]); ]);
}
// $inserted = InvoiceOutValidation::create([
// 'plant_id' => $plant->id,
// 'qr_code' => $qrcode,
// 'scanned_at' => $formattedDate,
// 'scanned_by' => $scannedBy,
// 'created_by' => $operatorName
// ]);
if (!$inserted) { if (!$inserted) {
throw new \Exception("Insert failed for QR: {$qrcode}"); throw new \Exception("{$curStat} failed for QR : {$qrcode}");
} }
$successCount++; $successCount++;
@@ -445,17 +476,16 @@ class InvoiceOutValidationResource extends Resource
Notification::make() Notification::make()
->title('Partial Import Warning') ->title('Partial Import Warning')
->body("{$successCount} records inserted. " . count($failedRecords) . " failed.\n\n{$failedSummary}") ->body("'{$successCount}' records inserted. " . count($failedRecords) . " failed.\n\n{$failedSummary}")
->warning() ->warning()
->send(); ->send();
} else { } else {
Notification::make() Notification::make()
->title('Import Success') ->title('Import Success')
->body("Successfully inserted: {$successCount} records") ->body("Successfully imported '{$successCount}' records")
->success() ->success()
->send(); ->send();
} }
} }
catch (\Exception $e) catch (\Exception $e)
{ {