diff --git a/app/Filament/Imports/PlantImporter.php b/app/Filament/Imports/PlantImporter.php index 569966f..270f244 100644 --- a/app/Filament/Imports/PlantImporter.php +++ b/app/Filament/Imports/PlantImporter.php @@ -2,10 +2,13 @@ namespace App\Filament\Imports; +use App\Models\Company; use App\Models\Plant; +use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; +use Str; class PlantImporter extends Importer { @@ -45,17 +48,33 @@ class PlantImporter extends Importer 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) { - 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) { - $plantCode = \App\Models\Plant::where('code', $this->data['code'])->first(); - $plantName = \App\Models\Plant::where('name', $this->data['name'])->first(); - if ($plantCode || $plantName) { - return null; + $plantCode = Plant::where('code', $this->data['code'])->first(); + $plantName = Plant::where('name', $this->data['name'])->first(); + if ($plantName) { + throw new RowImportFailedException("Duplicate plant name found"); + } + else if ($plantCode) { + throw new RowImportFailedException("Duplicate plant code found"); } }