feat(adopt): 决策273 单价行 Total 采纳逻辑迁回 API 侧并调整 x_side 读取

This commit is contained in:
lianjianwei2024
2026-11-05 08:24:06 +08:00
parent e113eb0264
commit 0dcdddc4a1
19 changed files with 196 additions and 216 deletions

View File

@@ -1,104 +0,0 @@
# -*- coding: utf-8 -*-
"""决策 273 实施:采纳外部定额的单价行组——行与列按本项目同区模板复制,
Total 按 Name 匹配外部(一致=外部该行 Total,不一致留空)。
改动:①api_server 采纳段 bnorms SELECT+INSERT 带 x_side(采纳时构造最终行组)
②export_cos._norm_xml 按 x_side 回填单价行 Total(无 x_side 维持清零现状)。"""
import io
p = r"D:\PricingReview\CostReview\app\server\api_server.py"
s = io.open(p, encoding="utf-8").read()
# ---- ① 两处 bnorms SELECT 加 x_side ----
old = ''' bnorms = sc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total,
NULL AS x_qty_coef, x_adjust, qty_expr
FROM s_norms WHERE bill_id=? AND (x_tdel IS NULL OR x_tdel=0) ORDER BY id""",
(src_self,)).fetchall()'''
new = ''' bnorms = sc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total,
NULL AS x_qty_coef, x_adjust, qty_expr, x_side
FROM s_norms WHERE bill_id=? AND (x_tdel IS NULL OR x_tdel=0) ORDER BY id""",
(src_self,)).fetchall()'''
assert old in s, "bnorms sc NOT FOUND"
s = s.replace(old, new)
old2 = ''' bnorms = hc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total, x_qty_coef, x_adjust
FROM norms WHERE bill_id=? ORDER BY id""", (src_id,)).fetchall()'''
new2 = ''' bnorms = hc.execute("""SELECT id, Number, Name, Unit, Quantity, Price, Total, x_qty_coef, x_adjust, x_side
FROM norms WHERE bill_id=? ORDER BY id""", (src_id,)).fetchall()'''
assert old2 in s, "bnorms hc NOT FOUND"
s = s.replace(old2, new2)
# ---- ② bnorms 取完后构造:外部 Name→Total 映射 + 本项目同区模板行组 ----
old3 = ''' def _norm_expr(nn):
# 决策 217(2026-10-26 主人定):采纳定额公式**四入口统一为分部分项同款 GCL×系数**——'''
new3 = ''' # 决策 273(2026-11-04 主人定):采纳定额的单价行组——行与列按本项目同区模板复制,
# Total 按 Name 匹配外部(一致=外部该行 Total,不一致留空)。外部单价行=norms.x_side
# (norm_side 通道 2026-11-03 已入库);本项目同区模板=同 x_ptag(措施/分部)任一条
# 非采纳定额的 x_side;本项目无同区模板→x_side 置空(骨架态,等原软件组价)。
_ext_by_norm = {}
if not src_self:
for _n2 in bnorms:
if not _n2["x_side"]:
continue
try:
_m = {}
for _rec in json.loads(_n2["x_side"]):
if _rec.get("tag") == "UnitPriceCalculationOfItem":
_a = _rec.get("attrs") or {}
_m[(_a.get("Name") or "").strip()] = _a.get("Total")
_ext_by_norm[_n2["id"]] = _m
except Exception:
pass
_tmpl_side = None
try:
_pt = sc.execute("SELECT x_ptag FROM s_bills WHERE id=?", (bid,)).fetchone()
_ptag = ((_pt["x_ptag"] if _pt else None) or "")
_t = sc.execute("""SELECT n.x_side FROM s_norms n JOIN s_bills b ON b.id=n.bill_id
WHERE b.session_id=? AND COALESCE(n.x_adopt,0)=0 AND n.x_side IS NOT NULL
AND b.id<>? AND COALESCE(b.x_ptag,'')=?
ORDER BY n.id LIMIT 1""", (sid, bid, _ptag)).fetchone()
if _t and _t["x_side"]:
_tmpl_side = _t["x_side"]
except Exception:
_tmpl_side = None
def _adopt_side(nn):
"""本条采纳定额的 x_side:本项目来源=源行组原样复制;外部来源=同区模板行组
+ Total 按 Name 匹配外部(一致=外部值,不一致留空);无模板=None。"""
if src_self:
return nn["x_side"]
_m = _ext_by_norm.get(nn["id"])
if not _tmpl_side or _m is None:
return None
try:
_side = json.loads(_tmpl_side)
for _rec in _side:
if _rec.get("tag") == "UnitPriceCalculationOfItem":
_a = _rec.get("attrs") or {}
_nm = (_a.get("Name") or "").strip()
_a["Total"] = _m.get(_nm, "")
_rec["attrs"] = _a
return json.dumps(_side, ensure_ascii=False)
except Exception:
return None
def _norm_expr(nn):
# 决策 217(2026-10-26 主人定):采纳定额公式**四入口统一为分部分项同款 GCL×系数**——'''
assert old3 in s, "norm_expr anchor NOT FOUND"
s = s.replace(old3, new3)
# ---- ③ 两处采纳 INSERT 加 x_side ----
old4 = ''' cur = sc.execute("""INSERT INTO s_norms(bill_id, Number, Name, Unit, Quantity, Price, Total,
x_adopt, x_src_norm_id, qty_expr, x_adjust)
VALUES(?,?,?,?,NULL,?,NULL,1,?,?,?)""",
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"],
None if src_self else nn["id"], _norm_expr(nn), nn["x_adjust"]))'''
new4 = ''' cur = sc.execute("""INSERT INTO s_norms(bill_id, Number, Name, Unit, Quantity, Price, Total,
x_adopt, x_src_norm_id, qty_expr, x_adjust, x_side)
VALUES(?,?,?,?,NULL,?,NULL,1,?,?,?,?)""",
(bid, nn["Number"], nn["Name"], nn["Unit"], nn["Price"],
None if src_self else nn["id"], _norm_expr(nn), nn["x_adjust"], _adopt_side(nn)))'''
c = s.count(old4)
assert c == 2, "INSERT count=%d" % c
s = s.replace(old4, new4)
io.open(p, "w", encoding="utf-8").write(s)
print("api_server adopt side done")

View File

@@ -1,51 +0,0 @@
# -*- coding: utf-8 -*-
"""决策 273 导出端:采纳定额(x_side 有值)的单价行 Total 按 x_side 回填——
一致=外部值/不一致=空串;无 x_side 维持清零现状(106 口径不动)。"""
import io
p = r"D:\PricingReview\CostReview\app\core\export_cos.py"
s = io.open(p, encoding="utf-8").read()
# ---- ① _norm_xml 里 pre/post 清零后按 x_side 回填单价行 Total ----
old = ''' pre, post = _zero_results(pre), _zero_results(post)
pre = re.sub(r'<ExpressElement\\b[^>]*/>', '', pre)
post = re.sub(r'<ExpressElement\\b[^>]*/>', '', post)'''
new = ''' pre, post = _zero_results(pre), _zero_results(post)
pre = re.sub(r'<ExpressElement\\b[^>]*/>', '', pre)
post = re.sub(r'<ExpressElement\\b[^>]*/>', '', post)
# 决策 273(2026-11-04 主人定):采纳定额的单价行 Total 按 Name 匹配外部——
# x_side(采纳时构造:同区模板行组+Total 按 Name 匹配外部,一致=外部值/不一致留空)
# 有值时回填 Total 覆盖清零;无 x_side 维持清零现状(106 口径不动)。
_sm = {}
if n.get("x_side"):
try:
for _rec in json.loads(n["x_side"]):
if _rec.get("tag") == "UnitPriceCalculationOfItem":
_a = _rec.get("attrs") or {}
_sm[(_a.get("Name") or "").strip()] = _a.get("Total")
except Exception:
_sm = {}
if _sm:
pre = _adopt_totals(pre, _sm)
post = _adopt_totals(post, _sm)'''
assert old in s, "zero block NOT FOUND"
s = s.replace(old, new)
# ---- ② _adopt_totals 函数(挂在 _zero_results 后面) ----
old2 = '''def _zero_results(frag):'''
new2 = '''def _adopt_totals(frag, sm):
"""决策 273:单价行 Total 按 Name 匹配回填——一致=外部该行 Total;不一致=空串(留空)。
只动骨架已有的 UnitPriceCalculationOfItem 自闭合行(卡槽在才写)。"""
def _f(m):
a = dict(re.findall(r'(\\w+)="([^"]*)"', m.group(1)))
nm = (a.get("Name") or "").strip()
v = sm.get(nm)
return _attr_set(m.group(0), "Total", "" if v is None else v)
return re.sub(r'<UnitPriceCalculationOfItem\\b([^>]*?)/>', _f, frag)
def _zero_results(frag):'''
assert old2 in s, "_zero_results anchor NOT FOUND"
s = s.replace(old2, new2)
io.open(p, "w", encoding="utf-8").write(s)
print("export side done")