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:46:43 +05:30
parent d59fb00e90
commit 63110298e7
2 changed files with 57 additions and 52 deletions

View File

@@ -14,6 +14,7 @@ class WorkGroupMasterExporter extends Exporter
public static function getColumns(): array public static function getColumns(): array
{ {
static $rowNumber = 0; static $rowNumber = 0;
return [ return [
ExportColumn::make('no') ExportColumn::make('no')
->label('NO') ->label('NO')
@@ -21,12 +22,12 @@ class WorkGroupMasterExporter 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('WORK GROUP NAME'),
ExportColumn::make('description') ExportColumn::make('description')
->label('DESCRIPTION'), ->label('WORK GROUP DESCRIPTION'),
ExportColumn::make('operation_number') ExportColumn::make('operation_number')
->label('OPERATION NUMBER'), ->label('OPERATION NUMBER'),
ExportColumn::make('created_by') ExportColumn::make('created_by')
@@ -43,10 +44,10 @@ class WorkGroupMasterExporter extends Exporter
public static function getCompletedNotificationBody(Export $export): string public static function getCompletedNotificationBody(Export $export): string
{ {
$body = 'Your work group master export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; $body = 'Your work group master 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

@@ -20,22 +20,22 @@ class WorkGroupMasterImporter extends Importer
return [ return [
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']),
ImportColumn::make('name') ImportColumn::make('name')
->requiredMapping() ->requiredMapping()
->exampleHeader('Name') ->exampleHeader('Work Group Name')
->example('RMGCEABC') ->example('RMGCEABC')
->label('Name') ->label('Work Group Name')
->rules(['required']), ->rules(['required']),
ImportColumn::make('description') ImportColumn::make('description')
->requiredMapping() ->requiredMapping()
->exampleHeader('Description') ->exampleHeader('Work Group Description')
->example('Testing Model 1') ->example('Testing Model 1')
->label('Description') ->label('Work Group Description')
->rules(['required']), ->rules(['required']),
ImportColumn::make('operation_number') ImportColumn::make('operation_number')
->requiredMapping() ->requiredMapping()
@@ -52,8 +52,6 @@ class WorkGroupMasterImporter extends Importer
]; ];
} }
public function resolveRecord(): ?WorkGroupMaster public function resolveRecord(): ?WorkGroupMaster
{ {
// return WorkGroupMaster::firstOrNew([ // return WorkGroupMaster::firstOrNew([
@@ -61,75 +59,81 @@ class WorkGroupMasterImporter extends Importer
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $plantCod = $this->data['plant'];
if (!$plant) { $plantId = null;
$warnMsg[] = "Plant not found";
if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = 'Invalid plant code found';
} else {
$plant = Plant::where('code', $plantCod)->first();
if (! $plant) {
$warnMsg[] = 'Plant not found';
} else {
$plantId = $plant->id;
}
} }
if (Str::length($this->data['name']) <= 0) { //|| !ctype_alnum($this->data['description']) if (Str::length($this->data['name']) <= 0) { // || !ctype_alnum($this->data['description'])
$warnMsg[] = "Invalid name found"; $warnMsg[] = 'Invalid work group name found';
} }
if (Str::length(trim($this->data['description'])) <= 0) { if (Str::length(trim($this->data['description'])) <= 0) {
$warnMsg[] = "Invalid description found"; $warnMsg[] = 'Invalid work group description found';
} }
$desc = trim($this->data['description']); $desc = trim($this->data['description']);
if (Str::length($desc) > 44) { if (Str::length($desc) > 44) {
$warnMsg[] = "Description should be less than 44 digits."; $warnMsg[] = ' work group description should be less than 44 digits.';
} }
if (Str::length($this->data['operation_number']) <= 0) { if (Str::length($this->data['operation_number']) <= 0) {
$warnMsg[] = "Invalid operation number found"; $warnMsg[] = 'Invalid operation number found';
} }
if(!is_numeric($this->data['operation_number'])) if (! is_numeric($this->data['operation_number'])) {
{ $warnMsg[] = 'Invalid operation number found must be numeric';
$warnMsg[] = "Invalid operation number found must be numeric";
} }
$user = User::where('name', $this->data['created_by'])->first(); $user = User::where('name', $this->data['created_by'])->first();
if (!$user) { if (! $user) {
$warnMsg[] = "Operator ID not found"; $warnMsg[] = 'Operator ID not found';
} }
if (!empty($warnMsg)) { if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} } else {
else // Check (plant_id, name)
{ $existingByName = WorkGroupMaster::where('plant_id', $plantId)
//Check (plant_id, name)
$existingByName = WorkGroupMaster::where('plant_id', $plant->id)
->where('name', $this->data['name']) ->where('name', $this->data['name'])
->first(); ->first();
if ($existingByName) { if ($existingByName) {
throw new RowImportFailedException("Work group name already exists for this plant!"); throw new RowImportFailedException('Work group name already exists for this plant!');
} }
//Check (plant_id, operation_number) // Check (plant_id, operation_number)
$existingByOpNum = WorkGroupMaster::where('plant_id', $plant->id) $existingByOpNum = WorkGroupMaster::where('plant_id', $plantId)
->where('operation_number', $this->data['operation_number']) ->where('operation_number', $this->data['operation_number'])
->where('name', $this->data['name']) ->where('name', $this->data['name'])
->first(); ->first();
if ($existingByOpNum) { if ($existingByOpNum) {
throw new RowImportFailedException("Operation number already exists for this plant!"); throw new RowImportFailedException('Operation number already exists for this plant!');
} }
//Check (plant_id) // Check (plant_id)
$existingByOperator = WorkGroupMaster::where('plant_id', $plant->id) $existingByOperator = WorkGroupMaster::where('plant_id', $plantId)
->where('name', $this->data['name']) ->where('name', $this->data['name'])
->first(); ->first();
if ($existingByOperator) { if ($existingByOperator) {
throw new RowImportFailedException("Already work group name assigned to another plant!"); throw new RowImportFailedException('Already work group name assigned to another plant!');
} }
} }
WorkGroupMaster::updateOrCreate([ WorkGroupMaster::updateOrCreate([
'plant_id' => $plant->id, 'plant_id' => $plantId,
'name' => $this->data['name'], 'name' => $this->data['name'],
'description' => $this->data['description'], 'description' => $this->data['description'],
'operation_number' => $this->data['operation_number'], 'operation_number' => $this->data['operation_number'],
@@ -138,15 +142,15 @@ class WorkGroupMasterImporter extends Importer
return null; return null;
//return new WorkGroupMaster(); // return new WorkGroupMaster();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your work group master import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your work group master 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;