feat: add construction installment calculator (single HTML)
This commit is contained in:
@@ -0,0 +1,495 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="th">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>คำนวณงวดงานสิ่งก่อสร้าง</title>
|
||||
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
|
||||
<style>
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: 'Sarabun', 'Segoe UI', sans-serif;
|
||||
background: #f0f2f5;
|
||||
padding: 20px;
|
||||
font-size: 14px;
|
||||
color: #222;
|
||||
}
|
||||
.page {
|
||||
max-width: 1200px;
|
||||
margin: auto;
|
||||
background: #fff;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 2px 12px rgba(0,0,0,0.1);
|
||||
overflow: hidden;
|
||||
}
|
||||
.page-header {
|
||||
background: #1a56a0;
|
||||
color: white;
|
||||
padding: 18px 28px;
|
||||
}
|
||||
.page-header h1 { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
|
||||
.page-header p { font-size: 13px; opacity: 0.85; }
|
||||
|
||||
/* Form */
|
||||
.form-section {
|
||||
background: #f7f9fc;
|
||||
border-bottom: 1px solid #e0e6ef;
|
||||
padding: 18px 28px;
|
||||
}
|
||||
.form-row { display: flex; gap: 20px; align-items: flex-end; flex-wrap: wrap; }
|
||||
.form-group { display: flex; flex-direction: column; gap: 5px; }
|
||||
.form-group label {
|
||||
font-size: 11px; color: #555; font-weight: 700;
|
||||
text-transform: uppercase; letter-spacing: 0.5px;
|
||||
}
|
||||
.form-group input[type="text"],
|
||||
.form-group input[type="number"],
|
||||
.form-group input[type="date"] {
|
||||
border: 1.5px solid #c5d3e8;
|
||||
border-radius: 6px;
|
||||
padding: 8px 12px;
|
||||
font-size: 14px;
|
||||
background: white;
|
||||
color: #333;
|
||||
font-family: inherit;
|
||||
}
|
||||
.form-group input[type="number"] { width: 90px; }
|
||||
.form-group input[type="date"] { width: 160px; }
|
||||
.form-group input#contractAmount { width: 180px; }
|
||||
.ratio-group { display: flex; align-items: center; gap: 6px; }
|
||||
.ratio-group span { font-size: 12px; color: #666; }
|
||||
|
||||
/* Buttons */
|
||||
.btn {
|
||||
border: none; border-radius: 6px; padding: 9px 20px;
|
||||
font-size: 14px; font-weight: 600; cursor: pointer; font-family: inherit;
|
||||
}
|
||||
.btn-calc { background: #1a56a0; color: white; }
|
||||
.btn-export { background: #217346; color: white; }
|
||||
.btn-export:disabled { background: #aaa; cursor: not-allowed; }
|
||||
.btn-add {
|
||||
background: #e8f0fb; color: #1a56a0;
|
||||
border: 1.5px solid #c5d3e8; border-radius: 5px;
|
||||
padding: 5px 14px; font-size: 13px; cursor: pointer;
|
||||
font-family: inherit; font-weight: 600;
|
||||
}
|
||||
.btn-del {
|
||||
background: none; border: none; color: #cc3333;
|
||||
font-size: 16px; cursor: pointer; padding: 0 6px; line-height: 1;
|
||||
}
|
||||
|
||||
/* Summary cards */
|
||||
.table-section { padding: 20px 28px 28px; }
|
||||
.summary-row { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
|
||||
.summary-card {
|
||||
background: #f0f6ff; border: 1px solid #c5d3e8;
|
||||
border-radius: 8px; padding: 10px 16px; min-width: 150px;
|
||||
}
|
||||
.summary-card .lbl { font-size: 11px; color: #666; font-weight: 600; }
|
||||
.summary-card .val { font-size: 15px; font-weight: 700; color: #1a56a0; margin-top: 3px; }
|
||||
.summary-card.warn .val { color: #c0392b; }
|
||||
.summary-card.ok .val { color: #217346; }
|
||||
|
||||
/* Table toolbar */
|
||||
.table-toolbar {
|
||||
display: flex; justify-content: space-between;
|
||||
align-items: center; margin-bottom: 12px;
|
||||
}
|
||||
.legend { display: flex; gap: 14px; font-size: 12px; color: #666; align-items: center; }
|
||||
.legend-box {
|
||||
width: 13px; height: 13px; border-radius: 3px;
|
||||
display: inline-block; vertical-align: middle; margin-right: 4px;
|
||||
}
|
||||
|
||||
/* Table */
|
||||
.table-wrapper { overflow-x: auto; }
|
||||
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
||||
th {
|
||||
background: #1a56a0; color: white;
|
||||
padding: 8px 8px; text-align: center;
|
||||
white-space: nowrap; font-size: 12px;
|
||||
}
|
||||
th.sub { background: #2a6abf; }
|
||||
th.editable-col { background: #c0832a; }
|
||||
td {
|
||||
padding: 6px 8px;
|
||||
border-bottom: 1px solid #e8edf5;
|
||||
text-align: right; white-space: nowrap;
|
||||
}
|
||||
td.center { text-align: center; }
|
||||
td.editable-cell { background: #fffbf0; text-align: center; padding: 3px 5px; }
|
||||
td.editable-cell input {
|
||||
border: 1.5px solid #f0c040; border-radius: 4px;
|
||||
padding: 4px 5px; width: 62px; text-align: center;
|
||||
font-size: 13px; font-family: inherit;
|
||||
}
|
||||
td.editable-cell input:focus { outline: none; border-color: #e67e22; }
|
||||
td.acc { background: #f0f6ff; font-weight: 600; color: #1a56a0; }
|
||||
td.calc { color: #333; }
|
||||
tr:nth-child(even) td { background: #fafcff; }
|
||||
tr:nth-child(even) td.editable-cell { background: #fffdf0; }
|
||||
tr:nth-child(even) td.acc { background: #e8f0fb; }
|
||||
tr:hover td { background: #f0f6ff !important; }
|
||||
tr.total-row td {
|
||||
background: #dce8f8 !important;
|
||||
font-weight: 700; color: #1a56a0;
|
||||
border-top: 2px solid #1a56a0;
|
||||
}
|
||||
.pct-status { font-size: 11px; font-weight: 700; margin-left: 6px; }
|
||||
.pct-warn { color: #c0392b; }
|
||||
.pct-ok { color: #217346; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="page">
|
||||
<div class="page-header">
|
||||
<h1>การคำนวณงวดงานสิ่งก่อสร้าง</h1>
|
||||
<p>กรอกข้อมูล กำหนดจำนวนงวด แล้วแก้ไขวันและร้อยละในตาราง ระบบจะคำนวณให้อัตโนมัติ</p>
|
||||
</div>
|
||||
|
||||
<div class="form-section">
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label>วงเงินสัญญา (บาท)</label>
|
||||
<input type="text" id="contractAmount" placeholder="เช่น 120000000" value="">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>วันเริ่มก่อสร้าง</label>
|
||||
<input type="date" id="startDate" value="">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>สัดส่วน งปม. / เงินนอก (%)</label>
|
||||
<div class="ratio-group">
|
||||
<input type="number" id="ngpmPct" value="95" min="0" max="100" style="width:70px">
|
||||
<span>งปม. /</span>
|
||||
<input type="number" id="nookPct" value="5" min="0" max="100" style="width:70px" readonly>
|
||||
<span>นอก</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>จำนวนงวด</label>
|
||||
<input type="number" id="installmentCount" value="27" min="1" max="100" style="width:75px">
|
||||
</div>
|
||||
<div class="form-group" style="flex-direction:row;gap:10px;align-items:flex-end">
|
||||
<button class="btn btn-export" id="btnExport" onclick="exportExcel()" disabled>⬇ Export Excel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-section">
|
||||
<div class="summary-row" id="summaryRow">
|
||||
<div class="summary-card">
|
||||
<div class="lbl">วงเงินสัญญา</div>
|
||||
<div class="val" id="s_contract">—</div>
|
||||
</div>
|
||||
<div class="summary-card">
|
||||
<div class="lbl">เงิน งปม.</div>
|
||||
<div class="val" id="s_ngpm">—</div>
|
||||
</div>
|
||||
<div class="summary-card">
|
||||
<div class="lbl">เงินนอก</div>
|
||||
<div class="val" id="s_nook">—</div>
|
||||
</div>
|
||||
<div class="summary-card" id="s_pct_card">
|
||||
<div class="lbl">รวมร้อยละทุกงวด</div>
|
||||
<div class="val" id="s_pct">0.00%</div>
|
||||
</div>
|
||||
<div class="summary-card">
|
||||
<div class="lbl">วันสิ้นสุดสัญญา</div>
|
||||
<div class="val" id="s_endDate">—</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-toolbar">
|
||||
<div class="legend">
|
||||
<span>
|
||||
<span class="legend-box" style="background:#fffbf0;border:1.5px solid #f0c040"></span>
|
||||
แก้ไขได้
|
||||
</span>
|
||||
<span>
|
||||
<span class="legend-box" style="background:#f0f6ff;border:1px solid #c5d3e8"></span>
|
||||
คำนวณอัตโนมัติ
|
||||
</span>
|
||||
</div>
|
||||
<button class="btn-add" onclick="addRow()">+ เพิ่มงวด</button>
|
||||
</div>
|
||||
|
||||
<div class="table-wrapper">
|
||||
<table id="mainTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th rowspan="2">งวดที่</th>
|
||||
<th class="editable-col" rowspan="2">จำนวนวัน<br><small>(แก้ไขได้)</small></th>
|
||||
<th class="editable-col" rowspan="2">ร้อยละ (%)<br><small>(แก้ไขได้)</small></th>
|
||||
<th rowspan="2">วันส่งมอบงาน</th>
|
||||
<th colspan="3">จำนวนเงินงวดนั้น (บาท)</th>
|
||||
<th colspan="3">จำนวนเงินสะสม (บาท)</th>
|
||||
<th rowspan="2"></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th class="sub">รวม</th>
|
||||
<th class="sub">เงิน งปม.</th>
|
||||
<th class="sub">เงินนอก</th>
|
||||
<th class="sub">รวม</th>
|
||||
<th class="sub">เงิน งปม.</th>
|
||||
<th class="sub">เงินนอก</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="tableBody"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// State
|
||||
let installments = [];
|
||||
|
||||
// Template data (27 งวด from Excel)
|
||||
const TEMPLATE_27 = [
|
||||
{days:20, percent:3.80},
|
||||
{days:20, percent:3.70},{days:20, percent:3.70},{days:20, percent:3.70},{days:20, percent:3.70},
|
||||
{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},
|
||||
{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},
|
||||
{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},
|
||||
{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},
|
||||
{days:30, percent:3.70},{days:30, percent:3.70},{days:30, percent:3.70},
|
||||
{days:40, percent:3.70},{days:40, percent:3.70},{days:40, percent:3.70}
|
||||
];
|
||||
|
||||
function parseAmount(str) {
|
||||
return parseFloat(String(str).replace(/,/g, '')) || 0;
|
||||
}
|
||||
|
||||
function formatNumber(n) {
|
||||
if (!n && n !== 0) return '—';
|
||||
return Math.round(n).toLocaleString('th-TH');
|
||||
}
|
||||
|
||||
function addDays(dateStr, days) {
|
||||
const d = new Date(dateStr);
|
||||
d.setDate(d.getDate() + days);
|
||||
return d;
|
||||
}
|
||||
|
||||
function toThaiDate(date) {
|
||||
if (!(date instanceof Date) || isNaN(date)) return '—';
|
||||
const y = date.getFullYear() + 543;
|
||||
const m = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const d = String(date.getDate()).padStart(2, '0');
|
||||
return `${d}/${m}/${y}`;
|
||||
}
|
||||
|
||||
function getInputs() {
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
function recalcAll() {
|
||||
const {contract, startDate, ngpmPct, nookPct} = getInputs();
|
||||
const hasStart = startDate && contract > 0;
|
||||
|
||||
let cumDays = 0, cumTotal = 0, cumNgpm = 0, cumNook = 0;
|
||||
let sumPct = 0;
|
||||
|
||||
installments.forEach((inst, i) => {
|
||||
const days = parseFloat(inst.days) || 0;
|
||||
const pct = parseFloat(inst.percent) || 0;
|
||||
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;
|
||||
cumTotal += total; cumNgpm += ngpm; cumNook += nook;
|
||||
|
||||
const deliveryDate = hasStart ? toThaiDate(addDays(startDate, cumDays)) : '—';
|
||||
|
||||
const row = document.getElementById(`row_${i}`);
|
||||
if (!row) return;
|
||||
row.querySelector('.cell-delivery').textContent = deliveryDate;
|
||||
row.querySelector('.cell-total').textContent = formatNumber(total);
|
||||
row.querySelector('.cell-ngpm').textContent = formatNumber(ngpm);
|
||||
row.querySelector('.cell-nook').textContent = formatNumber(nook);
|
||||
row.querySelector('.cell-acc-total').textContent = formatNumber(cumTotal);
|
||||
row.querySelector('.cell-acc-ngpm').textContent = formatNumber(cumNgpm);
|
||||
row.querySelector('.cell-acc-nook').textContent = formatNumber(cumNook);
|
||||
});
|
||||
|
||||
const totalRow = document.getElementById('totalRow');
|
||||
if (totalRow) {
|
||||
totalRow.querySelector('.cell-total').textContent = formatNumber(contract);
|
||||
totalRow.querySelector('.cell-ngpm').textContent = formatNumber(Math.round(contract * ngpmPct / 100));
|
||||
totalRow.querySelector('.cell-nook').textContent = formatNumber(Math.round(contract * nookPct / 100));
|
||||
totalRow.querySelector('.cell-pct').textContent = sumPct.toFixed(2) + '%';
|
||||
}
|
||||
|
||||
document.getElementById('s_contract').textContent = contract > 0 ? formatNumber(contract) + ' ฿' : '—';
|
||||
document.getElementById('s_ngpm').textContent = contract > 0 ? formatNumber(Math.round(contract * ngpmPct / 100)) + ' ฿' : '—';
|
||||
document.getElementById('s_nook').textContent = contract > 0 ? formatNumber(Math.round(contract * nookPct / 100)) + ' ฿' : '—';
|
||||
|
||||
const pctOk = Math.abs(sumPct - 100) < 0.001;
|
||||
const pctCard = document.getElementById('s_pct_card');
|
||||
pctCard.className = 'summary-card ' + (pctOk ? 'ok' : 'warn');
|
||||
document.getElementById('s_pct').innerHTML =
|
||||
sumPct.toFixed(2) + '%' +
|
||||
(pctOk
|
||||
? '<span class="pct-status pct-ok">✓ ครบ 100%</span>'
|
||||
: '<span class="pct-status pct-warn">⚠ ยังไม่ครบ 100%</span>');
|
||||
|
||||
if (hasStart) {
|
||||
document.getElementById('s_endDate').textContent = toThaiDate(addDays(startDate, cumDays));
|
||||
} else {
|
||||
document.getElementById('s_endDate').textContent = '—';
|
||||
}
|
||||
|
||||
document.getElementById('btnExport').disabled = !(pctOk && contract > 0 && startDate);
|
||||
}
|
||||
|
||||
function renderTable() {
|
||||
const tbody = document.getElementById('tableBody');
|
||||
tbody.innerHTML = '';
|
||||
|
||||
installments.forEach((inst, i) => {
|
||||
const tr = document.createElement('tr');
|
||||
tr.id = `row_${i}`;
|
||||
tr.innerHTML = `
|
||||
<td class="center">${i + 1}</td>
|
||||
<td class="editable-cell">
|
||||
<input type="number" value="${inst.days}" min="0"
|
||||
onchange="installments[${i}].days=this.value; recalcAll()"
|
||||
oninput="installments[${i}].days=this.value; recalcAll()">
|
||||
</td>
|
||||
<td class="editable-cell">
|
||||
<input type="number" value="${inst.percent}" min="0" max="100" step="0.01"
|
||||
onchange="installments[${i}].percent=this.value; recalcAll()"
|
||||
oninput="installments[${i}].percent=this.value; recalcAll()">
|
||||
</td>
|
||||
<td class="center calc cell-delivery">—</td>
|
||||
<td class="calc cell-total">—</td>
|
||||
<td class="calc cell-ngpm">—</td>
|
||||
<td class="calc cell-nook">—</td>
|
||||
<td class="acc cell-acc-total">—</td>
|
||||
<td class="acc cell-acc-ngpm">—</td>
|
||||
<td class="acc cell-acc-nook">—</td>
|
||||
<td class="center">
|
||||
<button class="btn-del" onclick="deleteRow(${i})" title="ลบงวดนี้">✕</button>
|
||||
</td>
|
||||
`;
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
|
||||
const totalTr = document.createElement('tr');
|
||||
totalTr.id = 'totalRow';
|
||||
totalTr.className = 'total-row';
|
||||
totalTr.innerHTML = `
|
||||
<td colspan="2" class="center">รวมทั้งสิ้น</td>
|
||||
<td class="center cell-pct">0.00%</td>
|
||||
<td class="center">—</td>
|
||||
<td class="cell-total">—</td>
|
||||
<td class="cell-ngpm">—</td>
|
||||
<td class="cell-nook">—</td>
|
||||
<td colspan="3" class="center">—</td>
|
||||
<td></td>
|
||||
`;
|
||||
tbody.appendChild(totalTr);
|
||||
|
||||
recalcAll();
|
||||
}
|
||||
|
||||
function addRow() {
|
||||
installments.push({days: 0, percent: 0});
|
||||
document.getElementById('installmentCount').value = installments.length;
|
||||
renderTable();
|
||||
}
|
||||
|
||||
function deleteRow(i) {
|
||||
installments.splice(i, 1);
|
||||
document.getElementById('installmentCount').value = installments.length;
|
||||
renderTable();
|
||||
}
|
||||
|
||||
function onCountChange(n) {
|
||||
n = parseInt(n) || 1;
|
||||
if (n < 1) n = 1;
|
||||
if (installments.length > 0) {
|
||||
if (!confirm(`การเปลี่ยนจำนวนงวดเป็น ${n} งวด จะล้างข้อมูลเดิม ต้องการดำเนินการต่อไหม?`)) {
|
||||
document.getElementById('installmentCount').value = installments.length;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (n === 27) {
|
||||
installments = TEMPLATE_27.map(t => ({...t}));
|
||||
} else {
|
||||
installments = Array.from({length: n}, () => ({days: 0, percent: 0}));
|
||||
}
|
||||
renderTable();
|
||||
}
|
||||
|
||||
function exportExcel() {
|
||||
const {contract, startDate, ngpmPct, nookPct} = getInputs();
|
||||
const wb = XLSX.utils.book_new();
|
||||
|
||||
const headers = [
|
||||
'งวดที่','จำนวนวัน','ร้อยละ (%)','วันส่งมอบงาน',
|
||||
'รวม (บาท)','เงิน งปม. (บาท)','เงินนอก (บาท)',
|
||||
'รวมสะสม (บาท)','งปม. สะสม (บาท)','เงินนอก สะสม (บาท)'
|
||||
];
|
||||
|
||||
const rows = [headers];
|
||||
let cumDays = 0, cumTotal = 0, cumNgpm = 0, cumNook = 0;
|
||||
|
||||
installments.forEach((inst, i) => {
|
||||
const days = parseFloat(inst.days) || 0;
|
||||
const pct = parseFloat(inst.percent) || 0;
|
||||
cumDays += days;
|
||||
const total = Math.round(contract * pct / 100);
|
||||
const ngpm = Math.round(total * ngpmPct / 100);
|
||||
const nook = total - ngpm;
|
||||
cumTotal += total; cumNgpm += ngpm; cumNook += nook;
|
||||
const delivery = toThaiDate(addDays(startDate, cumDays));
|
||||
rows.push([i+1, days, pct, delivery, total, ngpm, nook, cumTotal, cumNgpm, cumNook]);
|
||||
});
|
||||
|
||||
rows.push([
|
||||
'รวม', '', installments.reduce((s,r) => s + (parseFloat(r.percent)||0), 0).toFixed(2),
|
||||
'',
|
||||
Math.round(contract), Math.round(contract*ngpmPct/100), Math.round(contract*nookPct/100),
|
||||
'', '', ''
|
||||
]);
|
||||
|
||||
const ws = XLSX.utils.aoa_to_sheet(rows);
|
||||
ws['!cols'] = [
|
||||
{wch:8},{wch:12},{wch:12},{wch:18},
|
||||
{wch:18},{wch:18},{wch:16},
|
||||
{wch:18},{wch:18},{wch:18}
|
||||
];
|
||||
|
||||
XLSX.utils.book_append_sheet(wb, ws, 'งวดงาน');
|
||||
|
||||
const today = new Date();
|
||||
const fname = `งวดงาน_${today.getFullYear()}${String(today.getMonth()+1).padStart(2,'0')}${String(today.getDate()).padStart(2,'0')}.xlsx`;
|
||||
XLSX.writeFile(wb, fname);
|
||||
}
|
||||
|
||||
document.getElementById('contractAmount').addEventListener('input', recalcAll);
|
||||
document.getElementById('startDate').addEventListener('change', recalcAll);
|
||||
document.getElementById('ngpmPct').addEventListener('input', function() {
|
||||
const v = parseFloat(this.value) || 0;
|
||||
document.getElementById('nookPct').value = (100 - v).toFixed(2);
|
||||
recalcAll();
|
||||
});
|
||||
document.getElementById('installmentCount').addEventListener('change', function() {
|
||||
onCountChange(this.value);
|
||||
});
|
||||
|
||||
// Init
|
||||
installments = TEMPLATE_27.map(t => ({...t}));
|
||||
renderTable();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user