diff --git a/src/components/orders/OrderRegistration.tsx b/src/components/orders/OrderRegistration.tsx index ccc09b3d..4a373b99 100644 --- a/src/components/orders/OrderRegistration.tsx +++ b/src/components/orders/OrderRegistration.tsx @@ -234,13 +234,55 @@ export function OrderRegistration({ }, []) ); - // 아이템을 개소별(floor+code)로 그룹핑 + // 아이템을 개소별로 그룹핑 const itemGroups = useMemo(() => { const calcItems = form.selectedQuotation?.calculationInputs?.items; if (!calcItems || calcItems.length === 0) { return null; } + // floor+code 고유 키 수 확인 (모두 같은 값인지 판별) + const uniqueLocKeys = new Set( + calcItems.map(ci => `${ci.floor || '-'}|${ci.code || '-'}`) + ); + + // 개소가 여러 개인데 floor+code가 모두 동일 → 인덱스 기반 균등 분배 + const useIndexGrouping = calcItems.length > 1 && uniqueLocKeys.size === 1; + + if (useIndexGrouping) { + const itemsPerLocation = Math.ceil(form.items.length / calcItems.length); + const result: Array<{ + key: string; + label: string; + productCode: string; + locationCount: number; + quantity: number; + amount: number; + items: OrderItem[]; + }> = []; + + calcItems.forEach((ci, idx) => { + const start = idx * itemsPerLocation; + const end = Math.min(start + itemsPerLocation, form.items.length); + const groupItems = form.items.slice(start, end); + const amount = groupItems.reduce((sum, item) => sum + (item.amount ?? 0), 0); + const floor = ci.floor || '-'; + const code = ci.code || '-'; + + result.push({ + key: `loc_${idx}`, + label: `${idx + 1}. ${floor} / ${code}`, + productCode: ci.productName || ci.productCode || '', + locationCount: 1, + quantity: ci.quantity ?? 1, + amount, + items: groupItems, + }); + }); + + return result; + } + // floor+code → calculationInput 매핑 (개소 메타정보) const locationMetaMap = new Map