feat(dispatch): 기간별 발송 수량 엑셀 — 세트는 낱개로 풀어 합산

- 세트(MY-)는 itemcode_db BOM 으로 구성 낱개 분해 후 같은 날 같은 코드에 합산
- BOM 없는 세트가 있으면 422 로 해당 세트 목록 안내(조용히 틀린 파일 방지)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 16:08:18 +09:00
parent 667bdd813c
commit 7914fc6056
6 changed files with 83 additions and 24 deletions
+35 -10
View File
@@ -247,18 +247,39 @@ def stock_filename(dispatch_date: str) -> str:
# ── 기간별 발송 수량 엑셀(수퍼 관리자) ──
def missing_set_boms(
rows: list[dict[str, Any]], set_bom: dict[str, list[dict[str, Any]]]
) -> list[str]:
"""세트(MY-)인데 BOM 이 없어 낱개로 풀 수 없는 코드 목록(정렬)."""
return sorted({
b for b in (split_sku(r.get("seller_sku", ""))[0].strip().upper() for r in rows)
if b.startswith(_SET_PREFIX) and b not in set_bom
})
def aggregate_shipped(
rows: list[dict[str, Any]], name_map: dict[str, str] | None = None
rows: list[dict[str, Any]],
name_map: dict[str, str] | None = None,
set_bom: dict[str, list[dict[str, Any]]] | None = None,
) -> tuple[list[str], dict[str, str], dict[tuple[str, str], int]]:
"""날짜·SKU별 합산 행 → (날짜목록, {코드: 상품명}, {(날짜, 코드): 수량}).
seller_sku 의 _N 접미사는 배수로 곱해 실제 아이템코드로 합친다
(예: MT-0320_3 × 2 → MT-0320 6개). 콤보는 분해하지 않고 발송된 그대로 센다.
(예: MT-0320_3 × 2 → MT-0320 6개). 세트(set_bom 에 있는 코드)는 구성 낱개로
풀어 (수량 × 구성수량) 을 낱개 코드에 더한다.
"""
name_map = name_map or {}
set_bom = set_bom or {}
dates: set[str] = set()
names: dict[str, str] = {}
qty: dict[tuple[str, str], int] = {}
def _add(d: str, code: str, n: int, fallback: str) -> None:
key = (d, code)
qty[key] = qty.get(key, 0) + n
if not names.get(code):
names[code] = resolve_product_name(code, name_map, fallback=fallback)
for r in rows:
base, mult = split_sku(r.get("seller_sku", ""))
code = base.strip().upper()
@@ -269,12 +290,15 @@ def aggregate_shipped(
continue
d = r.get("dispatch_date", "") or ""
dates.add(d)
key = (d, code)
qty[key] = qty.get(key, 0) + pieces
if not names.get(code):
names[code] = resolve_product_name(
code, name_map, fallback=r.get("product_name", "") or ""
)
comps = set_bom.get(code)
if comps: # 세트 → 낱개 분해
for c in comps:
cc = str(c.get("component_code", "")).strip().upper()
n = pieces * int(c.get("component_qty", 0) or 0)
if cc and n > 0:
_add(d, cc, n, "")
else:
_add(d, code, pieces, r.get("product_name", "") or "")
return sorted(dates), names, qty
@@ -284,8 +308,9 @@ def build_period_xlsx(
date_from: str,
date_to: str,
name_map: dict[str, str] | None = None,
set_bom: dict[str, list[dict[str, Any]]] | None = None,
) -> bytes:
"""기간별 발송 수량 xlsx. 시트 2개.
"""기간별 발송 수량 xlsx(세트는 낱개로 풀어 합산). 시트 2개.
- 「날짜별」: 발송 날짜 / 아이템 코드 / 상품이름 / 수량 (날짜·코드 오름차순, 맨 끝 합계)
- 「아이템별 요약」: 아이템 코드 / 상품이름 / 날짜1..N / 합계 (맨 끝 날짜별 합계 행)
@@ -294,7 +319,7 @@ def build_period_xlsx(
from openpyxl.styles import Alignment, Font, PatternFill
from openpyxl.utils import get_column_letter
dates, names, qty = aggregate_shipped(rows, name_map)
dates, names, qty = aggregate_shipped(rows, name_map, set_bom)
codes = sorted(names)
head_fill = PatternFill("solid", fgColor="E2E8F0")