feat(cupang): 확정 시 Google 스프레드시트에 출고일 시트 기록
- app/modules/cupang/export.py: 출고리스트 양식(셀/병합)을 한 곳에서 생성 → xlsx 다운로드와 구글 시트 기록이 같은 표를 쓴다 - app/integrations/google_sheets.py: 서비스 계정 기반 시트 쓰기 공통 계층 (없거나 미설정이면 enabled=False 로 조용히 skip, 키는 로그에 남기지 않음) - 확정 응답에 sheet 결과 포함, 화면에 성공/실패 메시지 표시 - env: CUPANG_SHEET_ID, GOOGLE_SHEETS_CREDENTIALS(_JSON) 추가 - requirements: google-api-python-client, google-auth Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
"""쿠팡로켓 밀크런 출고리스트 양식 생성.
|
||||
|
||||
- 한 벌의 표 데이터를 만들어 두 곳에서 같이 쓴다.
|
||||
· xlsx 다운로드(openpyxl)
|
||||
· Google 스프레드시트 기록(app/integrations/google_sheets.py)
|
||||
- 표 구조(스크린샷 양식)
|
||||
2행 B:L 제목 "YYYYMMDD(요일) 쿠팡로켓 밀크런 출고리스트"
|
||||
4행 B 회사명
|
||||
5행 B:L 머리글
|
||||
6행~ 품목 1줄 = 1행, 센터 단위로 작성일/출고일/센터입고일/입고센터/
|
||||
출고방식/출고/작업자 칸이 세로 병합
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date as _date
|
||||
from typing import Any
|
||||
|
||||
COMPANY = "㈜더블엑스코퍼레이션"
|
||||
WORKER = "핫프렌즈"
|
||||
DOW = ["월", "화", "수", "목", "금", "토", "일"]
|
||||
HEADERS = [
|
||||
"구분", "작성일", "출고일", "센터입고일", "입고센터", "출고방식",
|
||||
"제품코드", "제품명", "수량", "출고", "작업자",
|
||||
]
|
||||
# 열 번호(1-based): B=2 … L=12
|
||||
COL_FIRST = 2
|
||||
COL_LAST = 12
|
||||
# 센터 단위로 병합되는 열
|
||||
MERGE_COLS = (3, 4, 5, 6, 7, 11, 12)
|
||||
WIDTHS = {2: 6, 3: 12, 4: 12, 5: 12, 6: 11, 7: 10, 8: 12, 9: 26, 10: 8, 11: 12, 12: 11}
|
||||
|
||||
TITLE_ROW = 2
|
||||
COMPANY_ROW = 4
|
||||
HEADER_ROW = 5
|
||||
FIRST_DATA_ROW = 6
|
||||
|
||||
|
||||
def sheet_title(ship_date: str) -> str:
|
||||
"""시트명 = 출고일 YYYYMMDD."""
|
||||
return _date.fromisoformat(ship_date).strftime("%Y%m%d")
|
||||
|
||||
|
||||
def build_table(ship_date: str, shipments: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
"""출고 묶음(라인 포함) 목록 → 셀 값/병합 정보.
|
||||
|
||||
반환:
|
||||
title 제목 문자열
|
||||
cells {(row, col): value} (1-based, 시트 좌표 그대로)
|
||||
merges [(row1, col1, row2, col2), ...] (1-based, 양끝 포함)
|
||||
last_row 마지막 데이터 행
|
||||
"""
|
||||
d = _date.fromisoformat(ship_date)
|
||||
tag = d.strftime("%Y%m%d")
|
||||
title = f"{tag}({DOW[d.weekday()]}) 쿠팡로켓 밀크런 출고리스트"
|
||||
|
||||
cells: dict[tuple[int, int], Any] = {(TITLE_ROW, COL_FIRST): title,
|
||||
(COMPANY_ROW, COL_FIRST): COMPANY}
|
||||
merges: list[tuple[int, int, int, int]] = [(TITLE_ROW, COL_FIRST, TITLE_ROW, COL_LAST)]
|
||||
|
||||
for i, name in enumerate(HEADERS):
|
||||
cells[(HEADER_ROW, COL_FIRST + i)] = name
|
||||
|
||||
row = FIRST_DATA_ROW
|
||||
seq = 0
|
||||
for sh in shipments:
|
||||
lines = sh.get("lines") or []
|
||||
if not lines:
|
||||
continue
|
||||
start = row
|
||||
for ln in lines:
|
||||
seq += 1
|
||||
cells[(row, 2)] = seq
|
||||
cells[(row, 8)] = ln.get("product_code") or ""
|
||||
cells[(row, 9)] = ln.get("product_name_snapshot") or ""
|
||||
cells[(row, 10)] = int(ln.get("quantity") or 0)
|
||||
row += 1
|
||||
end = row - 1
|
||||
|
||||
block = {
|
||||
3: str(sh.get("document_date") or ""),
|
||||
4: str(sh.get("ship_date") or ""),
|
||||
5: str(sh.get("center_arrival_date") or ""),
|
||||
6: sh.get("center_name_snapshot") or "",
|
||||
7: sh.get("ship_method") or "",
|
||||
11: sh.get("outbound_summary") or "",
|
||||
12: WORKER,
|
||||
}
|
||||
for col in MERGE_COLS:
|
||||
cells[(start, col)] = block[col]
|
||||
if end > start:
|
||||
merges.append((start, col, end, col))
|
||||
|
||||
return {"title": title, "cells": cells, "merges": merges, "last_row": row - 1}
|
||||
|
||||
|
||||
def build_workbook(ship_date: str, shipments: list[dict[str, Any]]) -> Any:
|
||||
"""xlsx 워크북(openpyxl). 시트명 = YYYYMMDD."""
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Alignment, Border, Font, Side
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
table = build_table(ship_date, shipments)
|
||||
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = sheet_title(ship_date)
|
||||
|
||||
thin = Side(style="thin", color="000000")
|
||||
box = Border(left=thin, right=thin, top=thin, bottom=thin)
|
||||
center_align = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
||||
left_align = Alignment(horizontal="left", vertical="center", wrap_text=True)
|
||||
|
||||
for (row, col), value in table["cells"].items():
|
||||
ws.cell(row=row, column=col, value=value)
|
||||
|
||||
for (r1, c1, r2, c2) in table["merges"]:
|
||||
ws.merge_cells(start_row=r1, start_column=c1, end_row=r2, end_column=c2)
|
||||
|
||||
title_cell = ws.cell(row=TITLE_ROW, column=COL_FIRST)
|
||||
title_cell.font = Font(size=16, bold=True)
|
||||
title_cell.alignment = center_align
|
||||
|
||||
for col in range(COL_FIRST, COL_LAST + 1):
|
||||
head = ws.cell(row=HEADER_ROW, column=col)
|
||||
head.font = Font(bold=True)
|
||||
head.alignment = center_align
|
||||
head.border = box
|
||||
|
||||
for row in range(FIRST_DATA_ROW, table["last_row"] + 1):
|
||||
for col in range(COL_FIRST, COL_LAST + 1):
|
||||
cell = ws.cell(row=row, column=col)
|
||||
cell.border = box
|
||||
cell.alignment = left_align if col == 9 else center_align
|
||||
|
||||
for col, width in WIDTHS.items():
|
||||
ws.column_dimensions[get_column_letter(col)].width = width
|
||||
ws.row_dimensions[TITLE_ROW].height = 28
|
||||
|
||||
return wb
|
||||
Reference in New Issue
Block a user