From 5a218d688d7dad44327144c2208b524cc3af29a9 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 13 Jan 2026 16:19:20 +0530 Subject: [PATCH] Added plant code instead of plant name on import and export --- app/Filament/Exports/BlockExporter.php | 17 ++++++------ app/Filament/Imports/BlockImporter.php | 38 +++++++++++++++----------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/Filament/Exports/BlockExporter.php b/app/Filament/Exports/BlockExporter.php index 08bd1d0..b48b17c 100644 --- a/app/Filament/Exports/BlockExporter.php +++ b/app/Filament/Exports/BlockExporter.php @@ -24,26 +24,27 @@ class BlockExporter 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('name') - ->label('NAME'), + ->label('BLOCK NAME'), ExportColumn::make('created_at') ->label('CREATED AT'), ExportColumn::make('updated_at') - ->label('UPDATED AT'), + ->label('UPDATED AT') + ->enabledByDefault(true), ExportColumn::make('deleted_at') - ->enabledByDefault(false) - ->label('DELETED AT'), + ->label('DELETED AT') + ->enabledByDefault(false), ]; } public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your block export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your block export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.'; if ($failedRowsCount = $export->getFailedRowsCount()) { - $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.'; } return $body; diff --git a/app/Filament/Imports/BlockImporter.php b/app/Filament/Imports/BlockImporter.php index ad0bc98..4e3cc93 100644 --- a/app/Filament/Imports/BlockImporter.php +++ b/app/Filament/Imports/BlockImporter.php @@ -8,7 +8,6 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; -use Notification; use Str; class BlockImporter extends Importer @@ -20,16 +19,16 @@ class BlockImporter extends Importer return [ ImportColumn::make('name') ->requiredMapping() - ->exampleHeader('Block Name') + ->exampleHeader('BLOCK NAME') ->example('Block A') - ->label('Block Name') + ->label('BLOCK NAME') ->rules(['required']), 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') ->rules(['required']), ]; } @@ -37,21 +36,28 @@ class BlockImporter extends Importer public function resolveRecord(): ?Block { $warnMsg = []; - $plant = Plant::where('name', $this->data['plant'])->first(); - if (!$plant) { - $warnMsg[] = "Plant not found"; - // $warnMsg[] = "Plant '" . $this->data['plant'] . "' not found"; + $plantCod = $this->data['plant']; + $plant = null; + 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'; + // $warnMsg[] = "Plant '" . $plantCod . "' not found"; + } } if (Str::length($this->data['name']) < 0) { - $warnMsg[] = "Block name not found"; + $warnMsg[] = 'Block name not found'; } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } + return Block::updateOrCreate([ 'name' => $this->data['name'], - 'plant_id' => $plant->id + 'plant_id' => $plant->id, ]); // return Block::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` @@ -63,10 +69,10 @@ class BlockImporter extends Importer public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your block import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your block import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.'; if ($failedRowsCount = $import->getFailedRowsCount()) { - $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; + $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.'; } return $body;