Merge pull request 'changed machine importer logic' (#227) from ranjith-dev into master
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

Reviewed-on: #227
This commit was merged in pull request #227.
This commit is contained in:
2026-09-20 08:55:07 +00:00

View File

@@ -21,36 +21,36 @@ class MachineImporter extends Importer
return [ return [
ImportColumn::make('name') ImportColumn::make('name')
->requiredMapping() ->requiredMapping()
->exampleHeader('Machine') ->exampleHeader('DESCRIPTION')
->example(['1600251']) ->example(['1600251'])
->label('Machine') ->label('DESCRIPTION')
->rules(['required']), ->rules(['required']),
ImportColumn::make('work_center') ImportColumn::make('work_center')
->requiredMapping() ->requiredMapping()
->exampleHeader('Work Center') ->exampleHeader('WORK CENTER')
->example('RMGCE001') ->example('RMGCE001')
->label('Work Center') ->label('WORK CENTER')
->rules(['required']), ->rules(['required']),
ImportColumn::make('workGroupMaster') ImportColumn::make('workGroupMaster')
->requiredMapping() ->requiredMapping()
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'name')
->exampleHeader('Work Group Center') ->exampleHeader('GROUP WORK CENTER')
->example(['RMGCGABC']) ->example(['RMGCGABC'])
->label('Work Group Center') ->label('GROUP WORK CENTER')
->rules(['required']), ->rules(['required']),
ImportColumn::make('line') ImportColumn::make('line')
->requiredMapping() ->requiredMapping()
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'name')
->exampleHeader('Line') ->exampleHeader('LINE NAME')
->example(['4 inch pump line']) ->example(['4 inch pump line'])
->label('Line') ->label('LINE NAME')
->rules(['required']), ->rules(['required']),
ImportColumn::make('plant') ImportColumn::make('plant')
->requiredMapping() ->requiredMapping()
->relationship(resolveUsing: 'name') ->relationship(resolveUsing: 'code')
->exampleHeader('Plant') ->exampleHeader('PLANT CODE')
->example(['Ransar Industries-I']) ->example(['1000'])
->label('Plant') ->label('PLANT CODE')
->rules(['required']), ->rules(['required']),
]; ];
} }
@@ -58,74 +58,79 @@ class MachineImporter extends Importer
public function resolveRecord(): ?Machine public function resolveRecord(): ?Machine
{ {
$warnMsg = []; $warnMsg = [];
$plant = Plant::where('name', $this->data['plant'])->first(); $plantCod = $this->data['plant'];
$plant = null;
$line = null; $line = null;
$lineNam = $this->data['line'];
$machine = $this->data['name']; $machine = $this->data['name'];
$workCenter = $this->data['work_center']; $workCenter = $this->data['work_center'];
$groupWorkCenter = WorkGroupMaster::where('name', $this->data['workGroupMaster'])->first(); $workGroupCenter = $this->data['workGroupMaster'];
if (!$plant) { if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
$warnMsg[] = "Plant not found!"; $warnMsg[] = 'Invalid plant code found';
} } elseif (Str::length($lineNam) <= 0) {
else { $warnMsg[] = "Line name can't be empty!";
$groupWorkCenter = WorkGroupMaster::where('name', $this->data['workGroupMaster'])->where('plant_id', $plant->id)->first(); } elseif (Str::length($machine) <= 0) {
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first(); $warnMsg[] = "Description can't be empty!";
if ($line) { } elseif (Str::length($workCenter) <= 0) {
$grpWrkCnr = $line->no_of_operation; $warnMsg[] = "Work center can't be empty!";
if (!$grpWrkCnr || $grpWrkCnr < 1)//Str::length($grpWrkCnr) < 1) } elseif (Str::length($workGroupCenter) <= 0) {
{ $warnMsg[] = "Group work center can't be empty!";
$warnMsg[] = "Group work center line not found!"; } else {
} $plant = Plant::where('code', $plantCod)->first();
else if (!$groupWorkCenter) { if (! $plant) {
$warnMsg[] = "Group work center not found!"; $warnMsg[] = 'Plant not found!';
} } else {
else { $groupWorkCenter = WorkGroupMaster::where('name', $workGroupCenter)->where('plant_id', $plant->id)->first();
$dupMachine = Machine::where('plant_id', $plant->id)->where('work_center', '!=', $workCenter)->where('name', $machine)->first(); $line = Line::where('name', $lineNam)->where('plant_id', $plant->id)->first();
if ($dupMachine) { if ($line) {
$warnMsg[] = "Duplicate machine name found!"; $grpWrkCnr = $line->no_of_operation;
} if (! $grpWrkCnr || $grpWrkCnr < 1) {// Str::length($grpWrkCnr) < 1)
else { $warnMsg[] = 'Group work center line not found!';
$isValidGroupWork = false; } elseif (! $groupWorkCenter) {
for ($i = 1; $i <= $line->no_of_operation; $i++) { $warnMsg[] = 'Group work center not found!';
$column = "work_group{$i}_id"; } else {
if (!empty($line->$column)) { $dupMachine = Machine::where('plant_id', $plant->id)->where('work_center', '!=', $workCenter)->where('name', $machine)->first();
if ($line->$column == $groupWorkCenter->id) { if ($dupMachine) {
$isValidGroupWork = true; $warnMsg[] = 'Duplicate machine name found!';
break; } else {
$isValidGroupWork = false;
for ($i = 1; $i <= $line->no_of_operation; $i++) {
$column = "work_group{$i}_id";
if (! empty($line->$column)) {
if ($groupWorkCenter->id == $line->$column) {
$isValidGroupWork = true;
break;
}
} }
} }
}
if (!$isValidGroupWork) { if (! $isValidGroupWork) {
$warnMsg[] = "Group work center does not match with line!"; $warnMsg[] = 'Group work center does not match with line!';
}
} }
} }
} else {
$warnMsg[] = 'Line not found!';
} }
} }
else
{
$warnMsg[] = "Line not found!";
}
} }
if (Str::length($machine) <= 0) { if (! empty($warnMsg)) {
$warnMsg[] = "Machine name not found!";
}
if (!empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
Machine::updateOrCreate( Machine::updateOrCreate(
[ [
'plant_id' => $plant->id, 'plant_id' => $plant->id,
'work_center' => $workCenter 'work_center' => $workCenter,
], ],
[ [
'line_id' => $line->id, 'line_id' => $line->id,
'name' => $machine, 'name' => $machine,
'work_group_master_id' => $groupWorkCenter->id 'work_group_master_id' => $groupWorkCenter->id,
] ]
); );
return null; return null;
// // return Machine::firstOrNew([ // // return Machine::firstOrNew([
// // // Update existing records, matching them by `$this->data['column_name']` // // // Update existing records, matching them by `$this->data['column_name']`
@@ -137,10 +142,10 @@ class MachineImporter extends Importer
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your machine import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your machine 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;