diff --git a/app/Filament/Exports/ItemExporter.php b/app/Filament/Exports/ItemExporter.php index 4bd5f6b..7bd8f0f 100644 --- a/app/Filament/Exports/ItemExporter.php +++ b/app/Filament/Exports/ItemExporter.php @@ -24,8 +24,8 @@ class ItemExporter extends Exporter // Increment and return the row number return ++$rowNumber; }), - ExportColumn::make('plant.name') - ->label('PLANT'), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), ExportColumn::make('category') ->label('CATEGORY'), ExportColumn::make('code') diff --git a/app/Filament/Imports/ItemImporter.php b/app/Filament/Imports/ItemImporter.php index ae05772..659a5e9 100644 --- a/app/Filament/Imports/ItemImporter.php +++ b/app/Filament/Imports/ItemImporter.php @@ -8,7 +8,8 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; -use Str; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; class ItemImporter extends Importer { @@ -48,10 +49,10 @@ class ItemImporter extends Importer ->label('Unit of Measure'), ImportColumn::make('plant') ->requiredMapping() - ->exampleHeader('Plant Name') - ->example('Ransar Industries-I') - ->label('Plant Name') - ->relationship(resolveUsing: 'name') + ->exampleHeader('Plant Code') + ->example('1000') + ->label('Plant Code') + ->relationship(resolveUsing: 'code') // Lookup Plant by code column ->rules(['required']), ]; } @@ -59,36 +60,46 @@ class ItemImporter extends Importer public function resolveRecord(): ?Item { $warnMsg = []; + $plantCod = $this->data['plant']; + $plant = null; + Log::info('ResolveRecord triggered', $this->data); $iCode = trim($this->data['code']); $description = trim($this->data['description']); - $plant = Plant::where('name', $this->data['plant'])->first(); - if (!$plant) { - $warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "' + + if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found'; // '" . $plantCod . "' + } } - if (Str::length($iCode) < 6 || !ctype_alnum($iCode)) { - $warnMsg[] = "Invalid item code found"; + + if (Str::length($iCode) < 6 || ! ctype_alnum($iCode)) { + $warnMsg[] = 'Invalid item code found'; } // if (Str::length($this->data['uom']) <= 0) { // $warnMsg[] = "Invalid unit of measure found"; // } if (Str::length($description) < 5) { - $warnMsg[] = "Invalid description found"; + $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 (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)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } + return Item::updateOrCreate([ - 'code' => $iCode, - 'plant_id' => $plant->id - ], + 'code' => $iCode, + 'plant_id' => $plant->id, + ], [ 'category' => trim($this->data['category']), 'description' => $description, 'hourly_quantity' => $this->data['hourly_quantity'], - 'uom' => trim($this->data['uom']) + 'uom' => trim($this->data['uom']), ] ); // return new Item;