Enhance invoice validation by adding plant type checks and improving error handling for imports

This commit is contained in:
dhanabalan
2025-11-04 10:41:56 +05:30
parent f83df4498b
commit 54e0760f94
2 changed files with 217 additions and 74 deletions

View File

@@ -9,6 +9,7 @@ use App\Models\InvoiceOutValidation;
use App\Models\Plant;
use App\Models\User;
use Carbon\Carbon;
use DB;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
@@ -23,6 +24,7 @@ use Filament\Notifications\Notification;
use Maatwebsite\Excel\Facades\Excel;
use Storage;
use Filament\Tables\Actions\ExportAction;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
class InvoiceOutValidationResource extends Resource
{
@@ -183,10 +185,10 @@ class InvoiceOutValidationResource extends Resource
{
if ($index == 0) continue; // Skip header
$plantCode = trim($row[0]);
$qrCode = trim($row[1]);
$scannedAt = trim($row[2]);
$scannedby = trim($row[3]);
$plantCode = trim($row[2]);
$scannedAt = trim($row[3]);
$scannedby = trim($row[4]);
//$createdBy = trim($row[4]);
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
@@ -267,67 +269,167 @@ class InvoiceOutValidationResource extends Resource
return;
}
// $successCount = 0;
// $failCount = 0;
// $lastErrorMessage = null;
// foreach ($rows as $index => $row) {
// if ($index == 0) continue;
// $qrcode = trim($row[1]);
// $plantCode = trim($row[2]);
// $scannedAt = trim($row[3]);
// $scannedBy = trim($row[4]);
// try
// {
// $plant = Plant::where('code', $plantCode)->first();
// $formattedDate = null;
// if (!empty($scannedAt)) {
// try {
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
// ->format('Y-m-d H:i:s');
// } catch (\Exception $e) {
// throw new \Exception("Invalid date format: {$scannedAt}");
// }
// }
// $inserted = InvoiceOutValidation::create([
// 'plant_id' => $plant->id,
// 'qr_code' => $qrcode,
// 'scanned_at' => $formattedDate,
// 'scanned_by' => $scannedBy,
// 'created_by' => $operatorName,
// ]);
// if ($inserted) {
// $successCount++;
// } else {
// $failCount++;
// }
// } catch (\Exception $e) {
// //$failCount++;
// $lastErrorMessage = $e->getMessage();
// }
// }
// if($successCount > 0)
// {
// Notification::make()
// ->title('Import Success')
// ->body("Successfully inserted: {$successCount} records")
// ->success()
// ->send();
// }
// else {
// $errorText = $lastErrorMessage
// ? "Failed to insert any records. Error: {$lastErrorMessage}"
// : "Failed to insert any records. Unknown error.";
// Notification::make()
// ->title('Import Failed')
// ->body($errorText)
// ->danger()
// ->send();
// }
$successCount = 0;
$failCount = 0;
$failedRecords = [];
$lastErrorMessage = null;
DB::beginTransaction();
foreach ($rows as $index => $row) {
if ($index == 0) continue;
try {
foreach ($rows as $index => $row) {
if ($index == 0) continue; // skip header
$plantCode = trim($row[0]);
$qrcode = trim($row[1]);
$scannedAt = trim($row[2]);
$scannedBy = trim($row[3]);
$rowNumber = $index + 1; // For Excel-style numbering
try
{
$plant = Plant::where('code', $plantCode)->first();
try {
$qrcode = trim($row[1]);
$plantCode = trim($row[2]);
$scannedAt = trim($row[3]);
$scannedBy = trim($row[4]);
$formattedDate = null;
if (!empty($scannedAt)) {
try {
$formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
->format('Y-m-d H:i:s');
} catch (\Exception $e) {
throw new \Exception("Invalid date format: {$scannedAt}");
if (empty($qrcode)) {
throw new \Exception("Missing QR Code");
}
}
$inserted = InvoiceOutValidation::create([
'plant_id' => $plant->id,
'qr_code' => $qrcode,
'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy,
'created_by' => $operatorName,
]);
$plant = Plant::where('code', $plantCode)->first();
if (!$plant) {
throw new \Exception("Invalid plant code: {$plantCode}");
}
$formattedDate = null;
if (!empty($scannedAt)) {
try {
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
// ->format('Y-m-d H:i:s');
if (is_numeric($scannedAt)) {
$formattedDate = ExcelDate::excelToDateTimeObject($scannedAt)
->format('Y-m-d H:i:s');
} else {
// Or handle as manual string date (d-m-Y H:i:s)
$formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
->format('Y-m-d H:i:s');
}
} catch (\Exception $e) {
throw new \Exception("Invalid date format: {$scannedAt}");
}
}
$inserted = InvoiceOutValidation::create([
'plant_id' => $plant->id,
'qr_code' => $qrcode,
'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy,
'created_by' => $operatorName,
]);
if (!$inserted) {
throw new \Exception("Insert failed for QR: {$qrcode}");
}
if ($inserted) {
$successCount++;
} else {
$failCount++;
} catch (\Exception $e) {
$failedRecords[] = [
'row' => $rowNumber,
'qrcode' => $qrcode ?? null,
'error' => $e->getMessage(),
];
}
} catch (\Exception $e) {
//$failCount++;
$lastErrorMessage = $e->getMessage();
}
}
if($successCount > 0)
{
Notification::make()
->title('Import Success')
->body("Successfully inserted: {$successCount} records")
->success()
->send();
}
else {
$errorText = $lastErrorMessage
? "Failed to insert any records. Error: {$lastErrorMessage}"
: "Failed to insert any records. Unknown error.";
DB::commit();
// ✅ After loop, show result summary
if (count($failedRecords) > 0) {
$failedSummary = collect($failedRecords)
->map(fn($f) => "Row {$f['row']} ({$f['qrcode']}): {$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 inserted: {$successCount} records")
->success()
->send();
}
} catch (\Exception $e) {
DB::rollBack();
Notification::make()
->title('Import Failed')
->body($errorText)
->body("No records were inserted. Error: {$e->getMessage()}")
->danger()
->send();
}