148 lines
8.1 KiB
JavaScript
148 lines
8.1 KiB
JavaScript
const EstEstimate = (() => {
|
|
let page = 0;
|
|
function usage(project) {
|
|
const sources = new Map();
|
|
for (const row of project.tables.EstiMateDat || []) {
|
|
if (!row.Code) continue;
|
|
const rows = sources.get(row.Code) || [];
|
|
rows.push(row);
|
|
sources.set(row.Code, rows);
|
|
}
|
|
return (project.tables.EstMainDat || []).flatMap((row, index) => {
|
|
if (Number(row.Level) > 0 || Number(row.LinkIndex) !== 101) return [];
|
|
const matches = sources.get(row.Code) || [];
|
|
const source =
|
|
matches.length === 1 ? EstNative.price(structuredClone(matches[0]), 101) : null;
|
|
const appliedPrice =
|
|
row.TU == null
|
|
? EstMath.units.reduce((sum, field) => sum + EstMath.num(row[field]), 0)
|
|
: EstMath.num(row.TU);
|
|
const sourcePrice = source ? EstMath.num(source.TC) : null;
|
|
const canApply =
|
|
matches.length === 1 && sourcePrice != null && Math.abs(appliedPrice - sourcePrice) > 1e-6;
|
|
return [
|
|
{
|
|
index,
|
|
Code: row.Code || "",
|
|
Name: row.Name || "",
|
|
Size: row.Size || "",
|
|
Unite: row.Unite || "",
|
|
GN: EstMath.num(row.GN),
|
|
appliedPrice,
|
|
sourcePrice,
|
|
difference: source ? appliedPrice - sourcePrice : null,
|
|
TC: EstMath.num(row.TC),
|
|
status: !matches.length ? "연결 없음" : matches.length > 1 ? "코드 중복" : "연결됨",
|
|
canApply,
|
|
},
|
|
];
|
|
});
|
|
}
|
|
function changes(project) {
|
|
const labels = { new: "신규", deleted: "삭제", changed: "변경", unchanged: "동일" };
|
|
return EstBusiness.changes(project)
|
|
.filter((row) => Number(row.LinkIndex) === 101 || Number(row.originalLinkIndex) === 101)
|
|
.map((row) => ({ ...row, status: labels[row.status] || row.status }));
|
|
}
|
|
function applyRowPrice(project, index) {
|
|
index = Number(index);
|
|
const row = project.tables?.EstMainDat?.[index];
|
|
if (!row || Number(row.Level) > 0 || Number(row.LinkIndex) !== 101)
|
|
throw Error("견적단가 내역 행을 찾을 수 없습니다.");
|
|
const sources = (project.tables?.EstiMateDat || []).filter(
|
|
(s) => s.Code && s.Code === row.Code,
|
|
);
|
|
if (!sources.length) throw Error(`견적단가 원천 자료가 없습니다: ${row.Code}`);
|
|
if (sources.length > 1) throw Error(`견적단가 원천 코드가 중복되었습니다: ${row.Code}`);
|
|
const source = EstNative.price(structuredClone(sources[0]), 101);
|
|
const sourcePrice = EstMath.num(source?.TC);
|
|
if (!Number.isFinite(sourcePrice)) throw Error("기초 단가를 계산할 수 없습니다.");
|
|
|
|
// 원천 비목 구성 (NC, GC, JC, SC)
|
|
const baseUnits = EstMath.components.map((comp) => EstMath.num(source[comp]));
|
|
// SCheck 재분류가 설정되어 있는 경우 비목 보존
|
|
const scheck = Number(row.SCheck);
|
|
if (scheck > 0) {
|
|
const targetUnit = { 1: "GU", 2: "NU", 3: "JU" }[scheck];
|
|
for (const u of EstMath.units) row[u] = u === targetUnit ? sourcePrice : 0;
|
|
} else {
|
|
// SCheck === 0 이거나 미지정인 경우 원천 비목 분할 유지 (NU, GU, JU, SU)
|
|
EstMath.units.forEach((u, i) => (row[u] = baseUnits[i]));
|
|
}
|
|
// AutoCalc가 비활성화된 수동계산 모드에서는 기존 금액을 보존하고, 활성화된 경우에만 재계산
|
|
if (project.options?.AutoCalc !== false) {
|
|
EstMath.amounts(row, project.options?.EstDigits || 0, project.options?.TotalFit);
|
|
} else {
|
|
row.TU = EstMath.units.reduce((sum, key) => sum + EstMath.num(row[key]), 0);
|
|
}
|
|
}
|
|
function applyAllPrices(project) {
|
|
const list = usage(project).filter((item) => item.canApply);
|
|
if (!list.length) throw Error("적용할 기초 단가 차이가 없습니다.");
|
|
for (const item of list) applyRowPrice(project, item.index);
|
|
return list.length;
|
|
}
|
|
function render() {
|
|
const { state, esc, number, byId } = EstApp,
|
|
isChange = state.view === "estimate-change",
|
|
rows = isChange ? changes(state.project) : usage(state.project);
|
|
page = Math.max(0, Math.min(page, Math.ceil(rows.length / 100) - 1));
|
|
const visible = rows.slice(page * 100, page * 100 + 100),
|
|
value = (v) => (v == null ? "" : number(v));
|
|
if (isChange) {
|
|
byId("work-content").innerHTML =
|
|
`<div class="business-tools"><b>견적단가 설계변경</b><span>${rows.length}건 · ${page + 1}/${Math.max(1, Math.ceil(rows.length / 100))}쪽</span><button data-estimate-page="-1" ${page === 0 ? "disabled" : ""}>이전</button><button data-estimate-page="1" ${(page + 1) * 100 >= rows.length ? "disabled" : ""}>다음</button></div><table class="sheet"><thead><tr><th>구분</th><th>코드</th><th>품명</th><th>당초 수량</th><th>변경 수량</th><th>당초 금액</th><th>변경 금액</th><th>증감</th><th>작업</th></tr></thead><tbody>${visible.map((row) => `<tr><td>${esc(row.status)}</td><td>${esc(row.Code)}</td><td>${esc(row.Name)}</td><td>${value(row.originalGN)}</td><td>${value(row.GN)}</td><td>${value(row.originalTC)}</td><td>${value(row.TC)}</td><td>${value(row.difference)}</td><td>${row.index == null ? "" : `<button data-estimate-row="${row.index}">내역 열기</button>`}</td></tr>`).join("")}</tbody></table>`;
|
|
return;
|
|
}
|
|
const canApplyCount = rows.filter((r) => r.canApply).length;
|
|
byId("work-content").innerHTML =
|
|
`<div class="business-tools"><b>견적단가 사용 내역</b><span>${rows.length}건 · ${page + 1}/${Math.max(1, Math.ceil(rows.length / 100))}쪽</span><button data-estimate-page="-1" ${page === 0 ? "disabled" : ""}>이전</button><button data-estimate-page="1" ${(page + 1) * 100 >= rows.length ? "disabled" : ""}>다음</button><button data-estimate-apply-all ${canApplyCount ? "" : "disabled"}>기초단가 일괄 적용 (${canApplyCount}건)</button></div><p>내역에 적용된 단가와 현재 견적단가 자료를 비교합니다.</p><table class="sheet"><thead><tr><th>코드</th><th>품명</th><th>규격</th><th>단위</th><th>수량</th><th>내역 단가</th><th>기초 단가</th><th>차이</th><th>금액</th><th>연결</th><th>작업</th></tr></thead><tbody>${visible.map((row) => `<tr><td>${esc(row.Code)}</td><td>${esc(row.Name)}</td><td>${esc(row.Size)}</td><td>${esc(row.Unite)}</td><td>${value(row.GN)}</td><td>${value(row.appliedPrice)}</td><td>${value(row.sourcePrice)}</td><td>${value(row.difference)}</td><td>${value(row.TC)}</td><td>${esc(row.status)}</td><td><button data-estimate-row="${row.index}">내역 열기</button>${row.canApply ? ` <button data-estimate-apply-row="${row.index}">단가 적용</button>` : ""}</td></tr>`).join("")}</tbody></table>`;
|
|
}
|
|
function install() {
|
|
document.addEventListener("click", (event) => {
|
|
const applyAll = event.target.closest("[data-estimate-apply-all]");
|
|
if (applyAll) {
|
|
try {
|
|
EstApp.change((p) => {
|
|
const count = applyAllPrices(p);
|
|
EstApp.state.notice = `견적단가 ${count}건의 기초단가를 일괄 적용했습니다.`;
|
|
});
|
|
} catch (err) {
|
|
EstApp.state.notice = err.message;
|
|
EstApp.render();
|
|
}
|
|
return;
|
|
}
|
|
const applyRow = event.target.closest("[data-estimate-apply-row]");
|
|
if (applyRow) {
|
|
const index = Number(applyRow.dataset.estimateApplyRow);
|
|
try {
|
|
EstApp.change((p) => {
|
|
applyRowPrice(p, index);
|
|
EstApp.state.notice = `행 ${index + 1}에 기초단가를 적용했습니다.`;
|
|
});
|
|
} catch (err) {
|
|
EstApp.state.notice = err.message;
|
|
EstApp.render();
|
|
}
|
|
return;
|
|
}
|
|
const step = event.target.closest("[data-estimate-page]");
|
|
if (step) {
|
|
page += Number(step.dataset.estimatePage);
|
|
render();
|
|
return;
|
|
}
|
|
const node = event.target.closest("[data-estimate-row]");
|
|
if (!node) return;
|
|
const index = Number(node.dataset.estimateRow);
|
|
if (!EstApp.state.project.tables.EstMainDat[index]) return;
|
|
EstApp.navigate("EstMainDat");
|
|
EstApp.state.page = Math.floor(index / 100);
|
|
EstApp.state.selection = new Set([index]);
|
|
EstApp.render();
|
|
});
|
|
}
|
|
return { usage, changes, render, install, applyRowPrice, applyAllPrices };
|
|
})();
|