"""B09 산근 덮개(PLAN 719-3) — 이 공사 단가산출근거 고친 칸(품 · 단가)을 서버 풀이 뒤에 덧씌움. ⚠ 짝 = `B09_Estimation_Sangeun_Cover.ts`(같은 식 · 차례 · 끝수) — 한쪽을 고치면 다른 쪽도 · 거울 시험 `resources/tester/test_719_3_sangeun_cover.py`. 조작 중 = 화면이 TS 짝으로 바로 셈 · [저장] = 서버가 다시 풀이(`Sangeun.build`) 뒤 이 파일로 정본. 덮개 = 사용자 값 `sg_cover` {산근 키: {줄 열쇠 「n:이름」: {qty?, labor?, material?, expense?}}} — qty = 1 단위 품 · 비목 = 단가 · 있는 칸만 셈 값 대신 · 줄이 없거나 이름이 다르면 짝 잃음 (`sg_orphans` · 알림만 · 덮개는 남음) · 산근 키 = 로직 + 입력값 → [업데이트] 뒤에도 같은 블록. 고칠 칸(`fields`) = 노무 · 자재 · 중기 · 빈 줄 = 품 + 쓰는 비목 단가 · 하위 산근 = 품 · % 덧줄(`of` 있는 것) = 율. 다시 셈 = 덮개가 닿은 블록과 그 블록을 부르는 블록만(안 닿은 블록 = 서버 값 그대로) — 줄 금액 ROUNDDOWN(단가 × 품, 1) · 하위 산근 줄 단가 = 다시 센 그 블록 비목 합 · % 덧줄 = 서버 값 × (지금 밑수 ÷ 처음 밑수) × (지금 율 ÷ 처음 율) · 처음 밑수 · 율이 0 · 빈칸이면 밑수 × 율 ÷ 100(밑수 = 앞 줄 그 비목 합 · `people` = 노무 줄 노무비 합) · 총계 = 비목마다 ROUNDDOWN(Σ, 0) · 합계 = 셋 합 · 목록표(`sg_list`) · 빈칸 목록(`sg_gaps`) 같이. 율 식 밖 덧줄(`of` 없음 · 기계손료 · 조건식) = 서버 값 그대로. ponytail: 인건비 % 덧줄 = 노무 줄 합 비례(식이 일부 직종만 부르면 어긋남 · 저장 뒤도 같은 값) 고친 줄 = `base`(덮기 전 {qty, 고칠 비목 단가}) · `covered`(고친 칸) · 목록표 줄 `covered`(고친 줄 수) · 다시 센 블록 · 목록 줄 `raw` = 덮기 전 값(두 번 덮어도 같음 — 서버 답이 덮인 채 오면 화면 짝이 벗기고 다시 덮음). """ from __future__ import annotations import copy from typing import Any from B09_Estimation.B09_Estimation_Calc_Base import gap, num, rounddown, total PARTS = ("labor", "material", "expense") COVER_KEYS = ("qty", *PARTS) DEPTH = 8 # 하위 산근 깊이 상한(서버 풀이 4 보다 넉넉히) def line_id(n: int, line: dict[str, Any]) -> str: """덮개 줄 열쇠 — 블록 안 차례 + 이름(로직 개정으로 줄이 밀리면 짝 잃음).""" return f"{n}:{line.get('name') or ''}" def fields(line: dict[str, Any]) -> list[str]: """고칠 수 있는 칸 — 품(qty) + 단가 비목.""" kind = line.get("kind") if kind == "sangeun": return ["qty"] if kind == "extra": return ["qty"] if line.get("of") else [] if kind == "machine": return ["qty", *PARTS] if kind == "labor": return ["qty", "labor"] if kind == "material": return ["qty", *[p for p in PARTS if p in (line.get("price") or {})]] if kind == "blank": return ["qty", *[p for p in PARTS if line.get(p) is None]] return [] def _money(price: float | None, qty: float | None) -> float | None: return None if price is None or qty is None else rounddown(price * qty, 1) def _base(lines: list[dict[str, Any]], n: int, of: str) -> float | None: """% 덧줄 밑수 — 앞 줄(0 … n-1) 비목 합 · people = 노무 줄 노무비 합.""" if of == "people": return total([ln.get("labor") for ln in lines[:n] if ln.get("kind") == "labor"]) return total([ln.get(of) for ln in lines[:n]]) def _settle(block: dict[str, Any], mine: dict[str, Any], by: dict[str, dict[str, Any]]) -> None: """블록 한 장 다시 셈 — 덮개 칸 · 하위 산근 새 합 · % 덧줄 · 총계.""" first = copy.deepcopy(block["lines"]) for n, ln in enumerate(block["lines"]): cell = mine.get(line_id(n, ln)) or {} can = fields(ln) keys = [k for k in can if num(cell.get(k)) is not None] qty0, price0 = ln.get("qty"), dict(ln.get("price") or {}) if keys: ln["base"] = {"qty": qty0, **{p: price0.get(p) for p in can if p != "qty"}} ln["covered"] = keys qty = num(cell["qty"]) if "qty" in keys else qty0 kind = ln.get("kind") if kind == "sangeun": sub = by.get((ln.get("ref") or {}).get("key")) or {} price = {p: sub.get(p) for p in PARTS} ln.update(qty=qty, price=price, **{p: _money(price[p], qty) for p in PARTS}) ln["why"] = ( None if sub.get("sum") is not None else f"하위 산근 {sub.get('no', '')} 빈칸" ) elif kind == "extra": of = ln.get("of") part = next(iter(price0), None) or next((p for p in PARTS if ln.get(p) is None), None) if not of or part is None: continue now, then, value0 = _base(block["lines"], n, of), _base(first, n, of), price0.get(part) if now is None or qty is None: value = None elif value0 is not None and then and qty0: value = value0 * (now / then) * (qty / qty0) else: value = now * qty / 100 ln.update(qty=qty, price={part: value}, **{part: _money(value, 1)}) ln["why"] = None if value is not None else "밑수 빈칸" else: price = {**price0, **{p: num(cell[p]) for p in keys if p != "qty"}} for p in can: if p != "qty": ln[p] = _money(price.get(p), qty) ln.update(qty=qty, price=price) if all(ln.get(p) is not None for p in PARTS): ln["why"] = None for part in PARTS: s = total([ln.get(part) for ln in block["lines"]]) block[part] = None if s is None else rounddown(s, 0) block["sum"] = total([block[p] for p in PARTS]) block["why"] = next((ln["why"] for ln in block["lines"] if ln.get("why")), None) def _orphans(by: dict[str, dict[str, Any]], cover: dict[str, Any]) -> list[dict[str, str]]: out = [] for key, lines in cover.items(): block = by.get(key) for lid in lines if isinstance(lines, dict) else {}: n, _, name = str(lid).partition(":") ln = ( block["lines"][int(n)] if block and n.isdigit() and int(n) < len(block["lines"]) else None ) if not ln or (ln.get("name") or "") != name or not fields(ln): out.append({"key": key, "line": str(lid)}) return out def _bare(row: dict[str, Any]) -> dict[str, Any]: """덮인 목록 줄 → 덮기 전 값(`raw`).""" if "raw" not in row: return row return {**{k: v for k, v in row.items() if k not in ("raw", "covered")}, **row["raw"]} def _gaps(blocks: list[dict[str, Any]]) -> list[dict[str, str]]: return [ gap("sg", b["key"], f"산근 {b['no']} {b['name']}", b["why"] or "단가 빈칸") for b in blocks if b["sum"] is None ] def cover_sangeun(sg: dict[str, Any], cover: Any) -> dict[str, Any]: """산근 답(`Sangeun.collect` 꼴) + 덮개 → 새 답(받은 답 안 고침) · 덮개 없으면 그대로 · 이미 덮인 답(서버 셈 답) = 블록 · 목록 줄 `raw`(덮기 전 값)로 벗긴 뒤 다시 덮음.""" cover = cover if isinstance(cover, dict) else {} worn = sg.get("sg") or [] blocks = [b["raw"] if "raw" in b else b for b in worn] orphans = _orphans({b["key"]: b for b in blocks}, cover) if not any(cover.get(b["key"]) for b in blocks): if not any("raw" in b for b in worn): return {**sg, "sg_orphans": orphans} rows = [_bare(r) for r in sg.get("sg_list") or []] return { **sg, "sg": blocks, "sg_list": rows, "sg_gaps": _gaps(blocks), "sg_orphans": orphans, } out = copy.deepcopy(blocks) by = {b["key"]: b for b in out} hit: dict[str, bool] = {} def touch(key: str, depth: int) -> bool: if key in hit: return hit[key] block = by.get(key) if block is None or depth > DEPTH: return False hit[key] = False # 돎 막음 subs = [ touch(ln["ref"]["key"], depth + 1) for ln in block["lines"] if ln.get("kind") == "sangeun" and (ln.get("ref") or {}).get("key") ] hit[key] = bool(cover.get(key)) or any(subs) if hit[key]: raw = copy.deepcopy(block) _settle(block, cover.get(key) or {}, by) block["raw"] = raw return hit[key] for b in out: touch(b["key"], 0) listing = [] for row in map(_bare, sg.get("sg_list") or []): b = by.get(row["key"]) if b is None or not hit.get(row["key"]): listing.append(row) continue n = sum(1 for ln in b["lines"] if ln.get("covered")) sums = {k: b[k] for k in (*PARTS, "sum")} raw = {k: row.get(k) for k in (*PARTS, "sum")} listing.append({**row, **sums, **({"covered": n} if n else {}), "raw": raw}) return {**sg, "sg": out, "sg_list": listing, "sg_gaps": _gaps(out), "sg_orphans": orphans}