diff --git a/app/Filament/Exports/GuardNameExporter.php b/app/Filament/Exports/GuardNameExporter.php index 5337331..e03ac25 100644 --- a/app/Filament/Exports/GuardNameExporter.php +++ b/app/Filament/Exports/GuardNameExporter.php @@ -24,8 +24,8 @@ class GuardNameExporter 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('GUARD NAME'), ExportColumn::make('identification1') @@ -46,10 +46,10 @@ class GuardNameExporter extends Exporter public static function getCompletedNotificationBody(Export $export): string { - $body = 'Your guard name export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + $body = 'Your guard 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/GuardNameImporter.php b/app/Filament/Imports/GuardNameImporter.php index 416fad5..276fa9f 100644 --- a/app/Filament/Imports/GuardNameImporter.php +++ b/app/Filament/Imports/GuardNameImporter.php @@ -20,10 +20,10 @@ class GuardNameImporter 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() @@ -54,32 +54,38 @@ class GuardNameImporter extends Importer public function resolveRecord(): ?GuardName { $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 guard name found"; + $warnMsg[] = 'Invalid guard name found'; } if (Str::length($this->data['identification1']) < 5) { - $warnMsg[] = "Invalid identification-1 found"; + $warnMsg[] = 'Invalid identification-1 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 GuardName::updateOrCreate([ 'name' => $this->data['name'], - 'plant_id' => $plant->id + 'plant_id' => $plant->id, ], - [ - 'identification1' => $this->data['identification1'], - 'identification2' => $this->data['identification2'], - 'created_by' => $this->data['created_by'] + [ + 'identification1' => $this->data['identification1'], + 'identification2' => $this->data['identification2'], + 'created_by' => $this->data['created_by'], ] ); @@ -87,15 +93,15 @@ class GuardNameImporter extends Importer // // // Update existing records, matching them by `$this->data['column_name']` // // 'email' => $this->data['email'], // // ]); - //return new GuardName(); + // return new GuardName(); } public static function getCompletedNotificationBody(Import $import): string { - $body = 'Your guard name import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + $body = 'Your guard 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;