From f1c66532206510fe76e1364b0446085d481de7a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=B3=B4=EA=B3=A4?= Date: Wed, 18 Mar 2026 16:49:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20[bom]=20BOM=20=ED=8A=B8=EB=A6=AC=20API?= =?UTF-8?q?=203=EB=8B=A8=EA=B3=84=20=EA=B5=AC=EC=A1=B0=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20(category=20=EA=B7=B8=EB=A3=B9=20=EB=85=B8=EB=93=9C?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - GET /items/{id}/bom/tree: category 필드가 있으면 CAT 그룹 노드 자동 생성 - expandBomItems에 category 필드 포함 - 3단계: FG → 카테고리(CAT) → PT 품목 --- .../Controllers/Api/V1/ItemsBomController.php | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/V1/ItemsBomController.php b/app/Http/Controllers/Api/V1/ItemsBomController.php index de217429..71d3b429 100644 --- a/app/Http/Controllers/Api/V1/ItemsBomController.php +++ b/app/Http/Controllers/Api/V1/ItemsBomController.php @@ -446,12 +446,13 @@ private function expandBomItems(array $bom): array 'child_item_type' => $childItem?->item_type, 'unit' => $childItem?->unit, 'quantity' => $entry['quantity'] ?? 1, + 'category' => $entry['category'] ?? null, ]; })->toArray(); } /** - * BOM 트리 구조 빌드 (재귀) + * BOM 트리 구조 빌드 (재귀, category 필드가 있으면 3단계 그룹화) */ private function buildBomTree(Item $item, int $maxDepth, int $currentDepth): array { @@ -478,14 +479,47 @@ private function buildBomTree(Item $item, int $maxDepth, int $currentDepth): arr ->get() ->keyBy('id'); - foreach ($bom as $entry) { - $childItemId = $entry['child_item_id'] ?? null; - $childItem = $childItems[$childItemId] ?? null; + // category 필드가 있으면 카테고리별 그룹 노드 생성 (3단계) + $hasCategory = collect($bom)->contains(fn ($b) => ! empty($b['category'])); - if ($childItem) { - $childTree = $this->buildBomTree($childItem, $maxDepth, $currentDepth + 1); - $childTree['quantity'] = $entry['quantity'] ?? 1; - $result['children'][] = $childTree; + if ($hasCategory) { + $grouped = []; + foreach ($bom as $entry) { + $cat = $entry['category'] ?? '기타'; + $grouped[$cat][] = $entry; + } + + foreach ($grouped as $catName => $catEntries) { + $catChildren = []; + foreach ($catEntries as $entry) { + $childItem = $childItems[$entry['child_item_id'] ?? null] ?? null; + if ($childItem) { + $childTree = $this->buildBomTree($childItem, $maxDepth, $currentDepth + 2); + $childTree['quantity'] = $entry['quantity'] ?? 1; + $catChildren[] = $childTree; + } + } + if (! empty($catChildren)) { + $result['children'][] = [ + 'id' => 0, + 'code' => '', + 'name' => $catName, + 'item_type' => 'CAT', + 'unit' => '', + 'depth' => $currentDepth + 1, + 'count' => count($catChildren), + 'children' => $catChildren, + ]; + } + } + } else { + foreach ($bom as $entry) { + $childItem = $childItems[$entry['child_item_id'] ?? null] ?? null; + if ($childItem) { + $childTree = $this->buildBomTree($childItem, $maxDepth, $currentDepth + 1); + $childTree['quantity'] = $entry['quantity'] ?? 1; + $result['children'][] = $childTree; + } } }