From 19d709305072041c730d52e9174bc68fd2f7b0a2 Mon Sep 17 00:00:00 2001 From: umsangdon Date: Sat, 7 Nov 2026 07:44:29 +0900 Subject: [PATCH] fix(est): avoid reused quantity references --- .../tester/test_est_quantity_workflow.py | 9 +++++++++ tools/est-test/native-actions.js | 20 +++++++++---------- tools/est-test/native-quantity.cjs | 10 +++++++++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/resources/tester/test_est_quantity_workflow.py b/resources/tester/test_est_quantity_workflow.py index 9c1bbcd18..7429b3b17 100644 --- a/resources/tester/test_est_quantity_workflow.py +++ b/resources/tester/test_est_quantity_workflow.py @@ -127,3 +127,12 @@ def test_manual_and_selected_sheet_totals_validate_before_application(): assert result["b"]["GN"] == 9 assert result["b"]["MulCode"] == {"binary": "YWJj"} assert result["invalid"] + + +def test_new_quantity_code_does_not_reuse_existing_or_referenced_codes(): + result = run( + "math", + """[EstQuantity.newCode([{Code:'Q2',formula:'[Q1]*3'}]), + EstQuantity.newCode([{Code:'Q2',formula:'1'}]), EstQuantity.newCode([])]""", + ) + assert result == ["Q3", "Q1", "Q1"] diff --git a/tools/est-test/native-actions.js b/tools/est-test/native-actions.js index 121e90aa4..57c17ec32 100644 --- a/tools/est-test/native-actions.js +++ b/tools/est-test/native-actions.js @@ -398,17 +398,15 @@ const EstActions = (() => { }, ); else if (name === "add-quantity") - EstApp.change( - () => - project.quantities.push({ - Code: `Q${project.quantities.length + 1}`, - Name: "새 수량", - TargetCode: "", - formula: "1", - digits: 2, - }), - false, - ); + EstApp.change(() => { + project.quantities.push({ + Code: EstQuantity.newCode(project.quantities), + Name: "새 수량", + TargetCode: "", + formula: "1", + digits: 2, + }); + }, false); else if (name === "apply-quantity") { const totals = EstQuantity.totals(project, state.quantitySheet); if (confirm("연결된 내역 수량을 산출 결과로 바꾸시겠습니까?")) diff --git a/tools/est-test/native-quantity.cjs b/tools/est-test/native-quantity.cjs index 4333c7506..d0aa0b5b8 100644 --- a/tools/est-test/native-quantity.cjs +++ b/tools/est-test/native-quantity.cjs @@ -151,6 +151,14 @@ const EstQuantity = (() => { for (const row of project.tables.EstMainDat) if (quantities.has(row.Code) && !Number(row.Level)) row.GN = quantities.get(row.Code); } + function newCode(items) { + const codes = new Set(items.map((row) => row.Code)); + for (const row of items) + for (const match of String(row.formula || "").matchAll(/\[([^\]]+)\]/g)) codes.add(match[1]); + let index = 1; + while (codes.has(`Q${index}`)) index++; + return `Q${index}`; + } function selector(state) { const { esc } = EstApp; return `
`; @@ -270,6 +278,6 @@ const EstQuantity = (() => { } }); } - return { entities, fromWpt, calculate, totals, applyTotals, selector, render, install }; + return { entities, fromWpt, calculate, totals, applyTotals, newCode, selector, render, install }; })(); if (typeof module !== "undefined" && module.exports) module.exports = EstQuantity;