Add plant code column to UserExporter and implement plant validation in UserImporter

This commit is contained in:
dhanabalan
2025-09-29 12:18:09 +05:30
parent 5f4945d997
commit af3ce0ea40
2 changed files with 29 additions and 2 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Filament\Imports;
use App\Models\Plant;
use App\Models\User;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn;
@@ -17,6 +18,12 @@ class UserImporter extends Importer
public static function getColumns(): array
{
return [
ImportColumn::make('plant')
->requiredMapping()
->exampleHeader('Plant Code')
->example('1000')
->label('PLANT CODE')
->relationship(resolveUsing: 'code'),
ImportColumn::make('name')
->requiredMapping()
->exampleHeader('Name')
@@ -25,9 +32,9 @@ class UserImporter extends Importer
->rules(['required']),//, 'max:255'
ImportColumn::make('email')
->requiredMapping()
->exampleHeader('E-mail')
->exampleHeader('E-Mail')
->example('RAW00001@cripumps.com')
->label('E-mail')
->label('E-Mail')
->rules(['required', 'email']),//, 'max:255'
ImportColumn::make('password')
->requiredMapping()
@@ -47,6 +54,22 @@ class UserImporter extends Importer
public function resolveRecord(): ?User
{
$warnMsg = [];
$plant = null;
if (Str::length($this->data['plant']) > 0) {
if (Str::length($this->data['plant']) < 4 || !is_numeric($this->data['plant'] || !preg_match('/^[1-9]\d{3,}$/', $this->data['plant']))) {
$warnMsg[] = "Invalid plant code found!";
}
else {
$plant = Plant::where('code', $this->data['plant'])->first();
if (!$plant) {
$warnMsg[] = "Plant not found";
}
else {
$plant = $plant->id ?? null;
}
}
}
if (Str::length($this->data['name']) < 1) {
$warnMsg[] = "User name not found!";
}
@@ -85,6 +108,7 @@ class UserImporter extends Importer
[
'name' => $this->data['name'],
'password' => $this->data['password'],
'plant_id' => $plant,
]);
// Assign roles