planner.py
7.73 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""补货算法(纯函数,无 IO)。口径来自需求文档 C③/D①②,改编自 demo/agent/planner.py:
建议订购量 = 期望 −(当前库存 + 在途),≤0 不订;在途按配置 business.in_transit_field 取:
trans_shipped = 调拨在途 transShippedQty(默认)/ unreceived = 未收货 unreceivedQty / max = 两者取大;两个原始值都保存
关注仓(默认 GTMC广州 + 华南GTMC)全部无货 → 厂家缺货,不订
超出当日剩余可订(dailySurplusPurQty,DMS 为空表示不限)→ 默认按上限订,并单独提示计划员
tag:normal / capped / oos / enough / in_transit_ok / removed / error
另外每行算几个提示用的标志(不入库,to_dict 时现算):
below_expected 库存 + 在途 < 期望
short_after 低于期望,且按本次订购量订完仍不够期望(被上限截断 / 缺货 / 剔除 / 查询失败)
wh_ok 每个关注仓能否单独满足本次需要的数量(订购量;没订时按建议量)
wh_short 关注仓合计有货,但合计不够本次需要的数量
attention 需要计划员留意(以上任一,或按上限 / 缺货 / 查询失败)
"""
from __future__ import annotations
import math
from dataclasses import asdict, dataclass, field
TAG_LABEL = {"normal": "", "capped": "按可订上限", "oos": "厂家缺货", "enough": "库存充足",
"in_transit_ok": "在途已达标", "removed": "计划员剔除", "error": "查询失败"}
@dataclass
class Line:
part_no: str
name: str
expected: int
stock: float = 0
trans_shipped: float = 0
unreceived: float = 0
in_transit: float = 0
suggested: int = 0
cap: int | None = None
final_qty: int = 0
tag: str = "normal"
note: str = ""
unit_price: float = 0
wh: dict = field(default_factory=dict)
error: str = ""
seq: int | None = None # 只给 suggested>0 的行编号(微信里说"第 N 项"用)
@property
def orderable(self) -> bool:
return self.final_qty > 0 and self.tag in ("normal", "capped")
@property
def have(self) -> float:
return (self.stock or 0) + (self.in_transit or 0)
@property
def gap(self) -> float:
return max((self.expected or 0) - self.have, 0)
@property
def below_expected(self) -> bool:
return self.tag != "error" and self.have < (self.expected or 0) # 查询失败的库存未知,不算
@property
def short_after(self) -> bool:
return self.below_expected and self.have + (self.final_qty if self.orderable else 0) < (self.expected or 0)
@property
def need(self) -> int:
"""关注仓需要满足的数量:订购量;还没订(0)时按建议量。"""
return int(self.final_qty or self.suggested or 0)
@property
def wh_ok(self) -> dict:
n = self.need
return {w: (float(q or 0) >= n if n > 0 else float(q or 0) > 0) for w, q in (self.wh or {}).items()}
@property
def wh_total(self) -> float:
return sum(float(q or 0) for q in (self.wh or {}).values())
@property
def wh_short(self) -> bool:
return bool(self.wh) and self.need > 0 and 0 < self.wh_total < self.need
@property
def attention(self) -> bool:
return self.short_after or self.wh_short or self.tag in ("capped", "oos", "error")
def wh_text(self) -> str:
"""「广州 719✔ 华南 3✘」:仓名去掉 GTMC 前缀,✔/✘ 表示能否单独满足本次需要的数量。"""
ok = self.wh_ok
return " ".join("%s %g%s" % (w.replace("GTMC", "") or w, float(q or 0), "✔" if ok.get(w) else "✘")
for w, q in (self.wh or {}).items())
def to_dict(self) -> dict:
d = asdict(self)
d.update(label=TAG_LABEL.get(self.tag, ""), orderable=self.orderable,
amount=round(self.final_qty * (self.unit_price or 0), 2), gap=self.gap,
below_expected=self.below_expected, short_after=self.short_after, need=self.need,
wh_ok=self.wh_ok, wh_total=self.wh_total, wh_short=self.wh_short, attention=self.attention)
return d
def _f(v) -> float:
try:
return float(v or 0)
except (TypeError, ValueError):
return 0.0
IN_TRANSIT_LABEL = {"trans_shipped": "调拨在途", "unreceived": "未收货", "max": "调拨在途/未收货取大"}
def pick_in_transit(trans_shipped: float, unreceived: float, field: str) -> float:
if field == "unreceived":
return unreceived
if field == "max":
return max(trans_shipped, unreceived)
return trans_shipped
def plan_item(it: dict, in_transit_field: str = "trans_shipped") -> Line:
"""it:取数端 scan 返回的一项。"""
ln = Line(part_no=it["part_no"], name=it.get("name") or it.get("name_dms") or "", expected=int(it.get("expected") or 0),
stock=_f(it.get("stock")), trans_shipped=_f(it.get("trans_shipped")), unreceived=_f(it.get("unreceived")),
unit_price=_f(it.get("unit_price")), cap=it.get("daily_surplus"), wh=dict(it.get("warehouses") or {}))
ln.in_transit = pick_in_transit(ln.trans_shipped, ln.unreceived, in_transit_field)
if it.get("error"):
ln.tag, ln.error, ln.note = "error", it["error"], "查询失败,未纳入清单"
return ln
ln.suggested = max(int(math.ceil(ln.expected - (ln.stock + ln.in_transit))), 0)
if ln.suggested <= 0:
ln.tag = "in_transit_ok" if (ln.in_transit > 0 and ln.stock < ln.expected) else "enough"
ln.note = TAG_LABEL[ln.tag]
return ln
if ln.wh and sum(ln.wh.values()) <= 0:
ln.tag, ln.note = "oos", "关注仓(%s)均无货,不订" % "/".join(ln.wh)
return ln
if ln.cap is not None and ln.suggested > ln.cap:
ln.tag, ln.final_qty = "capped", max(int(ln.cap), 0)
ln.note = (f"当日剩余可订仅 {ln.cap},差额 {ln.suggested - ln.cap} 次日再补" if ln.cap > 0 else "当日剩余可订为 0")
return ln
ln.tag, ln.final_qty = "normal", ln.suggested
return ln
def plan(items: list[dict], in_transit_field: str = "trans_shipped") -> list[Line]:
lines = [plan_item(it, in_transit_field) for it in items]
lines = [l for l in lines if l.suggested > 0] + [l for l in lines if l.suggested <= 0]
n = 0
for l in lines:
if l.suggested > 0:
n += 1
l.seq = n
return lines
def set_qty(ln: Line, qty: int) -> str:
"""计划员改数量。返回说明;不能改的抛 ValueError。"""
if ln.tag in ("oos", "error"):
raise ValueError(f"{ln.name} {TAG_LABEL[ln.tag]},不能订购")
qty = max(int(qty), 0)
if qty == 0:
ln.final_qty, ln.tag, ln.note = 0, "removed", "计划员剔除"
return ln.note
if ln.cap is not None and qty > ln.cap:
ln.final_qty, ln.tag, ln.note = int(ln.cap), "capped", f"超出当日剩余可订,按上限 {ln.cap}"
return ln.note
ln.final_qty, ln.tag, ln.note = qty, "normal", "计划员调整" if qty != ln.suggested else ""
return ln.note
def summary(lines: list[Line]) -> dict:
ok = [l for l in lines if l.orderable]
return {"need_count": sum(1 for l in lines if l.suggested > 0), "order_count": len(ok),
"order_qty": sum(l.final_qty for l in ok), "amount": round(sum(l.final_qty * l.unit_price for l in ok), 2),
"capped": [l.part_no for l in lines if l.tag == "capped"], "oos": [l.part_no for l in lines if l.tag == "oos"],
"errors": [l.part_no for l in lines if l.tag == "error"],
"below": [l.part_no for l in lines if l.below_expected],
"short_after": [l.part_no for l in lines if l.short_after],
"wh_short": [l.part_no for l in lines if l.wh_short and l.tag != "oos"],
"total": len(lines)}