test_planner.py
6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""补货算法(纯函数,不连库):出库驱动的默认期望(推荐 / 上次)、补货数、勾选、上限、缺货、单次补货。"""
from app.features.restock import planner
def facts(**kw):
base = {"stock": 0, "trans_shipped": 0, "unreceived": 0, "daily_surplus": None, "unit_price": 1,
"warehouses": {"GTMC广州": 5, "华南GTMC": 0}, "national_queried": True, "mad": 3.0}
base.update(kw)
return base
def line(outbound=5.0, grp="stock", checked=True, last=None, modified_to=None, field="trans_shipped", **kw):
ln = planner.Line(part_no="P1", name="件", grp=grp, outbound_qty=outbound, checked=checked)
planner.fill_facts(ln, facts(**kw), field)
if last is not None:
ln.last_expected = last
if modified_to is not None:
ln.modified, ln.expected = True, modified_to
planner.apply_defaults(ln)
return planner.compute(ln)
def test_recommend_is_stock_plus_outbound():
"""推荐期望 = 库存 + 出库(把用掉的补回来)→ 补货数 = 出库 − 在途。"""
l = line(outbound=18, stock=12)
assert (l.default_source, l.default_expected, l.expected) == ("recommend", 30, 30)
assert (l.suggested, l.final_qty, l.tag) == (18, 18, "normal")
l = line(outbound=12, stock=6, trans_shipped=2)
assert (l.suggested, l.final_qty) == (10, 10)
def test_last_value_takes_priority():
l = line(outbound=5, stock=8, last=20)
assert (l.default_source, l.expected, l.suggested, l.source_label) == ("last", 20, 12, "上次")
def test_manual_value_kept():
l = line(outbound=5, stock=8, last=20, modified_to=10)
assert (l.expected, l.suggested, l.source_label) == (10, 2, "已修改")
def test_zero_means_no_order():
l = line(outbound=5, stock=8, modified_to=0)
assert (l.suggested, l.final_qty, l.tag) == (0, 0, "enough")
def test_unchecked_other_group():
l = line(grp="other", checked=False, stock=1, outbound=3)
assert (l.suggested, l.final_qty, l.tag) == (3, 0, "unchecked")
assert not l.orderable
def test_in_transit_fields():
assert line(outbound=4, stock=2, trans_shipped=3, unreceived=5, field="unreceived").suggested == 0
assert line(outbound=4, stock=2, trans_shipped=3, unreceived=1, field="trans_shipped").suggested == 1
assert line(outbound=4, stock=2, trans_shipped=3, unreceived=1, field="max").suggested == 1
def test_in_transit_ok():
l = line(outbound=10, stock=0, trans_shipped=10)
assert (l.suggested, l.tag) == (0, "in_transit_ok")
def test_oos_and_capped():
assert line(warehouses={"GTMC广州": 0, "华南GTMC": 0}).tag == "oos"
l = line(outbound=10, stock=0, daily_surplus=4)
assert (l.suggested, l.final_qty, l.tag) == (10, 4, "capped") and "最多还能订 4 个" in l.note
assert line(outbound=10, daily_surplus=0).note == "DMS 当日可订为 0,今天订不了"
def test_zero_cap_reason_storage_limit():
"""门店 2026-11-03 例 90430C0005:库存 278 > DMS 库存上限 150 → DMS 当日可订 0;全国仓有货也订不了。"""
l = line(outbound=11, stock=278, daily_surplus=0.0, storage_limit=150, min_qty=50, pack_qty=50,
warehouses={"GTMC广州": 181469.0, "华南GTMC": 9449.0})
assert (l.suggested, l.final_qty, l.tag, l.orderable) == (11, 0, "capped", False)
assert "库存 278 已超过 DMS 库存上限 150" in l.note
assert "已经订过 3 个" in line(outbound=10, daily_surplus=0, purchased_today=3).note
def test_pack_and_min_rounding():
"""订购数按起订量 / 包装数向上取整(门店 2026-11-03 例:刹车油差 9、包装 24 → 订 24),否则 DMS 建单校验整单拒。"""
l = line(outbound=9, stock=54, daily_surplus=96, min_qty=24, pack_qty=24)
assert (l.suggested, l.final_qty, l.tag, l.orderable) == (9, 24, "normal", True)
assert "差 9 个,按包装 24 取整订 24" in l.note
assert line(outbound=26, stock=0, min_qty=24, pack_qty=24).final_qty == 48
assert line(outbound=3, stock=0, min_qty=10, pack_qty=4).final_qty == 12 # ≥ 起订量且是包装数倍数
assert line(outbound=5, stock=0).final_qty == 5 # 包装 1:不变
l = line(outbound=30, stock=0, daily_surplus=30, min_qty=24, pack_qty=24) # 48 > 上限 30 → 向下取到 24
assert (l.final_qty, l.tag) == (24, "capped") and "差的 6 个明天再补" in l.note
l = line(outbound=5, stock=0, daily_surplus=20, min_qty=24, pack_qty=24) # 上限凑不够一个包装
assert (l.final_qty, l.tag, l.orderable) == (0, "capped", False) and "不够包装 24" in l.note
assert [planner.pack_up(w, 10, 4) for w in (0, 1, 12, 13)] == [0, 12, 12, 16]
assert [planner.pack_down(c, 10, 4) for c in (0, 9, 12, 15)] == [0, 0, 12, 12]
def test_no_national_query_note():
l = line(grp="other", checked=True, warehouses={}, national_queried=False)
assert l.orderable and "没查全国仓" in l.note
def test_negative_outbound_never_recommends_less_than_stock():
l = line(outbound=-2, stock=5)
assert (l.recommend, l.suggested) == (5, 0)
def test_error_line():
ln = planner.Line(part_no="P1", name="件")
planner.fill_facts(ln, {"error": "boom"})
planner.apply_defaults(ln)
planner.compute(ln)
assert (ln.tag, ln.final_qty) == ("error", 0)
def test_number_and_summary():
a = line(outbound=5, stock=0)
b = planner.Line(part_no="P2", name="其他", grp="other", outbound_qty=9, checked=False)
planner.fill_facts(b, facts())
planner.apply_defaults(b)
planner.compute(b)
lines = planner.number([b, a])
assert [l.part_no for l in lines] == ["P1", "P2"] and (a.seq, b.seq) == (1, None)
s = planner.summary(lines)
assert (s["order_count"], s["order_qty"], s["stock_count"], s["other_count"], s["checked_count"]) == (1, 5, 1, 1, 1)
def test_single_restock():
"""单次补货:数量由人给,受当日可订上限 / 关注仓约束(门店 2026-10-28 例:空调滤 2 个,华南仓 1095)。"""
it = dict(facts(stock=6, trans_shipped=2, daily_surplus=30, unit_price=53.3,
warehouses={"GTMC广州": 0, "华南GTMC": 1095}), part_no="8713902280", name="空调滤清器滤芯分总成")
l = planner.plan_single(it, 2)
assert (l.grp, l.final_qty, l.tag, l.seq) == ("single", 2, "normal", 1)
assert l.to_dict()["amount"] == 106.6
assert planner.plan_single(dict(it, daily_surplus=1), 2).final_qty == 1
assert planner.plan_single(dict(it, warehouses={"GTMC广州": 0, "华南GTMC": 0}), 2).tag == "oos"
assert planner.plan_single(dict(it, error="查不到"), 2).tag == "error"