Merge pull request 'Added validation in gr master importer' (#1366) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 18s

Reviewed-on: #1366
This commit was merged in pull request #1366.
This commit is contained in:
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\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\Facades\Filament;
use Str; use Str;
class GrMasterImporter extends Importer class GrMasterImporter extends Importer
@@ -67,6 +68,8 @@ class GrMasterImporter extends Importer
$plant = null; $plant = null;
$item = null; $item = null;
$userName = Filament::auth()->user()?->name;
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found'; $warnMsg[] = 'Invalid plant code found';
} else { } else {
@@ -94,26 +97,38 @@ class GrMasterImporter extends Importer
$warnMsg[] = 'User not found'; $warnMsg[] = 'User not found';
} }
if (! empty($warnMsg)) { $serialExists = GrMaster::where('plant_id', $plant->id)
throw new RowImportFailedException(implode(', ', $warnMsg)); ->where('serial_number', $this->data['serial_number'])
} else { // if (empty($warnMsg)) ->first();
$grMaster = GrMaster::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number'])
->latest()
->first();
if ($grMaster) { if ($serialExists) {
throw new RowImportFailedException('Serial number already exist!'); $warnMsg[] = 'Serial number already exists for this plant!';
}
} }
GrMaster::updateOrCreate([ $grNumberExists = GrMaster::where('plant_id', $plant->id)
'plant_id' => $plant->id, ->where('gr_number', $this->data['gr_number'])
'item_id' => $item->id, ->first();
'serial_number' => $this->data['serial_number'],
'gr_number' => $this->data['gr_number'] ?? null, if ($grNumberExists) {
'created_by' => $this->data['created_by'], $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 null;
// return new GrMaster(); // return new GrMaster();