127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
from decimal import Decimal
|
|
|
|
from settle import (
|
|
PAID_STATUSES,
|
|
MallLine,
|
|
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"),
|
|
)
|
|
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_order_nos=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_order_nos=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"]
|
|
assert len(row["pay_order_dtl_id"]) <= 40
|
|
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_order_nos=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_order_two_lines_both_insert_with_distinct_dtl_ids():
|
|
lines = [
|
|
_line(order_no="A", qty=10, pay_price=Decimal("53.40")),
|
|
_line(order_no="A", qty=50, pay_price=Decimal("55.21")),
|
|
]
|
|
result = match_pay_lines(lines, existing_order_nos=set())
|
|
assert len(result.rows) == 2
|
|
assert [row["pay_order_no"] for row in result.rows] == ["A", "A"]
|
|
ids = [row["pay_order_dtl_id"] for row in result.rows]
|
|
assert all(ids)
|
|
assert ids[0] != ids[1]
|
|
assert all(row["oms_id"] == 0 for row in result.rows)
|
|
|
|
|
|
def test_skips_existing_order_no():
|
|
result = match_pay_lines([_line()], existing_order_nos={"ZQD1"})
|
|
assert result.rows == []
|
|
assert len(result.skipped_dup) == 1
|
|
|
|
|
|
def test_existing_order_no_skips_all_its_lines_other_orders_insert():
|
|
lines = [
|
|
_line(order_no="OLD", qty=10),
|
|
_line(order_no="OLD", qty=20),
|
|
_line(order_no="NEW", qty=5),
|
|
]
|
|
result = match_pay_lines(lines, existing_order_nos={"OLD"})
|
|
assert [row["pay_order_no"] for row in result.rows] == ["NEW"]
|
|
assert [line.order_no for line in result.skipped_dup] == ["OLD", "OLD"]
|
|
|
|
|
|
def test_unmatched_barcode_over_40_is_not_inserted():
|
|
result = match_pay_lines(
|
|
[_line(bar_code="1" * 41)],
|
|
existing_order_nos=set(),
|
|
)
|
|
assert result.rows == []
|
|
assert result.unmatched[0][1] == "BAR_CODE_TOO_LONG"
|
|
|
|
|
|
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
|
|
assert row["pay_order_dtl_id"]
|
|
assert len(row["pay_order_dtl_id"]) <= 40
|