Files
pricing_review/CostReview/app/tests/test_lme_chain219.py

101 lines
6.0 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 -*-
"""决策 219(2026-10-26 主人定):工料机取价链——单位级一码一行认第一行 + 项目级全字段三价一致才归并。
钉死五件事(样本对数法,数字全部取自会话75报障形态):
① 单位级取价:每单位工程每码认导入序**第一行**(同码不同价随便认一个);
② 项目级归并:全字段(名/规/单/类别)+三价一致的单位级行并一行、量=成员单位现算量之和;
③ 不一致独立:字段或三价不同 → 各自成行(机柜 两种规格两行);s_lme_proj 异类价(3.61)不再出现;
④ 兜底:单位级行缺价 → 项目级快照补;仅项目级有的码 → 独立成行(量=现算);
⑤ 单位级卡:本单位每码一行、量=本单位现算。
"""
import sqlite3
import sys, os
_HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "core"))
import dyn_lme as dyn # noqa: E402
def _mk():
sc = sqlite3.connect(":memory:")
sc.row_factory = sqlite3.Row
c = sc.cursor()
c.execute("""CREATE TABLE s_lme_proj(id INTEGER PRIMARY KEY, session_id INT, code TEXT,
name TEXT, spec TEXT, unit TEXT, kind INT, qty REAL,
ntp REAL, taxrate REAL, taxp REAL, x_merged_hide INTEGER DEFAULT 0,
x_remark TEXT, x_ntp0 REAL, x_rate0 REAL, x_taxp0 REAL)""")
c.execute("""CREATE TABLE s_lme_uw(id INTEGER PRIMARY KEY, session_id INT, uw_id INT,
code TEXT, name TEXT, spec TEXT, unit TEXT, kind INT,
qty REAL, ntp REAL, taxrate REAL, taxp REAL)""")
c.execute("""CREATE TABLE s_lme_res(id INTEGER PRIMARY KEY, session_id INT,
p_code TEXT, res_code TEXT, qty REAL, seq INTEGER, name TEXT)""")
c.execute("""CREATE TABLE s_settings(scope TEXT NOT NULL, k TEXT NOT NULL, v TEXT,
PRIMARY KEY(scope,k))""")
c.execute("CREATE TABLE s_bills(id INTEGER PRIMARY KEY, session_id INT, uw_id INT, Quantity REAL)")
c.execute("CREATE TABLE s_norms(id INTEGER PRIMARY KEY, bill_id INT, Quantity REAL, Unit TEXT)")
c.execute("CREATE TABLE s_norm_res(id INTEGER PRIMARY KEY, norm_id INT, res_code TEXT, qty REAL)")
# 单位级行(id 序=导入序)——101/102 各有电:101 认第一行 0.77(0.75 被随便认掉)
c.executemany("""INSERT INTO s_lme_uw(id, session_id, uw_id, code, name, spec, unit, kind,
qty, ntp, taxrate, taxp) VALUES(?,1,?,?,?,?,?,?,?,?,?,?)""",
[(1, 101, "E", "电", "", "度", 3, 1.0, 0.77, 0.0, 0.77),
(2, 101, "E", "电", "", "度", 3, 2.0, 0.75, 0.0, 0.75), # 同单位第二行 → 不认
(3, 102, "E", "电", "", "度", 3, 20.0, 0.77, 0.0, 0.77),
(4, 102, "G", "镀锌扁钢", "综合", "kg", 3, 5.0, 5.59, 0.0, 5.59),
(5, 101, "G", "镀锌扁钢", "综合", "kg", 3, 5.0, 5.59, 0.0, 5.59),
(6, 101, "M", "机柜", "", "个", 3, 1.0, 100.0, 0.0, 100.0),
(7, 101, "M", "机柜", "42U", "个", 3, 1.0, 100.0, 0.0, 100.0), # 同单位第二行 → 不认
(8, 102, "M", "机柜", "24U", "个", 3, 20.0, 100.0, 0.0, 100.0),
(9, 101, "P", "型钢", "", "kg", 3, 2.0, None, None, None)]) # 缺价 → 兜底项目级
# 项目级:E 是异类价 3.61(报障形态,不得再出现);Z 仅项目级有;P 供缺价兜底
c.executemany("""INSERT INTO s_lme_proj(id, session_id, code, name, spec, unit, kind,
qty, ntp, taxrate, taxp) VALUES(?,1,?,?,?,?,?,?,?,?,?)""",
[(901, "E", "电", "", "度", 3, 999.0, 3.61, 0.0, 0.0),
(902, "Z", "支架", "", "kg", 3, 0.0, 9.9, 0.0, 9.9),
(903, "P", "型钢", "", "kg", 3, 0.0, 2.0, 0.0, 2.0)])
# 现算链:101=3、102=20(E);G 101=5/102=5;M 101=1/102=20;Z 102=10;P 101=2
c.executemany("INSERT INTO s_bills(id, session_id, uw_id, Quantity) VALUES(?,1,?,?)",
[(1, 101, 1.0), (2, 101, 1.0), (3, 102, 1.0)])
c.executemany("INSERT INTO s_norms(id, bill_id, Quantity, Unit) VALUES(?,?,?,NULL)",
[(1, 1, 1.0), (2, 2, 1.0), (3, 3, 10.0)])
c.executemany("INSERT INTO s_norm_res(norm_id, res_code, qty) VALUES(?,?,?)",
[(1, "E", 3.0), (1, "G", 5.0), (1, "M", 1.0), (1, "P", 2.0),
(3, "E", 2.0), (3, "G", 0.5), (3, "M", 2.0), (3, "Z", 1.0)])
sc.commit()
return sc
def _by_code(rows):
return {(r[1], r[2], r[3]): r for r in rows}
def test_uw_first_row_and_merge_rules():
rows = dyn.dyn_rows(_mk(), 1, None)
d = _by_code(rows)
# ② 全一致 → 并一行,量=成员单位现算和(101:3 + 102:20),价=单位级 0.77(不是项目级异类 3.61)
e = d[("E", "电", "")]
assert (e[5], e[6], e[7], e[8]) == (23.0, 0.77, 0.0, 0.77)
g = d[("G", "镀锌扁钢", "综合")]
assert (g[5], g[6]) == (10.0, 5.59)
# ③ 字段不一致 → 各自独立两行(101 认无规格 1、102 是 24U 20)
m1 = d[("M", "机柜", "")]
m2 = d[("M", "机柜", "24U")]
assert m1[5] == 1.0 and m2[5] == 20.0
# ④ 兜底:P 单位级行缺价 → 项目级 2.0 补;Z 仅项目级有 → 独立成行(量=现算 10)
p = d[("P", "型钢", "")]
assert (p[5], p[6]) == (2.0, 2.0)
z = d[("Z", "支架", "")]
assert (z[5], z[6]) == (10.0, 9.9)
# 项目级 E 只有一行(0.75 与 3.61 都不出现在结果里)
assert len([r for r in rows if r[1] == "E"]) == 1
def test_uw_view_own_rows():
rows = dyn.dyn_uw_rows(_mk(), 1, 101)
d = {(r[0], r[1]): r for r in rows}
# ①⑤ 本单位每码一行:电认第一行 0.77(0.75 不认)、机柜认无规格;量为本单位现算
assert d[("E", "电")][5] == 3.0 and d[("E", "电")][6] == 0.77
assert ("M", "机柜") in d and d[("M", "机柜")][5] == 1.0
assert d[("P", "型钢")][6] == 2.0 # 缺价兜底
assert ("Z", "支架") not in d # 本单位表无行且无现算 → 不出