Enhance invoice validation by adding plant type checks and improving error handling for imports
This commit is contained in:
@@ -203,30 +203,35 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
$invalidCusLocation = [];
|
$invalidCusLocation = [];
|
||||||
$invalidUser = [];
|
$invalidUser = [];
|
||||||
$userNotFound = [];
|
$userNotFound = [];
|
||||||
|
$invalidPlantType = [];
|
||||||
|
|
||||||
foreach ($rows as $index => $row)
|
foreach ($rows as $index => $row)
|
||||||
{
|
{
|
||||||
if ($index == 0) continue; // Skip header
|
if ($index == 0) continue; // Skip header
|
||||||
|
|
||||||
$plantCode = trim($row[0]);
|
|
||||||
$DisChaDesc = trim($row[1]);
|
|
||||||
$CustomerCode = trim($row[2]);
|
|
||||||
$DocNo = trim($row[3]);
|
|
||||||
$DocDate = trim($row[4]);
|
|
||||||
$CusTradeName = trim($row[5]);
|
|
||||||
$CusLocation = trim($row[6]);
|
|
||||||
|
|
||||||
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
$DisChaDesc = trim($row[3]);
|
||||||
//if (empty($DisChaDesc)) $invalidDisChaDesc[] = "Row {$index}";
|
$plantCode = trim($row[4]);
|
||||||
if (empty($CustomerCode)) $invalidCustomerCode[] = "Row {$index}";
|
$CustomerCode = trim($row[5]);
|
||||||
|
$DocNo = trim($row[6]);
|
||||||
|
$DocDate = trim($row[7]);
|
||||||
|
$CusTradeName = trim($row[9]);
|
||||||
|
$CusLocation = trim($row[10]);
|
||||||
|
|
||||||
|
// if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
||||||
|
if (empty($DisChaDesc)) $invalidDisChaDesc[] = "Row {$index}";
|
||||||
|
if (empty($CustomerCode)) $invalidCustomerCode[] = "Row {$index}";
|
||||||
if (empty($DocNo)) $invalidDocNo[] = "Row {$index}";
|
if (empty($DocNo)) $invalidDocNo[] = "Row {$index}";
|
||||||
if (empty($CusTradeName)) $invalidCusTradeName[] = "Row {$index}";
|
if (empty($CusTradeName)) $invalidCusTradeName[] = "Row {$index}";
|
||||||
if (empty($CusLocation)) $invalidCusLocation[] = "Row {$index}";
|
if (empty($CusLocation)) $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;
|
||||||
}
|
}
|
||||||
|
if (!is_numeric($plantCode)) {
|
||||||
|
$invalidPlantType[] = $plantCode;
|
||||||
|
}
|
||||||
else if(!Plant::where('code', $plantCode)->first())
|
else if(!Plant::where('code', $plantCode)->first())
|
||||||
{
|
{
|
||||||
$invalidPlaCoFound[] = $plantCode;
|
$invalidPlaCoFound[] = $plantCode;
|
||||||
@@ -241,7 +246,7 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
|
|
||||||
//!empty($invalidDisChaDesc) ||
|
//!empty($invalidDisChaDesc) ||
|
||||||
|
|
||||||
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation) || !empty($invalidUser))
|
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation))
|
||||||
{
|
{
|
||||||
$errorMsg = '';
|
$errorMsg = '';
|
||||||
|
|
||||||
@@ -251,7 +256,6 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
|
if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
|
||||||
if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
|
if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
|
||||||
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
|
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
|
||||||
if (!empty($invalidUser)) $errorMsg .= 'Missing User in rows: ' . implode(', ', $invalidUser) . '<br>';
|
|
||||||
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Missing Mandatory Fields')
|
->title('Missing Mandatory Fields')
|
||||||
@@ -265,6 +269,30 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if (!empty($invalidDocNo) || !empty($invalidUser))
|
||||||
|
// {
|
||||||
|
// $errorMsg = '';
|
||||||
|
|
||||||
|
// //if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>';
|
||||||
|
// //if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '<br>';
|
||||||
|
// if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>';
|
||||||
|
// //if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
|
||||||
|
// //if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
|
||||||
|
// //if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
|
||||||
|
// //if (!empty($invalidUser)) $errorMsg .= 'Missing User in rows: ' . implode(', ', $invalidUser) . '<br>';
|
||||||
|
|
||||||
|
// Notification::make()
|
||||||
|
// ->title('Missing Mandatory Fields')
|
||||||
|
// ->body($errorMsg)
|
||||||
|
// ->danger()
|
||||||
|
// ->send();
|
||||||
|
|
||||||
|
// if ($disk->exists($path)) {
|
||||||
|
// $disk->delete($path);
|
||||||
|
// }
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
if (!empty($invalidPlantCode)) {
|
if (!empty($invalidPlantCode)) {
|
||||||
$invalidPlantCode = array_unique($invalidPlantCode);
|
$invalidPlantCode = array_unique($invalidPlantCode);
|
||||||
Notification::make()
|
Notification::make()
|
||||||
@@ -277,6 +305,19 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if(!empty($invalidPlantType))
|
||||||
|
{
|
||||||
|
$invalidPlantType = array_unique($invalidPlantType);
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid Plant Codes')
|
||||||
|
->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)) {
|
if (!empty($invalidPlaCoFound)) {
|
||||||
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
|
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
|
||||||
Notification::make()
|
Notification::make()
|
||||||
@@ -308,13 +349,13 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
foreach ($rows as $index => $row) {
|
foreach ($rows as $index => $row) {
|
||||||
if ($index == 0) continue;
|
if ($index == 0) continue;
|
||||||
|
|
||||||
$plantCode = trim($row[0]);
|
$DisChaDesc = trim($row[3]);
|
||||||
$DisChaDesc = trim($row[1]);
|
$plantCode = trim($row[4]);
|
||||||
$CustomerCode = trim($row[2]);
|
$CustomerCode = trim($row[5]);
|
||||||
$DocNo = trim($row[3]);
|
$DocNo = trim($row[6]);
|
||||||
$DocDate = trim($row[4]);
|
$DocDate = trim($row[7]);
|
||||||
$CusTradeName = trim($row[5]);
|
$CusTradeName = trim($row[9]);
|
||||||
$CusLocation = trim($row[6]);
|
$CusLocation = trim($row[10]);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -375,9 +416,9 @@ class InvoiceDataValidationResource extends Resource
|
|||||||
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');
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use App\Models\InvoiceOutValidation;
|
|||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
use Filament\Resources\Resource;
|
use Filament\Resources\Resource;
|
||||||
@@ -23,6 +24,7 @@ use Filament\Notifications\Notification;
|
|||||||
use Maatwebsite\Excel\Facades\Excel;
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
use Storage;
|
use Storage;
|
||||||
use Filament\Tables\Actions\ExportAction;
|
use Filament\Tables\Actions\ExportAction;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
|
||||||
|
|
||||||
class InvoiceOutValidationResource extends Resource
|
class InvoiceOutValidationResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -183,10 +185,10 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
{
|
{
|
||||||
if ($index == 0) continue; // Skip header
|
if ($index == 0) continue; // Skip header
|
||||||
|
|
||||||
$plantCode = trim($row[0]);
|
|
||||||
$qrCode = trim($row[1]);
|
$qrCode = trim($row[1]);
|
||||||
$scannedAt = trim($row[2]);
|
$plantCode = trim($row[2]);
|
||||||
$scannedby = trim($row[3]);
|
$scannedAt = trim($row[3]);
|
||||||
|
$scannedby = trim($row[4]);
|
||||||
//$createdBy = trim($row[4]);
|
//$createdBy = trim($row[4]);
|
||||||
|
|
||||||
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
|
||||||
@@ -267,67 +269,167 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
return;
|
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;
|
$successCount = 0;
|
||||||
$failCount = 0;
|
$failedRecords = [];
|
||||||
|
|
||||||
$lastErrorMessage = null;
|
DB::beginTransaction();
|
||||||
|
|
||||||
foreach ($rows as $index => $row) {
|
try {
|
||||||
if ($index == 0) continue;
|
foreach ($rows as $index => $row) {
|
||||||
|
if ($index == 0) continue; // skip header
|
||||||
|
|
||||||
$plantCode = trim($row[0]);
|
$rowNumber = $index + 1; // For Excel-style numbering
|
||||||
$qrcode = trim($row[1]);
|
|
||||||
$scannedAt = trim($row[2]);
|
|
||||||
$scannedBy = trim($row[3]);
|
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
$qrcode = trim($row[1]);
|
||||||
$plant = Plant::where('code', $plantCode)->first();
|
$plantCode = trim($row[2]);
|
||||||
|
$scannedAt = trim($row[3]);
|
||||||
|
$scannedBy = trim($row[4]);
|
||||||
|
|
||||||
$formattedDate = null;
|
if (empty($qrcode)) {
|
||||||
if (!empty($scannedAt)) {
|
throw new \Exception("Missing QR Code");
|
||||||
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 = Plant::where('code', $plantCode)->first();
|
||||||
'plant_id' => $plant->id,
|
if (!$plant) {
|
||||||
'qr_code' => $qrcode,
|
throw new \Exception("Invalid plant code: {$plantCode}");
|
||||||
'scanned_at' => $formattedDate,
|
}
|
||||||
'scanned_by' => $scannedBy,
|
|
||||||
'created_by' => $operatorName,
|
$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++;
|
$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)
|
DB::commit();
|
||||||
{
|
|
||||||
Notification::make()
|
// ✅ After loop, show result summary
|
||||||
->title('Import Success')
|
if (count($failedRecords) > 0) {
|
||||||
->body("Successfully inserted: {$successCount} records")
|
$failedSummary = collect($failedRecords)
|
||||||
->success()
|
->map(fn($f) => "Row {$f['row']} ({$f['qrcode']}): {$f['error']}")
|
||||||
->send();
|
->take(5) // limit preview to first 5 errors
|
||||||
}
|
->implode("\n");
|
||||||
else {
|
|
||||||
$errorText = $lastErrorMessage
|
Notification::make()
|
||||||
? "Failed to insert any records. Error: {$lastErrorMessage}"
|
->title('Partial Import Warning')
|
||||||
: "Failed to insert any records. Unknown error.";
|
->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()
|
Notification::make()
|
||||||
->title('Import Failed')
|
->title('Import Failed')
|
||||||
->body($errorText)
|
->body("No records were inserted. Error: {$e->getMessage()}")
|
||||||
->danger()
|
->danger()
|
||||||
->send();
|
->send();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user