Updated alignments on import
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-13 16:32:47 +05:30
parent b9cb492d37
commit f592852b77

View File

@@ -23,7 +23,7 @@ class PlantImporter extends Importer
->exampleHeader('Plant Code') ->exampleHeader('Plant Code')
->example('1000') ->example('1000')
->label('Plant Code') ->label('Plant Code')
->rules(['required']), //, 'integer' ->rules(['required']), // , 'integer'
ImportColumn::make('name') ImportColumn::make('name')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('Plant Name')
@@ -41,7 +41,7 @@ class PlantImporter extends Importer
->exampleHeader('Company Name') ->exampleHeader('Company Name')
->example('C.R.I. Pumps Private Limited') ->example('C.R.I. Pumps Private Limited')
->label('Company Name') ->label('Company Name')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'name')
->rules(['required']), ->rules(['required']),
]; ];
} }
@@ -50,41 +50,40 @@ class PlantImporter extends Importer
{ {
$warnMsg = []; $warnMsg = [];
$company = Company::where('name', $this->data['company'])->first(); $company = Company::where('name', $this->data['company'])->first();
if (!$company) { if (! $company) {
$warnMsg[] = "Company name not found"; $warnMsg[] = 'Company name not found';
} }
if (Str::length($this->data['name']) < 0) { if (Str::length($this->data['name']) < 0) {
$warnMsg[] = "Plant name not found"; $warnMsg[] = 'Plant name not found';
} }
if (Str::length($this->data['code']) < 4 || !is_numeric($this->data['code']) || !preg_match('/^[1-9]\d{3,}$/', $this->data['code'])) { 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"; $warnMsg[] = 'Invalid plant code found';
} }
if (Str::length($this->data['address']) < 3) { if (Str::length($this->data['address']) < 3) {
$warnMsg[] = "Invalid address found"; $warnMsg[] = 'Invalid address found';
} }
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(); $plantCN = Plant::where('code', $this->data['code'])->where('name', $this->data['name'])->first();
if (!$plantCN) { if (! $plantCN) {
$plantCode = Plant::where('code', $this->data['code'])->first(); $plantCode = Plant::where('code', $this->data['code'])->first();
$plantName = Plant::where('name', $this->data['name'])->first(); $plantName = Plant::where('name', $this->data['name'])->first();
if ($plantName) { if ($plantName) {
throw new RowImportFailedException("Duplicate plant name found"); throw new RowImportFailedException('Duplicate plant name found');
} } elseif ($plantCode) {
else if ($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' => $this->data['code'],
'name' => $this->data['name'], 'name' => $this->data['name'],
], ],
[ [
'address' => $this->data['address'], 'address' => $this->data['address'],
'company_id' => $company->id 'company_id' => $company->id,
] ]
); );
// return Plant::firstOrNew([ // return Plant::firstOrNew([
@@ -97,10 +96,10 @@ class PlantImporter extends Importer
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your plant import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your plant import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) { 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; return $body;