135 lines
4.4 KiB
Python
135 lines
4.4 KiB
Python
from decimal import Decimal
|
|
|
|
from settle import (
|
|
PAID_STATUSES,
|
|
MallLine,
|
|
attach_detail_ids,
|
|
build_pay_row,
|
|
match_pay_lines,
|
|
money4,
|
|
)
|
|
|
|
|
|
def _line(**overrides) -> MallLine:
|
|
data = dict(
|
|
order_no="ZQD1",
|
|
parent_order_no="",
|
|
supplier_name="广东壹号药业有限公司",
|
|
status="待收货",
|
|
goods_name="阿莫西林",
|
|
product_code="2918",
|
|
bar_code="6921665004933",
|
|
unit_price=Decimal("10.00"),
|
|
pay_price=Decimal("9.85"),
|
|
qty=200,
|
|
goods_amount=Decimal("2000.00"),
|
|
order_paid=Decimal("1970.00"),
|
|
pay_order_dtl_id="881",
|
|
)
|
|
data.update(overrides)
|
|
return MallLine(**data)
|
|
|
|
|
|
def test_money4_rounds_half_up():
|
|
assert money4("65.49") == Decimal("65.4900")
|
|
assert money4(Decimal("9.85") * 200) == Decimal("1970.0000")
|
|
|
|
|
|
def test_drops_cancelled_and_keeps_paid_statuses():
|
|
lines = [
|
|
_line(status="已取消", order_no="C1"),
|
|
_line(status="待发货", order_no="P1"),
|
|
_line(status="待收货", order_no="P2"),
|
|
_line(status="已完成", order_no="P3"),
|
|
_line(status="退款/退换货", order_no="R1"),
|
|
]
|
|
result = match_pay_lines(lines, existing_keys=set())
|
|
kept = {row["pay_order_no"] for row in result.rows}
|
|
assert kept == {"P1", "P2", "P3"}
|
|
assert {line.order_no for line in result.skipped_status} == {"C1", "R1"}
|
|
assert PAID_STATUSES == {"待发货", "待收货", "已完成"}
|
|
|
|
|
|
def test_purchase_total_uses_discounted_price_times_qty():
|
|
result = match_pay_lines([_line()], existing_keys=set())
|
|
assert len(result.rows) == 1
|
|
row = result.rows[0]
|
|
assert row["purchase_price"] == Decimal("9.8500")
|
|
assert row["purchase_qty"] == 200
|
|
assert row["purchase_total"] == Decimal("1970.0000")
|
|
assert row["operate_type"] == 2
|
|
assert row["platform_type"] == 66
|
|
assert row["status"] is None
|
|
assert row["pay_order_no"] == "ZQD1"
|
|
assert row["pay_order_dtl_id"] == "881"
|
|
assert row["oms_id"] == 0
|
|
assert row["org_code"] is None
|
|
assert row["multi_platform_flag"] == 0
|
|
|
|
|
|
def test_inserts_excel_line_without_cart_match():
|
|
result = match_pay_lines([_line(bar_code="999")], existing_keys=set())
|
|
assert len(result.rows) == 1
|
|
assert result.rows[0]["bar_code"] == "999"
|
|
assert result.rows[0]["oms_id"] == 0
|
|
assert result.unmatched == []
|
|
|
|
|
|
def test_same_barcode_two_lines_both_insert():
|
|
lines = [
|
|
_line(order_no="A", qty=10, pay_price=Decimal("53.40"), pay_order_dtl_id="1"),
|
|
_line(order_no="A", qty=50, pay_price=Decimal("55.21"), pay_order_dtl_id="2"),
|
|
]
|
|
result = match_pay_lines(lines, existing_keys=set())
|
|
assert [row["pay_order_dtl_id"] for row in result.rows] == ["1", "2"]
|
|
assert all(row["oms_id"] == 0 for row in result.rows)
|
|
|
|
|
|
def test_skips_existing_pay_order_and_dtl():
|
|
result = match_pay_lines([_line()], existing_keys={("ZQD1", "881")})
|
|
assert result.rows == []
|
|
assert len(result.skipped_dup) == 1
|
|
|
|
|
|
def test_unmatched_without_dtl_is_not_inserted():
|
|
result = match_pay_lines([_line(pay_order_dtl_id="")], existing_keys=set())
|
|
assert result.rows == []
|
|
assert result.unmatched[0][1] == "NO_DTL_ID"
|
|
|
|
|
|
def test_attach_detail_ids_matches_qty_and_pay_price():
|
|
lines = [
|
|
_line(pay_order_dtl_id="", qty=10, pay_price=Decimal("5.29"), goods_name="乳酶生片"),
|
|
_line(pay_order_dtl_id="", qty=50, pay_price=Decimal("55.21"), goods_name="阿德福韦酯胶囊"),
|
|
]
|
|
details = {
|
|
"ZQD1": {
|
|
"productList": [
|
|
{
|
|
"orderDetailId": 1001,
|
|
"productName": "乳酶生片",
|
|
"quantity": 10,
|
|
"payPrice4": 5.29,
|
|
},
|
|
{
|
|
"orderDetailId": 1002,
|
|
"productName": "阿德福韦酯胶囊",
|
|
"quantity": 50,
|
|
"payPrice4": 55.21,
|
|
},
|
|
]
|
|
}
|
|
}
|
|
attached = attach_detail_ids(lines, details)
|
|
assert [line.pay_order_dtl_id for line in attached] == ["1001", "1002"]
|
|
|
|
|
|
def test_build_pay_row_from_excel_fields():
|
|
row = build_pay_row(_line())
|
|
assert row["oms_id"] == 0
|
|
assert row["org_code"] is None
|
|
assert row["multi_platform_flag"] == 0
|
|
assert row["supplier_name"] == "广东壹号药业有限公司"
|
|
assert row["bar_code"] == "6921665004933"
|
|
assert row["push_status"] == 0
|