From 1e57326e8e00853f95ac8b9c7ef43b38eefaaa85 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 13 Jan 2026 16:18:10 +0530 Subject: [PATCH] Added plant code instead of plant name on import and export --- .../Exports/CheckPointNameExporter.php | 8 ++-- .../Imports/CheckPointNameImporter.php | 38 +++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/Filament/Exports/CheckPointNameExporter.php b/app/Filament/Exports/CheckPointNameExporter.php index 315c90d..566ace1 100644 --- a/app/Filament/Exports/CheckPointNameExporter.php +++ b/app/Filament/Exports/CheckPointNameExporter.php @@ -24,8 +24,8 @@ class CheckPointNameExporter 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('CHECK POINT NAME'), ExportColumn::make('created_at') @@ -42,10 +42,10 @@ class CheckPointNameExporter extends Exporter public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your check point name export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your check point name 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/CheckPointNameImporter.php b/app/Filament/Imports/CheckPointNameImporter.php index 9f447bc..96ce37e 100644 --- a/app/Filament/Imports/CheckPointNameImporter.php +++ b/app/Filament/Imports/CheckPointNameImporter.php @@ -20,10 +20,10 @@ class CheckPointNameImporter extends Importer return [ 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']), ImportColumn::make('name') ->requiredMapping() @@ -43,27 +43,33 @@ class CheckPointNameImporter extends Importer public function resolveRecord(): ?CheckPointName { $warnMsg = []; - $plant = Plant::where('name', $this->data['plant'])->first(); - if (!$plant) { - $warnMsg[] = "Plant not found"; // '" . $this->data['plant'] . "' + $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'; // '" . $plantCod . "' + } } if (Str::length($this->data['name']) < 3) { // || !ctype_alnum($this->data['name']) - $warnMsg[] = "Invalid check point name found"; + $warnMsg[] = 'Invalid check point name found'; } $createdBy = $this->data['created_by']; if (Str::length($createdBy) < 3) { // || !ctype_alnum($createdBy) - $warnMsg[] = "Invalid created by name found"; + $warnMsg[] = 'Invalid created by name found'; } - if (!empty($warnMsg)) { + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } return CheckPointName::updateOrCreate([ 'name' => $this->data['name'], - 'plant_id' => $plant->id - ], - [ - 'created_by' => $this->data['created_by'] + 'plant_id' => $plant->id, + ], + [ + 'created_by' => $this->data['created_by'], ] ); // // return CheckPointName::firstOrNew([ @@ -76,10 +82,10 @@ class CheckPointNameImporter extends Importer public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your check point name import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your check point name 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;