feat(lme): 名称规格单位编辑回写与项目级联动显示
This commit is contained in:
@@ -844,23 +844,11 @@ def _summary_node(fields, template=None):
|
||||
"" if np_ is None else np_, "" if tp is None else tp, "" if kd is None else kd))
|
||||
|
||||
|
||||
def _is_amount_aggregate(opentag):
|
||||
"""金额汇总行判定(决策 106 补记九):广联达文件里「人工费/管理费」这类汇总行的
|
||||
惯例是 数量=金额合计、单价恒为 1(元/元恒等)。这类行回写会话里的资源单价
|
||||
(如人工 500 元/工日)会被按 数量×单价 重算 → 人工费爆炸外溢(样例工程实测)。
|
||||
判据:单位=元 且 原除税价数值=1。"""
|
||||
if (_attr(opentag, "Unit") or "").strip() != "元":
|
||||
return False
|
||||
try:
|
||||
return abs(float(_attr(opentag, "NoTaxPrice") or 0) - 1.0) < 1e-9
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
_PRICE_ATTRS = ("NoTaxPrice", "TaxRate", "TaxPrice")
|
||||
|
||||
|
||||
def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, recode_children=None):
|
||||
def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, recode_children=None,
|
||||
field_updates=None):
|
||||
"""本单位工程的工料机汇总 = 单价被查找的对象(主人口径 5.1/5.3):
|
||||
①采纳新增的品类行追加到本单位汇总末尾(克隆完整属性集);
|
||||
②合并改价按编码写回本单位汇总节点(除税/税率/含税,只动不一致项)——
|
||||
@@ -869,6 +857,9 @@ def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, re
|
||||
③规格 第11/12 条(2026-10-16):合并改码——被合并码节点 Number 换成保留码(母数据只替换
|
||||
编码),其子级组成元素**全字段重建**为保留行子级(子数据全部字段替换,主人纠偏定稿)。
|
||||
放在改价之后执行:改价按保留码找节点,别让刚改码的节点被二次命中。
|
||||
④2026-10-22 主人定(编辑名称/规格/单位要写回):field_updates 里**编辑过的编码**回写
|
||||
Name/Specification/Unit(api_lme_field 登记圈定,不做全量同步防零编辑误改);
|
||||
改码命中的节点(该单位原是旧码)同样吃到保留码的编辑值。
|
||||
决策 93:改价+改码的全部修改按**一次扫描的索引**定位、一次拼接——此前每码全文重扫
|
||||
(翠香件每文件 ~1300 遍 = 单文件 4~6 秒)。索引建在追加之后的文本上:新增行也吃合并价。"""
|
||||
tmpl = _full_summary_template(txt)
|
||||
@@ -886,8 +877,6 @@ def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, re
|
||||
if not span:
|
||||
continue
|
||||
s0, e0, _bend, opentag = span
|
||||
if _is_amount_aggregate(opentag):
|
||||
continue # 金额汇总行不回写价格(补记九:人工费外溢实测)
|
||||
newtag = opentag
|
||||
for attr, v in (("NoTaxPrice", ntp), ("TaxRate", rate), ("TaxPrice", taxp)):
|
||||
if v is None:
|
||||
@@ -895,6 +884,25 @@ def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, re
|
||||
newtag = _attr_set(newtag, attr, v)
|
||||
if newtag != opentag:
|
||||
edits[s0] = (s0, e0, newtag)
|
||||
for code, (name, spec, unit) in (field_updates or {}).items():
|
||||
# ④编辑回写:Name/Specification/Unit 与改价同一索引通道、同一合成规则。
|
||||
# 2026-10-22 决策 169(主人定):金额汇总行(数量=金额合计、单价=1 的费用科目行)
|
||||
# **视同普通工料机**——价格/名规格一律照常回写,不再有任何跳过
|
||||
# (原「补记九金额行不回写价格」防护撤除:误写路径已随 merged_codes/field_codes
|
||||
# 白名单通道消失,只有编辑/合并过的编码才会被写)。
|
||||
span = idx.get(str(code))
|
||||
if not span:
|
||||
continue
|
||||
s0, e0, _bend, opentag = span
|
||||
base = edits.get(s0)
|
||||
start_tag = base[2] if base else opentag
|
||||
newtag = start_tag
|
||||
for attr, v in (("Name", name), ("Specification", spec), ("Unit", unit)):
|
||||
if v is None:
|
||||
continue
|
||||
newtag = _attr_set(newtag, attr, v)
|
||||
if newtag != start_tag:
|
||||
edits[s0] = (s0, e0, newtag)
|
||||
blocks = {} # span起点 → (s0, bend, 整块替换)(改码带子级重建)
|
||||
for old, new in (recode_map or {}).items():
|
||||
if old == new:
|
||||
@@ -907,6 +915,16 @@ def transform_unit_summaries(txt, price_updates, added_rows, recode_map=None, re
|
||||
# 索引化后同一节点两笔编辑要显式合成——先改价后换码,别把价格覆盖丢掉)
|
||||
base = edits.get(s0)
|
||||
newtag = _attr_set(base[2] if base else opentag, "Number", str(new))
|
||||
_fu = (field_updates or {}).get(str(new))
|
||||
if _fu: # 改码后的节点也吃到保留码的编辑值(该单位原是旧码时)
|
||||
for attr, v in (("Name", _fu[0]), ("Specification", _fu[1]), ("Unit", _fu[2])):
|
||||
if v is not None:
|
||||
newtag = _attr_set(newtag, attr, v)
|
||||
_pu = (price_updates or {}).get(str(new))
|
||||
if _pu: # 决策 171 补(主人定「三价要同步更新」):换码节点同样
|
||||
for attr, v in (("NoTaxPrice", _pu[0]), ("TaxRate", _pu[1]), ("TaxPrice", _pu[2])):
|
||||
if v is not None: # 带上保留行三价——只换码不换价会让导出件里
|
||||
newtag = _attr_set(newtag, attr, v) # 新旧两价并存(实测:导入广联达显示 126)
|
||||
rows = (recode_children or {}).get(str(old)) or []
|
||||
if rows:
|
||||
kids = re.findall(r'<LabourMaterialsEquipmentsMachinesElement\b[^>]*/>', txt[e0:bend])
|
||||
@@ -931,6 +949,37 @@ def _child_node(tmpl, code, qty):
|
||||
return t
|
||||
|
||||
|
||||
_LME_ROW_TAG = "LabourMaterialsEquipmentsMachinesElement"
|
||||
|
||||
|
||||
def _recode_norm_lme_rows(txt, recode_map):
|
||||
"""定额消耗量行的编码替换(2026-10-22 主人定:合并改码贯通到全部定额)。
|
||||
Summary 块**外**的 LabourMaterialsEquipmentsMachinesElement = 定额下工料机行
|
||||
(清单直挂行同标签同处理);块内的是台班/配合比子级组成,已由 recode_children
|
||||
重建为保留行子级,绝不再碰。开标签一次扫描定位替换(同决策 93 索引手法),
|
||||
顺序在 transform_unit_summaries 之后:Summary 节点已换好码,这里只管行。"""
|
||||
if not recode_map:
|
||||
return txt
|
||||
spans = []
|
||||
for m in re.finditer(r'<' + _SUM_TAG + r'\b[^>]*>', txt):
|
||||
if m.group(0).rstrip().endswith('/>'):
|
||||
spans.append((m.start(), m.end()))
|
||||
else:
|
||||
e = txt.find('</' + _SUM_TAG + '>', m.end())
|
||||
if e >= 0:
|
||||
spans.append((m.start(), e + len('</' + _SUM_TAG + '>')))
|
||||
edits = []
|
||||
for m in re.finditer(r'<' + _LME_ROW_TAG + r'\b[^>]*>', txt):
|
||||
if any(s <= m.start() < e for s, e in spans):
|
||||
continue
|
||||
tag = m.group(0)
|
||||
old = _attr(tag, "Number")
|
||||
new = recode_map.get(old) if old else None
|
||||
if new and new != old:
|
||||
edits.append((m.start(), m.end(), _attr_set(tag, "Number", str(new))))
|
||||
return _apply_edits(txt, edits) if edits else txt
|
||||
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- Project.xml
|
||||
|
||||
@@ -1024,15 +1073,13 @@ def transform_project(txt, added_summaries, hidden_codes, sync_rows=None):
|
||||
if not span:
|
||||
continue
|
||||
s0, e0, _bend, opentag = span
|
||||
aggregate = _is_amount_aggregate(opentag)
|
||||
newtag = opentag
|
||||
for attr, want in (("Name", name), ("Specification", spec), ("Unit", unit),
|
||||
("NoTaxPrice", ntp), ("TaxRate", rate), ("TaxPrice", taxp)):
|
||||
cur = _attr(newtag, attr)
|
||||
if cur is None or want is None:
|
||||
continue
|
||||
if aggregate and attr in _PRICE_ATTRS:
|
||||
continue # 金额汇总行价格不回写(补记九)
|
||||
# 2026-10-22 决策 169:金额汇总行视同普通工料机,原「aggregate 价格跳过」撤除
|
||||
if attr in _PRICE_ATTRS and _num_eq(cur, want):
|
||||
continue
|
||||
if attr in ("Name", "Specification", "Unit") and (cur or "").strip() == (want or "").strip():
|
||||
@@ -1083,6 +1130,7 @@ def _source_ext(src_name):
|
||||
|
||||
def export_zip(src_bytes, proj_name, src_name, unit_plans, added_summaries, hidden_codes, res_by_norm,
|
||||
price_updates=None, added_lme_by_uw=None, sync_rows=None, recode_map=None, recode_children=None,
|
||||
field_updates=None,
|
||||
progress=None):
|
||||
"""src_bytes=原文件字节;unit_plans={uw键(Number,Name): plan};
|
||||
plan={"pairs":{...}, "news":{...}, "norms_by_bill":{...}, "added_lme":[(码,名,规格,单位,量,税率,除税,含税,类别)]}。
|
||||
@@ -1149,11 +1197,15 @@ def export_zip(src_bytes, proj_name, src_name, unit_plans, added_summaries, hidd
|
||||
added_rows = []
|
||||
for k in keys:
|
||||
added_rows.extend(unit_plans[k].get("added_lme") or [])
|
||||
# 单位汇总只接受**合并改价**(被查找对象的精准更新),
|
||||
# 全字段 sync 是项目级的事——否则零编辑也会大面积误改(已实测踩坑)
|
||||
# 单位汇总只接受**合并改价**(被查找对象的精准更新)+ 编辑过的名称/规格/单位
|
||||
# (field_updates,2026-10-22)——全字段 sync 是项目级的事——
|
||||
# 否则零编辑也会大面积误改(已实测踩坑)
|
||||
upd = {c: v for c, v in (price_updates or {}).items()}
|
||||
if upd or added_rows:
|
||||
txt = transform_unit_summaries(txt, upd, added_rows, recode_map, recode_children)
|
||||
if upd or added_rows or field_updates:
|
||||
txt = transform_unit_summaries(txt, upd, added_rows, recode_map, recode_children,
|
||||
field_updates=field_updates)
|
||||
if recode_map: # 2026-10-22:定额消耗量行编码替换(Summary 块外)
|
||||
txt = _recode_norm_lme_rows(txt, recode_map)
|
||||
raw = _encode(txt, enc)
|
||||
# 条目名按原始字节保真回写(修单位工程名乱码)
|
||||
zi = _RawNameZipInfo(item.filename, date_time=item.date_time)
|
||||
|
||||
Reference in New Issue
Block a user