From db08e07e484a1ccf9be74792b41a4cdd97d1dc21 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 12 May 2025 19:40:33 +0530 Subject: [PATCH] Added uom column and updated validation func. on import --- app/Filament/Imports/ItemImporter.php | 31 ++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/app/Filament/Imports/ItemImporter.php b/app/Filament/Imports/ItemImporter.php index 0a12d11d6..38bccb8e9 100644 --- a/app/Filament/Imports/ItemImporter.php +++ b/app/Filament/Imports/ItemImporter.php @@ -3,9 +3,12 @@ namespace App\Filament\Imports; use App\Models\Item; +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 ItemImporter extends Importer { @@ -33,6 +36,11 @@ class ItemImporter extends Importer ->label('Hourly Quantity') ->numeric() ->rules(['required', 'integer']), + ImportColumn::make('uom') + ->requiredMapping() + ->exampleHeader('Unit of Measure') + ->example('EA') + ->label('Unit of Measure'), ImportColumn::make('plant') ->requiredMapping() ->exampleHeader('Plant Name') @@ -45,9 +53,25 @@ class ItemImporter extends Importer public function resolveRecord(): ?Item { - $plant = \App\Models\Plant::where('name', $this->data['plant'])->first(); + $warnMsg = []; + $plant = Plant::where('name', $this->data['plant'])->first(); if (!$plant) { - return null; + $warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "' + } + if (Str::length($this->data['code']) < 6 || !ctype_alnum($this->data['code'])) { + $warnMsg[] = "Invalid item code found"; + } + // if (Str::length($this->data['uom']) <= 0) { + // $warnMsg[] = "Invalid unit of measure found"; + // } + if (Str::length($this->data['description']) < 5) { + $warnMsg[] = "Invalid description found"; + } + if (Str::length($this->data['hourly_quantity']) < 0 || !is_numeric($this->data['hourly_quantity']) || $this->data['hourly_quantity'] <= 0) { + $warnMsg[] = "Invalid hourly quantity found"; + } + if (!empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); } return Item::updateOrCreate([ 'code' => $this->data['code'], @@ -55,7 +79,8 @@ class ItemImporter extends Importer ], [ 'description' => $this->data['description'], - 'hourly_quantity' => $this->data['hourly_quantity'] + 'hourly_quantity' => $this->data['hourly_quantity'], + 'uom' => $this->data['uom'] ] ); // return new Item;