Added plant code instead of plant name on import and export
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:23:05 +05:30
parent 5a218d688d
commit 7f96d9631a
2 changed files with 46 additions and 42 deletions

View File

@@ -24,12 +24,12 @@ class LineExporter extends Exporter
// Increment and return the row number // Increment and return the row number
return ++$rowNumber; return ++$rowNumber;
}), }),
ExportColumn::make('plant.name') ExportColumn::make('plant.code')
->label('PLANT'), ->label('PLANT CODE'),
ExportColumn::make('name') ExportColumn::make('name')
->label('NAME'), ->label('LINE NAME'),
ExportColumn::make('type') ExportColumn::make('type')
->label('TYPE'), ->label('LINE TYPE'),
ExportColumn::make('no_of_operation') ExportColumn::make('no_of_operation')
->label('NO OF OPERATION'), ->label('NO OF OPERATION'),
ExportColumn::make('workGroup1.name') ExportColumn::make('workGroup1.name')
@@ -84,10 +84,10 @@ class LineExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string public static function getCompletedNotificationBody(Export $export): string
{ {
$body = 'Your line export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; $body = 'Your line export has completed and '.number_format($export->successful_rows).' '.str('row')->plural($export->successful_rows).' exported.';
if ($failedRowsCount = $export->getFailedRowsCount()) { if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to export.';
} }
return $body; return $body;

View File

@@ -87,10 +87,10 @@ class LineImporter extends Importer
->label('Work Group Center 10'), ->label('Work Group Center 10'),
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->exampleHeader('Plant Name') ->exampleHeader('Plant Code')
->example('Ransar Industries-I') ->example('1000')
->label('Plant Name') ->label('Plant Code')
->relationship(resolveUsing:'name') ->relationship(resolveUsing: 'code')
->rules(['required']), ->rules(['required']),
]; ];
} }
@@ -98,28 +98,33 @@ class LineImporter extends Importer
public function resolveRecord(): ?Line public function resolveRecord(): ?Line
{ {
$warnMsg = []; $warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null;
$plant = Plant::where('name', $this->data['plant'])->first(); if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
if (!$plant) { $warnMsg[] = 'Invalid plant code found';
throw new RowImportFailedException("Plant '{$this->data['plant']}' not found"); } else {
$plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
throw new RowImportFailedException('Plant not found');
}
} }
if (Str::length($this->data['name'] ?? '') <= 0) { if (Str::length($this->data['name'] ?? '') <= 0) {
throw new RowImportFailedException("Line name not found"); throw new RowImportFailedException('Line name not found');
} }
if (Str::length($this->data['type'] ?? '') <= 0) { if (Str::length($this->data['type'] ?? '') <= 0) {
throw new RowImportFailedException("Line type not found"); throw new RowImportFailedException('Line type not found');
} }
$noOfOps = (int) ($this->data['no_of_operation'] ?? 0); $noOfOps = (int) ($this->data['no_of_operation'] ?? 0);
if (($noOfOps == null || $noOfOps == '' || !is_numeric($noOfOps)) && $noOfOps != 0) { if (($noOfOps == null || $noOfOps == '' || ! is_numeric($noOfOps)) && $noOfOps != 0) {
throw new RowImportFailedException("'No of Operation' is required and must be a number $noOfOps"); throw new RowImportFailedException("'No of Operation' is required and must be a number $noOfOps");
} }
if ($noOfOps > 10) if ($noOfOps > 10) {
{
throw new RowImportFailedException("Invalid 'No Of Operation' value: {$noOfOps}, maximum allowed is 10"); throw new RowImportFailedException("Invalid 'No Of Operation' value: {$noOfOps}, maximum allowed is 10");
} }
@@ -130,28 +135,28 @@ class LineImporter extends Importer
$missingGroups[] = "work_group{$i}_id"; $missingGroups[] = "work_group{$i}_id";
} }
} }
if (!empty($missingGroups)) { if (! empty($missingGroups)) {
throw new RowImportFailedException( throw new RowImportFailedException(
"Invalid data: Required work groups missing values in: " . implode(', ', $missingGroups) 'Invalid data: Required work groups missing values in: '.implode(', ', $missingGroups)
); );
} }
// Ensure no extra work groups are filled // Ensure no extra work groups are filled
$invalidGroups = []; $invalidGroups = [];
for ($i = $noOfOps + 1; $i <= 10; $i++) { for ($i = $noOfOps + 1; $i <= 10; $i++) {
if (!empty($this->data["work_group{$i}_id"])) { if (! empty($this->data["work_group{$i}_id"])) {
$invalidGroups[] = "work_group{$i}_id"; $invalidGroups[] = "work_group{$i}_id";
} }
} }
if (!empty($invalidGroups)) { if (! empty($invalidGroups)) {
throw new RowImportFailedException( throw new RowImportFailedException(
"Invalid data: Only first {$noOfOps} work groups should be filled, but values found in: " . implode(', ', $invalidGroups) "Invalid data: Only first {$noOfOps} work groups should be filled, but values found in: ".implode(', ', $invalidGroups)
); );
} }
for ($i = 1; $i <= 10; $i++) { for ($i = 1; $i <= 10; $i++) {
$workGroupName = $this->data["work_group{$i}_id"] ?? null; $workGroupName = $this->data["work_group{$i}_id"] ?? null;
if (!$workGroupName) { if (! $workGroupName) {
continue; continue;
} }
@@ -159,8 +164,8 @@ class LineImporter extends Importer
->where('plant_id', $plant->id) ->where('plant_id', $plant->id)
->first(); ->first();
if (!$workGroupRecord) { if (! $workGroupRecord) {
throw new RowImportFailedException("Work group '{$workGroupName}' not found in plant '{$this->data['plant']}'"); throw new RowImportFailedException("Work group '{$workGroupName}' not found in plant '{$plantCod}'");
} }
$existsInLines = Line::where('plant_id', $plant->id) $existsInLines = Line::where('plant_id', $plant->id)
@@ -169,21 +174,20 @@ class LineImporter extends Importer
->first(); ->first();
if ($existsInLines) { if ($existsInLines) {
$warnMsg[] = "Work group '{$workGroupName}' is already assigned to another line in plant '{$this->data['plant']}'"; $warnMsg[] = "Work group '{$workGroupName}' is already assigned to another line in plant '{$plantCod}'";
} }
$this->data["work_group{$i}_id"] = $workGroupRecord->id; $this->data["work_group{$i}_id"] = $workGroupRecord->id;
} }
if (!empty($warnMsg)) if (! empty($warnMsg)) {
{
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
Line::updateOrCreate( Line::updateOrCreate(
[ [
'name' => $this->data['name'], 'name' => $this->data['name'],
'plant_id' => $plant->id 'plant_id' => $plant->id,
], ],
[ [
'type' => $this->data['type'], 'type' => $this->data['type'],
@@ -206,10 +210,10 @@ class LineImporter extends Importer
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your line import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your line 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;