diff --git a/construction-installment-calculator/index.html b/construction-installment-calculator/index.html index c2a0e09..6cdd2bd 100644 --- a/construction-installment-calculator/index.html +++ b/construction-installment-calculator/index.html @@ -281,11 +281,12 @@ function toThaiDate(date) { } function getInputs() { + const ngpmPct = parseFloat(document.getElementById('ngpmPct').value) || 0; return { contract: parseAmount(document.getElementById('contractAmount').value), startDate: document.getElementById('startDate').value, - ngpmPct: parseFloat(document.getElementById('ngpmPct').value) || 0, - nookPct: parseFloat(document.getElementById('nookPct').value) || 0, + ngpmPct, + nookPct: 100 - ngpmPct, }; } @@ -299,12 +300,18 @@ function recalcAll() { installments.forEach((inst, i) => { const days = parseFloat(inst.days) || 0; const pct = parseFloat(inst.percent) || 0; + const isLast = i === installments.length - 1; cumDays += days; sumPct += pct; - const total = contract > 0 ? Math.round(contract * pct / 100) : 0; - const ngpm = Math.round(total * ngpmPct / 100); - const nook = total - ngpm; + let total, ngpm, nook; + if (contract > 0) { + total = isLast ? (contract - cumTotal) : Math.round(contract * pct / 100); + ngpm = isLast ? (Math.round(contract * ngpmPct / 100) - cumNgpm) : Math.round(total * ngpmPct / 100); + nook = total - ngpm; + } else { + total = 0; ngpm = 0; nook = 0; + } cumTotal += total; cumNgpm += ngpm; cumNook += nook; const deliveryDate = hasStart ? toThaiDate(addDays(startDate, cumDays)) : '—'; @@ -360,12 +367,12 @@ function renderTable() { tr.innerHTML = `