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

82 lines
5.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 -*-
"""决策 220(2026-10-27 主人定):③「两级都没有→补来源行」时,带子级的母行**子级组成整套随行带入**
(来源 lme_res → 会话 s_lme_res);①②(本单位有/仅项目有)绝不动子级——
主人原话「只有新加入的带子级工料机,本项目完全没有,采纳才会补进来」。函数级直调 _adopt_summary_rows。"""
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"))
sys.path.insert(0, os.path.join(os.path.dirname(_HERE), "server"))
import api_server as srv # noqa: E402
def _mk_sc():
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_adopt_added INTEGER DEFAULT 0, x_merged_hide INTEGER DEFAULT 0, ntp_org REAL, ntp_expr TEXT, taxp_org REAL, taxp_expr TEXT, x_flags TEXT)""")
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, x_adopt_added INTEGER DEFAULT 0, ntp_org REAL, ntp_expr TEXT, taxp_org REAL, taxp_expr TEXT, x_flags TEXT)""")
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))""")
sc.commit()
return sc
def _mk_hc():
hc = sqlite3.connect(":memory:")
hc.row_factory = sqlite3.Row
c = hc.cursor()
c.execute("CREATE TABLE bills(id INTEGER PRIMARY KEY, batch_id INT, Number TEXT, Name TEXT)")
c.execute("""CREATE TABLE lmes(id INTEGER PRIMARY KEY, batch_id INT, Number TEXT, Name TEXT,
Specification TEXT, Unit TEXT, Kind REAL, Quantity REAL, TaxRate REAL,
NoTaxPrice REAL, TaxPrice REAL)""")
c.execute("""CREATE TABLE lme_res(id INTEGER PRIMARY KEY, batch_id INT, p_code TEXT,
res_code TEXT, qty REAL, seq INTEGER, name TEXT)""")
c.execute("INSERT INTO bills(id, batch_id, Number, Name) VALUES(900, 5, 'S1', '来源清单')")
# 两个来源工料机:M1=本项目完全没有的带子级母行(③+子级);M2=本项目已有同名规单(①/②,不动)
c.execute("INSERT INTO lmes(batch_id, Number, Name, Specification, Unit, NoTaxPrice, TaxPrice) "
"VALUES(5, 'M1', '灰浆搅拌机', '200L', '台班', 100.0, 100.0)")
c.execute("INSERT INTO lmes(batch_id, Number, Name, Specification, Unit, NoTaxPrice, TaxPrice) "
"VALUES(5, 'M2X', '电动夯实机', '20-62t·m', '台班', 50.0, 50.0)")
c.executemany("INSERT INTO lme_res(batch_id, p_code, res_code, qty, seq, name) VALUES(5,'M1',?,?,?,?)",
[("C1", 2.0, 1, "人工"), ("C2", 3.0, 2, "电")])
hc.commit()
return hc
def test_adopt_children_full_path():
sc, hc = _mk_sc(), _mk_hc()
hc.execute("CREATE TABLE norms(id INTEGER PRIMARY KEY, bill_id INT)")
hc.execute("CREATE TABLE norm_res(norm_id INT, res_code TEXT, qty REAL)")
hc.execute("INSERT INTO norms(id, bill_id) VALUES(10, 900)")
hc.executemany("INSERT INTO norm_res(norm_id, res_code, qty) VALUES(?,?,?)",
[(10, "M1", 0.5), (10, "M2X", 0.2)])
hc.commit()
# 本项目已有 M2 同名规单(项目级),自带子级
sc.execute("""INSERT INTO s_lme_proj(session_id, code, name, spec, unit, kind, qty, ntp, taxrate, taxp)
VALUES(1, 'M2P', '电动夯实机', '20-62t·m', '台班', 3, 1, 60, 0, 60)""")
sc.execute("INSERT INTO s_lme_res(session_id, p_code, res_code, qty, seq, name) VALUES(1,'M2P','OWN1',9,1,'本项目子级')")
sc.commit()
n, code_map = srv._adopt_summary_rows(sc, hc, 1, 101, 900)
# M1:③补两级 + 子级整套带入(决策 220)
m1 = sc.execute("SELECT ntp FROM s_lme_proj WHERE session_id=1 AND code='M1'").fetchone()
assert m1 and m1["ntp"] == 100.0
assert sc.execute("SELECT 1 FROM s_lme_uw WHERE session_id=1 AND uw_id=101 AND code='M1'").fetchone()
kids = [(r["res_code"], r["qty"]) for r in sc.execute(
"SELECT res_code, qty FROM s_lme_res WHERE session_id=1 AND p_code='M1' ORDER BY seq")]
assert kids == [("C1", 2.0), ("C2", 3.0)] # 来源子级整套进来
assert code_map.get("M1") == "M1"
# M2X:②按项目编码落地(M2P),两级零新增、子级**不动**(本项目子级 OWN1 唯一在场)
assert code_map.get("M2X") == "M2P"
assert sc.execute("SELECT COUNT(*) FROM s_lme_res WHERE session_id=1 AND p_code='M2P'").fetchone()[0] == 1
assert sc.execute("SELECT 1 FROM s_lme_res WHERE session_id=1 AND p_code='M2P' AND res_code='OWN1'").fetchone()