Added plant code instead of plant name on import and export
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-13 16:19:20 +05:30
parent 1e57326e8e
commit 5a218d688d
2 changed files with 31 additions and 24 deletions

View File

@@ -24,17 +24,18 @@ class BlockExporter extends Exporter
// Increment and return the row number // Increment and return the row number
return ++$rowNumber; return ++$rowNumber;
}), }),
ExportColumn::make('plant.name') ExportColumn::make('plant.code')
->label('PLANT'), ->label('PLANT CODE'),
ExportColumn::make('name') ExportColumn::make('name')
->label('NAME'), ->label('BLOCK NAME'),
ExportColumn::make('created_at') ExportColumn::make('created_at')
->label('CREATED AT'), ->label('CREATED AT'),
ExportColumn::make('updated_at') ExportColumn::make('updated_at')
->label('UPDATED AT'), ->label('UPDATED AT')
->enabledByDefault(true),
ExportColumn::make('deleted_at') ExportColumn::make('deleted_at')
->enabledByDefault(false) ->label('DELETED AT')
->label('DELETED AT'), ->enabledByDefault(false),
]; ];
} }

View File

@@ -8,7 +8,6 @@ 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 Notification;
use Str; use Str;
class BlockImporter extends Importer class BlockImporter extends Importer
@@ -20,16 +19,16 @@ class BlockImporter extends Importer
return [ return [
ImportColumn::make('name') ImportColumn::make('name')
->requiredMapping() ->requiredMapping()
->exampleHeader('Block Name') ->exampleHeader('BLOCK NAME')
->example('Block A') ->example('Block A')
->label('Block Name') ->label('BLOCK NAME')
->rules(['required']), ->rules(['required']),
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('PLANT CODE')
->example('Ransar Industries-I') ->example('1000')
->label('Plant Name') ->label('PLANT CODE')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
]; ];
} }
@@ -37,21 +36,28 @@ class BlockImporter extends Importer
public function resolveRecord(): ?Block public function resolveRecord(): ?Block
{ {
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $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) { if (! $plant) {
$warnMsg[] = "Plant not found"; $warnMsg[] = 'Plant not found';
// $warnMsg[] = "Plant '" . $this->data['plant'] . "' not found"; // $warnMsg[] = "Plant '" . $plantCod . "' not found";
}
} }
if (Str::length($this->data['name']) < 0) { 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)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
return Block::updateOrCreate([ return Block::updateOrCreate([
'name' => $this->data['name'], 'name' => $this->data['name'],
'plant_id' => $plant->id 'plant_id' => $plant->id,
]); ]);
// return Block::firstOrNew([ // return Block::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`