Added validation in gr master importer #1366

Merged
jothi merged 1 commits from ranjith-dev into master 2026-11-03 05:40:22 +00:00

View File

@@ -10,6 +10,7 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Str;
class GrMasterImporter extends Importer
@@ -67,6 +68,8 @@ class GrMasterImporter extends Importer
$plant = null;
$item = null;
$userName = Filament::auth()->user()?->name;
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found';
} else {
@@ -94,26 +97,38 @@ class GrMasterImporter extends Importer
$warnMsg[] = 'User not found';
}
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
} else { // if (empty($warnMsg))
$grMaster = GrMaster::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number'])
->latest()
->first();
$serialExists = GrMaster::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number'])
->first();
if ($grMaster) {
throw new RowImportFailedException('Serial number already exist!');
}
if ($serialExists) {
$warnMsg[] = 'Serial number already exists for this plant!';
}
GrMaster::updateOrCreate([
'plant_id' => $plant->id,
'item_id' => $item->id,
'serial_number' => $this->data['serial_number'],
'gr_number' => $this->data['gr_number'] ?? null,
'created_by' => $this->data['created_by'],
]);
$grNumberExists = GrMaster::where('plant_id', $plant->id)
->where('gr_number', $this->data['gr_number'])
->first();
if ($grNumberExists) {
$warnMsg[] = 'GR Number already exists for this plant!';
}
if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
}
GrMaster::updateOrCreate(
[
'plant_id' => $plant->id,
'gr_number' => $this->data['gr_number'] ?? null,
'serial_number' => $this->data['serial_number'],
],
[
'item_id' => $item->id,
'created_by' => $userName,
'updated_by' => $userName,
]
);
return null;
// return new GrMaster();