Merge pull request 'Updated validations on Importer and Resource file' (#553) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Reviewed-on: #553
This commit was merged in pull request #553.
This commit is contained in:
@@ -17,31 +17,31 @@ class PlantImporter extends Importer
|
|||||||
public static function getColumns(): array
|
public static function getColumns(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
ImportColumn::make('company')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('COMPANY')
|
||||||
|
->example('C.R.I. Pumps Private Limited')
|
||||||
|
->label('COMPANY')
|
||||||
|
->relationship(resolveUsing: 'name')
|
||||||
|
->rules(['required']),
|
||||||
ImportColumn::make('code')
|
ImportColumn::make('code')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->numeric()
|
->numeric()
|
||||||
->exampleHeader('Plant Code')
|
->exampleHeader('CODE')
|
||||||
->example('1000')
|
->example('1000')
|
||||||
->label('Plant Code')
|
->label('CODE')
|
||||||
->rules(['required']), // , 'integer'
|
->rules(['required']),
|
||||||
ImportColumn::make('name')
|
ImportColumn::make('name')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plant Name')
|
->exampleHeader('NAME')
|
||||||
->example('Ransar Industries-I')
|
->example('Ransar Industries-I')
|
||||||
->label('Plant Name')
|
->label('NAME')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('address')
|
ImportColumn::make('address')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plant Address')
|
->exampleHeader('ADDRESS')
|
||||||
->example('7/51-A, Keeranatham Road, Saravanampatty, Coimbatore - 641035')
|
->example('7/51-A, Keeranatham Road, Saravanampatty, Coimbatore - 641035')
|
||||||
->label('Plant Address')
|
->label('ADDRESS')
|
||||||
->rules(['required']),
|
|
||||||
ImportColumn::make('company')
|
|
||||||
->requiredMapping()
|
|
||||||
->exampleHeader('Company Name')
|
|
||||||
->example('C.R.I. Pumps Private Limited')
|
|
||||||
->label('Company Name')
|
|
||||||
->relationship(resolveUsing: 'name')
|
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -49,41 +49,66 @@ class PlantImporter extends Importer
|
|||||||
public function resolveRecord(): ?Plant
|
public function resolveRecord(): ?Plant
|
||||||
{
|
{
|
||||||
$warnMsg = [];
|
$warnMsg = [];
|
||||||
$company = Company::where('name', $this->data['company'])->first();
|
$compId = null;
|
||||||
if (! $company) {
|
$comp = trim($this->data['company']) ?? null;
|
||||||
$warnMsg[] = 'Company name not found';
|
$code = trim($this->data['code']) ?? null;
|
||||||
|
$name = trim($this->data['name']) ?? null;
|
||||||
|
$addr = trim($this->data['address']) ?? null;
|
||||||
|
|
||||||
|
if ($comp == null || $comp == '' || ! $comp) {
|
||||||
|
$warnMsg[] = "Company name can't be empty!";
|
||||||
|
} elseif (Str::length($comp) < 5) {
|
||||||
|
$warnMsg[] = 'Company name should contain at least 5 characters!';
|
||||||
}
|
}
|
||||||
if (Str::length($this->data['name']) < 0) {
|
if ($code == null || $code == '') {
|
||||||
$warnMsg[] = 'Plant name not found';
|
$warnMsg[] = "Code can't be empty!";
|
||||||
|
} elseif (! is_numeric($code)) {
|
||||||
|
$warnMsg[] = 'Code should contain only numeric values!';
|
||||||
|
} elseif (Str::length($code) < 4 || Str::length($code) > 7) {
|
||||||
|
$warnMsg[] = 'Code must be between 4 and 7 digits only!';
|
||||||
|
} elseif (! preg_match('/^[1-9]\d{6,}$/', $code)) {
|
||||||
|
$warnMsg[] = 'Invalid plant code found!';
|
||||||
}
|
}
|
||||||
if (Str::length($this->data['code']) < 4 || ! is_numeric($this->data['code']) || ! preg_match('/^[1-9]\d{3,}$/', $this->data['code'])) {
|
if ($name == null || $name == '' || ! $name) {
|
||||||
$warnMsg[] = 'Invalid plant code found';
|
$warnMsg[] = "Name can't be empty!";
|
||||||
|
} elseif (Str::length($name) < 5) {
|
||||||
|
$warnMsg[] = 'Name should contain at least 5 characters!';
|
||||||
}
|
}
|
||||||
if (Str::length($this->data['address']) < 3) {
|
if ($addr == null || $addr == '' || ! $addr) {
|
||||||
$warnMsg[] = 'Invalid address found';
|
$warnMsg[] = "Address can't be empty!";
|
||||||
|
} elseif (Str::length($addr) < 5) {
|
||||||
|
$warnMsg[] = 'Address should contain at least 5 characters!';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! empty($warnMsg)) {
|
if (! empty($warnMsg)) {
|
||||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantCN = Plant::where('code', $this->data['code'])->where('name', $this->data['name'])->first();
|
$company = Company::where('name', $comp)->first();
|
||||||
|
if (! $company) {
|
||||||
|
throw new RowImportFailedException('Company name not found!');
|
||||||
|
} else {
|
||||||
|
$compId = $company->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$plantCN = Plant::where('code', $code)->where('name', $name)->first();
|
||||||
if (! $plantCN) {
|
if (! $plantCN) {
|
||||||
$plantCode = Plant::where('code', $this->data['code'])->first();
|
$plantCode = Plant::where('code', $code)->first();
|
||||||
$plantName = Plant::where('name', $this->data['name'])->first();
|
$plantName = Plant::where('name', $name)->first();
|
||||||
if ($plantName) {
|
if ($plantName) {
|
||||||
throw new RowImportFailedException('Duplicate plant name found');
|
throw new RowImportFailedException('Duplicate plant name found!');
|
||||||
} elseif ($plantCode) {
|
} elseif ($plantCode) {
|
||||||
throw new RowImportFailedException('Duplicate plant code found');
|
throw new RowImportFailedException('Duplicate plant code found!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Plant::updateOrCreate([
|
return Plant::updateOrCreate([
|
||||||
'code' => $this->data['code'],
|
'code' => $code,
|
||||||
'name' => $this->data['name'],
|
'name' => $name,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'address' => $this->data['address'],
|
'address' => $addr,
|
||||||
'company_id' => $company->id,
|
'company_id' => $compId,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
// return Plant::firstOrNew([
|
// return Plant::firstOrNew([
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ use Filament\Tables\Table;
|
|||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
|
||||||
|
// use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
class PlantResource extends Resource
|
class PlantResource extends Resource
|
||||||
{
|
{
|
||||||
protected static ?string $model = Plant::class;
|
protected static ?string $model = Plant::class;
|
||||||
@@ -42,18 +44,21 @@ class PlantResource extends Resource
|
|||||||
->reactive()
|
->reactive()
|
||||||
->placeholder('Scan the valid code')
|
->placeholder('Scan the valid code')
|
||||||
->autofocus(true)
|
->autofocus(true)
|
||||||
|
// ->minLength(4)
|
||||||
|
// ->maxLength(7)
|
||||||
->minValue(1000)
|
->minValue(1000)
|
||||||
|
->maxValue(9999999)
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$codeId = $get('code');
|
$codeId = $get('code');
|
||||||
// Ensure `linestop_id` is not cleared
|
// Ensure `linestop_id` is not cleared
|
||||||
if (!$codeId) {
|
if (! $codeId) {
|
||||||
$set('pCodeError', 'Scan the valid code.');
|
$set('pCodeError', 'Scan the valid code.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else
|
if (strlen($codeId) < 4 || strlen($codeId) > 7) {
|
||||||
{
|
$set('pCodeError', 'Code must be between 4 and 7 digits only!'); // Code must be at least 4 digits.
|
||||||
if (strlen($codeId) < 4) {
|
|
||||||
$set('pCodeError', 'Code must be at least 4 digits.');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if (!preg_match('/^[1-9][0-9]{3,}$/', $codeId)) {
|
// if (!preg_match('/^[1-9][0-9]{3,}$/', $codeId)) {
|
||||||
@@ -64,6 +69,9 @@ class PlantResource extends Resource
|
|||||||
$set('pCodeError', null);
|
$set('pCodeError', null);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// ->rule(function (callable $get) {
|
||||||
|
// return Rule::unique('plants', 'code')->ignore($get('id'));
|
||||||
|
// })
|
||||||
->extraAttributes(fn ($get) => [
|
->extraAttributes(fn ($get) => [
|
||||||
'class' => $get('pCodeError') ? 'border-red-500' : '',
|
'class' => $get('pCodeError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
@@ -77,16 +85,15 @@ class PlantResource extends Resource
|
|||||||
->default(function () {
|
->default(function () {
|
||||||
return optional(Plant::latest()->first())->company_id;
|
return optional(Plant::latest()->first())->company_id;
|
||||||
})
|
})
|
||||||
->disabled(fn (Get $get) => !empty($get('id')))
|
->disabled(fn (Get $get) => ! empty($get('id')))
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$companyId = $get('company_id');
|
$companyId = $get('company_id');
|
||||||
// Ensure `linestop_id` is not cleared
|
// Ensure `linestop_id` is not cleared
|
||||||
if (!$companyId) {
|
if (! $companyId) {
|
||||||
$set('pCompanyError', 'Please select a company first.');
|
$set('pCompanyError', 'Please select a company first.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$set('pCompanyError', null);
|
$set('pCompanyError', null);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -101,18 +108,21 @@ class PlantResource extends Resource
|
|||||||
->unique(ignoreRecord: true)
|
->unique(ignoreRecord: true)
|
||||||
->columnSpanFull()
|
->columnSpanFull()
|
||||||
->reactive()
|
->reactive()
|
||||||
|
->minLength(5)
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$nameId = $get('name');
|
$nameId = $get('name');
|
||||||
// Ensure `linestop_id` is not cleared
|
// Ensure `linestop_id` is not cleared
|
||||||
if (!$nameId) {
|
if (! $nameId) {
|
||||||
$set('pNameError', 'Scan the valid name.');
|
$set('pNameError', 'Scan the valid name.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$set('pNameError', null);
|
$set('pNameError', null);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// ->rule(function (callable $get) {
|
||||||
|
// return Rule::unique('plants', 'name')->ignore($get('id'));
|
||||||
|
// })
|
||||||
->extraAttributes(fn ($get) => [
|
->extraAttributes(fn ($get) => [
|
||||||
'class' => $get('pNameError') ? 'border-red-500' : '',
|
'class' => $get('pNameError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
@@ -124,15 +134,15 @@ class PlantResource extends Resource
|
|||||||
->unique(ignoreRecord: true)
|
->unique(ignoreRecord: true)
|
||||||
->columnSpanFull()
|
->columnSpanFull()
|
||||||
->reactive()
|
->reactive()
|
||||||
|
->minLength(5)
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
$nameId = $get('address');
|
$nameId = $get('address');
|
||||||
// Ensure `linestop_id` is not cleared
|
// Ensure `linestop_id` is not cleared
|
||||||
if (!$nameId) {
|
if (! $nameId) {
|
||||||
$set('pAddressError', 'Scan the valid address.');
|
$set('pAddressError', 'Scan the valid address.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$set('pAddressError', null);
|
$set('pAddressError', null);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -159,6 +169,7 @@ class PlantResource extends Resource
|
|||||||
$paginator = $livewire->getTableRecords();
|
$paginator = $livewire->getTableRecords();
|
||||||
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
|
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
|
||||||
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
|
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
|
||||||
|
|
||||||
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
|
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
|
||||||
})
|
})
|
||||||
->toggleable(isToggledHiddenByDefault: false),
|
->toggleable(isToggledHiddenByDefault: false),
|
||||||
@@ -225,14 +236,14 @@ class PlantResource extends Resource
|
|||||||
->label('Import Plants')
|
->label('Import Plants')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
->importer(PlantImporter::class)
|
->importer(PlantImporter::class)
|
||||||
->visible(function() {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import plant');
|
return Filament::auth()->user()->can('view import plant');
|
||||||
}),
|
}),
|
||||||
ExportAction::make()
|
ExportAction::make()
|
||||||
->label('Export Plants')
|
->label('Export Plants')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
->exporter(PlantExporter::class)
|
->exporter(PlantExporter::class)
|
||||||
->visible(function() {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view export plant');
|
return Filament::auth()->user()->can('view export plant');
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user