21018a5332
구글 시트 요청에 pixelSize 를 8배로 넣어 A열이 80px 로 벌어져 있었다. WIDTHS_PX 를 픽셀 그대로 쓰고, xlsx 는 px/7 로 환산한다. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
191 lines
6.5 KiB
Python
191 lines
6.5 KiB
Python
"""쿠팡로켓 밀크런 출고리스트 양식 생성.
|
|
|
|
- 한 벌의 표 데이터를 만들어 두 곳에서 같이 쓴다.
|
|
· 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)
|
|
# 열 너비 — 구글 시트 기준 픽셀. xlsx 는 문자폭(px/7)으로 환산해서 쓴다.
|
|
WIDTHS_PX = {
|
|
1: 10, # A (여백)
|
|
2: 45, # B 구분
|
|
3: 106, # C 작성일
|
|
4: 106, # D 출고일
|
|
5: 106, # E 센터입고일
|
|
6: 90, # F 입고센터
|
|
7: 80, # G 출고방식
|
|
8: 95, # H 제품코드
|
|
9: 210, # I 제품명
|
|
10: 60, # J 수량
|
|
11: 100, # K 출고
|
|
12: 90, # L 작업자
|
|
}
|
|
PX_PER_CHAR = 7.0
|
|
HEADER_BG = "DBE9F7" # 머리글 행 배경
|
|
PALLET_BG = "FAE2D5" # 출고방식이 파렛트인 센터 블록 배경
|
|
PALLET_METHOD = "파렛트"
|
|
|
|
TITLE_ROW = 2
|
|
COMPANY_ROW = 4
|
|
HEADER_ROW = 5
|
|
FIRST_DATA_ROW = 6
|
|
|
|
|
|
def with_dow(value: str) -> str:
|
|
"""YYYY-MM-DD → "YYYY-MM-DD(요일)". 날짜가 아니면 원문 그대로."""
|
|
text = (value or "").strip()
|
|
try:
|
|
d = _date.fromisoformat(text)
|
|
except ValueError:
|
|
return text
|
|
return f"{text}({DOW[d.weekday()]})"
|
|
|
|
|
|
def sheet_title(ship_date: str) -> str:
|
|
"""시트명 = 출고일 YYYYMMDD(요일)."""
|
|
d = _date.fromisoformat(ship_date)
|
|
return f"{d.strftime('%Y%m%d')}({DOW[d.weekday()]})"
|
|
|
|
|
|
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()]}) 쿠팡로켓 밀크런 출고리스트"
|
|
pallet_ranges: list[tuple[int, int]] = []
|
|
|
|
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
|
|
|
|
method = sh.get("ship_method") or ""
|
|
if method == PALLET_METHOD:
|
|
pallet_ranges.append((start, end))
|
|
|
|
block = {
|
|
# 날짜는 요일까지 표기 — 2026-09-03(목)
|
|
3: with_dow(str(sh.get("document_date") or "")),
|
|
4: with_dow(str(sh.get("ship_date") or "")),
|
|
5: with_dow(str(sh.get("center_arrival_date") or "")),
|
|
6: sh.get("center_name_snapshot") or "",
|
|
7: method,
|
|
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,
|
|
"pallet_ranges": pallet_ranges,
|
|
}
|
|
|
|
|
|
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, PatternFill, 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
|
|
|
|
header_fill = PatternFill("solid", fgColor=HEADER_BG)
|
|
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
|
|
head.fill = header_fill
|
|
|
|
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
|
|
|
|
# 파렛트 출고 블록은 배경색으로 구분
|
|
pallet_fill = PatternFill("solid", fgColor=PALLET_BG)
|
|
for (r1, r2) in table["pallet_ranges"]:
|
|
for row in range(r1, r2 + 1):
|
|
for col in range(COL_FIRST, COL_LAST + 1):
|
|
ws.cell(row=row, column=col).fill = pallet_fill
|
|
|
|
for col, px in WIDTHS_PX.items():
|
|
ws.column_dimensions[get_column_letter(col)].width = round(px / PX_PER_CHAR, 2)
|
|
ws.row_dimensions[TITLE_ROW].height = 28
|
|
|
|
return wb
|