Updated import validation functionality

This commit is contained in:
dhanabalan
2025-05-06 12:35:20 +05:30
parent f698909cd2
commit 4108a438c7

View File

@@ -2,10 +2,13 @@
namespace App\Filament\Imports; namespace App\Filament\Imports;
use App\Models\Company;
use App\Models\Plant; use App\Models\Plant;
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 Str;
class PlantImporter extends Importer class PlantImporter extends Importer
{ {
@@ -45,17 +48,33 @@ class PlantImporter extends Importer
public function resolveRecord(): ?Plant public function resolveRecord(): ?Plant
{ {
$company = \App\Models\Company::where('name', $this->data['company'])->first(); $warnMsg = [];
$company = Company::where('name', $this->data['company'])->first();
if (!$company) { if (!$company) {
return null; $warnMsg[] = "Company name not found";
}
if (Str::length($this->data['name']) < 0) {
$warnMsg[] = "Plant name not found";
}
if (Str::length($this->data['code']) < 4 || !is_numeric($this->data['code']) || !preg_match('/^[1-9]\d{3,}$/', $this->data['code'])) {
$warnMsg[] = "Invalid plant code found";
}
if (Str::length($this->data['address']) < 3) {
$warnMsg[] = "Invalid address found";
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg));
} }
$plantCN = \App\Models\Plant::where('code', $this->data['code'])->where('name', $this->data['name'])->first(); $plantCN = Plant::where('code', $this->data['code'])->where('name', $this->data['name'])->first();
if (!$plantCN) { if (!$plantCN) {
$plantCode = \App\Models\Plant::where('code', $this->data['code'])->first(); $plantCode = Plant::where('code', $this->data['code'])->first();
$plantName = \App\Models\Plant::where('name', $this->data['name'])->first(); $plantName = Plant::where('name', $this->data['name'])->first();
if ($plantCode || $plantName) { if ($plantName) {
return null; throw new RowImportFailedException("Duplicate plant name found");
}
else if ($plantCode) {
throw new RowImportFailedException("Duplicate plant code found");
} }
} }