Updated validations on Importer and Resource file
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled

This commit is contained in:
dhanabalan
2026-04-30 11:06:12 +05:30
parent a3561aa301
commit 43b907a9e2
2 changed files with 89 additions and 53 deletions

View File

@@ -17,31 +17,31 @@ class PlantImporter extends Importer
public static function getColumns(): array
{
return [
ImportColumn::make('company')
->requiredMapping()
->exampleHeader('COMPANY')
->example('C.R.I. Pumps Private Limited')
->label('COMPANY')
->relationship(resolveUsing: 'name')
->rules(['required']),
ImportColumn::make('code')
->requiredMapping()
->numeric()
->exampleHeader('Plant Code')
->exampleHeader('CODE')
->example('1000')
->label('Plant Code')
->rules(['required']), // , 'integer'
->label('CODE')
->rules(['required']),
ImportColumn::make('name')
->requiredMapping()
->exampleHeader('Plant Name')
->exampleHeader('NAME')
->example('Ransar Industries-I')
->label('Plant Name')
->label('NAME')
->rules(['required']),
ImportColumn::make('address')
->requiredMapping()
->exampleHeader('Plant Address')
->exampleHeader('ADDRESS')
->example('7/51-A, Keeranatham Road, Saravanampatty, Coimbatore - 641035')
->label('Plant Address')
->rules(['required']),
ImportColumn::make('company')
->requiredMapping()
->exampleHeader('Company Name')
->example('C.R.I. Pumps Private Limited')
->label('Company Name')
->relationship(resolveUsing: 'name')
->label('ADDRESS')
->rules(['required']),
];
}
@@ -49,41 +49,66 @@ class PlantImporter extends Importer
public function resolveRecord(): ?Plant
{
$warnMsg = [];
$company = Company::where('name', $this->data['company'])->first();
if (! $company) {
$warnMsg[] = 'Company name not found';
$compId = null;
$comp = trim($this->data['company']) ?? null;
$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) {
$warnMsg[] = 'Plant name not found';
if ($code == null || $code == '') {
$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'])) {
$warnMsg[] = 'Invalid plant code found';
if ($name == null || $name == '' || ! $name) {
$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) {
$warnMsg[] = 'Invalid address found';
if ($addr == null || $addr == '' || ! $addr) {
$warnMsg[] = "Address can't be empty!";
} elseif (Str::length($addr) < 5) {
$warnMsg[] = 'Address should contain at least 5 characters!';
}
if (! empty($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) {
$plantCode = Plant::where('code', $this->data['code'])->first();
$plantName = Plant::where('name', $this->data['name'])->first();
$plantCode = Plant::where('code', $code)->first();
$plantName = Plant::where('name', $name)->first();
if ($plantName) {
throw new RowImportFailedException('Duplicate plant name found');
throw new RowImportFailedException('Duplicate plant name found!');
} elseif ($plantCode) {
throw new RowImportFailedException('Duplicate plant code found');
throw new RowImportFailedException('Duplicate plant code found!');
}
}
return Plant::updateOrCreate([
'code' => $this->data['code'],
'name' => $this->data['name'],
'code' => $code,
'name' => $name,
],
[
'address' => $this->data['address'],
'company_id' => $company->id,
'address' => $addr,
'company_id' => $compId,
]
);
// return Plant::firstOrNew([

View File

@@ -19,6 +19,8 @@ use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
// use Illuminate\Validation\Rule;
class PlantResource extends Resource
{
protected static ?string $model = Plant::class;
@@ -42,18 +44,21 @@ class PlantResource extends Resource
->reactive()
->placeholder('Scan the valid code')
->autofocus(true)
// ->minLength(4)
// ->maxLength(7)
->minValue(1000)
->maxValue(9999999)
->afterStateUpdated(function ($state, callable $set, callable $get) {
$codeId = $get('code');
// Ensure `linestop_id` is not cleared
if (!$codeId) {
if (! $codeId) {
$set('pCodeError', 'Scan the valid code.');
return;
}
else
{
if (strlen($codeId) < 4) {
$set('pCodeError', 'Code must be at least 4 digits.');
} 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.
return;
}
// if (!preg_match('/^[1-9][0-9]{3,}$/', $codeId)) {
@@ -64,6 +69,9 @@ class PlantResource extends Resource
$set('pCodeError', null);
}
})
// ->rule(function (callable $get) {
// return Rule::unique('plants', 'code')->ignore($get('id'));
// })
->extraAttributes(fn ($get) => [
'class' => $get('pCodeError') ? 'border-red-500' : '',
])
@@ -77,16 +85,15 @@ class PlantResource extends Resource
->default(function () {
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) {
$companyId = $get('company_id');
// Ensure `linestop_id` is not cleared
if (!$companyId) {
if (! $companyId) {
$set('pCompanyError', 'Please select a company first.');
return;
}
else
{
} else {
$set('pCompanyError', null);
}
})
@@ -101,18 +108,21 @@ class PlantResource extends Resource
->unique(ignoreRecord: true)
->columnSpanFull()
->reactive()
->minLength(5)
->afterStateUpdated(function ($state, callable $set, callable $get) {
$nameId = $get('name');
// Ensure `linestop_id` is not cleared
if (!$nameId) {
if (! $nameId) {
$set('pNameError', 'Scan the valid name.');
return;
}
else
{
} else {
$set('pNameError', null);
}
})
// ->rule(function (callable $get) {
// return Rule::unique('plants', 'name')->ignore($get('id'));
// })
->extraAttributes(fn ($get) => [
'class' => $get('pNameError') ? 'border-red-500' : '',
])
@@ -124,15 +134,15 @@ class PlantResource extends Resource
->unique(ignoreRecord: true)
->columnSpanFull()
->reactive()
->minLength(5)
->afterStateUpdated(function ($state, callable $set, callable $get) {
$nameId = $get('address');
// Ensure `linestop_id` is not cleared
if (!$nameId) {
if (! $nameId) {
$set('pAddressError', 'Scan the valid address.');
return;
}
else
{
} else {
$set('pAddressError', null);
}
})
@@ -159,6 +169,7 @@ class PlantResource extends Resource
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
})
->toggleable(isToggledHiddenByDefault: false),
@@ -225,14 +236,14 @@ class PlantResource extends Resource
->label('Import Plants')
->color('warning')
->importer(PlantImporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view import plant');
}),
ExportAction::make()
->label('Export Plants')
->color('warning')
->exporter(PlantExporter::class)
->visible(function() {
->visible(function () {
return Filament::auth()->user()->can('view export plant');
}),
]);