diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 8eeb1eb4..59870222 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -1533,6 +1533,35 @@ public function createProductionOrder(int $orderId, array $data) } $itemsByProcess[$key]['items'][] = $orderItem; } + + // 보조 공정(재고생산 등) 품목을 메인 공정으로 병합 + $auxiliaryProcessIds = \App\Models\Process::where('tenant_id', $tenantId) + ->where('is_active', true) + ->get() + ->filter(fn ($p) => ! empty($p->options['is_auxiliary'])) + ->pluck('id') + ->all(); + + $auxiliaryKeys = []; + $mainKeys = []; + foreach ($itemsByProcess as $key => $group) { + if ($key === 'none' || in_array($group['process_id'], $auxiliaryProcessIds, true)) { + $auxiliaryKeys[] = $key; + } else { + $mainKeys[] = $key; + } + } + + if (! empty($auxiliaryKeys) && ! empty($mainKeys)) { + // 메인 공정 중 품목 수가 가장 많은 공정에 병합 + $targetKey = collect($mainKeys)->sortByDesc(fn ($k) => count($itemsByProcess[$k]['items']))->first(); + foreach ($auxiliaryKeys as $auxKey) { + foreach ($itemsByProcess[$auxKey]['items'] as $item) { + $itemsByProcess[$targetKey]['items'][] = $item; + } + unset($itemsByProcess[$auxKey]); + } + } } return DB::transaction(function () use ($order, $data, $tenantId, $userId, $itemsByProcess, $nodesBomMap, $isStock) {