1
0
forked from poc/pds

Updated alignment and validation logic on import

This commit is contained in:
dhanabalan
2026-01-13 16:43:48 +05:30
parent 9bed5f6937
commit 2521c04f03

View File

@@ -2,15 +2,14 @@
namespace App\Filament\Imports;
use App\Models\Plant;
use App\Models\StickerPrinting;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use App\Models\Plant;
use App\Models\User;
use Str;
use Filament\Facades\Filament;
use Str;
class StickerPrintingImporter extends Importer
{
@@ -49,30 +48,26 @@ class StickerPrintingImporter extends Importer
// ]);
$warnMsg = [];
$plant = Plant::where('code', $this->data['plant'])->first();
if (Str::length($this->data['serial_number']) < 9 || !ctype_alnum($this->data['serial_number'])) {
$warnMsg[] = "Invalid serial number found";
}
$existing = StickerPrinting::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number'])
->first();
if ($existing) {
$warnMsg[] = "Serial number already exists for this plant!";//throw new RowImportFailedException("Serial number already exists for this plant!");
}
$plantCod = $this->data['plant'];
$plant = null;
$serial = $this->data['serial_number'];
// --- Check duplicate in DB ---
$existsInDB = StickerPrinting::where('plant_id', $plant->id)
->where('serial_number', $serial)
->first();
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found';
} else {
$plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
$warnMsg[] = 'Plant not found!';
}
}
if ($existsInDB) {
//throw new RowImportFailedException("Serial number '{$serial}' already exists in DB for this plant!");
$warnMsg[] = "Serial number '{$serial}' already exists in DB for this plant!";
if (Str::length($serial) < 9 || ! ctype_alnum($serial)) {
$warnMsg[] = 'Invalid serial number found';
} elseif ($plant) {
$existing = StickerPrinting::where('plant_id', $plant->id)->where('serial_number', $serial)->first();
if ($existing) {
$warnMsg[] = "Serial number '{$serial}' already exists for plant '{$plantCod}'!"; // throw new RowImportFailedException("Serial number already exists for this plant!");
}
}
if (! empty($warnMsg)) {