feat(采纳): 采纳工料机改按名称/规格/单位匹配两级汇总(决策108)

按决策108重写 _adopt_summary_rows:①单位工料机命中→按单位编码落地;
②仅项目工料机命中→按项目编码落地并补单位行;③都没有→来源行补两级
(x_adopt_added,取消采纳回收)。匹配键为名称/规格/单位(strip 全等),
编码只随行走,取代决策74/75采纳侧编码口径;同键多条按除税价升序取中位、
2条取低价、偶数多取居中偏低;合并工料机后的保留行现值为准,被合并行不参选;
采纳定额消耗量按映射改码,采纳来源价不再落两级汇总。边界:来源行名称为空
直接走③、同码异物不重复插行。

测试:新增 test_adopt_lme_match.py 6 项;api_harness 造数支持 proj_lmes/
uw_lmes 覆盖两级汇总。另更新决策台账与术语表,清理过时文档与临时 bat 脚本。
This commit is contained in:
lianjianwei2024
2026-10-18 23:08:51 +08:00
parent b0dc236245
commit 4508671a00
10 changed files with 317 additions and 431 deletions

View File

@@ -3040,14 +3040,63 @@ def api_norm_edit(sc, body):
# 连续采纳认最后一次;临删状态不可采纳(9.2);取消采纳是反向操作。
def _adopt_summary_rows(sc, hc, sid, uw_id, src_bill_id):
"""9.4 品类齐全:采纳定额的消耗量编码 → 单位级/项目级汇总缺行则补(数量照入库项目带,可改)。
补的行标 x_adopt_added=1(取消采纳时撤下)。返回补行数。"""
"""9.4 品类齐全(决策 108,2026-10-18 主人改口径·取代决策74/75采纳侧编码匹配):
采纳工料机按 **名称/规格/单位** 匹配(编码只随行走,不作匹配键,主人定「要以名称/规格/单位去控制」):
① 单位工料机(s_lme_uw 本单位)有同名称/规格/单位 → 直接按**单位编码**落地,两级汇总都不动;
② 单位没有、项目工料机(s_lme_proj)有 → 按**项目编码**落地,并把项目行补进单位汇总(x_adopt_added=1);
③ 都没有 → 按采纳来源行补 项目+单位 两级(x_adopt_added=1,取消采纳时撤下)。
同名称/规格/单位多条(同物不同码)按**除税价居中**取行(升序中位);只有 2 条取**低价**;
偶数多于 2 条取居中偏低((n-1)//2 位,"2条取低"偏低取向的自然延伸,主人未明示、台账注记)。
合并工料机后的现值即为准(x_merged_hide=1 的被合并行不参选,保留行价格已是弹框改后值——主人定「按合并后的」)。
后采纳按前采纳:前次采纳补的行已在两级汇总,天然被①②命中,不重复补。
返回 (补行数, {源编码: 落地编码})——采纳定额的消耗量(s_norm_res)随映射改码(决策 108),
导出件里定额按落地编码查本地价,采纳来源价不落两级汇总。
边界:来源行名称为空不作匹配键、直接走③;③里目标码已存在(同码异物)不重复插行(文件一级一码)照用该码。"""
codes = [r["res_code"] for r in hc.execute(
"""SELECT DISTINCT nr.res_code FROM norm_res nr JOIN norms n ON n.id=nr.norm_id
WHERE n.bill_id=? AND nr.res_code <> ''""", (src_bill_id,))]
if not codes:
return 0
return 0, {}
def _ident(name, spec, unit):
return ((name or "").strip(), (spec or "").strip(), (unit or "").strip())
def _pick(cands):
# 除税价升序、空价靠后、id 定稳定序;(n-1)//2:1条→它、2条→低价、3条→居中(决策 108 单价规则)
rs = sorted(cands, key=lambda c: (c["price"] is None,
c["price"] if c["price"] is not None else 0.0, c["id"]))
return rs[(len(rs) - 1) // 2]
uw_by_ident, pj_by_ident = {}, {}
for r in sc.execute("""SELECT code, name, spec, unit, kind, qty, ntp, taxrate, taxp, id
FROM s_lme_uw WHERE session_id=? AND uw_id IS ?""", (sid, uw_id)):
uw_by_ident.setdefault(_ident(r["name"], r["spec"], r["unit"]), []).append(
{"id": r["id"], "code": r["code"], "price": r["ntp"], "row": dict(r)})
for r in sc.execute("""SELECT code, name, spec, unit, kind, qty, ntp, taxrate, taxp, id
FROM s_lme_proj WHERE session_id=? AND COALESCE(x_merged_hide,0)=0""", (sid,)):
pj_by_ident.setdefault(_ident(r["name"], r["spec"], r["unit"]), []).append(
{"id": r["id"], "code": r["code"], "price": r["ntp"], "row": dict(r)})
# 「要是合并了工料机,就按合并后的」:被合并码归并到保留码;同组保留行本码在场时
# 优先于残留的被合并行(其价格是合并前旧值)。单位级无隐藏标记,靠 recode_map 收敛。
rm_row = sc.execute("SELECT v FROM s_settings WHERE scope=? AND k=?",
("session:%d" % sid, "recode_map")).fetchone()
recode = json.loads(rm_row[0] or "{}") if rm_row else {}
def _canon(cands):
by_eff = {}
for c in cands:
eff = recode.get(c["code"], c["code"]) or c["code"]
cur = by_eff.get(eff)
if cur is None or c["code"] == eff:
by_eff[eff] = {"id": c["id"], "code": eff, "price": c["price"], "row": c["row"]}
return list(by_eff.values())
uw_by_ident = {k: _canon(v) for k, v in uw_by_ident.items()}
pj_by_ident = {k: _canon(v) for k, v in pj_by_ident.items()}
n = 0
code_map = {}
for code in codes:
lm = hc.execute("""SELECT Number, Name, Specification, Unit, Kind, Quantity, TaxRate, NoTaxPrice, TaxPrice
FROM lmes WHERE batch_id=(SELECT batch_id FROM bills WHERE id=?)
@@ -3061,38 +3110,42 @@ def _adopt_summary_rows(sc, hc, sid, uw_id, src_bill_id):
AND code=?""", (src_bill_id, code)).fetchone()
if lm is None:
continue
if not sc.execute("SELECT 1 FROM s_lme_uw WHERE session_id=? AND uw_id=? AND code=? LIMIT 1",
(sid, uw_id, code)).fetchone():
sc.execute("""INSERT INTO s_lme_uw(session_id, uw_id, code, name, spec, unit, kind,
qty, ntp, taxp, x_adopt_added) VALUES(?,?,?,?,?,?,?,?,?,?,1)""",
(sid, uw_id, code, lm["Name"], lm["Specification"], lm["Unit"],
f_(lm["Kind"]), f_(lm["Quantity"]), f_(lm["NoTaxPrice"]), f_(lm["TaxPrice"])))
n += 1
# 决策 75(2026-10-15 主人定数据流):Project.xml 工料机 = 单位工程汇总的汇总。
# 采纳带来的增量同步进项目级:编码缺 → 补行(x_adopt_added=1,撤销时可撤);
# 编码已存在 → 只更新 名称/规格/单位/除税价/税率/含税价 **不一致** 的字段(最小更新)。
row = sc.execute("SELECT id, name, spec, unit, ntp, taxrate, taxp FROM s_lme_proj WHERE session_id=? AND code=? LIMIT 1",
(sid, code)).fetchone()
if not row:
ident = _ident(lm["Name"], lm["Specification"], lm["Unit"])
cands = uw_by_ident.get(ident) if ident[0] else None
if cands: # ① 单位有:直接按单位编码,两级汇总不动
code_map[code] = _pick(cands)["code"]
continue
cands = pj_by_ident.get(ident) if ident[0] else None
if cands: # ② 仅项目有:按项目编码,项目行补进单位汇总
hit = _pick(cands)
code_map[code] = hit["code"]
if not sc.execute("SELECT 1 FROM s_lme_uw WHERE session_id=? AND uw_id IS ? AND code=? LIMIT 1",
(sid, uw_id, hit["code"])).fetchone():
r = hit["row"]
sc.execute("""INSERT INTO s_lme_uw(session_id, uw_id, code, name, spec, unit, kind,
qty, ntp, taxrate, taxp, x_adopt_added) VALUES(?,?,?,?,?,?,?,?,?,?,?,1)""",
(sid, uw_id, r["code"], r["name"], r["spec"], r["unit"], r["kind"],
r["qty"], r["ntp"], r["taxrate"], r["taxp"]))
n += 1
continue
# ③ 都没有:按采纳来源行补两级(码已存在=同码异物,不重复插行、照用该码)
code_map[code] = code
if not sc.execute("SELECT 1 FROM s_lme_proj WHERE session_id=? AND code=? LIMIT 1",
(sid, code)).fetchone():
sc.execute("""INSERT INTO s_lme_proj(session_id, code, name, spec, unit, kind,
qty, ntp, taxrate, taxp, x_adopt_added) VALUES(?,?,?,?,?,?,?,?,?,?,1)""",
(sid, code, lm["Name"], lm["Specification"], lm["Unit"],
f_(lm["Kind"]), f_(lm["Quantity"]), f_(lm["NoTaxPrice"]),
f_(lm["TaxRate"]), f_(lm["TaxPrice"])))
n += 1
else:
sets, vals = [], []
for col, want in (("name", lm["Name"]), ("spec", lm["Specification"]), ("unit", lm["Unit"]),
("ntp", f_(lm["NoTaxPrice"])), ("taxrate", f_(lm["TaxRate"])),
("taxp", f_(lm["TaxPrice"]))):
if (want or "") != (row[col] or ""):
sets.append(col + "=?")
vals.append(want)
if sets:
vals.append(row["id"])
sc.execute("UPDATE s_lme_proj SET %s WHERE id=?" % ",".join(sets), vals)
n += 1
return n
if not sc.execute("SELECT 1 FROM s_lme_uw WHERE session_id=? AND uw_id IS ? AND code=? LIMIT 1",
(sid, uw_id, code)).fetchone():
sc.execute("""INSERT INTO s_lme_uw(session_id, uw_id, code, name, spec, unit, kind,
qty, ntp, taxp, x_adopt_added) VALUES(?,?,?,?,?,?,?,?,?,?,1)""",
(sid, uw_id, code, lm["Name"], lm["Specification"], lm["Unit"],
f_(lm["Kind"]), f_(lm["Quantity"]), f_(lm["NoTaxPrice"]), f_(lm["TaxPrice"])))
n += 1
return n, code_map
def f_(x):
@@ -3128,7 +3181,9 @@ def api_bill_adopt(sc, hc, body):
9.3 原有清单:现清单单价按入库项目覆盖(主人 2026-10-17:采纳后清单单价=采纳的单价,
合价=工程量×新单价),工程量不动;原定额整组转临删;入库定额追加在下方(排序按入库项目,
工程量默认挂 GCL 公式),x_adopt=1 + x_src_norm_id=批次定额id;此前采纳追加的定额撤下(连续采纳认最后一次)。
9.4 品类追加:采纳定额的工料机 → 单位级/项目级汇总缺行补行。
9.4 品类齐全(决策 108):采纳定额的工料机按 **名称/规格/单位** 匹配本地两级汇总——
单位有→按单位编码;仅项目有→项目行补进单位;都没有→来源行补两级;
单价多条取居中、2条取低;消耗量(s_norm_res)随匹配结果改码,本地价随码生效。
9.2 临删状态不可采纳。"""
sid = int(body.get("session_id", 0) or 0)
bid = int(body.get("bill_id", 0) or 0)
@@ -3152,6 +3207,7 @@ def api_bill_adopt(sc, hc, body):
src_qty = f_(bb["Quantity"])
if is_new:
# 9.1 整体覆盖
adopted_norm_ids = []
sc.execute("""UPDATE s_bills SET Number=?, Name=?, Attr=?, Unit=?, Price=?, Quantity=0,
Total=0, x_adopt=1 WHERE id=?""",
(bb["Number"], bb["Name"], bb["Attr"], bb["Unit"], new_price, bid))
@@ -3164,11 +3220,13 @@ def api_bill_adopt(sc, hc, body):
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"],
_gcl_coef_expr(nn["Quantity"], bb["Quantity"], nn["x_qty_coef"], nn["Unit"]),
nn["x_adjust"]))
adopted_norm_ids.append(cur.lastrowid)
for rr in hc.execute("SELECT res_code, qty, express FROM norm_res WHERE norm_id=? ORDER BY id", (nn["id"],)):
sc.execute("INSERT INTO s_norm_res(norm_id, res_code, qty, express) VALUES(?,?,?,?)",
(cur.lastrowid, rr["res_code"], rr["qty"], rr["express"]))
else:
# 9.3 定额追加 + 原定额转临删 + 清单单价按采纳价覆盖
adopted_norm_ids = []
sc.execute("DELETE FROM s_norm_res WHERE norm_id IN (SELECT id FROM s_norms WHERE bill_id=? AND x_adopt=1)", (bid,))
sc.execute("DELETE FROM s_norms WHERE bill_id=? AND x_adopt=1", (bid,)) # 连续采纳认最后一次
sc.execute("UPDATE s_norms SET x_tdel=1 WHERE bill_id=?", (bid,)) # 原有定额 → 临删
@@ -3179,14 +3237,23 @@ def api_bill_adopt(sc, hc, body):
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"], nn["id"],
_gcl_coef_expr(nn["Quantity"], bb["Quantity"], nn["x_qty_coef"], nn["Unit"]),
nn["x_adjust"]))
adopted_norm_ids.append(cur.lastrowid)
for rr in hc.execute("SELECT res_code, qty, express FROM norm_res WHERE norm_id=? ORDER BY id", (nn["id"],)):
sc.execute("INSERT INTO s_norm_res(norm_id, res_code, qty, express) VALUES(?,?,?,?)",
(cur.lastrowid, rr["res_code"], rr["qty"], rr["express"]))
bqty = sc.execute("SELECT Quantity FROM s_bills WHERE id=?", (bid,)).fetchone()["Quantity"]
btotal = round((float(bqty) if bqty is not None else 0.0) * (float(new_price) if new_price is not None else 0.0), 2)
sc.execute("UPDATE s_bills SET Price=?, Total=?, x_adopt=1 WHERE id=?", (new_price, btotal, bid))
n_sum = _adopt_summary_rows(sc, hc, sid, sb["uw_id"], src_id)
return {"ok": True, "sid": sid, "n_norm": len(bnorms), "n_sum": n_sum,
# 决策 108:消耗量随名称/规格/单位匹配结果改码(逐行读现值再映射,防 A→B→C 链式误改)
n_sum, code_map = _adopt_summary_rows(sc, hc, sid, sb["uw_id"], src_id)
n_recode = 0
for nid in adopted_norm_ids:
for rr in sc.execute("SELECT id, res_code FROM s_norm_res WHERE norm_id=?", (nid,)).fetchall():
new = code_map.get(rr["res_code"] or "")
if new and new != rr["res_code"]:
sc.execute("UPDATE s_norm_res SET res_code=? WHERE id=?", (new, rr["id"]))
n_recode += 1
return {"ok": True, "sid": sid, "n_norm": len(bnorms), "n_sum": n_sum, "n_recode": n_recode,
"mode": "覆盖" if is_new else "追加", "price": new_price}