331 lines
11 KiB
JavaScript
331 lines
11 KiB
JavaScript
// JDLCHK.dll field declarations: 4e926c..4fb010; EST writer e44f28/e4739c/e541ec.
|
|
const { pack, unpack } = require("./archive.cjs");
|
|
const { cp949 } = require("./export-estx.cjs");
|
|
const schema = require("./jdl-schema.json");
|
|
const decoder = new TextDecoder("euc-kr", { fatal: true });
|
|
const num = (v) => {
|
|
const n = Number(v || 0);
|
|
if (!Number.isFinite(n)) throw Error("JDL 수치 오류");
|
|
return n;
|
|
};
|
|
function text(v, max = 40) {
|
|
const s = String(v ?? "");
|
|
if (/[\x00-\x1f]/.test(s)) throw Error("JDL 문자열에 제어문자가 있습니다.");
|
|
if (cp949(s).length > max) throw Error(`JDL 문자열 길이 ${max}바이트 초과: ${s}`);
|
|
return s;
|
|
}
|
|
function encode(name, rows) {
|
|
const fields = schema[name];
|
|
return cp949(
|
|
rows
|
|
.map(
|
|
(row) =>
|
|
fields
|
|
.map((f) => {
|
|
const v = row[f.name];
|
|
return f.kind === 6 ? String(num(v)) : text(v, f.kind === 16 ? 100000 : f.size || 1);
|
|
})
|
|
.join("\t") + "\x05\r\n",
|
|
)
|
|
.join(""),
|
|
);
|
|
}
|
|
function parse(name, bytes) {
|
|
const fields = schema[name];
|
|
if (!fields || !bytes) return [];
|
|
return decoder
|
|
.decode(bytes)
|
|
.split(/\r?\n/)
|
|
.filter((v) => v.trim())
|
|
.map((line, i) => {
|
|
const values = line
|
|
.replace(/\x05\s*$/, "")
|
|
.split("\t")
|
|
.map((v) => v.trim());
|
|
if (values.length !== fields.length)
|
|
throw Error(
|
|
`${name} ${i + 1}행: ${fields.length}개 필드가 필요합니다 (${values.length}개).`,
|
|
);
|
|
return Object.fromEntries(
|
|
fields.map((f, j) => [
|
|
f.name,
|
|
f.kind === 6 ? num(values[j]) : text(values[j], f.kind === 16 ? 100000 : f.size || 1),
|
|
]),
|
|
);
|
|
});
|
|
}
|
|
function output(project) {
|
|
const tables = Object.fromEntries(Object.keys(schema).map((k) => [k, []])),
|
|
warnings = [],
|
|
map = new Map();
|
|
let serial = 0;
|
|
function source(kind, row) {
|
|
const key = kind + ":" + row.Code;
|
|
if (map.has(key)) return map.get(key);
|
|
const prefix = kind === 5 ? "M" : kind === 6 ? "L" : "W",
|
|
code = prefix + String(++serial).padStart(10, "0");
|
|
const price =
|
|
kind === 5
|
|
? { F_MPRICE: num(row.TC) }
|
|
: kind === 6
|
|
? { F_LPRICE: num(row.TC) }
|
|
: { F_MPRICE: num(row.GC), F_LPRICE: num(row.NC), F_EPRICE: num(row.JC) + num(row.SC) };
|
|
const name = prefix === "M" ? "m.txt" : prefix === "L" ? "l.txt" : "w.txt";
|
|
tables[name].push({
|
|
F_CODE: code,
|
|
F_DES: row.Name,
|
|
F_SIZE: row.Size,
|
|
F_UNIT: row.Unite,
|
|
...price,
|
|
F_ORICODE: "00000000000",
|
|
});
|
|
map.set(key, code);
|
|
return code;
|
|
}
|
|
for (const [kind, table] of [
|
|
[5, "StuffcDat"],
|
|
[6, "LaborcDat"],
|
|
[21, "SilJukDat"],
|
|
[101, "EstiMateDat"],
|
|
])
|
|
for (const row of project.tables[table] || []) source(kind, row);
|
|
for (const table of ["CostTitleDat", "MachTitleDat", "HeadTitleDat"])
|
|
for (const row of project.tables[table] || [])
|
|
source({ CostTitleDat: 104, MachTitleDat: 105, HeadTitleDat: 103 }[table], row);
|
|
let group = null,
|
|
groupNumber = 0,
|
|
sequence = 0;
|
|
function addGroup(name) {
|
|
if (++groupNumber > 99) throw Error("JDL 단일 레벨 변환은 공종 99개까지 가능합니다.");
|
|
group = {
|
|
F_CODE: String(groupNumber).padStart(2, "0"),
|
|
F_DES: text(name),
|
|
F_QQTY: 1,
|
|
F_MPRICE: 0,
|
|
F_LPRICE: 0,
|
|
F_EPRICE: 0,
|
|
F_ORICODE: "00000000000",
|
|
};
|
|
tables["gongjong.txt"].push(group);
|
|
sequence = 0;
|
|
}
|
|
for (const row of project.tables.EstMainDat) {
|
|
if (num(row.LinkIndex) === 54) break;
|
|
if (num(row.Level) > 0) {
|
|
addGroup(row.Name);
|
|
continue;
|
|
}
|
|
if (!group) addGroup(project.name);
|
|
if (num(row.CalcIndex) >= 2) {
|
|
warnings.push(`${row.Code || row.Name}: 합산 제외 행 생략`);
|
|
continue;
|
|
}
|
|
let key = map.get(num(row.LinkIndex) + ":" + row.Code),
|
|
gn = num(row.GN),
|
|
m = num(row.GU),
|
|
l = num(row.NU),
|
|
e = num(row.JU) + num(row.SU);
|
|
if (
|
|
!key ||
|
|
num(row.CalcIndex) === 1 ||
|
|
num(row.CalcIndex) === 3 ||
|
|
Math.abs(gn * (m + l + e) - num(row.TC)) > 0.000001
|
|
) {
|
|
key = source(101, {
|
|
Code: "LITERAL" + ++serial,
|
|
Name: row.Name,
|
|
Size: row.Size,
|
|
Unite: row.Unite,
|
|
GC: row.GC,
|
|
NC: row.NC,
|
|
JC: row.JC,
|
|
SC: row.SC,
|
|
});
|
|
gn = 1;
|
|
m = num(row.GC);
|
|
l = num(row.NC);
|
|
e = num(row.JC) + num(row.SC);
|
|
warnings.push(`${row.Code || row.Name}: 저장 금액을 독립 항목으로 전개`);
|
|
}
|
|
tables["mainpart.txt"].push({
|
|
F_CODE: group.F_CODE,
|
|
F_SEQ: String(++sequence),
|
|
F_CODE1: key,
|
|
F_DES: row.Name,
|
|
F_SIZE: row.Size,
|
|
F_UNIT: row.Unite,
|
|
F_QQTY: gn,
|
|
F_MPRICE: m,
|
|
F_LPRICE: l,
|
|
F_EPRICE: e,
|
|
F_SPROPER: "0",
|
|
F_LOADBM: "T",
|
|
F_SAVEBM: "T",
|
|
F_SRATE: 0,
|
|
F_KWAN: "0",
|
|
F_CLASS: "0",
|
|
F_ORICODE: "00000000000",
|
|
});
|
|
group.F_MPRICE += num(row.GC);
|
|
group.F_LPRICE += num(row.NC);
|
|
group.F_EPRICE += num(row.JC) + num(row.SC);
|
|
}
|
|
if (!tables["gongjong.txt"].length) addGroup(project.name);
|
|
const direct = tables["gongjong.txt"].reduce(
|
|
(a, r) => ({ m: a.m + r.F_MPRICE, l: a.l + r.F_LPRICE, e: a.e + r.F_EPRICE }),
|
|
{ m: 0, l: 0, e: 0 },
|
|
);
|
|
const global = `[BASIC]\r\nMAKER=ESTWEB\r\nVERSION=2.00\r\nCDATE=${new Date().toISOString().slice(0, 10).replaceAll("-", "")}\r\n[CPART]\r\nMQBIT=7C\r\nMABIT=0C\r\nMTBIT=0C\r\nMCMETHOD=0LME\r\nUABIT=0C\r\nUTBIT=0C\r\nUCMETHOD=0LME\r\nDCMETHOD=0LME\r\nJABIT=0C\r\nJTBIT=0C\r\nJCMETHOD=0LME\r\nDQBIT=2R\r\nDABIT=0C\r\nDTBIT=0C\r\n[JPART]\r\n[VPART]\r\n[COST]\r\nMCOST=${direct.m}\r\nLCOST=${direct.l}\r\nECOST=${direct.e}\r\nTCOST=${direct.m + direct.l + direct.e}\r\n`;
|
|
const files = new Map([
|
|
["Global.ini", cp949(global)],
|
|
["Main.txt", cp949(["0", text(project.name, 80), "02"].join("\t") + "\x05\r\n")],
|
|
]);
|
|
for (const [name, rows] of Object.entries(tables)) files.set(name, encode(name, rows));
|
|
const fees = (project.tables.Wonga || [])
|
|
.map(
|
|
(r) =>
|
|
[
|
|
text(r.Gubun, 12),
|
|
text(r.Name),
|
|
text(r.Youl, 25),
|
|
text(r.VSusic, 200),
|
|
String(num(r.Cost)),
|
|
].join("\t") + "\x05\r\n",
|
|
)
|
|
.join("");
|
|
files.set("CostAccount.txt", cp949(fees));
|
|
warnings.unshift(
|
|
"JDL의 기본 표로 금액을 전개합니다. 산근·기계·일위의 원본 편집 산식과 관급/특수 업무 구조는 JSON/ESTX에 보관하세요. 원본 검사 DLL의 인증은 미확인입니다.",
|
|
);
|
|
return { bytes: pack(files), warnings };
|
|
}
|
|
function read(bytes, project) {
|
|
const files = new Map([...unpack(bytes)].map(([k, v]) => [k.toLowerCase(), v]));
|
|
if (!files.has("global.ini") || !files.has("main.txt"))
|
|
throw Error("Global.ini/Main.txt 없는 JDL입니다.");
|
|
const main = decoder
|
|
.decode(files.get("main.txt"))
|
|
.replace(/\x05\s*$/, "")
|
|
.split("\t");
|
|
if (main.length !== 3) throw Error("JDL Main.txt 필드 오류");
|
|
project.name = main[1].trim();
|
|
project.options.AutoCalc = false;
|
|
project.options.SourceRefresh = false;
|
|
const rows = Object.fromEntries(
|
|
Object.keys(schema).map((name) => [name, parse(name, files.get(name))]),
|
|
);
|
|
const byCode = new Map();
|
|
for (const [file, table, kind] of [
|
|
["m.txt", "StuffcDat", 5],
|
|
["l.txt", "LaborcDat", 6],
|
|
["e.txt", "EstiMateDat", 101],
|
|
["w.txt", "EstiMateDat", 101],
|
|
["j.txt", "EstiMateDat", 101],
|
|
["unit.txt", "HeadTitleDat", 103],
|
|
["junit.txt", "MachTitleDat", 105],
|
|
["danga.txt", "CostTitleDat", 104],
|
|
])
|
|
for (const r of rows[file]) {
|
|
if (byCode.has(r.F_CODE)) throw Error("JDL 자원 코드 중복: " + r.F_CODE);
|
|
const row = {
|
|
PkID: project.tables[table].length + 1,
|
|
Code: r.F_CODE,
|
|
Name: r.F_DES,
|
|
Size: r.F_SIZE,
|
|
Unite: r.F_UNIT,
|
|
GC: num(r.F_MPRICE),
|
|
NC: num(r.F_LPRICE),
|
|
JC: num(r.F_EPRICE),
|
|
SC: 0,
|
|
};
|
|
row.TC = row.InputTC = row.GC + row.NC + row.JC;
|
|
row.ValueIndex = 4;
|
|
project.tables[table].push(row);
|
|
byCode.set(row.Code, { row, kind });
|
|
}
|
|
const headings = new Set();
|
|
let pk = 0;
|
|
for (const group of rows["gongjong.txt"]) {
|
|
if (headings.has(group.F_CODE)) throw Error("JDL 공종 코드 중복");
|
|
headings.add(group.F_CODE);
|
|
project.tables.EstMainDat.push({
|
|
PkID: ++pk,
|
|
Level: 1,
|
|
Code: group.F_CODE,
|
|
Name: group.F_DES,
|
|
GC: group.F_MPRICE,
|
|
NC: group.F_LPRICE,
|
|
JC: group.F_EPRICE,
|
|
SC: 0,
|
|
TC: group.F_MPRICE + group.F_LPRICE + group.F_EPRICE,
|
|
});
|
|
for (const r of rows["mainpart.txt"].filter((r) => r.F_CODE === group.F_CODE)) {
|
|
const source = byCode.get(r.F_CODE1);
|
|
if (!source) throw Error("JDL 연결 자원 없음: " + r.F_CODE1);
|
|
const row = {
|
|
PkID: ++pk,
|
|
Code: r.F_CODE1,
|
|
Name: r.F_DES,
|
|
Size: r.F_SIZE,
|
|
Unite: r.F_UNIT,
|
|
GN: r.F_QQTY,
|
|
GU: r.F_MPRICE,
|
|
NU: r.F_LPRICE,
|
|
JU: r.F_EPRICE,
|
|
SU: 0,
|
|
LinkIndex: source.kind,
|
|
CalcIndex: 0,
|
|
};
|
|
row.GC = Math.trunc(row.GN * row.GU);
|
|
row.NC = Math.trunc(row.GN * row.NU);
|
|
row.JC = Math.trunc(row.GN * row.JU);
|
|
row.SC = 0;
|
|
row.TC = row.GC + row.NC + row.JC;
|
|
row.TU = row.GU + row.NU + row.JU;
|
|
project.tables.EstMainDat.push(row);
|
|
}
|
|
}
|
|
if (rows["mainpart.txt"].some((r) => !headings.has(r.F_CODE))) throw Error("JDL 부모 공종 없음");
|
|
const direct = project.tables.EstMainDat.filter((r) => !r.Level),
|
|
sum = (k) => direct.reduce((a, r) => a + num(r[k]), 0);
|
|
project.tables.EstMainDat.push({
|
|
PkID: ++pk,
|
|
Name: "순공사비",
|
|
LinkIndex: 54,
|
|
GC: sum("GC"),
|
|
NC: sum("NC"),
|
|
JC: sum("JC"),
|
|
SC: 0,
|
|
TC: sum("TC"),
|
|
});
|
|
if (files.has("costaccount.txt")) {
|
|
const lines = decoder
|
|
.decode(files.get("costaccount.txt"))
|
|
.split(/\r?\n/)
|
|
.filter((s) => s.trim());
|
|
for (const line of lines) {
|
|
const f = line.replace(/\x05\s*$/, "").split("\t");
|
|
if (f.length < 5) throw Error("JDL 원가 필드 오류");
|
|
project.tables.Wonga.push({
|
|
Gubun: f[0].trim(),
|
|
Name: f[1].trim(),
|
|
Youl: f[2].trim(),
|
|
VSusic: f[3].trim(),
|
|
Cost: num(f[4]),
|
|
});
|
|
}
|
|
}
|
|
project.originalJdl = bytes.toString("base64");
|
|
project.importWarnings = [
|
|
"JDL 저장 금액을 보존하기 위해 자동계산을 껐습니다. 복잡한 단가산출식·손료/기성 확장 파일은 원문에 보존되며 자동 변환하지 않습니다.",
|
|
];
|
|
project.calculation = {
|
|
errors: [],
|
|
warnings: project.importWarnings,
|
|
direct: project.tables.EstMainDat.at(-1),
|
|
total: project.tables.Wonga.at(-1)?.Cost ?? sum("TC"),
|
|
};
|
|
return project;
|
|
}
|
|
module.exports = { output, read, parse };
|