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

102 lines
5.2 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 -*-
"""原数据树清单单价现算(决策 232,2026-10-28 主人定)。
口径:清单单价=∑定额合价÷清单工程量(四舍五入 2 位)、清单合价=单价×清单量;
定额合价=显示工程量×单价现算(公式行用求值量,采纳定额同样算);
清单工程量=0 → 量/单价/合价三值 None(前端留空)、不算不报错;
临删定额不进 ∑(导出时真实省略);无定额清单(手工)照捕获值。
"""
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 = sqlite3.connect(":memory:")
sc.row_factory = sqlite3.Row
c = sc.cursor()
c.execute("CREATE TABLE sessions(id INTEGER PRIMARY KEY, name TEXT, src_name TEXT)")
c.execute("""CREATE TABLE s_unit_works(id INTEGER PRIMARY KEY, session_id INT,
section_id INT, Number TEXT, Name TEXT, Total REAL, Specialty TEXT)""")
c.execute("""CREATE TABLE s_divides(id INTEGER PRIMARY KEY, session_id INT, uw_id INT,
parent_id INT, Kind TEXT, Number TEXT, Name TEXT, Total REAL, Code TEXT, x_seq INT)""")
c.execute("""CREATE TABLE s_bills(id INTEGER PRIMARY KEY, session_id INT, uw_id INT,
div_id INT, Number TEXT, Name TEXT, Attr TEXT, Unit TEXT,
Quantity REAL, Price REAL, Total REAL, Kind TEXT, src_remark TEXT,
x_new INT, x_tdel INT, x_adopt INT, x_seq INT, x_remark TEXT)""")
c.execute("""CREATE TABLE s_norms(id INTEGER PRIMARY KEY, bill_id INT,
Number TEXT, Name TEXT, Unit TEXT, Quantity REAL, Price REAL, Total REAL,
x_tdel INT, x_adopt INT, qty_expr TEXT)""")
c.execute("INSERT INTO sessions VALUES(1,'工程甲','H_甲.cos4')")
c.execute("INSERT INTO s_unit_works VALUES(10, 1, NULL, 'UW-1', '土建', 660, '建筑')")
# B1 清单量 100,两条定额:429 + 121 = 550 → 单价 5.50、合价 550.00
c.execute("INSERT INTO s_bills VALUES(1,1,10,NULL,'B1','挖方',NULL,'m3',100,4.29,429,NULL,NULL,0,0,0,1,NULL)")
c.execute("INSERT INTO s_norms VALUES(11,1,'D1','挖槽','1000m3',0.429,1000,429,0,0,NULL)")
c.execute("INSERT INTO s_norms VALUES(12,1,'D2','运土','1000m3',0.121,1000,121,0,0,NULL)")
# B2 清单量 0:三值全 None
c.execute("INSERT INTO s_bills VALUES(2,1,10,NULL,'B2','空量',NULL,'m2',0,3.5,0,NULL,NULL,0,0,0,2,NULL)")
c.execute("INSERT INTO s_norms VALUES(21,2,'D3','垫层','10m2',5,70,350,0,0,NULL)")
# B3 清单量 100,临删定额 500 不进 ∑,有效定额 300 → 单价 3.00
c.execute("INSERT INTO s_bills VALUES(3,1,10,NULL,'B3','回填',NULL,'m3',100,5,500,NULL,NULL,0,0,0,3,NULL)")
c.execute("INSERT INTO s_norms VALUES(31,3,'D4','临删行','10m3',5,100,500,1,0,NULL)")
c.execute("INSERT INTO s_norms VALUES(32,3,'D5','有效行','10m3',3,100,300,0,1,NULL)")
# B4 公式定额:GCL*0.5 → 50×2=100 → 单价 1.00
c.execute("INSERT INTO s_bills VALUES(4,1,10,NULL,'B4','公式',NULL,'m2',100,1,100,NULL,NULL,0,0,0,4,NULL)")
c.execute("INSERT INTO s_norms VALUES(41,4,'D6','公式行','10m2',NULL,2,NULL,0,1,'GCL*0.5')")
# B5 手工清单(无定额):照捕获值
c.execute("INSERT INTO s_bills VALUES(5,1,10,NULL,'B5','手工',NULL,'项',3,12.34,37.02,NULL,NULL,0,0,0,5,NULL)")
# B6 单定额四舍五入半升:0.535×5=2.675 → 2.68(银行家舍入会得 2.67)
c.execute("INSERT INTO s_bills VALUES(6,1,10,NULL,'B6','半升',NULL,'t',1,2.67,2.67,NULL,NULL,0,0,0,6,NULL)")
c.execute("INSERT INTO s_norms VALUES(61,6,'D7','半发行','t',0.535,5,2.67,0,0,NULL)")
sc.commit()
return sc
def _bill(d, bid):
return next(b for b in d["loose"] if b["id"] == bid)
def test_price_from_norm_totals():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b1 = _bill(d, 1)
assert (b1["Price"], b1["Total"]) == (5.5, 550.0)
tots = sorted(n[6] for n in b1["norms"])
assert tots == [121.0, 429.0] # 定额合价现算,弃显库存值
def test_zero_qty_all_none():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b2 = _bill(d, 2)
assert b2["Quantity"] is None and b2["Price"] is None and b2["Total"] is None
def test_tdel_norm_excluded():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b3 = _bill(d, 3)
assert (b3["Price"], b3["Total"]) == (3.0, 300.0)
def test_formula_norm_evaluated():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b4 = _bill(d, 4)
# 决策 216:GCL*0.5 先按清单量 100 求值=50,再 ÷ 单位因子(10m2→10)=5;合价=5×2=10
assert (b4["Price"], b4["Total"]) == (0.1, 10.0)
assert b4["norms"][0][6] == 10.0 # 公式行合价=求值量5×价2
def test_manual_bill_keeps_captured():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b5 = _bill(d, 5)
assert (b5["Quantity"], b5["Price"], b5["Total"]) == (3, 12.34, 37.02)
def test_round_half_up():
d = srv._raw_uw(_mk(), srv.TBL["session"], 10, "item")
b6 = _bill(d, 6)
assert (b6["Price"], b6["Total"]) == (2.68, 2.68)