fix(performance): 카페24 환불합계를 일별 매출내역과 같게 — 적립금 환불 + 네이버페이 취소

실데이터 대조 결과 카페24 일별 매출 환불합계 =
  환불 목록(실제 환불액 + 적립금·예치금 환불)
  + 환불 목록에 없는 취소·반품(네이버페이 취소 등, 품목 취소일 기준)
9/26: 178,220 + 1,080 + 96,500 = 275,800, 9/27: 145,000 + 55,800 = 200,800 일치.

- normalize_refund 금액에 적립금·예치금 환불 포함(refund_total)
- unrefunded_claims: orders date_type=cancel_date|return_complete_date 의 C40~43/R40~43
  품목 중 앞뒤 60일 환불 목록에 없는 주문 → CANCEL/RETURN 사건(c24:{품목코드}-CL)
- 대조 화면 환불합계를 적용 규칙으로 표시 + "적용 규칙" 후보
- 테스트 추가

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 17:59:24 +09:00
parent b3ee219858
commit 1d12ac878b
6 changed files with 157 additions and 22 deletions
@@ -425,7 +425,7 @@ class _FakeCafe24(__import__("app.modules.performance.adapters.cafe24", fromlist
if params.get("date_type") == "order_date": # 원 주문 market_id 조회
ids = params["order_id"].split(",")
return {"orders": [{"order_id": i, "market_id": self.lookup.get(i, "self")} for i in ids]}
if params.get("date_type", "").endswith("_complete_date"):
if params.get("date_type", "").endswith("_complete_date") or params.get("date_type") == "cancel_date":
return {"orders": self.claims.get(params["date_type"], []) if idx == 0 else []}
return {"orders": self.orders_pages[idx] if idx < len(self.orders_pages) else []}
@@ -549,6 +549,50 @@ def test_cafe24_normalize_matches_daily_report():
assert calls[1]["offset"] == 1000 and all(c["limit"] == 1000 and c["date_type"] == "pay_date" for c in calls)
def test_cafe24_refund_total_includes_points_and_naverpay_cancels():
"""카페24 일별 매출 환불합계 = 환불 목록(실제 + 적립금·예치금) + 환불 목록에 없는 취소·반품(네이버페이 등).
실데이터: 9/26 = 178,220 + 1,080 + 네이버페이 취소 96,500 = 275,800, 9/27 = 145,000 + 네이버페이 55,800."""
import app.timezone as _tz
from app.modules.performance.adapters.base import AdapterContext
ctx = AdapterContext(code="cafe24", credentials=None)
item = lambda code, st, price, qty=1, day="2026-09-26 15:00:00", **kw: {
"order_item_code": code, "order_status": st, "quantity": qty, "product_price": f"{price}.00",
"option_price": "0.00", "product_name": "상품", "cancel_date": day, **kw}
refund = {**_c24_refund("C20260926-01", "O1", 63_720), "used_points": "1080.00",
"refund_date": "2026-09-26T20:25:08+09:00"}
fake = _FakeCafe24([[]], [[refund]], claims={
"cancel_date": [
{**_c24_order("O1"), "items": [item("O1-01", "C40", 64_800)]}, # 환불 목록 있음 → 제외
{**_c24_order("NP1", market="NCHECKOUT"), "items": [item("NP1-01", "C40", 55_800),
item("NP1-02", "N40", 9_000)]}, # 정상 품목 제외
{**_c24_order("NP2", market="NCHECKOUT"), "items": [item("NP2-01", "C40", 20_000, 2,
coupon_discount_price="1000.00")]},
{**_c24_order("NP3", market="NCHECKOUT"), "items": [item("NP3-01", "C40", 5_000, day="2026-09-27 01:00:00")]},
{**_c24_order("NP4", market="NCHECKOUT"), "items": [item("NP4-01", "C00", 5_000)]}, # 취소신청만 → 제외
],
"return_complete_date": [{**_c24_order("NP1", market="NCHECKOUT"), "items": [item("NP1-01", "C40", 55_800)]}],
})
orig, _tz.today_kst = _tz.today_kst, (lambda: date(2026, 9, 29))
try:
_, events = fake.collect(ctx, date(2026, 9, 26), date(2026, 9, 26))
rec = fake.reconcile(ctx, date(2026, 9, 26))
finally:
_tz.today_kst = orig
by = {e.external_event_id: e for e in events if e.event_type != "SALE"}
assert by["c24:C20260926-01"].amount == -(63_720 + 1_080) # 적립금 환불 포함
assert by["c24:NP1-01-CL"].amount == -55_800 and by["c24:NP1-01-CL"].event_type == "CANCEL"
assert by["c24:NP1-01-CL"].event_date == date(2026, 9, 26) and by["c24:NP1-01-CL"].marketplace_code == "cafe24"
assert by["c24:NP2-01-CL"].amount == -(40_000 - 1_000) # 판매가×수량 − 품목 쿠폰
assert set(by) == {"c24:C20260926-01", "c24:NP1-01-CL", "c24:NP2-01-CL"} # 다른 날·신청·환불 기록 있는 주문 제외
rs = rec["refunds"]["self"]
assert rs["no_record_count"] == 2 and rs["no_record"] == 55_800 + 39_000
assert rs["total"] == 63_720 + 1_080 + 55_800 + 39_000
cand = {c["key"]: c["amount"] for c in rec["refund_candidates"]}
assert cand["applied"] == rs["total"]
assert {c["item_code"] for c in rec["no_record_claims"]} == {"NP1-01", "NP2-01"}
def test_cafe24_scope_and_replace_range():
import app.modules.performance.sync as syncmod
from app.modules.performance.adapters.base import AdapterContext