179 lines
5.9 KiB
JavaScript
179 lines
5.9 KiB
JavaScript
document.addEventListener("DOMContentLoaded", () => {
|
||
console.log("trial-balance.js 已执行");
|
||
|
||
const API_URL = "http://127.0.0.1:18080/trial-balance";
|
||
|
||
// デフォルト期間を計算(5/31決算:6月開始~現在月末)
|
||
function getDefaultPeriod() {
|
||
const today = new Date();
|
||
const currentYear = today.getFullYear();
|
||
const currentMonth = today.getMonth() + 1; // 1-12
|
||
|
||
let fiscalStartYear, fiscalEndYear;
|
||
|
||
// 6月~5月が1会計年度
|
||
if (currentMonth >= 6) {
|
||
// 6月以降:当年6月1日から
|
||
fiscalStartYear = currentYear;
|
||
fiscalEndYear = currentYear;
|
||
} else {
|
||
// 1-5月:前年6月1日から
|
||
fiscalStartYear = currentYear - 1;
|
||
fiscalEndYear = currentYear;
|
||
}
|
||
|
||
const dateFrom = `${fiscalStartYear}-06-01`;
|
||
|
||
// 現在月の末日を計算
|
||
const lastDayOfMonth = new Date(fiscalEndYear, currentMonth, 0).getDate();
|
||
const dateTo = `${fiscalEndYear}-${String(currentMonth).padStart(
|
||
2,
|
||
"0"
|
||
)}-${String(lastDayOfMonth).padStart(2, "0")}`;
|
||
|
||
return { dateFrom, dateTo };
|
||
}
|
||
|
||
// 試算表データを読み込む関数
|
||
function loadTrialBalance(dateFrom, dateTo) {
|
||
const url = `${API_URL}?date_from=${dateFrom}&date_to=${dateTo}`;
|
||
console.log("読み込み URL:", url);
|
||
|
||
fetch(url)
|
||
.then((r) => {
|
||
if (!r.ok) throw new Error("API 返回错误: " + r.status);
|
||
return r.json();
|
||
})
|
||
.then((data) => {
|
||
console.log("試算表 API データ:", data);
|
||
|
||
// 期間情報を更新
|
||
updatePeriodInfo(dateFrom, dateTo);
|
||
|
||
const tbody = document.querySelector("#trialBalanceTable tbody");
|
||
if (!tbody) {
|
||
console.error("未找到 #trialBalanceTable tbody");
|
||
return;
|
||
}
|
||
|
||
tbody.innerHTML = "";
|
||
|
||
let totalOpening = 0;
|
||
let totalDebit = 0;
|
||
let totalCredit = 0;
|
||
let totalClosing = 0;
|
||
|
||
data.accounts.forEach((row) => {
|
||
const debit = Number(row.debit ?? 0);
|
||
const credit = Number(row.credit ?? 0);
|
||
const opening = Number(row.opening_balance ?? 0);
|
||
const closing = Number(row.closing_balance ?? 0);
|
||
|
||
// =========================
|
||
// 行タイプ判定
|
||
// =========================
|
||
const isTotalRow = !row.account_code; // null / "" / undefined 全覆盖
|
||
|
||
// 明细行だけ合計に含める
|
||
if (!isTotalRow) {
|
||
totalOpening += opening;
|
||
totalDebit += debit;
|
||
totalCredit += credit;
|
||
totalClosing += closing;
|
||
}
|
||
|
||
const tr = document.createElement("tr");
|
||
|
||
if (isTotalRow) {
|
||
tr.classList.add("total-row");
|
||
}
|
||
|
||
const ratio = row.ratio ?? "0.00";
|
||
|
||
tr.innerHTML = `
|
||
<td class="left">${row.account_code ?? ""}</td>
|
||
<td class="left">${row.account_name ?? ""}</td>
|
||
<td class="right">${opening ? opening.toLocaleString() : ""}</td>
|
||
<td class="right">${debit ? debit.toLocaleString() : ""}</td>
|
||
<td class="right">${credit ? credit.toLocaleString() : ""}</td>
|
||
<td class="right">${closing ? closing.toLocaleString() : ""}</td>
|
||
<td class="right">${ratio}</td>
|
||
`;
|
||
|
||
tbody.appendChild(tr);
|
||
});
|
||
|
||
// =========================
|
||
// フッター合計
|
||
// =========================
|
||
document.getElementById("totalOpening").textContent =
|
||
totalOpening.toLocaleString();
|
||
document.getElementById("totalDebit").textContent =
|
||
totalDebit.toLocaleString();
|
||
document.getElementById("totalCredit").textContent =
|
||
totalCredit.toLocaleString();
|
||
document.getElementById("totalClosing").textContent =
|
||
totalClosing.toLocaleString();
|
||
document.getElementById("totalRatio").textContent =
|
||
data.totals?.ratio ?? "100.00";
|
||
})
|
||
.catch((err) => {
|
||
console.error("試算表読取失敗:", err);
|
||
alert("試算表読取失敗、コンソールログを確認してください");
|
||
});
|
||
}
|
||
|
||
// 期間情報を更新する関数
|
||
function updatePeriodInfo(dateFrom, dateTo) {
|
||
const fromDate = new Date(dateFrom);
|
||
const toDate = new Date(dateTo);
|
||
|
||
const fromYear = fromDate.getFullYear() - 2018; // 令和変換(2019年=令和1年)
|
||
const toYear = toDate.getFullYear() - 2018;
|
||
|
||
const periodText = `令和${fromYear}年 ${
|
||
fromDate.getMonth() + 1
|
||
}月 ${fromDate.getDate()}日 ~ 令和${toYear}年 ${
|
||
toDate.getMonth() + 1
|
||
}月 ${toDate.getDate()}日`;
|
||
document.getElementById("periodInfo").textContent = periodText;
|
||
}
|
||
|
||
// 検索ボタンのイベント
|
||
document.getElementById("btnSearch").addEventListener("click", () => {
|
||
const dateFrom = document.getElementById("dateFrom").value;
|
||
const dateTo = document.getElementById("dateTo").value;
|
||
|
||
if (!dateFrom || !dateTo) {
|
||
alert("開始年月日と終了年月日を入力してください");
|
||
return;
|
||
}
|
||
|
||
if (dateFrom > dateTo) {
|
||
alert("開始年月日は終了年月日より前の日付を指定してください");
|
||
return;
|
||
}
|
||
|
||
loadTrialBalance(dateFrom, dateTo);
|
||
});
|
||
|
||
// 全年度表示ボタンのイベント
|
||
document.getElementById("btnFullYear").addEventListener("click", () => {
|
||
const currentYear = new Date().getFullYear();
|
||
const dateFrom = `${currentYear}-01-01`;
|
||
const dateTo = `${currentYear}-12-31`;
|
||
|
||
document.getElementById("dateFrom").value = dateFrom;
|
||
document.getElementById("dateTo").value = dateTo;
|
||
|
||
loadTrialBalance(dateFrom, dateTo);
|
||
});
|
||
|
||
// 初期読み込み(5/31決算に対応)
|
||
const defaultPeriod = getDefaultPeriod();
|
||
document.getElementById("dateFrom").value = defaultPeriod.dateFrom;
|
||
document.getElementById("dateTo").value = defaultPeriod.dateTo;
|
||
|
||
loadTrialBalance(defaultPeriod.dateFrom, defaultPeriod.dateTo);
|
||
});
|