/* ============================================================================= * M02_MasterTemplete_Sangeun_Pick.ts * M02 단가산출(SK) 고르기(PLAN 717-4) — 위 층 SK 목록 한 벌 · 단가산출 더한줄 고르기. * · `listSangeun` = 층 목록 길(`/layers/{층}/templates` · 종류 `sangeun`)을 마스터 › 회사 › 개인 차례로 이음 · * 금액 = 서버가 붙인 1 단위 `기준금액` · 일위대가 [로직 추가] 「단가산출」 탭 · B09 재료 창고 출처가 씀 * · 고른 SK = 참조 `{층, 주인, 키: SK-…}` — 일위대가 줄은 옛 참조 줄 꼴(서버 `M02_Labor_Expand` 가 펼침) * · `openSangeunLinePicker` = 단가산출 더한줄 고르기 — 같은 [로직 추가] 창(M01 로직 · 일위대가 · 단가산출 · * M01 재료) → SK 문서 `더한줄` 한 줄(PLAN 715 이음 꼴) * ========================================================================== */ import { t as L } from "@ui/ui_template_locale"; import { call, type Layer } from "./M02_MasterTemplete_Api_Fetch"; import type { LaborRef } from "./M02_MasterTemplete_Labor_Calc"; import { openLogicPicker } from "./M02_MasterTemplete_Labor_Pick"; import { tc } from "./M02_MasterTemplete_Labor_Text"; import { pickMaterial } from "./M02_Material_Pick"; export const SANGEUN_KIND = "sangeun"; /** 층 차례 — 위에서 아래 */ export const LAYER_ORDER = ["system", "company", "personal"] as const; /** SK 목록 한 줄 */ export interface SangeunRow { ref: LaborRef; 이름: string; 제목: string | null; 단위: string | null; 구분: string | null; 상세구분: string | null; 기준금액: number | null; } interface ListRow { 종류: string; 이름: string; 제목?: string | null; 단위?: string | null; 구분?: string | null; 상세구분?: string | null; 기준금액?: number | null; } /** 위 층 SK — 층 차례(마스터 › 회사 › 개인) · 층 안은 서버 차례 */ export async function listSangeun(scopes: Omit[]): Promise { const rank = (x: Omit): number => LAYER_ORDER.indexOf(x.층 as (typeof LAYER_ORDER)[number]); const sorted = [...scopes].sort((a, b) => rank(a) - rank(b)); const lists = await Promise.all( sorted.map(async (at) => { const q = at.주인 && at.층 !== "system" ? `?owner=${encodeURIComponent(at.주인)}` : ""; const got = await call<{ 양식: ListRow[] }>(`/layers/${at.층}/templates${q}`); return got.양식 .filter((r) => r.종류 === SANGEUN_KIND) .map((r) => ({ ref: { 층: at.층, 주인: at.층 === "system" ? null : at.주인, 키: r.이름 }, 이름: r.이름, 제목: r.제목 ?? null, 단위: r.단위 ?? null, 구분: r.구분 ?? null, 상세구분: r.상세구분 ?? null, 기준금액: r.기준금액 ?? null, })); }), ); return lists.flat(); } /** 단가산출 더한줄 한 줄(PLAN 715 이음 꼴) */ export type SangeunLine = | { 종류: "로직"; 로직: string; 입력값: Record; 수량: number } | { 참조: LaborRef; 수량: number } | { 종류: "재료"; 요소: string; 이름: string | null; 단위: string | null; 수량: number }; /** 단가산출 더한줄 고르기 — `scope` = 그 SK 문서 층(고를 수 있는 서고 = 위 층만) */ export function openSangeunLinePicker( scope: { layer: Layer; owner: string | null }, done: (line: SangeunLine) => void, ): void { const element = L("M02_PickElement"); openLogicPicker((x) => done({ 종류: "로직", 로직: x.키, 입력값: {}, 수량: 1 }), { ...scope, sangeun: true, pick: (ref) => done({ 참조: ref, 수량: 1 }), kindFilter: [ { value: "logic", text: tc("Kind_Logic") }, { value: "precombo", text: L("M02_PrecomboTitle") }, { value: "element", text: element }, ], extra: [ { kind: "element", key: "element:material", tag: element, name: L("M02_PickMaterial"), sub: "", unit: "", onPick: () => pickMaterial((m) => done({ 종류: "재료", 요소: m.코드, 이름: m.이름, 단위: m.단위, 수량: 1 }), ), }, ], }); }