Files
pricing_review/_analysis/定额单价专题/patch_273_api.py

105 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- 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")