- 고르기 창 상태 줄 「마스터가 바뀜」 배지 걷음 · [재적용] 그대로 - B08 「구조물 조합 고르기」 칩: 「마스터가 바뀜」 배지 · 노랑 테두리 걷음 · [업데이트] = 원본이 있으면 늘 켬(다시 가져올지는 사용자가) · 호버 「원본을 다시 가져와 이 사본에 덮음」 · 글 키 둘 걷음 - 서버 `M02_Item_Copy.list_items`: `마스터바뀜`(원본 다시 모아 판 대조) 걷고 `원본없음` 만 — `source_changed` → `source_gone` - 시험: 마스터바뀜 단언만 걷음(156_2 · item_copy_complete · item_picker_detail · m02_empty_master) · 원본판 · 재적용 단언 그대로 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
217 lines
10 KiB
Python
217 lines
10 KiB
Python
"""집계 항목 원본 — 층(시스템 · 회사 · 개인)에서 항목 내용을 모음 (PLAN 21-1 · 25-1b · 25-2b).
|
|
|
|
모으는 것 = `{파일 이름: 내용}`:
|
|
`집계.json` SS 줄 + 열 정의 · `도면.json` SD · `면적.json` SA ·
|
|
`일위대가.json` = 층 `labor/<열id>.json` 문서(39-1c · 없으면 옛 UA 조합 줄 ·
|
|
규격 열 = summary_item `spec` 열 수량을 손으로 굳힘 · 704-4)
|
|
+ 담은 로직 줄(M01 시스템) + 알약(가져온 때 그대로) +
|
|
참조사본(단가승격 · 사전조합을 값으로 굳힘 · 단가산출 SK = 수식 문서 사본 따로 · 855-F B9-5) ·
|
|
`단가.json` 계산에 닿는 마스터 줄 전부(담은 로직 + 일위대가 자재 MT)
|
|
`원본판` = 모은 내용의 해시(가져온 때 기록 · 859-2 「마스터가 바뀜」 대조 걷음).
|
|
프로젝트가 고치는 값(`입력값.json` · `고른자재.json`)은 원본이 아니라 여기서 안 모음.
|
|
원본 층 자리 = `M02_Template_Layers` 경로 · 일위대가 · 단가는 M01 시스템 것.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from M02_MasterTemplete import M02_Material_Own as own
|
|
from M02_MasterTemplete import M02_Template_Layers as layers
|
|
|
|
TABLE_NAME = "구조물집계표"
|
|
#: 열 id → 스냅샷 파일(구조물 도면 · 면적산출)
|
|
PART_FILES = (("structure", "도면.json"), ("area", "면적.json"))
|
|
LABOR_KIND = "labor" # 열 id → 일위대가 문서(39-1c) — 스냅샷은 `LABOR_FILE` 에 감싸서
|
|
PRICE_FILE = "단가.json"
|
|
LABOR_FILE = "일위대가.json"
|
|
|
|
|
|
def layer_root(item: dict[str, Any]) -> Path:
|
|
"""항목 원본 층 뿌리 — 시스템 · 회사 · 개인."""
|
|
if item["layer"] == "system":
|
|
return layers.system_dir()
|
|
if item["layer"] == "company":
|
|
return layers.company_dir(item["owner"])
|
|
company, user = str(item["owner"]).split("/")
|
|
return layers.personal_dir(company, user)
|
|
|
|
|
|
def read_json(path: Path) -> Any:
|
|
try:
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
return None
|
|
|
|
|
|
def row_of(document: Any, key: str) -> dict[str, Any] | None:
|
|
rows = document.get("줄") if isinstance(document, dict) else None
|
|
for row in rows or []:
|
|
# 옛 키로도 — 회사 · 개인 층 SS 키는 새 모양(SC · SP)으로 옮기며 `옛키` 에 남김(PLAN 26-4a)
|
|
if isinstance(row, dict) and (row.get("키") == key or key in (row.get("옛키") or [])):
|
|
return row
|
|
return None
|
|
|
|
|
|
def row_path(row: dict[str, Any]) -> list[str]:
|
|
"""SS 줄의 경로(26-1) — `경로` 칸이 있으면 그것 · 옛 줄은 종류 · 분류1~3(빈 분류는 건너뜀)."""
|
|
if isinstance(row.get("경로"), list):
|
|
return [str(p) for p in row["경로"] if str(p or "").strip()]
|
|
return [str(row[k]) for k in ("종류", "분류1", "분류2", "분류3") if row.get(k)]
|
|
|
|
|
|
def item_path(item: dict[str, Any]) -> list[str]:
|
|
"""층 SS 줄의 경로 — 없으면 LookupError."""
|
|
row = row_of(read_json(layer_root(item) / layers.COMBO_FILE), str(item.get("key")))
|
|
if row is None:
|
|
raise LookupError(f"집계 항목 {item.get('key')} 이 {item.get('layer')} 층에 없음")
|
|
return row_path(row) or [str(row["키"])]
|
|
|
|
|
|
def item_name(item: dict[str, Any]) -> str:
|
|
"""층 SS 줄의 보이는 이름(경로를 이은 글) — 없으면 LookupError."""
|
|
return " ".join(item_path(item))
|
|
|
|
|
|
def version_of(content: Any) -> str:
|
|
raw = json.dumps(content, ensure_ascii=False, sort_keys=True).encode("utf-8")
|
|
return hashlib.sha256(raw).hexdigest()[:16]
|
|
|
|
|
|
def labor_rows(ua_key: str) -> dict[str, Any] | None:
|
|
"""옛 조합 줄(마스터 UA · 회사 UC · 개인 UP) + 담은 로직 줄 — labor 문서가 없는 열
|
|
(옮기기 전)."""
|
|
from M02_MasterTemplete import M02_MasterTemplete_Combo_Store as combos
|
|
|
|
ua = combos.find(ua_key)
|
|
return None if ua is None else logic_rows(ua)
|
|
|
|
|
|
def labor_doc_rows(doc: dict[str, Any]) -> dict[str, Any]:
|
|
"""일위대가 문서(39-1a) + 담은 로직 줄 + 알약(`M02_Labor_Doc.pills` · 가져온 때 그대로 —
|
|
복사본 계산이 마스터를 안 읽게) + 참조사본 `{ref_id: 문서}`(50-3 · 원본이 바뀌어도 그대로)."""
|
|
from M02_MasterTemplete import M02_Item_Sangeun as sk
|
|
from M02_MasterTemplete import M02_Labor_Doc as labor_doc
|
|
from M02_MasterTemplete import M02_Labor_Ref as refs
|
|
|
|
rows = {**logic_rows(doc), "알약": labor_doc.pills(doc)}
|
|
copies = sk.freeze(refs.frozen(doc)) # 단가산출(SK) = 수식 문서 사본(855-F B9-5)
|
|
return {**rows, "참조사본": copies} if copies else rows
|
|
|
|
|
|
def logic_rows(row: dict[str, Any]) -> dict[str, Any]:
|
|
"""`{줄: row, 빠진로직: M01 수식 문서에 없는 로직 키}` — 로직 줄 사본 안 실음(855-F B9-6 ·
|
|
셈 = 사본 `수식.json`)."""
|
|
from M02_MasterTemplete import M02_Doc_Copy as dc
|
|
|
|
have = dc.folder()
|
|
wanted = [str(entry.get("로직")) for entry in row.get("담은로직") or [] if entry.get("로직")]
|
|
return {"줄": row, "빠진로직": [key for key in wanted if key not in have]}
|
|
|
|
|
|
def formula_keys(labor: dict[str, Any] | None) -> list[str]:
|
|
"""사본 셈이 부르는 로직 키 — 담은 로직 · 굳힌 참조사본 안 로직 줄 · SK 바탕 · 수식 사본 부름."""
|
|
from M02_MasterTemplete import M02_Item_Sangeun as sk
|
|
from M02_MasterTemplete import M02_Labor_Ref as refs
|
|
|
|
doc = (labor or {}).get("줄") or {}
|
|
copies = (labor or {}).get("참조사본") or {}
|
|
keys = [
|
|
str(one["로직"])
|
|
for one in doc.get("담은로직") or []
|
|
if isinstance(one, dict) and one.get("로직")
|
|
]
|
|
return list(dict.fromkeys([*keys, *refs.frozen_logics(copies), *sk.bundle_keys(copies)[0]]))
|
|
|
|
|
|
def formula_docs(labor: dict[str, Any] | None) -> dict[str, dict]:
|
|
"""사본 수식 문서 닫힌 묶음(`수식.json` 몫) — 원본 일위대가 · 굳힌 참조사본의 수식 사본 부름(만든 때
|
|
것 · 키 겹침 = 원본 문서 것) 먼저 · 그 밖 = 지금 M01 문서(`M02_Doc_Copy.closure` · 값 사본 로직 줄
|
|
안 읽음 · 855-F B9-6)."""
|
|
from M02_MasterTemplete import M02_Doc_Copy as dc
|
|
from M02_MasterTemplete import M02_Item_Prices as prices
|
|
|
|
doc = (labor or {}).get("줄") or {}
|
|
copies = (labor or {}).get("참조사본") or {}
|
|
have: dict[str, dict] = {}
|
|
for one in [*copies.values(), doc]:
|
|
if dc.has(one):
|
|
have.update(one[dc.KEY].get("부름") or {})
|
|
return dc.closure(prices.master()[1], formula_keys(labor), have)
|
|
|
|
|
|
def build_source(item: dict[str, Any]) -> dict[str, Any]:
|
|
"""원본 층 · 마스터에서 항목 내용을 모음 — SS 줄이 없으면 LookupError."""
|
|
root = layer_root(item)
|
|
row = row_of(read_json(root / layers.COMBO_FILE), item["key"])
|
|
if row is None:
|
|
raise LookupError(f"집계 항목 {item['key']} 이 {item['layer']} 층에 없음")
|
|
column_id = str(row.get("열") or "")
|
|
table = read_json(root / "table" / f"{TABLE_NAME}.json") or {}
|
|
column = next((c for c in table.get("열") or [] if c.get("id") == column_id), None)
|
|
files: dict[str, Any] = {"집계.json": {"줄": row, "열": column}}
|
|
for kind, name in PART_FILES:
|
|
part = read_json(root / kind / f"{column_id}.json") if column_id else None
|
|
if part is not None:
|
|
files[name] = part
|
|
doc = read_json(root / LABOR_KIND / f"{column_id}.json") if column_id else None
|
|
if isinstance(doc, dict):
|
|
from M02_MasterTemplete import M02_Labor_Spec as labor_spec
|
|
|
|
labor = labor_doc_rows(labor_spec.freeze(doc, item.get("spec"))) # 고른 규격 열(704-4)
|
|
else:
|
|
labor = labor_rows(str(row["일위대가"])) if row.get("일위대가") else None
|
|
if labor is not None:
|
|
files[LABOR_FILE] = labor
|
|
files[PRICE_FILE] = price_bundle(labor, root)
|
|
from M02_MasterTemplete import M02_Doc_B09 as doc_b09
|
|
|
|
return doc_b09.attach(files) # 수식 문서 사본(802-4a · 로직 몫 · 원본판에 듦 · 855-F B9-6)
|
|
|
|
|
|
def price_bundle(labor: dict[str, Any] | None, root: Path) -> dict[str, Any]:
|
|
"""사본 단가 묶음(`단가.json`) — 수식 문서 묶음이 잇는 자원 · 표 줄 · 자재 · 고른 자재 · 사전조합
|
|
자재 · 개인 자재(`root` = 원본 층 · B09 설계내역서 서고 사본도 이 길 · 204-2) · M01 로직 줄 없음
|
|
(855-F B9-6 · 셈 = `수식.json`)."""
|
|
from M02_MasterTemplete import M02_Item_Bundle as item_bundle
|
|
from M02_MasterTemplete import M02_Item_Prices as prices
|
|
from M02_MasterTemplete import M02_Item_Sangeun as sk
|
|
from M02_MasterTemplete import M02_Labor_Fixed as fixed
|
|
from M02_MasterTemplete import M02_Labor_Ref as refs
|
|
|
|
copies = (labor or {}).get("참조사본") or {}
|
|
_logic, sk_res = sk.bundle_keys(copies) # SK 수식 사본 셈이 읽는 자원 줄(855-F B9-5)
|
|
doc = (labor or {}).get("줄") or {}
|
|
materials = [str(m.get("코드")) for m in doc.get("자재") or []]
|
|
materials += [ # 고른 로직 자재(103-1) 품목 줄도 — 복사본 계산이 그 단가를 씀
|
|
str(key) for one in doc.get("담은로직") or [] for key in (one.get("고름") or {}).values()
|
|
]
|
|
materials += refs.frozen_materials((labor or {}).get("참조사본") or {}) # 175-2 사전조합 자재
|
|
materials += fixed.link_keys(doc) # 705-1 고정 줄이 잇는 노임 · 자재 · 기계
|
|
materials += sk_res
|
|
docs = formula_docs(labor) # 855-F B9-6 — 자원 · 표 줄 = 수식 문서가 잇는 것(로직 줄 없음)
|
|
wanted = list(docs) + [code for code in materials if code]
|
|
over = copy_rows([doc, *copies.values()]) # M02 값 사본(795-4) = 만든 때 줄 먼저
|
|
bundle = prices.to_json(item_bundle.collect(docs, materials, over)) if wanted else {}
|
|
if mine := own.bundle(
|
|
[c for c in materials if own.is_own(c)], root
|
|
): # 개인 자재(148-3 · 사전조합 것도 175-2) — 원본 목록이 바뀌어도 사본 그대로
|
|
bundle[own.BUNDLE] = mine
|
|
return bundle
|
|
|
|
|
|
def copy_rows(docs: list[Any]) -> list[dict[str, Any]]:
|
|
"""문서 · 굳힌 참조사본의 로직 사본 줄(795-4 · 값 사본 = `skc.held` · 855 2-D) —
|
|
키 겹침 = 앞 문서 것."""
|
|
from M02_MasterTemplete import M02_Sangeun_Copy as skc
|
|
|
|
out: dict[str, dict] = {}
|
|
for doc in docs:
|
|
held = skc.held(doc)
|
|
for row in [*(held or {}).get("로직", []), *((held or {}).get("단가") or [])]:
|
|
out.setdefault(str(row.get("키")), row)
|
|
return list(out.values())
|