Add plant code column to UserExporter and implement plant validation in UserImporter
This commit is contained in:
@@ -14,6 +14,7 @@ class UserExporter extends Exporter
|
|||||||
public static function getColumns(): array
|
public static function getColumns(): array
|
||||||
{
|
{
|
||||||
static $rowNumber = 0;
|
static $rowNumber = 0;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
// ExportColumn::make('id')
|
// ExportColumn::make('id')
|
||||||
// ->label('ID'),
|
// ->label('ID'),
|
||||||
@@ -23,6 +24,8 @@ class UserExporter extends Exporter
|
|||||||
// Increment and return the row number
|
// Increment and return the row number
|
||||||
return ++$rowNumber;
|
return ++$rowNumber;
|
||||||
}),
|
}),
|
||||||
|
ExportColumn::make('plant.code')
|
||||||
|
->label('PLANT CODE'),
|
||||||
ExportColumn::make('name')
|
ExportColumn::make('name')
|
||||||
->label('NAME'),
|
->label('NAME'),
|
||||||
ExportColumn::make('email')
|
ExportColumn::make('email')
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Filament\Imports;
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Plant;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||||
use Filament\Actions\Imports\ImportColumn;
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
@@ -17,6 +18,12 @@ class UserImporter extends Importer
|
|||||||
public static function getColumns(): array
|
public static function getColumns(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
ImportColumn::make('plant')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Plant Code')
|
||||||
|
->example('1000')
|
||||||
|
->label('PLANT CODE')
|
||||||
|
->relationship(resolveUsing: 'code'),
|
||||||
ImportColumn::make('name')
|
ImportColumn::make('name')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Name')
|
->exampleHeader('Name')
|
||||||
@@ -25,9 +32,9 @@ class UserImporter extends Importer
|
|||||||
->rules(['required']),//, 'max:255'
|
->rules(['required']),//, 'max:255'
|
||||||
ImportColumn::make('email')
|
ImportColumn::make('email')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('E-mail')
|
->exampleHeader('E-Mail')
|
||||||
->example('RAW00001@cripumps.com')
|
->example('RAW00001@cripumps.com')
|
||||||
->label('E-mail')
|
->label('E-Mail')
|
||||||
->rules(['required', 'email']),//, 'max:255'
|
->rules(['required', 'email']),//, 'max:255'
|
||||||
ImportColumn::make('password')
|
ImportColumn::make('password')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
@@ -47,6 +54,22 @@ class UserImporter extends Importer
|
|||||||
public function resolveRecord(): ?User
|
public function resolveRecord(): ?User
|
||||||
{
|
{
|
||||||
$warnMsg = [];
|
$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) {
|
if (Str::length($this->data['name']) < 1) {
|
||||||
$warnMsg[] = "User name not found!";
|
$warnMsg[] = "User name not found!";
|
||||||
}
|
}
|
||||||
@@ -85,6 +108,7 @@ class UserImporter extends Importer
|
|||||||
[
|
[
|
||||||
'name' => $this->data['name'],
|
'name' => $this->data['name'],
|
||||||
'password' => $this->data['password'],
|
'password' => $this->data['password'],
|
||||||
|
'plant_id' => $plant,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Assign roles
|
// Assign roles
|
||||||
|
|||||||
Reference in New Issue
Block a user