From 24c04e0d2931b8da4025a351c08f3966070e0b6a Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 15 Jan 2026 17:34:26 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/journal_entries.py | 59 ++++++++++++++++++-------- backend/sql/update_parent_entry_id.sql | 25 +++++++++++ frontend/journal-entry.html | 18 +++++++- 3 files changed, 84 insertions(+), 18 deletions(-) create mode 100644 backend/sql/update_parent_entry_id.sql diff --git a/backend/app/routers/journal_entries.py b/backend/app/routers/journal_entries.py index dcf21c8..b32ac7d 100644 --- a/backend/app/routers/journal_entries.py +++ b/backend/app/routers/journal_entries.py @@ -39,7 +39,8 @@ def get_journal_entries( from_date: date = None, to_date: date = None, keyword: str = None, - account_id: int = None + account_id: int = None, + include_history: bool = False ): """ 仕訳一覧を検索して取得 @@ -47,20 +48,44 @@ def get_journal_entries( with get_connection() as conn: with conn.cursor() as cur: # クエリ構築 - sql = """ - SELECT - je.journal_entry_id, - je.entry_date, - je.description, - je.fiscal_year, - COALESCE(SUM(jl.debit), 0) as debit_total, - COALESCE(SUM(jl.credit), 0) as credit_total - FROM journal_entries je - LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id - WHERE je.is_deleted = false - """ - - params = [] + if include_history: + # 修正履歴を含むすべてのバージョンを表示 + sql = """ + SELECT + je.journal_entry_id, + je.entry_date, + je.description, + je.fiscal_year, + je.version, + COALESCE(SUM(jl.debit), 0) as debit_total, + COALESCE(SUM(jl.credit), 0) as credit_total + FROM journal_entries je + LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id + WHERE je.is_deleted = false + """ + params = [] + else: + # 最新バージョンのみ表示:parent_entry_idを持つ古い仕訳と逆仕訳を除外 + sql = """ + SELECT + je.journal_entry_id, + je.entry_date, + je.description, + je.fiscal_year, + je.version, + COALESCE(SUM(jl.debit), 0) as debit_total, + COALESCE(SUM(jl.credit), 0) as credit_total + FROM journal_entries je + LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id + WHERE je.is_deleted = false + AND je.description NOT LIKE %s + AND je.journal_entry_id NOT IN ( + SELECT parent_entry_id + FROM journal_entries + WHERE parent_entry_id IS NOT NULL + ) + """ + params = ['%[逆仕訳]%'] if from_date: sql += " AND je.entry_date >= %s" @@ -83,8 +108,8 @@ def get_journal_entries( params.append(account_id) sql += """ - GROUP BY je.journal_entry_id, je.entry_date, je.description, je.fiscal_year - ORDER BY je.entry_date DESC, je.journal_entry_id DESC + GROUP BY je.journal_entry_id, je.entry_date, je.description, je.fiscal_year, je.version + ORDER BY je.entry_date DESC, je.journal_entry_id DESC, je.version DESC """ cur.execute(sql, params) diff --git a/backend/sql/update_parent_entry_id.sql b/backend/sql/update_parent_entry_id.sql new file mode 100644 index 0000000..f6abce5 --- /dev/null +++ b/backend/sql/update_parent_entry_id.sql @@ -0,0 +1,25 @@ +-- 修正後の仕訳に対して、元の仕訳IDをparent_entry_idに設定 + +-- ステップ1: 【修正後】の仕訳を確認 +SELECT + je_modified.journal_entry_id as modified_id, + je_modified.description as modified_desc, + je_original.journal_entry_id as original_id, + je_original.description as original_desc +FROM journal_entries je_modified +LEFT JOIN journal_entries je_original + ON REPLACE(je_modified.description, '【修正後】', '') = je_original.description + AND je_modified.description LIKE '【修正後】%' + AND je_original.description NOT LIKE '【修正後】%' + AND je_original.description NOT LIKE '[逆仕訳]%' +WHERE je_modified.description LIKE '【修正後】%' +ORDER BY je_modified.journal_entry_id; + +-- ステップ2: 確認後、以下のUPDATE文を実行 +UPDATE journal_entries +SET parent_entry_id = je_original.journal_entry_id +FROM journal_entries je_original +WHERE REPLACE(journal_entries.description, '【修正後】', '') = je_original.description + AND journal_entries.description LIKE '【修正後】%' + AND je_original.description NOT LIKE '【修正後】%' + AND je_original.description NOT LIKE '[逆仕訳]%'; diff --git a/frontend/journal-entry.html b/frontend/journal-entry.html index bcbec3d..d478ceb 100644 --- a/frontend/journal-entry.html +++ b/frontend/journal-entry.html @@ -184,6 +184,11 @@ + + @@ -870,6 +875,9 @@ const to = document.getElementById("searchToDate").value; const key = document.getElementById("searchKeyword").value; const accountId = document.getElementById("searchAccountId").value; + const includeHistory = document.getElementById( + "searchIncludeHistory" + ).checked; // 検索条件を保存 localStorage.setItem( @@ -879,6 +887,7 @@ toDate: to, keyword: key, accountId: accountId, + includeHistory: includeHistory, }) ); @@ -887,6 +896,7 @@ if (to) qs.append("to_date", to); if (key) qs.append("keyword", key); if (accountId) qs.append("account_id", accountId); + if (includeHistory) qs.append("include_history", "true"); const res = await fetch(`${API}/journal-entries?${qs.toString()}`); const data = await res.json(); @@ -896,9 +906,12 @@ data.forEach((e) => { const tr = document.createElement("tr"); + // 修正履歴がある場合はバージョン番号を表示 + const versionInfo = + includeHistory && e.version > 1 ? ` (v${e.version})` : ""; tr.innerHTML = ` ${e.entry_date} - ${e.description} + ${e.description}${versionInfo} ${e.debit_total.toLocaleString()} ${e.credit_total.toLocaleString()} @@ -1109,6 +1122,9 @@ if (conditions.accountId) document.getElementById("searchAccountId").value = conditions.accountId; + if (conditions.includeHistory) + document.getElementById("searchIncludeHistory").checked = + conditions.includeHistory; } // ページロード時に自動的に検索を実行して最新データを表示