- B09_Estimation_Labor_Rule: `ms`(items · uses · _cover · skipped · ADDR · STEP — 줄 꼴만 읽는 작은 함수 · 셈 몸 없음)를 이 파일로 옮김 · master_formula_sum import 0 · B09_Estimation_Sangeun_Sum 도 이것을 씀 - 검증: 묶음 ① 커밋 글과 같음(B09 골든 · 792_3 · 실제 B09 해시 같음) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
8.3 KiB
Python
185 lines
8.3 KiB
Python
"""B09 대가 블록 셈법(PLAN 792-4) — 791-1 엔진 새 의미(줄 자리 · 합밖 · 번지 · 단계 · 블록 끝전 ·
|
||
결과 조정)를 쓰는 로직만 블록 줄 · 블록에 표지를 붙임(`B09_Estimation_Labor_Source._poomsem`):
|
||
|
||
줄 `place` {방법, 자리} 줄 금액 끝수(없으면 ROUNDDOWN 1)
|
||
줄 `out` true 비목 합에서 뺌(합밖 줄 · 제외 단계가 담은 줄 · 번지 · 단계엔 쌓임)
|
||
덧줄 = 수 하나 × 밑 합 꼴이면 % 줄 밑 `rows:`(그 줄 차례 · `@비목` · % = 그 수 × 100) —
|
||
밑 = `줄.'이름'` · 앞 덧줄 이름 · `번지.X.비목` · `단계.'이름'.비목` · `노무비` 등(앞 줄 합)
|
||
덧줄 = 이름 없는 수 식 = 고정 줄(수량 1 · 엔진 값)
|
||
블록 `rule` {끝전?, 조정?, 끝수?} 블록 비목 합 = `B09_Estimation_Calc_Labor.settle`
|
||
그 밖 꼴(밑마다 다른 수 · 곱 · 중간 이름) = 빈 줄 그대로(대가 단가로 못 셈 · 틀린 값 대신 빈칸).
|
||
|
||
셈 = 셈 짝(`B09_Estimation_Calc_Labor` .py · .ts)이 B09 단가로 · 옛 로직(새 칸 없음) = 표지 없음 ·
|
||
지킴 시험 = `test_792_3_engine_paths.py`(엔진 · SK · SC · B09 산근 · B09 대가 다섯 길 같은 금액).
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import importlib
|
||
from decimal import Decimal
|
||
from typing import Any
|
||
|
||
from M01_MasterData import M01_MasterData_Store as _m01 # noqa: F401 — scripts 길을 둠
|
||
|
||
md = importlib.import_module("master_doc") # 식 꼴 읽기 = 새 계산기 파서(855-F B9-3)
|
||
|
||
PART = {"노무비": "labor", "재료비": "material", "경비": "expense"}
|
||
FLAGS = ("place", "out") # 블록 줄 덧칸 — 단가산출 블록이 줄을 바꿔도 이어 받음
|
||
Refs = list[tuple[int, str | None]] # (줄 차례, 비목 | None = 줄 전체)
|
||
|
||
|
||
# ── 블록 줄 꼴 읽기(옛 `master_formula_sum` 의 줄 꼴 함수 · 셈 아님 · 855-E 2-E4 에서 옮김) ──────────
|
||
class ms: # noqa: N801 — 부르는 쪽 이름(`ms.items` …) 그대로
|
||
"""줄 꼴(`Doc_Run.row` — 호표 · 덧줄 · 단계)만 읽는 작은 함수 — 엔진 셈 몸 없음."""
|
||
|
||
ROW_KEYS = ("단계", "끝전", "조정") # 블록 칸
|
||
LINE_KEYS = ("번지", "합밖", "자리") # 호표 · 덧줄 칸
|
||
ADDR, STEP = "번지", "단계" # 덧줄 식 이름 — `번지.A01.재료비` · `단계.'소계'.계`
|
||
|
||
@staticmethod
|
||
def items(row: dict) -> list[dict]:
|
||
"""줄 차례 = 호표 하나씩 뒤 덧줄 하나씩."""
|
||
return [*row.get("호표", []), *row.get("덧줄", [])]
|
||
|
||
@staticmethod
|
||
def uses(row: dict) -> bool:
|
||
"""새 의미(번지 · 단계 · 끝전 · 조정 · 합밖 · 줄 자리)를 쓰는 블록."""
|
||
lines = ms.items(row)
|
||
return any(k in row for k in ms.ROW_KEYS) or any(
|
||
k in x for x in lines for k in ms.LINE_KEYS
|
||
)
|
||
|
||
@staticmethod
|
||
def _cover(row: dict) -> list[tuple[dict, set]]:
|
||
"""단계마다 (단계, 담은 줄 차례) — 단 1 = 제 `줄` · 단 k = 제 `줄` + 아직 안 담긴 아래 단 단계 ·
|
||
제외 단계는 위로 안 올라감."""
|
||
where = {x.get("이름"): i for i, x in enumerate(ms.items(row)) if x.get("이름")}
|
||
out, open_ = [], []
|
||
for st in row.get(ms.STEP) or []:
|
||
level = int(st.get("단") or 1)
|
||
mine = {where[n] for n in st.get("줄") or [] if n in where}
|
||
keep = []
|
||
for low, got in open_:
|
||
if low < level:
|
||
mine |= got
|
||
else:
|
||
keep.append((low, got))
|
||
open_ = keep if st.get("제외") else [*keep, (level, mine)]
|
||
out.append((st, mine))
|
||
return out
|
||
|
||
@staticmethod
|
||
def skipped(row: dict) -> set:
|
||
"""비목 합에서 빼는 줄 차례 — `합밖` 줄(번지엔 쌓음) · 제외 단계가 담은 줄."""
|
||
out = {i for i, x in enumerate(ms.items(row)) if x.get("합밖")}
|
||
for st, mine in ms._cover(row):
|
||
if st.get("제외"):
|
||
out |= mine
|
||
return out
|
||
|
||
|
||
def _f(value: Any) -> float | None:
|
||
try:
|
||
return None if value is None else float(value)
|
||
except (TypeError, ValueError):
|
||
return None
|
||
|
||
|
||
def _cost(rows: list[int], cost: str) -> Refs | None:
|
||
if cost == "계":
|
||
return [(i, None) for i in rows]
|
||
return [(i, PART[cost]) for i in rows] if cost in PART else None
|
||
|
||
|
||
def _ref(row: dict, node: tuple, n: int) -> Refs | None:
|
||
"""밑 이름 하나 → 줄 차례(n 앞만) · 모름 = None."""
|
||
lines = ms.items(row)
|
||
names = {x.get("이름"): i for i, x in reversed(list(enumerate(lines[:n]))) if x.get("이름")}
|
||
if node[0] == "var":
|
||
if node[1] in PART: # 비목 합 = 앞 줄 중 합에 드는 줄
|
||
skip = ms.skipped(row)
|
||
return [(i, PART[node[1]]) for i in range(n) if i not in skip]
|
||
return [(names[node[1]], None)] if node[1] in names else None
|
||
if node[0] != "field":
|
||
return None
|
||
head, name = node[1], node[2]
|
||
if head == ("var", "줄"):
|
||
return [(names[name], None)] if name in names else None
|
||
if head[0] != "field" or head[1][0] != "var":
|
||
return None
|
||
group, key = head[1][1], head[2]
|
||
if group == ms.ADDR:
|
||
return _cost([i for i, x in enumerate(lines[:n]) if x.get(ms.ADDR) == key], name)
|
||
if group == ms.STEP:
|
||
got = next((mine for st, mine in ms._cover(row) if st.get("이름") == key), None)
|
||
return None if got is None else _cost(sorted(i for i in got if i < n), name)
|
||
return None
|
||
|
||
|
||
def _linear(row: dict, node: tuple, n: int) -> tuple[Refs, Decimal] | None:
|
||
"""식 = 수 하나 × 밑 합 → (밑, 수) · 다른 꼴 = None."""
|
||
if node[0] in ("var", "field"):
|
||
got = _ref(row, node, n)
|
||
return None if got is None else (got, Decimal(1))
|
||
if node[0] != "bin":
|
||
return None
|
||
op, a, b = node[1], node[2], node[3]
|
||
if op in ("*", "/") and b[0] == "num" and (left := _linear(row, a, n)):
|
||
return left[0], left[1] * b[1] if op == "*" else left[1] / b[1]
|
||
if op == "*" and a[0] == "num" and (right := _linear(row, b, n)):
|
||
return right[0], a[1] * right[1]
|
||
if op == "+" and (left := _linear(row, a, n)) and (right := _linear(row, b, n)):
|
||
return (left[0] + right[0], left[1]) if left[1] == right[1] else None
|
||
return None
|
||
|
||
|
||
def _pct(row: dict, formula: str, n: int) -> tuple[str, float] | None:
|
||
"""덧줄 식 → (`rows:` 밑, %) · 못 바꿈 = None(밑 없음 · 겹친 밑 포함)."""
|
||
try:
|
||
got = _linear(row, md.parse(formula), n)
|
||
except md.DocError:
|
||
return None
|
||
if not got or not got[0] or len(set(got[0])) != len(got[0]):
|
||
return None
|
||
spec = ",".join(str(i) if part is None else f"{i}@{part}" for i, part in got[0])
|
||
return f"rows:{spec}", float(got[1] * 100)
|
||
|
||
|
||
def mark(row: dict, lines: list[dict], engine: list[dict]) -> dict | None:
|
||
"""새 의미 로직 = 블록 줄에 `place` · `out` · 덧줄 % 밑 · 상수 덧줄(제자리) → 블록 `rule` ·
|
||
옛 로직 = None."""
|
||
if not ms.uses(row):
|
||
return None
|
||
items, skip, heads = ms.items(row), ms.skipped(row), len(row.get("호표") or [])
|
||
for n, (item, line) in enumerate(zip(items, lines)):
|
||
if item.get("자리"):
|
||
line["place"] = dict(item["자리"])
|
||
if n in skip:
|
||
line["out"] = True
|
||
if n < heads:
|
||
continue
|
||
formula = str(item.get("식") or "")
|
||
part = PART.get(str(item.get("비목")), "expense")
|
||
try:
|
||
plain = _plain(md.parse(formula))
|
||
except md.DocError:
|
||
continue
|
||
value = _f(engine[n].get("금액")) if n < len(engine) else None
|
||
if plain and value is not None: # 이름 없는 수 식 = 고정 줄(수량 1)
|
||
line.update(kind="fixed", qty=1.0, own={part: value}, link={}, unit="식")
|
||
elif got := _pct(row, formula, n):
|
||
line.update(kind="pct", part=part, base=got[0], spec=formula, unit="%", qty=got[1])
|
||
else:
|
||
continue
|
||
line.pop("why", None)
|
||
return {k: row[k] for k in ("끝전", "조정", "끝수") if k in row}
|
||
|
||
|
||
def _plain(node: tuple) -> bool:
|
||
"""수 · 사칙 · 부호만 있는 식(이름 없음)."""
|
||
if node[0] == "num":
|
||
return True
|
||
if node[0] == "bin":
|
||
return _plain(node[2]) and _plain(node[3])
|
||
return node[0] == "neg" and _plain(node[1])
|