Added validation in gr master importer
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 19s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 15s
Laravel Larastan / larastan (pull_request) Failing after 7m6s
Laravel Pint / pint (pull_request) Successful in 7m5s

This commit is contained in:
dhanabalan
2026-11-03 11:10:07 +05:30
parent d4186d11c9
commit 20e64ba356

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));
} else { // if (empty($warnMsg))
$grMaster = GrMaster::where('plant_id', $plant->id)
->where('serial_number', $this->data['serial_number']) ->where('serial_number', $this->data['serial_number'])
->latest()
->first(); ->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)
->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, 'plant_id' => $plant->id,
'item_id' => $item->id,
'serial_number' => $this->data['serial_number'],
'gr_number' => $this->data['gr_number'] ?? null, 'gr_number' => $this->data['gr_number'] ?? null,
'created_by' => $this->data['created_by'], '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();