"""쿠팡 밀크런 모듈 라우터. - 경로: /cupang - 권한: 로그인 + `cupang` 모듈 권한 (관리자는 항상 통과). 서버 측 검사. - 데이터: CupangDBStore (cupang_db / PostgreSQL) 전용. CUPANG_DB_URL 미설정 시 store 가 None 이며, 각 페이지는 "설정 필요" 안내를 보여준다. - 상품 검색: itemcode_db 읽기 전용(ItemcodeReader). 미설정 시 수동 입력 폴백. """ from __future__ import annotations import calendar as _calendar import json from decimal import ROUND_HALF_UP, Decimal from fractions import Fraction from typing import Any from datetime import date as _date, timedelta as _timedelta from app.timezone import today_kst from fastapi import APIRouter, Body, Depends, File, Form, HTTPException, Request, UploadFile from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse from .export import WORKER as OUTBOUND_WORKER from .holidays import holiday_name, is_holiday from .store import SHIP_METHODS, parse_weight_g router = APIRouter(prefix="/cupang", tags=["cupang"]) # ──────────────────────────────────────────────────────────── # 공용 헬퍼 # ──────────────────────────────────────────────────────────── def _store(request: Request) -> Any: """CupangDBStore 또는 None(CUPANG_DB_URL 미설정).""" return getattr(request.app.state, "cupang_store", None) def _itemcode(request: Request) -> Any: return getattr(request.app.state, "itemcode_reader", None) def _require_user(request: Request) -> dict[str, Any]: from app.main import get_current_user_record # noqa: WPS433 from app.store import has_module # noqa: WPS433 user = get_current_user_record(request) if user is None: raise HTTPException(status_code=401, detail="로그인이 필요합니다.") if not has_module(user, "cupang"): raise HTTPException(status_code=403, detail="쿠팡 밀크런 모듈 권한이 없습니다.") return user def _render_config_needed(request: Request, user: dict[str, Any]) -> HTMLResponse: from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 return render_template( request, "denied.html", { "reason": "쿠팡 밀크런 모듈이 아직 설정되지 않았습니다. " "CUPANG_DB_URL 환경변수를 설정하고 scripts/sql/cupang_db_init.sql 로 " "cupang_db 를 초기화한 뒤 컨테이너를 재기동하세요.", "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), }, status_code=503, ) def _guard(request: Request) -> tuple[Any, dict[str, Any]] | HTMLResponse | RedirectResponse: """로그인+권한+store 점검을 한 번에. 페이지 핸들러 진입부에서 사용.""" from app.main import get_current_user_record, render_template # noqa: WPS433 from app.store import has_module, is_admin, login_redirect # noqa: WPS433 user = get_current_user_record(request) if user is None: return login_redirect(request) if not has_module(user, "cupang"): return render_template( request, "denied.html", {"reason": "쿠팡 밀크런 모듈 접근 권한이 없습니다.", "is_admin": is_admin(user)}, status_code=403, ) store = _store(request) if store is None: return _render_config_needed(request, user) return store, user def _ym(request: Request) -> tuple[int, int]: today = today_kst() try: year = int(request.query_params.get("year") or today.year) month = int(request.query_params.get("month") or today.month) except ValueError: year, month = today.year, today.month if not (1 <= month <= 12): year, month = today.year, today.month return year, month # ════════════════════════════════════════════════════════════ # 메인 — 월간 달력 + 선택일 출고 리스트 # ════════════════════════════════════════════════════════════ @router.get("/", response_class=HTMLResponse) def index(request: Request) -> HTMLResponse: from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard year, month = _ym(request) next_y, next_m = (year + 1, 1) if month == 12 else (year, month + 1) prev_y, prev_m = (year - 1, 12) if month == 1 else (year, month - 1) # 달력은 현재 달 + 다음 달 2개월을 함께 보여준다. # 취소된 묶음은 달력·목록 어디에도 보이지 않는다(soft delete = 삭제로 취급). merged: dict[Any, dict[str, Any]] = {} for y, m in ((year, month), (next_y, next_m)): for s in store.list_shipments(year=y, month=m): if s.get("status") == "취소": continue merged[s["id"]] = s shipments = list(merged.values()) # 달력 칸에는 출고 건수와 센터 수만 보여준다. counts: dict[str, dict[str, Any]] = {} for s in shipments: d = s.get("ship_date") if not d: continue cell = counts.setdefault(str(d), {"ship": 0, "centers": set()}) cell["ship"] += 1 cell["centers"].add(s.get("center_id") or s.get("center_name_snapshot") or "") for cell in counts.values(): cell["centers"] = len(cell["centers"]) # 선택 날짜 (기본: 오늘이 보이는 두 달 안이면 오늘, 아니면 첫 달 1일) sel = request.query_params.get("date") or "" today = today_kst() if not sel: in_view = (today.year, today.month) in ((year, month), (next_y, next_m)) sel = today.isoformat() if in_view else f"{year:04d}-{month:02d}-01" # 선택일의 묶음 — 달력과 같은 기준(출고일) sel_shipments = [s for s in shipments if s.get("ship_date") == sel] # 오른쪽 상세: 센터별 상품 목록·수량·상자 수·출고방식 for s in sel_shipments: full = store.get_shipment(shipment_id=s["id"]) lines = (full.get("lines") if full else []) or [] s["items"] = [ { "name": ln.get("product_name_snapshot") or ln.get("product_code"), "qty": int(ln.get("quantity") or 0), "boxes": int(ln.get("calculated_boxes") or 0), } for ln in lines ] s["total_qty"] = sum(it["qty"] for it in s["items"]) s["total_boxes"] = sum(it["boxes"] for it in s["items"]) cal = _calendar.Calendar(firstweekday=6) # 일요일 시작 def _build_month(y: int, m: int) -> dict[str, Any]: weeks = [ [ { "date": d.isoformat(), "day": d.day, "in_month": d.month == m, "is_today": d == today, "is_selected": d.isoformat() == sel, "is_sunday": d.weekday() == 6, "is_saturday": d.weekday() == 5, "is_holiday": is_holiday(d), "holiday_name": holiday_name(d), "counts": counts.get(d.isoformat(), {}), } for d in week ] for week in cal.monthdatescalendar(y, m) ] ship_total = sum( int(c.get("ship") or 0) for day, c in counts.items() if day[:7] == f"{y:04d}-{m:02d}" ) return { "year": y, "month": m, "label": f"{y}년 {m}월", "weeks": weeks, "ship_total": ship_total, } months = [_build_month(year, month), _build_month(next_y, next_m)] return render_template( request, "cupang/index.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": "쿠팡 밀크런", "page_subtitle": f"{year}년 {month}월 · {next_y}년 {next_m}월 출고 일정", "year": year, "month": month, "prev_y": prev_y, "prev_m": prev_m, "next_y": next_y, "next_m": next_m, "today_iso": today.isoformat(), "weekdays": ["일", "월", "화", "수", "목", "금", "토"], "months": months, "selected_date": sel, "sel_shipments": sel_shipments, "outbound_worker": OUTBOUND_WORKER, }, ) # ════════════════════════════════════════════════════════════ # 출고리스트 엑셀 — 확정한 출고일 기준 (쿠팡로켓 밀크런 양식) # ════════════════════════════════════════════════════════════ def _shipments_for_date(store: Any, ship_date: str) -> list[dict[str, Any]]: """해당 출고일의 출고 묶음(라인 포함) — 취소 제외, 센터명 순.""" heads = [ h for h in store.list_shipments(date_from=ship_date, date_to=ship_date) if h.get("status") != "취소" ] out: list[dict[str, Any]] = [] for h in sorted(heads, key=lambda x: (x.get("center_name_snapshot") or "")): full = store.get_shipment(shipment_id=h["id"]) if full: out.append(full) return out # 출고리스트 문서 — 구글 드라이브에 올려 둔 엑셀(.xlsx) 파일(2026-10-23). 구글 시트 문서여도 된다. DEFAULT_SHEET_ID = "1J74op7lBZOgE27p4R28I3RWsv0EtXdii" def _sheet_target(store: Any) -> tuple[Any, str, dict[str, Any] | None]: """(writer, 문서 id, skip 사유) — 쓸 수 없으면 skip 정보를 돌려준다. 문서 형식은 드라이브에서 보고 고른다 — .xlsx 파일이면 drive_xlsx(내려받아 고친 뒤 다시 올림), 구글 시트 문서면 google_sheets. 두 writer 는 같은 메서드를 갖는다.""" import os # noqa: WPS433 from app.integrations import drive_xlsx, google_sheets # noqa: WPS433 spreadsheet_id = (os.getenv("CUPANG_SHEET_ID") or "").strip() or DEFAULT_SHEET_ID drive = drive_xlsx.get_writer() if not drive.enabled: # 드라이브 인증이 없으면 예전처럼 Sheets API 로(구글 시트 문서일 때만 된다) writer = google_sheets.get_writer() if not writer.enabled: return None, "", {"ok": False, "skipped": True, "reason": writer.reason} return writer, spreadsheet_id, None try: kind = drive.file_kind(spreadsheet_id) except Exception as exc: # noqa: BLE001 - 사유만(비밀값 없음) return None, "", {"ok": False, "skipped": True, "reason": str(exc) or type(exc).__name__} if kind == "xlsx": return drive, spreadsheet_id, None if kind != "sheets": return None, "", {"ok": False, "skipped": True, "reason": f"엑셀·구글 시트 파일이 아닙니다({kind})"} writer = google_sheets.get_writer() if not writer.enabled: return None, "", {"ok": False, "skipped": True, "reason": writer.reason} return writer, spreadsheet_id, None def _in_sheet(writer: Any, fn: Any) -> Any: """`with writer.lock:` 안에서 fn() — 읽기~쓰기를 한 덩어리로. 엑셀 파일(drive_xlsx)은 내려받은 뒤 올리기 전에 누가 파일을 고쳤으면 처음부터 다시(최대 3번).""" from app.integrations.drive_xlsx import XlsxChangedError # noqa: WPS433 for attempt in range(3): try: with writer.lock: return fn() except XlsxChangedError: if attempt == 2: raise return None def _sync_google_sheet_after_delete(store: Any, ship_date: str) -> dict[str, Any]: """출고 삭제 후 구글 시트를 현재 상태에 맞춘다. - 그날 남은 출고가 없으면 해당 날짜 시트를 삭제 - 남아 있으면 남은 내용으로 다시 기록 실패해도 DB 삭제는 이미 끝났으므로 예외를 밖으로 던지지 않는다. """ from .export import sheet_title # noqa: WPS433 writer, spreadsheet_id, skip = _sheet_target(store) if skip: return skip if _shipments_for_date(store, ship_date): return _push_to_google_sheet(store, ship_date) try: res = _in_sheet(writer, lambda: writer.delete_sheet(spreadsheet_id=spreadsheet_id, title=sheet_title(ship_date))) except Exception as exc: # noqa: BLE001 - 사유만 남긴다 return {"ok": False, "skipped": False, "reason": f"{type(exc).__name__}: {exc}"} return {"ok": True, "skipped": False, **res} def _log_sheet(action: str, ship_date: str, res: dict[str, Any]) -> None: print( # noqa: T201 - 컨테이너 로그 확인용(비밀값 없음) f"[cupang] sheet {action} date={ship_date} ok={res.get('ok')} " f"skipped={res.get('skipped')} detail={res.get('action', '')} " f"reason={res.get('reason', '')}", flush=True, ) def _push_to_google_sheet(store: Any, ship_date: str) -> dict[str, Any]: """확정한 출고일을 출고리스트 문서(드라이브 엑셀 또는 구글 시트)에 시트 1장으로 기록. - 대상 문서: 환경변수 `CUPANG_SHEET_ID`(기본 DEFAULT_SHEET_ID), 형식은 `_sheet_target` 이 고른다. - 인증: 서비스 계정 또는 OAuth(app/integrations/drive_xlsx.py·google_sheets.py). 미설정이면 조용히 skip. - 실패해도 출고 묶음 저장은 이미 끝났으므로 예외를 밖으로 던지지 않는다. """ writer, spreadsheet_id, skip = _sheet_target(store) if skip: return skip shipments = _shipments_for_date(store, ship_date) if not shipments: return {"ok": False, "skipped": True, "reason": "해당 출고일의 출고 묶음 없음"} try: res = _in_sheet(writer, lambda: _write_full_sheet(writer, spreadsheet_id, ship_date, shipments)) except Exception as exc: # noqa: BLE001 - 시트 기록 실패는 경고로만 알린다 return {"ok": False, "skipped": False, "reason": f"{type(exc).__name__}: {exc}"} return {"ok": True, "skipped": False, **res} def _write_full_sheet( writer: Any, spreadsheet_id: str, ship_date: str, shipments: list[dict[str, Any]] ) -> dict[str, Any]: """출고일 시트를 양식(1~5행)부터 통째로 다시 쓴다. 실패는 예외로 알린다.""" from .export import ( # noqa: WPS433 COL_FIRST, COL_LAST, FIRST_DATA_ROW, HEADER_BG, HEADER_ROW, PALLET_BG, TITLE_ROW, WIDTHS_PX, build_table, sheet_title, ) table = build_table(ship_date, shipments) res = writer.write_table( spreadsheet_id=spreadsheet_id, title=sheet_title(ship_date), cells=table["cells"], merges=table["merges"], header_row=HEADER_ROW, first_data_row=FIRST_DATA_ROW, last_row=table["last_row"], first_col=COL_FIRST, last_col=COL_LAST, title_row=TITLE_ROW, widths=WIDTHS_PX, header_bg=HEADER_BG, row_highlights=[(r1, r2, PALLET_BG) for (r1, r2) in table["pallet_ranges"]], ) return {**res, "first_row": FIRST_DATA_ROW, "last_row": table["last_row"]} # ════════════════════════════════════════════════════════════ # 밀크런 출고리스트 직접 작성 — 상자 계산 없이 팝업에서 입력한 센터 1곳을 # 출고리스트 문서(드라이브 엑셀 파일)의 출고일 시트(YYYYMMDD(요일)) 맨 아래에 덧붙인다. cupang_db 에는 저장하지 않는다. # ════════════════════════════════════════════════════════════ OUTBOUND_SHEET_METHODS: tuple[str, ...] = ("파렛트", "택배") _SHEET_CENTERS_TTL_SECONDS = 600 _sheet_centers_cache: dict[str, Any] = {"at": 0.0, "sheet": "", "names": []} # 팝업이 쓰는 문서 — 항상 드라이브 엑셀(.xlsx) 파일. CUPANG_SHEET_ID(분배 확정 동기화)와 따로 둔다: # 서버의 CUPANG_SHEET_ID 가 구글 시트 문서를 가리켜도 팝업은 엑셀 파일에만 쓴다(구글 시트로 대신 쓰지 않음). DEFAULT_OUTBOUND_XLSX_ID = "1J74op7lBZOgE27p4R28I3RWsv0EtXdii" def _outbound_target() -> tuple[Any, str, dict[str, Any] | None]: """(drive_xlsx writer, 엑셀 파일 id, skip 사유). 드라이브 인증이 없거나 .xlsx 가 아니면 skip.""" import os # noqa: WPS433 from app.integrations import drive_xlsx # noqa: WPS433 file_id = (os.getenv("CUPANG_OUTBOUND_XLSX_ID") or "").strip() or DEFAULT_OUTBOUND_XLSX_ID writer = drive_xlsx.get_writer() if not writer.enabled: return None, "", {"ok": False, "skipped": True, "reason": writer.reason} try: kind = writer.file_kind(file_id) except Exception as exc: # noqa: BLE001 - 사유만(비밀값 없음) return None, "", {"ok": False, "skipped": True, "reason": str(exc) or type(exc).__name__} if kind != "xlsx": return None, "", {"ok": False, "skipped": True, "reason": f"출고리스트 파일이 엑셀(.xlsx)이 아닙니다({kind})"} return writer, file_id, None def _sheet_text(value: Any, *, label: str, max_len: int, required: bool = True) -> str: text = str(value or "").strip() if required and not text: raise HTTPException(status_code=400, detail=f"{label}을(를) 입력해 주세요.") if len(text) > max_len: raise HTTPException(status_code=400, detail=f"{label}은(는) {max_len}자까지 입력할 수 있습니다.") # USER_ENTERED 로 쓰므로 '=' 로 시작하면 수식이 된다 — 글자 그대로 남긴다. return "'" + text if text.startswith("=") else text def _sheet_date(value: Any, *, label: str) -> str: try: return _date.fromisoformat(str(value or "").strip()).isoformat() except ValueError: raise HTTPException(status_code=400, detail=f"{label}을(를) 선택해 주세요.") from None def _clean_outbound_entry(payload: dict[str, Any]) -> dict[str, Any]: """팝업 입력 → export.build_block 이 읽는 출고 묶음 모양.""" method = str(payload.get("ship_method") or "").strip() if method not in OUTBOUND_SHEET_METHODS: raise HTTPException(status_code=400, detail="출고방식은 파렛트 또는 택배 중에서 골라 주세요.") raw_lines = payload.get("lines") if not isinstance(raw_lines, list) or not raw_lines: raise HTTPException(status_code=400, detail="제품을 1개 이상 추가해 주세요.") if len(raw_lines) > 100: raise HTTPException(status_code=400, detail="제품은 한 번에 100줄까지 입력할 수 있습니다.") lines = [] for i, ln in enumerate(raw_lines, start=1): if not isinstance(ln, dict): raise HTTPException(status_code=400, detail=f"{i}번째 제품 정보를 확인해 주세요.") try: qty = int(str(ln.get("quantity") or "").replace(",", "").strip()) except ValueError: qty = 0 if not 1 <= qty <= 1_000_000: raise HTTPException(status_code=400, detail=f"{i}번째 제품의 수량을 확인해 주세요.") lines.append({ "product_code": _sheet_text(ln.get("code"), label=f"{i}번째 제품코드", max_len=60), "product_name_snapshot": _sheet_text(ln.get("name"), label=f"{i}번째 제품명", max_len=200), "quantity": qty, }) return { "document_date": _sheet_date(payload.get("document_date"), label="작성일"), "ship_date": _sheet_date(payload.get("ship_date"), label="출고일"), "center_arrival_date": _sheet_date(payload.get("center_arrival_date"), label="센터입고일"), "center_name_snapshot": _sheet_text(payload.get("center"), label="입고센터", max_len=60), "ship_method": method, "outbound_summary": _sheet_text(payload.get("outbound"), label="출고", max_len=100, required=False), "worker": _sheet_text(payload.get("worker") or OUTBOUND_WORKER, label="작업자", max_len=30), "lines": lines, } def _sheet_centers(writer: Any, spreadsheet_id: str) -> list[str]: """출고리스트 문서의 모든 시트 F열(입고센터, 6행부터)에 적힌 센터 이름. 10분 캐시.""" import time # noqa: WPS433 from app.integrations.google_sheets import a1_sheet # noqa: WPS433 from .export import FIRST_DATA_ROW # noqa: WPS433 cache = _sheet_centers_cache if cache["sheet"] == spreadsheet_id and time.monotonic() - cache["at"] < _SHEET_CENTERS_TTL_SECONDS: return list(cache["names"]) with writer.lock: # 엑셀 파일이면 한 번만 내려받는다 titles = writer.sheet_titles(spreadsheet_id=spreadsheet_id) columns = writer.read_ranges( spreadsheet_id=spreadsheet_id, ranges=[f"{a1_sheet(t)}!F{FIRST_DATA_ROW}:F" for t in titles], ) names = sorted({ str(row[0]).strip() for rows in columns for row in rows if row and str(row[0]).strip() }) cache.update(at=time.monotonic(), sheet=spreadsheet_id, names=names) return list(names) def _append_outbound_entry(writer: Any, spreadsheet_id: str, entry: dict[str, Any]) -> dict[str, Any]: """출고일 시트 맨 아래에 덧붙인다. 시트가 없거나 양식(5행 머리글)이 없으면 양식째 만든다.""" from app.integrations.google_sheets import a1_sheet, col_letter # noqa: WPS433 from .export import ( # noqa: WPS433 COL_FIRST, COL_LAST, FIRST_DATA_ROW, HEADER_ROW, PALLET_BG, build_block, sheet_title, ) title = sheet_title(entry["ship_date"]) rows: list[list[Any]] = [] if title in writer.sheet_titles(spreadsheet_id=spreadsheet_id): rows = writer.read_ranges( spreadsheet_id=spreadsheet_id, ranges=[f"{a1_sheet(title)}!A1:{col_letter(COL_LAST)}"], )[0] last_row = max( (i for i, row in enumerate(rows, start=1) if any(str(v).strip() for v in row)), default=0 ) if last_row < HEADER_ROW: return _write_full_sheet(writer, spreadsheet_id, entry["ship_date"], [entry]) # 순번(B열)은 시트에 이미 있는 가장 큰 번호 다음부터 last_seq = 0 for row in rows[FIRST_DATA_ROW - 1:]: text = str(row[1]).strip() if len(row) > 1 else "" if text.isdigit(): last_seq = max(last_seq, int(text)) block = build_block(entry, start_row=last_row + 1, start_seq=last_seq) return writer.append_block( spreadsheet_id=spreadsheet_id, title=title, cells=block["cells"], merges=block["merges"], first_row=block["first_row"], last_row=block["last_row"], first_col=COL_FIRST, last_col=COL_LAST, highlight=PALLET_BG if block["is_pallet"] else "", ) @router.get("/api/outbound-sheet/centers") def outbound_sheet_centers( request: Request, _: dict[str, Any] = Depends(_require_user) ) -> JSONResponse: """입고센터 후보 — 출고리스트 문서에 적힌 센터 + 설정에 등록된 센터.""" store = _store(request) names = {c["name"] for c in store.list_centers()} if store else set() reason = "" writer, spreadsheet_id, skip = _outbound_target() if skip: reason = str(skip.get("reason") or "") else: try: names.update(_sheet_centers(writer, spreadsheet_id)) except Exception as exc: # noqa: BLE001 - 목록은 등록 센터만으로도 쓸 수 있다 reason = f"출고리스트 파일을 읽지 못했습니다({str(exc) or type(exc).__name__})" return JSONResponse({"centers": sorted(names), "reason": reason}) _OUTBOUND_MAX_CENTERS = 20 def _clean_outbound_entries(payload: dict[str, Any]) -> list[dict[str, Any]]: """팝업 입력 → 입고센터마다 출고 묶음 1건. 공통(작성일·출고일·센터입고일·작업자) + `centers: [{center, ship_method, outbound, lines}]`. `centers` 가 없으면 payload 자체를 센터 1곳으로 본다.""" raw = payload.get("centers") if raw is None: return [_clean_outbound_entry(payload)] if not isinstance(raw, list) or not raw: raise HTTPException(status_code=400, detail="입고센터를 1곳 이상 추가해 주세요.") if len(raw) > _OUTBOUND_MAX_CENTERS: raise HTTPException(status_code=400, detail=f"입고센터는 한 번에 {_OUTBOUND_MAX_CENTERS}곳까지 입력할 수 있습니다.") common = {k: payload.get(k) for k in ("document_date", "ship_date", "center_arrival_date", "worker")} entries = [] for i, c in enumerate(raw, start=1): if not isinstance(c, dict): raise HTTPException(status_code=400, detail=f"{i}번째 입고센터 정보를 확인해 주세요.") try: entries.append(_clean_outbound_entry({**c, **common})) except HTTPException as exc: prefix = f"{i}번째 입고센터: " if len(raw) > 1 else "" raise HTTPException(status_code=exc.status_code, detail=prefix + str(exc.detail)) from None return entries def _run_outbound_write( writer: Any, file_id: str, entries: list[dict[str, Any]], report: Any ) -> dict[str, Any]: """센터들을 차례로 출고일 시트 아래에 덧붙인다(엑셀은 내려받기·올리기 1번씩). report(진행률, 다음 단계 진행률, 단계 이름) 로 진행 상황을 알린다. 실패는 HTTPException(502).""" from app.integrations.drive_xlsx import XlsxChangedError # noqa: WPS433 from app.integrations.google_drive import GoogleDriveError # noqa: WPS433 ship_date = entries[0]["ship_date"] n = len(entries) def run() -> list[dict[str, Any]]: report(5, 40, "출고리스트 엑셀 내려받는 중") writer.sheet_titles(spreadsheet_id=file_id) # 이 덩어리에서 한 번 내려받는다 done = [] for i, entry in enumerate(entries): report(40 + 40 * i // n, 40 + 40 * (i + 1) // n, f"{entry['center_name_snapshot']} 추가 중 ({i + 1}/{n})") done.append(_append_outbound_entry(writer, file_id, entry)) report(80, 100, "출고리스트 엑셀 올리는 중") return done try: # 시트 끝 위치를 읽고 쓰는 사이에 다른 요청이 끼어들지 않게 한 덩어리로 results = _in_sheet(writer, run) except Exception as exc: # noqa: BLE001 - 사유만 남긴다(비밀값 미출력) _log_sheet("manual-append", ship_date, {"ok": False, "reason": type(exc).__name__}) if isinstance(exc, XlsxChangedError): why = "다른 사람이 파일을 고치는 중입니다" elif isinstance(exc, GoogleDriveError): why = str(exc) else: why = type(exc).__name__ raise HTTPException( status_code=502, detail=f"출고리스트 파일에 기록하지 못했습니다({why}). 잠시 후 다시 시도해 주세요." ) from exc first, last = results[0], results[-1] _log_sheet("manual-append", ship_date, {"ok": True, "action": f"xlsx {file_id} centers {n} rows {first.get('first_row')}-{last.get('last_row')}"}) if _sheet_centers_cache["sheet"] == file_id: _sheet_centers_cache["names"] = sorted( {*_sheet_centers_cache["names"], *(e["center_name_snapshot"] for e in entries)}) return { "ok": True, "title": last.get("title"), "url": last.get("url"), "first_row": first.get("first_row"), "last_row": last.get("last_row"), "line_count": sum(len(e["lines"]) for e in entries), "centers": [ {"center": e["center_name_snapshot"], "first_row": r.get("first_row"), "last_row": r.get("last_row"), "line_count": len(e["lines"])} for e, r in zip(entries, results) ], } @router.post("/api/outbound-sheet") def outbound_sheet_write( request: Request, payload: dict[str, Any] = Body(...), stream: bool = False, _: dict[str, Any] = Depends(_require_user), ) -> Any: """출고리스트 엑셀에 센터들을 덧붙인다. `?stream=1` 이면 한 줄에 JSON 하나씩(NDJSON) 진행 상황을 흘려보낸다 — `{"progress", "until", "step"}` … 마지막에 `{"done": true, ...결과}` 또는 `{"error": 사유}`. 입력 오류(400)·설정 없음(503)은 흘려보내기 전에 보통 JSON 오류로 돌려준다.""" entries = _clean_outbound_entries(payload) writer, file_id, skip = _outbound_target() if skip: _log_sheet("manual-append", entries[0]["ship_date"], skip) raise HTTPException( status_code=503, detail=f"출고리스트 엑셀 파일에 쓸 수 없습니다: {skip.get('reason') or '설정 확인 필요'}" ) if not stream: return JSONResponse(_run_outbound_write(writer, file_id, entries, lambda *_a: None)) import queue # noqa: WPS433 import threading # noqa: WPS433 from fastapi.responses import StreamingResponse # noqa: WPS433 events: Any = queue.Queue() def work() -> None: # 잠금(writer.lock)은 잡은 스레드에서 풀어야 하므로 쓰기 전체를 스레드 하나에서 try: res = _run_outbound_write( writer, file_id, entries, lambda p, until, step: events.put({"progress": p, "until": until, "step": step}), ) events.put({"progress": 100, "until": 100, "step": "완료", "done": True, **res}) except HTTPException as exc: events.put({"error": str(exc.detail)}) except Exception as exc: # noqa: BLE001 events.put({"error": f"출고리스트 파일에 기록하지 못했습니다({type(exc).__name__})."}) finally: events.put(None) threading.Thread(target=work, name="cupang-outbound-write", daemon=True).start() def lines() -> Any: while True: item = events.get() if item is None: return yield json.dumps(item, ensure_ascii=False) + "\n" return StreamingResponse(lines(), media_type="application/x-ndjson", headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}) @router.get("/export.xlsx") def export_shipments_xlsx(request: Request, date: str = "") -> Any: """출고일 기준 출고리스트 엑셀 다운로드. 시트명 = YYYYMMDD.""" from io import BytesIO # noqa: WPS433 from fastapi.responses import StreamingResponse # noqa: WPS433 from .export import build_workbook # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, _user = guard ship_date = (date or "").strip() try: _date.fromisoformat(ship_date) except ValueError: raise HTTPException(status_code=400, detail="출고일자(date=YYYY-MM-DD)가 필요합니다.") shipments = _shipments_for_date(store, ship_date) if not shipments: raise HTTPException(status_code=404, detail="해당 출고일의 출고 묶음이 없습니다.") wb = build_workbook(ship_date, shipments) buf = BytesIO() wb.save(buf) buf.seek(0) filename = f"{ship_date.replace('-', '')}_cupang_milkrun.xlsx" return StreamingResponse( buf, media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", headers={"Content-Disposition": f'attachment; filename="{filename}"'}, ) # ════════════════════════════════════════════════════════════ # 출고 묶음 — 등록 / 수정 / 상세 # ════════════════════════════════════════════════════════════ @router.get("/new") def new_form(request: Request) -> RedirectResponse: """신규 등록 폼은 없앴다. 출고 묶음은 상자 계산의 [분배 확정] 으로만 만든다. 예전 링크/북마크가 404 나지 않도록 상자 계산으로 보낸다. """ return RedirectResponse(url="/cupang/box-calc", status_code=303) @router.get("/{shipment_id:int}", response_class=HTMLResponse) def detail(request: Request, shipment_id: int) -> HTMLResponse: """출고 묶음 보기 — 상자 계산 화면과 같은 3열 구성(읽기 전용). 수정 기능은 없앴다. 잘못 만들었으면 달력에서 삭제하고 다시 확정한다. """ from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard ship = store.get_shipment(shipment_id=shipment_id) if not ship: return render_template( request, "denied.html", {"reason": "출고 묶음을 찾을 수 없습니다.", "is_admin": is_admin(user)}, status_code=404, ) lines = ship.get("lines") or [] items = [ { "product_code": ln.get("product_code"), "product_name": ln.get("product_name_snapshot") or ln.get("product_code"), "quantity": int(ln.get("quantity") or 0), } for ln in lines ] calc = _compute_boxes(store, items) total_qty = sum(it["quantity"] for it in items) box_list = _expand_box_list(ship, calc, items) box_weight = _attach_box_weights(store, box_list) # 상자 목록 합계 행 — 수량 합계와 무게 합계(kg, 소수 첫째 자리) box_qty_total = sum(int(it.get("quantity") or 0) for b in box_list for it in (b.get("items") or [])) box_weight_total = None box_weight_partial = False if box_weight: for b in box_list: b["weight_kg"] = _g_to_kg(b.get("weight_g")) if b.get("weight_g") is not None: box_weight_total = (box_weight_total or 0) + int(b["weight_g"]) if b.get("weight_missing"): box_weight_partial = True return render_template( request, "cupang/view.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": f"출고 #{ship['id']}", "page_subtitle": f"{ship['ship_date']} · {ship['center_name_snapshot']}", "shipment": ship, "items": items, "calc": calc, "box_list": box_list, # 상자별 무게 합계(g) 칸 — 포장무게(마이그레이션 007) 가 있을 때만 "box_weight": box_weight, "box_qty_total": box_qty_total, "box_weight_total_kg": _g_to_kg(box_weight_total), "box_weight_partial": box_weight_partial, "total_qty": total_qty, }, ) @router.get("/{shipment_id:int}/boxes.xlsx") def export_box_list_xlsx(request: Request, shipment_id: int) -> Any: """상자 목록(상자번호·제품명·제품코드·수량·무게) 엑셀 다운로드.""" from io import BytesIO # noqa: WPS433 from fastapi.responses import StreamingResponse # noqa: WPS433 from .export import build_box_list_workbook # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, _user = guard ship = store.get_shipment(shipment_id=shipment_id) if not ship: raise HTTPException(status_code=404, detail="출고 묶음을 찾을 수 없습니다.") items = [ { "product_code": ln.get("product_code"), "product_name": ln.get("product_name_snapshot") or ln.get("product_code"), "quantity": int(ln.get("quantity") or 0), } for ln in (ship.get("lines") or []) ] box_list = _expand_box_list(ship, _compute_boxes(store, items), items) with_weight = _attach_box_weights(store, box_list) wb = build_box_list_workbook(ship, box_list, with_weight=with_weight) buf = BytesIO() wb.save(buf) buf.seek(0) stamp = str(ship.get("ship_date") or "").replace("-", "") filename = f"{stamp}_cupang_boxes_{shipment_id}.xlsx" return StreamingResponse( buf, media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", headers={"Content-Disposition": f'attachment; filename="{filename}"'}, ) def _safe_next(raw: str, fallback: str) -> str: """열린 리다이렉트 방지 — /cupang/ 안쪽 경로만 허용.""" nxt = (raw or "").strip() if nxt.startswith("/cupang/") and "//" not in nxt[1:]: return nxt return fallback @router.post("/{shipment_id:int}/delete") def delete( request: Request, shipment_id: int, next: str = Form(""), user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: """운영 안전: 기본은 status='취소' soft delete.""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") ship = store.get_shipment(shipment_id=shipment_id, with_lines=False) ship_date = str((ship or {}).get("ship_date") or "") try: store.soft_delete(shipment_id=shipment_id) except KeyError: raise HTTPException(status_code=404, detail="출고 묶음을 찾을 수 없습니다.") if ship_date: _log_sheet("sync", ship_date, _sync_google_sheet_after_delete(store, ship_date)) return RedirectResponse( url=_safe_next(next, f"/cupang/?date={ship_date}" if ship_date else "/cupang/"), status_code=303, ) @router.post("/{shipment_id:int}/hard-delete") def hard_delete( request: Request, shipment_id: int, user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: """완전 삭제(헤더+라인 CASCADE). 달력으로 복귀.""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") ship = store.get_shipment(shipment_id=shipment_id, with_lines=False) ship_date = str((ship or {}).get("ship_date") or "") try: store.hard_delete(shipment_id=shipment_id) except KeyError: raise HTTPException(status_code=404, detail="출고 묶음을 찾을 수 없습니다.") if ship_date: _log_sheet("sync", ship_date, _sync_google_sheet_after_delete(store, ship_date)) return RedirectResponse(url="/cupang/", status_code=303) @router.post("/day-delete") def day_delete( request: Request, date: str = Form(...), next: str = Form(""), user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: """선택한 출고일의 묶음을 한 번에 취소 처리한다(soft delete).""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") day = (date or "").strip() try: _date.fromisoformat(day) except ValueError: raise HTTPException(status_code=400, detail="날짜 형식이 올바르지 않습니다.") for ship in store.list_shipments(date_from=day, date_to=day): if ship.get("status") == "취소": continue try: store.soft_delete(shipment_id=ship["id"]) except KeyError: continue # 그날 출고가 모두 사라졌으면 구글 시트의 해당 날짜 시트도 지운다. _log_sheet("sync", day, _sync_google_sheet_after_delete(store, day)) return RedirectResponse( url=_safe_next(next, f"/cupang/?date={day}"), status_code=303 ) # ════════════════════════════════════════════════════════════ # 쿠팡 로켓 매출 (cupang_sales) # ════════════════════════════════════════════════════════════ def _sales_totals(rows: list[dict[str, Any]]) -> dict[str, Any]: """조회 결과 합계 — 화면 KPI 와 엑셀 하단에 같이 쓴다.""" total = { "count": len(rows), "quantity": 0, "supply_amount": 0.0, "cost_amount": 0.0, "logistics_total": 0.0, "margin": 0.0, } for r in rows: total["quantity"] += int(r.get("quantity") or 0) for key in ("supply_amount", "cost_amount", "logistics_total", "margin"): total[key] += float(r.get(key) or 0) supply = total["supply_amount"] total["margin_rate"] = round(total["margin"] / supply * 100, 2) if supply else 0.0 total["logistics_ratio"] = ( round(total["logistics_total"] / supply * 100, 2) if supply else 0.0 ) return total def _sales_filters(request: Request) -> dict[str, str]: q = request.query_params return { "date_from": (q.get("from") or "").strip(), "date_to": (q.get("to") or "").strip(), "center_name": (q.get("center") or "").strip(), "keyword": (q.get("q") or "").strip(), "po_type": (q.get("type") or "").strip(), "week_label": (q.get("week") or "").strip(), } @router.get("/sales", response_class=HTMLResponse) def sales_page(request: Request) -> HTMLResponse: """매출 표 — 기간·센터·발주유형·검색어로 조회, 합계 표시.""" from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard filters = _sales_filters(request) rows = store.list_sales(**filters) weekly = {w["week_label"]: w for w in store.list_sales_weekly()} # 주차별 광고비/할인/장려금은 조회에 걸린 주차만 더한다. weeks = {r.get("week_label") for r in rows if r.get("week_label")} extra = {"ad_cost": 0.0, "promo_discount": 0.0, "incentive": 0.0} for label in weeks: w = weekly.get(label) if not w: continue for key in extra: extra[key] += float(w.get(key) or 0) # 줄무늬 — 기본은 입고센터가 바뀔 때마다, 센터로 검색 중이면 출고일이 바뀔 때마다. band_key = "ship_date" if filters["center_name"] else "center_name" band = 0 prev = None for row in rows: value = row.get(band_key) if prev is not None and value != prev: band ^= 1 prev = value row["band"] = band totals = _sales_totals(rows) totals.update(extra) totals["net_margin"] = ( totals["margin"] - extra["ad_cost"] - extra["promo_discount"] + extra["incentive"] ) totals["net_margin_rate"] = ( round(totals["net_margin"] / totals["supply_amount"] * 100, 2) if totals["supply_amount"] else 0.0 ) return render_template( request, "cupang/sales.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": "쿠팡 로켓 매출", "page_subtitle": "발주 라인별 공급가·원가·물류비·마진", "rows": rows, "totals": totals, "filters": filters, "centers": store.sales_centers(), "weeks": store.sales_weeks(), "po_types": ["일반", "벤더플렉스"], "weekly": sorted(weekly.values(), key=lambda w: w.get("week_from") or ""), }, ) @router.post("/sales/api") def sales_create( request: Request, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: row = store.create_sale(data=payload) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return JSONResponse({"ok": True, "row": row}) @router.post("/sales/api/{sale_id:int}") def sales_update( request: Request, sale_id: int, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: row = store.update_sale(sale_id=sale_id, data=payload) except KeyError: raise HTTPException(status_code=404, detail="매출 행을 찾을 수 없습니다.") except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return JSONResponse({"ok": True, "row": row}) @router.post("/sales/api/{sale_id:int}/delete") def sales_delete( request: Request, sale_id: int, user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.delete_sale(sale_id=sale_id) except KeyError: raise HTTPException(status_code=404, detail="매출 행을 찾을 수 없습니다.") return JSONResponse({"ok": True}) @router.post("/sales/upload") def sales_upload( request: Request, files: list[UploadFile] = File(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """구글 시트 양식 그대로의 xlsx/csv 를 올려 일괄 등록(같은 라인은 덮어씀).""" from .sales_import import parse_upload # noqa: WPS433 store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") saved = 0 weekly_saved = 0 names: list[str] = [] for up in files: data = up.file.read() # sync def(스레드풀) — 동기 읽기 if not data: continue try: parsed = parse_upload(up.filename or "", data) except ValueError as exc: raise HTTPException(status_code=400, detail=f"{up.filename}: {exc}") except Exception as exc: # noqa: BLE001 - 파일 형식 문제를 사용자에게 알린다 raise HTTPException( status_code=400, detail=f"{up.filename}: 읽을 수 없습니다 ({type(exc).__name__})" ) if parsed["lines"]: saved += store.upsert_sales_bulk(parsed["lines"])["saved"] for wk in parsed["weeklies"]: try: store.upsert_sales_weekly(data=wk) weekly_saved += 1 except ValueError: continue names.append(up.filename or "") if not saved and not weekly_saved: raise HTTPException(status_code=400, detail="읽어들인 매출 행이 없습니다.") return JSONResponse( {"ok": True, "saved": saved, "weekly": weekly_saved, "files": names} ) @router.get("/sales/export.xlsx") def sales_export_xlsx(request: Request) -> Any: """현재 조회 조건 그대로 엑셀 다운로드.""" from io import BytesIO # noqa: WPS433 from fastapi.responses import StreamingResponse # noqa: WPS433 from .export import build_sales_workbook # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, _user = guard filters = _sales_filters(request) rows = store.list_sales(**filters) wb = build_sales_workbook(rows, _sales_totals(rows)) buf = BytesIO() wb.save(buf) buf.seek(0) stamp = (filters["date_from"] or "all").replace("-", "") return StreamingResponse( buf, media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", headers={"Content-Disposition": f'attachment; filename="cupang_sales_{stamp}.xlsx"'}, ) # ════════════════════════════════════════════════════════════ # 상자 입수량 관리 # ════════════════════════════════════════════════════════════ @router.get("/box-rules", response_class=HTMLResponse) def box_rules_page(request: Request) -> HTMLResponse: from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard return render_template( request, "cupang/box_rules.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": "쿠팡 밀크런 — 상자 입수량", "page_subtitle": "제품코드별 쿠팡상자 1상자당 입수량 설정", "box_rules": store.list_box_rules(include_inactive=True), "products": store.list_products(), "search_enabled": bool((_itemcode(request)) and _itemcode(request).enabled), }, ) @router.post("/box-rules") def box_rule_upsert( request: Request, product_code: str = Form(...), units_per_box: int = Form(...), product_name_snapshot: str = Form(""), box_name: str = Form("쿠팡상자"), memo: str = Form(""), user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.upsert_box_rule( product_code=product_code, units_per_box=units_per_box, product_name_snapshot=product_name_snapshot, box_name=box_name, memo=memo, ) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return RedirectResponse(url="/cupang/box-rules", status_code=303) @router.post("/box-rules/{rule_id}/delete") def box_rule_delete( request: Request, rule_id: int, user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.delete_box_rule(rule_id=rule_id) except KeyError: raise HTTPException(status_code=404, detail="규칙을 찾을 수 없습니다.") return RedirectResponse(url="/cupang/box-rules", status_code=303) # ════════════════════════════════════════════════════════════ # 상자 계산기 — 제품명 + 수량 → 상자 수 / 남은 낱개 # 저장하지 않는 계산 전용 화면. 규칙은 cupang_box_rules 를 그대로 사용한다. # ════════════════════════════════════════════════════════════ @router.get("/box-calc", response_class=HTMLResponse) def box_calc_page(request: Request) -> HTMLResponse: from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard return render_template( request, "cupang/box_calc.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": "쿠팡 밀크런 — 쿠팡 발주 업로드", "page_subtitle": "쿠팡 발주 엑셀을 올리면 센터별 수량을 합산해 상자를 계산합니다.", "box_rules": store.list_box_rules(), # 센터 선택 드롭다운은 가나다순 (한글 음절은 코드포인트 순 = 가나다순) "centers": sorted(store.list_centers(), key=lambda c: (c.get("name") or "")), }, ) # ──────────────────────────────────────────────────────────── # 쿠팡 발주 엑셀 업로드 — F13 입고예정일 / 22행부터 상품 # B열 = 쿠팡상품코드, F열 = 물류센터, G열 = 발주수량 # ──────────────────────────────────────────────────────────── PO_FIRST_ROW = 22 # 상품이 시작되는 행 PO_ARRIVAL_CELL = "F13" # 입고예정일시 def _po_cell_date(value: Any) -> _date | None: """F13 값(datetime / date / 문자열) → date. 인식 못 하면 None.""" from datetime import datetime as _dt # noqa: WPS433 if isinstance(value, _dt): return value.date() if isinstance(value, _date): return value text = str(value or "").strip() if not text: return None text = text.split(" ")[0].replace(".", "-").replace("/", "-") try: return _date.fromisoformat(text) except ValueError: return None def _po_int(value: Any) -> int: try: return int(float(str(value).replace(",", "").strip())) except (TypeError, ValueError): return 0 def _parse_po_sheet(ws: Any) -> dict[str, Any]: """발주서 시트 1장 → {arrival_date, rows:[{coupang_item_code, center_name, quantity}]}.""" arrival = _po_cell_date(ws[PO_ARRIVAL_CELL].value) rows: list[dict[str, Any]] = [] for r in range(PO_FIRST_ROW, (ws.max_row or PO_FIRST_ROW) + 1): code = str(ws.cell(row=r, column=2).value or "").strip() # B if not code or code in ("합계", "소계"): continue code = code.split(".")[0] if code.replace(".", "").isdigit() else code center = str(ws.cell(row=r, column=6).value or "").strip() # F qty = _po_int(ws.cell(row=r, column=7).value) # G if not center or qty <= 0: continue rows.append({"coupang_item_code": code, "center_name": center, "quantity": qty}) return {"arrival_date": arrival, "rows": rows} @router.post("/api/box-calc/upload") def box_calc_upload( request: Request, files: list[UploadFile] = File(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """쿠팡 발주 엑셀 여러 개 → 센터별 제품 수량 합산. - 출고일 = F13(입고예정일)의 하루 전 - 쿠팡상품코드(B) → cupang_products.coupang_item_code 로 제품 매칭 - 센터명(F) → cupang_centers.name 으로 매칭(없으면 그 이름으로 자동 등록) """ from io import BytesIO # noqa: WPS433 from datetime import timedelta as _timedelta # noqa: WPS433 from openpyxl import load_workbook # noqa: WPS433 store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") if not files: raise HTTPException(status_code=400, detail="엑셀 파일을 선택하세요.") products = store.list_products(include_inactive=True) by_coupang = { (p.get("coupang_item_code") or "").strip(): p for p in products if (p.get("coupang_item_code") or "").strip() } centers = store.list_centers(include_inactive=True) by_center_name = {(c.get("name") or "").strip(): c for c in centers} next_center_order = max((int(c.get("sort_order") or 0) for c in centers), default=0) + 1 rules = {r["product_code"]: r for r in store.list_box_rules()} warnings: list[str] = [] file_infos: list[dict[str, Any]] = [] ship_dates: list[str] = [] # (출고일, 센터명, 제품코드) → 수량 agg: dict[tuple[str, str, str], dict[str, Any]] = {} unknown_codes: set[str] = set() added_centers: set[str] = set() no_rule: set[str] = set() for up in files: raw = up.file.read() # sync def(스레드풀) — 동기 읽기 name = up.filename or "(이름 없음)" try: wb = load_workbook(BytesIO(raw), data_only=True) except Exception: # noqa: BLE001 - 엑셀이 아니거나 손상 warnings.append(f"{name}: 엑셀 파일을 읽지 못했습니다.") continue parsed = _parse_po_sheet(wb.worksheets[0]) wb.close() arrival = parsed["arrival_date"] ship = (arrival - _timedelta(days=1)) if arrival else None if ship: ship_dates.append(ship.isoformat()) else: warnings.append(f"{name}: F13 입고예정일을 읽지 못했습니다.") ship_iso = ship.isoformat() if ship else "" used = 0 for row in parsed["rows"]: prod = by_coupang.get(row["coupang_item_code"]) if not prod: unknown_codes.add(row["coupang_item_code"]) continue cname = row["center_name"] if cname not in by_center_name: # 미등록 센터는 발주서에 적힌 이름 그대로 자동 등록해 계산에 포함한다. try: created = store.create_center(name=cname, sort_order=next_center_order) next_center_order += 1 except Exception: # noqa: BLE001 - 등록 실패해도 나머지는 계산 warnings.append(f"센터 자동 등록 실패: {cname}") continue by_center_name[cname] = created added_centers.add(cname) code = prod["product_code"] if code not in rules: no_rule.add(f'{prod["product_name"]}({code})') key = (ship_iso, cname, code) cell = agg.setdefault( key, { "ship_date": ship_iso, "center_name": cname, "product_code": code, "product_name": prod["product_name"], "coupang_item_code": row["coupang_item_code"], "quantity": 0, }, ) cell["quantity"] += row["quantity"] used += row["quantity"] file_infos.append( { "filename": name, "arrival_date": arrival.isoformat() if arrival else "", "ship_date": ship_iso, "rows": len(parsed["rows"]), "quantity": used, } ) if unknown_codes: warnings.append( "등록되지 않은 쿠팡상품코드 " + str(len(unknown_codes)) + "건 제외: " + ", ".join(sorted(unknown_codes)[:10]) + (" 외" if len(unknown_codes) > 10 else "") ) if added_centers: warnings.append("새 입고센터 자동 등록: " + ", ".join(sorted(added_centers))) if no_rule: warnings.append("상자 입수량 미설정: " + ", ".join(sorted(no_rule))) uniq_dates = sorted(set(ship_dates)) # 출고일 → 센터 → 품목. 업로드한 파일의 출고일이 다르면 날짜별로 나뉜다. by_date: dict[str, dict[str, dict[str, Any]]] = {} for (ship_iso, cname, _code), cell in agg.items(): centers_of_date = by_date.setdefault(ship_iso, {}) g = centers_of_date.setdefault( cname, { "center_name": cname, "center_id": (by_center_name.get(cname) or {}).get("id"), "items": [], }, ) g["items"].append( { "product_code": cell["product_code"], "product_name": cell["product_name"], "coupang_item_code": cell["coupang_item_code"], "quantity": cell["quantity"], } ) groups: list[dict[str, Any]] = [] for ship_iso in sorted(by_date): centers_of_date = by_date[ship_iso] for g in centers_of_date.values(): g["items"].sort(key=lambda it: it["product_code"]) groups.append( { "ship_date": ship_iso, "centers": sorted(centers_of_date.values(), key=lambda g: g["center_name"]), "files": [f for f in file_infos if f["ship_date"] == ship_iso], "quantity": sum( it["quantity"] for g in centers_of_date.values() for it in g["items"] ), } ) return JSONResponse( { "ok": True, "ship_date": uniq_dates[0] if uniq_dates else "", "ship_dates": uniq_dates, "files": file_infos, "groups": groups, "warnings": warnings, } ) @router.post("/api/box-calc") def box_calc_api( request: Request, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """[{product_code, quantity}] → 제품별 상자 계산 + 상자명별 합계. 클라이언트 계산을 신뢰하지 않고 store.compute_boxes 로 서버에서 계산한다. """ store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") raw_items = payload.get("items") if not isinstance(raw_items, list): raise HTTPException(status_code=400, detail="items 는 배열이어야 합니다.") return JSONResponse(_compute_boxes(store, raw_items)) def _compute_boxes(store: Any, raw_items: list[Any]) -> dict[str, Any]: """[{product_code, quantity}] → 제품별 상자 + 자투리 혼합 상자 + 합계. 상자 계산 화면(API)과 출고 상세 보기가 같은 결과를 쓰도록 한 곳에 둔다. """ from .store import compute_boxes # noqa: WPS433 rules = {r["product_code"]: r for r in store.list_box_rules()} results: list[dict[str, Any]] = [] totals: dict[str, dict[str, Any]] = {} for raw in raw_items: if not isinstance(raw, dict): continue code = str(raw.get("product_code") or "").strip() if not code: continue try: qty = int(raw.get("quantity") or 0) except (TypeError, ValueError): qty = 0 qty = max(qty, 0) rule = rules.get(code) upb = rule["units_per_box"] if rule else None calc = compute_boxes(qty, upb) box_name = (rule or {}).get("box_name") or "" results.append( { "product_code": code, "product_name": (rule or {}).get("product_name_snapshot") or code, "box_name": box_name, "units_per_box": calc["units_per_box"], "quantity": qty, "configured": calc["configured"], "full_boxes": calc["full_boxes"], "remainder_units": calc["remainder_units"], "required_boxes": calc["required_boxes"], } ) if calc["configured"]: agg = totals.setdefault( box_name, {"box_name": box_name, "full_boxes": 0, "required_boxes": 0, "remainder_units": 0} ) agg["full_boxes"] += calc["full_boxes"] agg["required_boxes"] += calc["required_boxes"] agg["remainder_units"] += calc["remainder_units"] mixes = _pack_leftovers(results) grand_total = sum(r["full_boxes"] or 0 for r in results if r["configured"]) grand_total += sum(m["box_count"] for m in mixes) return { "results": results, "totals": sorted(totals.values(), key=lambda t: t["box_name"]), "mixes": mixes, "grand_total_boxes": grand_total, } def _g_to_kg(grams: Any) -> str | None: """g → kg 표시 글자 — 소수 둘째 자리에서 반올림(사사오입)해 첫째 자리까지. 예: 1,250g → "1.3".""" if grams is None: return None kg = (Decimal(int(grams)) / 1000).quantize(Decimal("0.1"), rounding=ROUND_HALF_UP) return f"{kg:,.1f}" def _attach_box_weights(store: Any, box_list: list[dict[str, Any]]) -> bool: """상자마다 무게 합계(g) — 상자에 든 제품마다 수량 × 포장무게(설정(제품명)의 지금 값, cupang_products.weight_g). 상자에 넣는 값: weight_g = 아는 무게의 합(상자 안 제품 무게가 하나도 없으면 None), weight_missing = 무게가 없는 제품코드 목록(일부만 없으면 합계는 그 제품을 뺀 값). 포장무게 칸(마이그레이션 007)이 없으면 아무것도 넣지 않고 False. """ if not store.weight_enabled(): return False weights = { str(p.get("product_code") or "").strip(): p.get("weight_g") for p in store.list_products(include_inactive=True) } for box in box_list: contents = box.get("items") or [] total, missing = 0, [] for it in contents: code = str(it.get("product_code") or "").strip() w = weights.get(code) if w is None: if code not in missing: missing.append(code) continue total += int(w) * int(it.get("quantity") or 0) known = any(weights.get(str(it.get("product_code") or "").strip()) is not None for it in contents) box["weight_g"] = total if known else None box["weight_missing"] = missing return True def _expand_box_list( shipment: dict[str, Any], calc: dict[str, Any], items: list[dict[str, Any]] ) -> list[dict[str, Any]]: """전체 상자를 1번부터 번호 붙여 펼친다 — 상자마다 무엇이 몇 개 들었는지. 확정 당시 스냅샷(`box_plan`)이 있으면 그것을 그대로 펼치고, 없는 예전 데이터는 계산 결과(`calc`)로 대신 만든다. """ box_name_by_code = { r["product_code"]: (r.get("box_name") or "") for r in calc.get("results", []) } out: list[dict[str, Any]] = [] plan = shipment.get("box_plan") or [] if plan: for entry in plan: count = int(entry.get("count") or 0) units = int(entry.get("units") or 0) if count <= 0: continue if entry.get("kind") == "mix": contents = [ { "product_name": it.get("product_name") or it.get("product_code") or "", "product_code": it.get("product_code") or "", "quantity": int(it.get("quantity") or 0), } for it in (entry.get("items") or []) ] box_name = entry.get("box_type") or "" for _ in range(count): out.append({"kind": "mix", "box_name": box_name, "items": contents}) else: code = str(entry.get("product_code") or "") contents = [ { "product_name": entry.get("name") or code, "product_code": code, "quantity": units, } ] box_name = box_name_by_code.get(code, "") for _ in range(count): out.append({"kind": "product", "box_name": box_name, "items": contents}) else: for r in calc.get("results", []): if not r.get("configured"): continue for _ in range(int(r.get("full_boxes") or 0)): out.append( { "kind": "product", "box_name": r.get("box_name") or "", "items": [ { "product_name": r["product_name"], "product_code": r["product_code"], "quantity": r["units_per_box"], } ], } ) for mix in calc.get("mixes", []): for box in mix.get("boxes", []): out.append( { "kind": "mix", "box_name": mix.get("box_name") or "", "items": [ { "product_name": it.get("product_name") or "", "product_code": it.get("product_code") or "", "quantity": int(it.get("quantity") or 0), } for it in (box.get("items") or []) ], } ) for idx, box in enumerate(out, start=1): box["no"] = idx box["quantity"] = sum(int(it["quantity"]) for it in box["items"]) return out def _pack_leftovers(results: list[dict[str, Any]]) -> list[dict[str, Any]]: """제품별 자투리(remainder_units)를 같은 상자명끼리 모아 혼합 상자에 담는다. - 한 상자의 용량을 1 로 두고, 제품 1개가 차지하는 부피를 1/units_per_box 로 본다. (예: 쿠팡 2호 = 8개들이 → 1개 = 1/8 상자) - 같은 상자명끼리만 섞는다. 서로 다른 입수량이 섞여도 부피 합으로 정확히 계산된다. - 한 제품의 자투리가 두 상자에 나뉘어 담기는 것은 허용(그래야 상자 수가 최소). - 오차 없이 계산하려고 float 대신 Fraction 을 쓴다. """ groups: dict[str, list[dict[str, Any]]] = {} for r in results: if not r["configured"] or not r["remainder_units"]: continue groups.setdefault(r["box_name"], []).append(r) out: list[dict[str, Any]] = [] for box_name in sorted(groups): # 자투리가 많은 제품부터 담아 상자 안 품목 수를 줄인다. items = sorted(groups[box_name], key=lambda r: -r["remainder_units"]) boxes: list[dict[str, Any]] = [] cur: list[dict[str, Any]] = [] free = Fraction(1) for r in items: unit = Fraction(1, int(r["units_per_box"])) left = int(r["remainder_units"]) while left > 0: take = min(left, int(free / unit)) if take == 0: # 남은 자리 없음 → 새 상자 boxes.append({"items": cur, "fill_percent": float(round((1 - free) * 100, 1))}) cur, free = [], Fraction(1) continue cur.append( { "product_code": r["product_code"], "product_name": r["product_name"], "quantity": take, } ) free -= unit * take left -= take if cur: boxes.append({"items": cur, "fill_percent": float(round((1 - free) * 100, 1))}) out.append( { "box_name": box_name, "box_count": len(boxes), "leftover_units": sum(int(r["remainder_units"]) for r in items), "boxes": boxes, } ) return out # ════════════════════════════════════════════════════════════ # 설정 — 제품명 카탈로그 관리 (itemcode_db 에서 등록) # ════════════════════════════════════════════════════════════ @router.get("/products", response_class=HTMLResponse) def products_page(request: Request) -> HTMLResponse: from app.main import build_erp_nav, render_template # noqa: WPS433 from app.store import is_admin # noqa: WPS433 guard = _guard(request) if not isinstance(guard, tuple): return guard store, user = guard reader = _itemcode(request) return render_template( request, "cupang/products.html", { "user": user, "is_admin": is_admin(user), "nav_items": build_erp_nav(user, active="cupang"), "page_title": "쿠팡 밀크런 — 설정 (제품명)", "page_subtitle": "왼쪽 itemcode_db 목록에서 선택해 등록하면 폼 드롭다운에 노출됩니다.", "products": store.list_products(include_inactive=True), "registered_codes": [p["product_code"] for p in store.list_products(include_inactive=True)], # 포장무게(g) — 마이그레이션 007 적용 전이면 무게 칸을 숨긴다 "weight_enabled": store.weight_enabled(), "search_enabled": bool(reader and reader.enabled), "search_reason": (reader.reason if reader else ""), }, ) @router.get("/api/products/all") def product_all( request: Request, _: dict[str, Any] = Depends(_require_user) ) -> JSONResponse: """itemcode_db 전체 상품 목록(낱개+세트). 설정 화면 왼쪽 리스트 소스.""" reader = _itemcode(request) results = reader.list_all() if reader else [] return JSONResponse( { "enabled": bool(reader and reader.enabled), "reason": (reader.reason if reader else "itemcode 리더 미초기화"), "error": (getattr(reader, "last_error", "") if reader else ""), "count": len(results), "results": results, } ) @router.post("/products/bulk") def product_bulk( request: Request, items: list[dict[str, Any]] = Body(..., embed=True), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """선택한 상품들을 일괄 등록(upsert). body: {"items":[{"code","name","coupang_item_code"}, ...]} coupang_item_code 키가 없으면 기존 값을 유지한다. """ store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") added = 0 for it in items: code = str(it.get("code") or "").strip() name = str(it.get("name") or "").strip() cic = it.get("coupang_item_code") if not code or not name: continue try: store.upsert_product( product_code=code, product_name=name, coupang_item_code=(None if cic is None else str(cic)), ) added += 1 except ValueError: continue return JSONResponse({"ok": True, "added": added}) @router.post("/products") def product_upsert( request: Request, product_code: str = Form(...), product_name: str = Form(...), coupang_item_code: str = Form(""), weight_g: str = Form(""), sort_order: int = Form(0), user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.upsert_product( product_code=product_code, product_name=product_name, # 빈 값은 "미입력" 으로 보고 기존 쿠팡상품코드·포장무게를 유지한다. coupang_item_code=(coupang_item_code.strip() or None), sort_order=sort_order, weight_g=parse_weight_g(weight_g), ) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return RedirectResponse(url="/cupang/products", status_code=303) @router.post("/products/{product_id:int}/active") def product_set_active( request: Request, product_id: int, active: str = Form(...), user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.set_product_active( product_id=product_id, active=active in ("1", "true", "on", "True") ) except KeyError: raise HTTPException(status_code=404, detail="제품을 찾을 수 없습니다.") return RedirectResponse(url="/cupang/products", status_code=303) @router.post("/api/products/{product_id:int}/edit") def product_edit( request: Request, product_id: int, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """등록된 제품 수정. body: {product_name, product_code, coupang_item_code, weight_g?}. weight_g 키가 있으면 저장(빈 값 = 지움), 없으면 무게는 그대로.""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: product = store.update_product( product_id=product_id, product_name=str(payload.get("product_name") or ""), product_code=str(payload.get("product_code") or ""), coupang_item_code=str(payload.get("coupang_item_code") or ""), weight_g=(parse_weight_g(payload.get("weight_g")) if "weight_g" in payload else ...), ) except KeyError: raise HTTPException(status_code=404, detail="제품을 찾을 수 없습니다.") except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return JSONResponse({"ok": True, "product": product}) @router.post("/api/products/{product_id:int}/toggle") def product_toggle_active( request: Request, product_id: int, user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """상태 배지 클릭 → 활성/비활성 토글(페이지 새로고침 없이).""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: product = store.toggle_product_active(product_id=product_id) except KeyError: raise HTTPException(status_code=404, detail="제품을 찾을 수 없습니다.") return JSONResponse({"ok": True, "active": bool(product["active"])}) @router.post("/products/{product_id:int}/delete") def product_delete( request: Request, product_id: int, user: dict[str, Any] = Depends(_require_user), ) -> RedirectResponse: """완전 삭제(hard delete).""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.delete_product(product_id=product_id) except KeyError: raise HTTPException(status_code=404, detail="제품을 찾을 수 없습니다.") return RedirectResponse(url="/cupang/products", status_code=303) # ════════════════════════════════════════════════════════════ # 분배 확정 — 센터별 출고 묶음 생성 # ════════════════════════════════════════════════════════════ def _clean_box_plan(raw: Any) -> list[dict[str, Any]]: """화면이 보낸 상자 구성을 표시용으로만 정리해서 저장한다. 수량·상자 수는 라인(제품 합계)이 정답이고, 이 값은 "어느 상자에 무엇이 담겼는지"를 보여주기 위한 스냅샷이다. """ if not isinstance(raw, list): return [] out: list[dict[str, Any]] = [] for entry in raw[:200]: if not isinstance(entry, dict): continue kind = "mix" if str(entry.get("kind")) == "mix" else "product" try: count = max(int(entry.get("count") or 0), 0) units = max(int(entry.get("units") or 0), 0) except (TypeError, ValueError): continue if count <= 0: continue item: dict[str, Any] = { "kind": kind, "name": str(entry.get("name") or "")[:120], "count": count, "units": units, "quantity": count * units, } if kind == "product": item["product_code"] = str(entry.get("product_code") or "")[:60] else: item["box_type"] = str(entry.get("box_type") or "")[:40] contents = [] for it in (entry.get("items") or [])[:50]: if not isinstance(it, dict): continue try: qty = max(int(it.get("quantity") or 0), 0) except (TypeError, ValueError): continue if qty <= 0: continue contents.append( { "product_name": str(it.get("product_name") or "")[:120], "product_code": str(it.get("product_code") or "")[:60], "quantity": qty, } ) item["items"] = contents out.append(item) return out @router.post("/api/box-calc/confirm") def box_calc_confirm( request: Request, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """{ship_date, centers:[{center_id, ship_method, boxes, items:[...]}]} → 센터마다 출고 묶음 1건 생성. 화면에서 보낸 상자 수는 요약 표시용이고, 라인의 상자 수는 저장 시 서버(store.compute_boxes)가 수량과 입수량으로 다시 계산한다. """ from .store import SHIP_METHODS as _METHODS # noqa: WPS433 store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") ship_date = str(payload.get("ship_date") or "").strip() try: _date.fromisoformat(ship_date) except ValueError: raise HTTPException(status_code=400, detail="출고일자를 올바르게 선택하세요.") raw_centers = payload.get("centers") if not isinstance(raw_centers, list) or not raw_centers: raise HTTPException(status_code=400, detail="확정할 센터가 없습니다.") rules = {r["product_code"]: r for r in store.list_box_rules()} known_centers = {str(c["id"]): c for c in store.list_centers(include_inactive=True)} today = today_kst().isoformat() arrival_date = (_date.fromisoformat(ship_date) + _timedelta(days=1)).isoformat() worker = str(user.get("name") or user.get("email") or "") plans: list[dict[str, Any]] = [] for raw in raw_centers: if not isinstance(raw, dict): continue cid = str(raw.get("center_id") or "").strip() center = known_centers.get(cid) if center is None: raise HTTPException(status_code=400, detail=f"알 수 없는 센터입니다: {cid}") method = str(raw.get("ship_method") or "").strip() if method not in _METHODS: raise HTTPException( status_code=400, detail=f"{center['name']} 의 출고방식을 선택하세요." ) # 같은 제품이 여러 상자로 나뉘어 담겼을 수 있으므로 제품코드로 합친다. merged: dict[str, int] = {} for it in raw.get("items") or []: if not isinstance(it, dict): continue code = str(it.get("product_code") or "").strip() if not code: continue try: qty = int(it.get("quantity") or 0) except (TypeError, ValueError): qty = 0 if qty <= 0: continue merged[code] = merged.get(code, 0) + qty if not merged: continue lines = [] for code, qty in merged.items(): rule = rules.get(code) lines.append( { "product_code": code, "product_name_snapshot": (rule or {}).get("product_name_snapshot") or code, "quantity": qty, "units_per_box": (rule or {}).get("units_per_box"), "box_rule_id": (rule or {}).get("id"), } ) try: boxes = max(int(raw.get("boxes") or 0), 0) except (TypeError, ValueError): boxes = 0 pieces = sum(merged.values()) # 화면에서 고른 상자 종류별 상자 수(예: "쿠팡상자41"). 출고리스트 엑셀의 "출고" 칸. summary = str(raw.get("box_summary") or "").strip() if not summary: summary = f"{boxes}상자 · {pieces}개" if boxes else f"{pieces}개" plans.append( { "center": center, "method": method, "lines": lines, "summary": summary, "box_plan": _clean_box_plan(raw.get("box_plan")), } ) if not plans: raise HTTPException(status_code=400, detail="담긴 품목이 없습니다.") created: list[dict[str, Any]] = [] for plan in plans: center = plan["center"] try: ship = store.create_shipment( created_by=str(user.get("email") or ""), header={ "document_date": today, "ship_date": ship_date, # 센터입고일 = 출고일 다음 날(쿠팡 발주서의 입고예정일). 상세에서 고칠 수 있다. "center_arrival_date": arrival_date, "center_id": center["id"], "center_name_snapshot": center["name"], "ship_method": plan["method"], "outbound_summary": plan["summary"], "worker": worker, "status": "출고준비", "memo": "상자 계산에서 분배 확정", "box_plan": plan["box_plan"], }, lines=plan["lines"], ) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) created.append({"id": ship["id"], "center_name": center["name"]}) # 구글 스프레드시트에 출고일 시트 기록(설정돼 있을 때만). sheet = _push_to_google_sheet(store, ship_date) _log_sheet("push", ship_date, sheet) return JSONResponse({"created": created, "ship_date": ship_date, "sheet": sheet}) # ════════════════════════════════════════════════════════════ # 상자 계산 임시 저장 (화면 상태 스냅샷) # ════════════════════════════════════════════════════════════ @router.get("/api/box-calc/drafts") def box_calc_drafts_list( request: Request, user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") return JSONResponse({"drafts": store.list_box_calc_drafts()}) @router.get("/api/box-calc/drafts/{draft_id:int}") def box_calc_draft_get( request: Request, draft_id: int, user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") draft = store.get_box_calc_draft(draft_id=draft_id) if draft is None: raise HTTPException(status_code=404, detail="저장된 내용을 찾을 수 없습니다.") return JSONResponse({"draft": draft}) @router.post("/api/box-calc/drafts") def box_calc_draft_save( request: Request, payload: dict[str, Any] = Body(...), user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: """{title, payload, draft_id?} → 저장(같은 제목이면 덮어쓰기).""" store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") title = str(payload.get("title") or "").strip() if not title: raise HTTPException(status_code=400, detail="제목을 입력하세요.") if len(title) > 100: title = title[:100] snapshot = payload.get("payload") if not isinstance(snapshot, dict): raise HTTPException(status_code=400, detail="payload 는 객체여야 합니다.") raw_id = payload.get("draft_id") draft_id = int(raw_id) if isinstance(raw_id, int) or (isinstance(raw_id, str) and raw_id.isdigit()) else None try: draft = store.save_box_calc_draft( title=title, payload=snapshot, created_by=str(user.get("name") or user.get("email") or ""), draft_id=draft_id, ) except KeyError: raise HTTPException(status_code=404, detail="저장된 내용을 찾을 수 없습니다.") except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) return JSONResponse({"draft": draft}) @router.delete("/api/box-calc/drafts/{draft_id:int}") def box_calc_draft_delete( request: Request, draft_id: int, user: dict[str, Any] = Depends(_require_user), ) -> JSONResponse: store = _store(request) if store is None: raise HTTPException(status_code=503, detail="cupang_db 미설정") try: store.delete_box_calc_draft(draft_id=draft_id) except KeyError: raise HTTPException(status_code=404, detail="저장된 내용을 찾을 수 없습니다.") return JSONResponse({"deleted": True}) @router.get("/health") def health() -> dict[str, str]: return {"status": "ok", "module": "cupang"}