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