170 lines
5.2 KiB
Python
170 lines
5.2 KiB
Python
from decimal import Decimal
|
|
|
|
from settle import (
|
|
PAID_STATUSES,
|
|
CartCandidate,
|
|
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 _cart(**overrides) -> CartCandidate:
|
|
data = dict(
|
|
fid=1,
|
|
oms_id=2026100201,
|
|
supplier_name="广东壹号药业有限公司",
|
|
bar_code="6921665004933",
|
|
purchase_qty=200,
|
|
org_code="901",
|
|
multi_platform_flag=0,
|
|
)
|
|
data.update(overrides)
|
|
return CartCandidate(**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"),
|
|
]
|
|
carts = [
|
|
_cart(fid=1, oms_id=1),
|
|
_cart(fid=2, oms_id=2),
|
|
_cart(fid=3, oms_id=3),
|
|
]
|
|
result = match_pay_lines(lines, carts, 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()], [_cart()], 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"] == 2026100201
|
|
assert row["org_code"] == "901"
|
|
|
|
|
|
def test_match_prefers_equal_qty_then_earlier_fid():
|
|
carts = [
|
|
_cart(fid=10, oms_id=1, purchase_qty=50),
|
|
_cart(fid=20, oms_id=2, purchase_qty=200),
|
|
_cart(fid=5, oms_id=3, purchase_qty=200),
|
|
]
|
|
result = match_pay_lines([_line()], carts, existing_keys=set())
|
|
assert result.rows[0]["oms_id"] == 3
|
|
|
|
|
|
def test_same_barcode_two_lines_consume_distinct_cart_rows():
|
|
carts = [
|
|
_cart(fid=1, oms_id=11, purchase_qty=10),
|
|
_cart(fid=2, oms_id=22, purchase_qty=50),
|
|
]
|
|
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, carts, existing_keys=set())
|
|
oms = sorted(row["oms_id"] for row in result.rows)
|
|
assert oms == [11, 22]
|
|
|
|
|
|
def test_skips_existing_pay_order_and_dtl():
|
|
result = match_pay_lines(
|
|
[_line()],
|
|
[_cart()],
|
|
existing_keys={("ZQD1", "881")},
|
|
)
|
|
assert result.rows == []
|
|
assert len(result.skipped_dup) == 1
|
|
|
|
|
|
def test_unmatched_without_cart_or_dtl_is_not_inserted():
|
|
no_cart = match_pay_lines([_line(bar_code="999")], [_cart()], existing_keys=set())
|
|
assert no_cart.rows == []
|
|
assert no_cart.unmatched[0][1] == "NO_CART_ROW"
|
|
|
|
no_dtl = match_pay_lines(
|
|
[_line(pay_order_dtl_id="")],
|
|
[_cart()],
|
|
existing_keys=set(),
|
|
)
|
|
assert no_dtl.rows == []
|
|
assert no_dtl.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_copies_multi_platform_flag():
|
|
row = build_pay_row(_line(), _cart(multi_platform_flag=1))
|
|
assert row["multi_platform_flag"] == 1
|
|
assert row["supplier_name"] == "广东壹号药业有限公司"
|
|
assert row["bar_code"] == "6921665004933"
|
|
assert row["push_status"] == 0
|