feat(performance): 카페24 매출 대조 — 결제일 기준 환불 후보

9/26 에 실제로 일어난 환불은 4건(178,220)뿐인데 카페24 환불합계는 275,800.
카페24 일별 매출이 환불을 원 주문 결제일에 빼는지 확인하기 위해 후보 추가:
- 그날 결제한 주문이 오늘까지 환불된 금액(실제 / 적립금 포함, 환불일 표시)
- 그날 결제한 주문의 결제 당시 금액 − 현재 금액(actual_order_amount)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 17:49:02 +09:00
parent 370834b58b
commit 1eca66838b
5 changed files with 42 additions and 3 deletions
@@ -141,6 +141,14 @@ def order_total(order: dict[str, Any]) -> int:
- sum(a[k] for k in DISCOUNT_FIELDS) - sum(a[k] for k in COUPON_FIELDS))
def order_total_now(order: dict[str, Any]) -> int:
"""취소·반품 반영 후 현재 결제합계(actual_order_amount). 없으면 결제 당시 금액."""
a = order.get("actual_order_amount") or order.get("initial_order_amount") or {}
w = {k: _won(a.get(k)) for k in AMOUNT_FIELDS}
return (w["order_price_amount"] + w["shipping_fee"]
- sum(w[k] for k in DISCOUNT_FIELDS) - sum(w[k] for k in COUPON_FIELDS))
def is_self_market(market_id: Any) -> bool:
"""카페24 자체몰 채널(PC·모바일·네이버페이·톡체크아웃 등)인가."""
return channel_kind(market_id) == "self"
@@ -409,6 +417,7 @@ class Cafe24Adapter(MarketplaceAdapter):
by_channel: dict[str, dict[str, Any]] = {}
by_payment: dict[str, dict[str, Any]] = {} # 결제수단 조합별(카페24 열 주문만)
unpaid = 0
paid_self: dict[str, int] = {} # 그날 결제된 카페24 열 주문 → (결제 당시 − 현재) 금액
for o in self.paid_orders(ctx, day, day):
if o.get("paid") != "T":
unpaid += 1
@@ -436,6 +445,8 @@ class Cafe24Adapter(MarketplaceAdapter):
for k, v in order_amounts(o).items():
g[k] += v
g["total"] += order_total(o)
if ch["target"] == "cafe24":
paid_self[str(o.get("order_id") or "")] = order_total(o) - order_total_now(o)
rows = [r for r in self.refunds(ctx, day, day) if str(r.get("status") or "T") == "T"]
markets = self.market_ids(ctx, rows)
refunds = {"self": {"count": 0, "actual_refund_amount": 0, "used_points": 0, "used_credits": 0},
@@ -507,6 +518,17 @@ class Cafe24Adapter(MarketplaceAdapter):
return {"count": len(sel), "payment_amount": sum(c["payment_amount"] for c in sel),
"product_amount": sum(c["product_amount"] for c in sel)}
# 결제일 기준 — 그날 결제된 주문이 그 뒤(오늘까지) 환불된 금액. 카페24 일별 매출이 환불을 원 주문 결제일에
# 빼는지 확인하는 용도(최근일만 맞고 지난 날일수록 차이가 커지는 현상).
from app.timezone import today_kst # noqa: WPS433
later = [x for x in self.refunds(ctx, day, max(day, today_kst()))
if str(x.get("order_id") or "") in paid_self and str(x.get("status") or "T") == "T"]
by_pay = rsum(later)
by_pay["amount_diff"] = sum(v for v in paid_self.values() if v > 0)
by_pay["refund_dates"] = sorted({str(x.get("refund_date") or "")[:10] for x in later})
refund_basis["by_pay_date"] = by_pay
comp = refund_basis["completed"]
no_refund = csum(lambda c: not c["has_refund"])
claimed = csum(lambda c: True)
@@ -514,6 +536,12 @@ class Cafe24Adapter(MarketplaceAdapter):
candidates = [
{"key": "completed", "label": "환불완료일 · 실제 환불액 (현재 적용)", "amount": comp["actual_refund_amount"]},
{"key": "completed_pts", "label": "환불완료일 · 실제 환불액 + 적립금·예치금", "amount": comp["with_points"]},
{"key": "pay_date", "label": "결제일 기준 · 그날 결제 주문의 환불(오늘까지) · 실제 환불액",
"amount": by_pay["actual_refund_amount"]},
{"key": "pay_date_pts", "label": "결제일 기준 · 그날 결제 주문의 환불(오늘까지) · 실제 + 적립금·예치금",
"amount": by_pay["with_points"]},
{"key": "pay_date_diff", "label": "결제일 기준 · 그날 결제 주문의 (결제 당시 금액 − 현재 금액)",
"amount": by_pay["amount_diff"]},
{"key": "accepted", "label": "환불접수일(전체 상태) · 실제 환불액", "amount": refund_basis["accepted_all"]["actual_refund_amount"]},
{"key": "accepted_pts", "label": "환불접수일(전체 상태) · 실제 + 적립금·예치금", "amount": refund_basis["accepted_all"]["with_points"]},
{"key": "claim_pay", "label": "취소·반품·교환 완료 품목 · 품목 결제금액", "amount": claimed["payment_amount"]},