AutoRpa/rpa_cart/settle.py

87 lines
2.3 KiB
Python

"""1 号药城导出行 → 表 2 operate_type=2。不对加购行。"""
from __future__ import annotations
import uuid
from dataclasses import dataclass
from decimal import Decimal, ROUND_HALF_UP
from typing import Any
YAOEX_PLATFORM_TYPE = 66
OPERATE_TYPE_PAY = 2
PUSH_NOT_SENT = 0
UNASSIGNED_OMS_ID = 0
PAID_STATUSES = {"待发货", "待收货", "已完成"}
def money4(value: object) -> Decimal:
return Decimal(str(value)).quantize(Decimal("0.0001"), rounding=ROUND_HALF_UP)
def new_pay_dtl_id() -> str:
return uuid.uuid4().hex
@dataclass(frozen=True)
class MallLine:
order_no: str
parent_order_no: str
supplier_name: str
status: str
goods_name: str
product_code: str
bar_code: str
unit_price: Decimal
pay_price: Decimal
qty: int
goods_amount: Decimal
order_paid: Decimal
@dataclass
class MatchResult:
rows: list[dict[str, Any]]
unmatched: list[tuple[MallLine, str]]
skipped_dup: list[MallLine]
skipped_status: list[MallLine]
def match_pay_lines(
lines: list[MallLine],
existing_order_nos: set[str],
) -> MatchResult:
result = MatchResult(rows=[], unmatched=[], skipped_dup=[], skipped_status=[])
for line in lines:
if line.status not in PAID_STATUSES:
result.skipped_status.append(line)
continue
if line.order_no in existing_order_nos:
result.skipped_dup.append(line)
continue
if len(line.bar_code) > 40:
result.unmatched.append((line, "BAR_CODE_TOO_LONG"))
continue
result.rows.append(build_pay_row(line))
return result
def build_pay_row(line: MallLine) -> dict[str, Any]:
price = money4(line.pay_price)
qty = int(line.qty)
return {
"operate_type": OPERATE_TYPE_PAY,
"oms_id": UNASSIGNED_OMS_ID,
"platform_type": YAOEX_PLATFORM_TYPE,
"supplier_name": line.supplier_name,
"bar_code": line.bar_code,
"purchase_price": price,
"purchase_qty": qty,
"purchase_total": money4(price * qty),
"status": None,
"fail_reason": None,
"pay_order_no": line.order_no,
"pay_order_dtl_id": new_pay_dtl_id(),
"push_status": PUSH_NOT_SENT,
"org_code": None,
"multi_platform_flag": 0,
}