# -*- coding: utf-8 -*-
"""决策 244 补记三(2026-11-01 主人定「导出=显示端所见,与源文件无关」)防回归。
钉死三件事:
①单位件:码不在现算图(没被引用)→ 汇总节点不导出——即使节点带着源文件原量;
②现算 0 的码同样不导出;现算 >0 的保留;
③项目级聚合重建:聚合中完全没有的纯项目级行不再照源文件保留(旧兜底撤除)。
"""
import sys, os
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core"))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "server"))
import export_cos as xcos # noqa: E402
S = "LabourMaterialsEquipmentsMachinesSummary"
def _node(code, qty, name="电", price="0.75"):
return ('<{S} Number="{c}" Name="{n}" Specification="" Unit="kw·h" LossRate="0"'
' Quantity="{q}" TaxRate="0" NoTaxOrgPrice="{p}" NoTaxPriceExpress="{p}"'
' NoTaxPrice="{p}" TaxOrgPrice="{p}" TaxPriceExpress="{p}" TaxPrice="{p}"'
' NoTaxOrgTotal="0" NoTaxTotal="0" TaxOrgTotal="0" TaxTotal="0" Class=""'
' Concrete="false" Kind="2" ExtKind="1" AddiKind="" MainMaterial="false"'
' ProvisionalMaterial="false" Provider="1" PriceSource="" Delivery=""'
' Location="" ProducingArea="" Supplier="" Character="" Remark=""/>').format(
S=S, c=code, n=name, q="%.4f" % qty, p=price)
def test_live_filter_drops_unreferenced_and_zero():
"""①②:源文件节点带原量(柴油-0002=13.3),但不在现算图 → 删;现算 0 → 删;现算 >0 → 留。"""
txt = ("" + _node("99450680-0002", 13.3, "柴油")
+ _node("99450700", 435.3279)
+ _node("99450700-0001", 0.0) + "")
live = {"99450700": 435.3279} # 显示端只有基码(变体零引用/0 量)
out = xcos._drop_zero_qty_summary_rows(txt, live)
assert "99450680-0002" not in out # 没被引用 → 不导(源量 13.3 也不带)
assert "99450700-0001" not in out # 现算 0 → 不导
assert 'Number="99450700"' in out # 显示端有 → 全量导出
# live=None 退老行为:只有 Quantity==0 删(老会话/链故障兼容)
out2 = xcos._drop_zero_qty_summary_rows(txt)
assert "99450680-0002" in out2 and "99450700" in out2 and "99450700-0001" not in out2
def test_rebuild_project_drops_non_displayed_rows():
"""③:聚合重建收紧——聚合中完全没有的纯项目级行不再照源文件保留。"""
unit_txt = "" + _node("99450700", 238024.1197) + ""
proj_txt = ("" + _node("99450700", 238024.1197)
+ _node("99450700-0001", 435.3279) # 源 Project 有、单位聚合没有 → 新口径删
+ _node("ZMZJRG", 10065754.48, "子目增加费") + "") # 同上(费用行)
out = xcos._rebuild_project_lme(proj_txt, [unit_txt], [], [])
assert 'Number="99450700"' in out
assert "99450700-0001" not in out # 显示端没有 → 不导
assert "ZMZJRG" not in out # 费用行现算无 → 不导(留在原文件里)
# 数量=各单位之和严格成立
import re
q = re.search(r'Number="99450700"[^>]*Quantity="([0-9.]+)"', out)
assert abs(float(q.group(1)) - 238024.1197) < 0.01