Files
njts-accounting-core/replacement_analysis.py
2026-03-03 00:01:43 +09:00

113 lines
4.4 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sys
sys.path.insert(0, '/app')
from app.core.database import get_connection
from datetime import datetime
print("=" * 80)
print("【account_id 53小口現金 → 20未払金の置換前分析】")
print("=" * 80)
with get_connection() as conn:
with conn.cursor() as cur:
# 1. 影響する仕訳の件数
print("\n【1】影響する仕訳行の件数")
cur.execute("""
SELECT COUNT(*) as cnt
FROM journal_lines
WHERE account_id = 53
""")
result = cur.fetchone()
print(f" • account_id 53 を含む仕訳行: {result['cnt']}")
if result['cnt'] > 0:
# 2. 金額の影響
print("\n【2】金額の影響")
cur.execute("""
SELECT
SUM(CASE WHEN debit > 0 THEN debit ELSE 0 END) as total_debit,
SUM(CASE WHEN credit > 0 THEN credit ELSE 0 END) as total_credit
FROM journal_lines
WHERE account_id = 53
""")
result_amount = cur.fetchone()
print(f" • 借方合計: {result_amount['total_debit']}")
print(f" • 貸方合計: {result_amount['total_credit']}")
# 3. 影響する仕訳の詳細
print("\n【3】影響する仕訳の詳細最初の 20 件)")
cur.execute("""
SELECT
je.journal_entry_id,
je.entry_date,
je.description,
a.account_code,
a.account_name,
jl.debit,
jl.credit
FROM journal_lines jl
JOIN journal_entries je ON jl.journal_entry_id = je.journal_entry_id
JOIN accounts a ON jl.account_id = a.account_id
WHERE jl.account_id = 53
ORDER BY je.entry_date DESC
LIMIT 20
""")
for i, row in enumerate(cur.fetchall(), 1):
side = '借方' if row['debit'] > 0 else '貸方'
amount = row['debit'] if row['debit'] > 0 else row['credit']
print(f" {i}. {row['entry_date']} - {row['description'][:40]}...")
print(f" {row['account_code']} {row['account_name']}: {side} {amount}")
# 4. 会計的影響の分析
print("\n【4】会計的な影響警告")
print(" 【問題1】資産 vs 負債の分類変更")
print(" • 小口現金105= 流動資産")
print(" • 未払金502= 流動負債")
print(" → 貸借対照表の構成が大きく変わります")
print("\n 【問題2】簿記ルール違反の可能性")
cur.execute("""
SELECT
COUNT(DISTINCT je.journal_entry_id) as journal_count,
MAX(je.entry_date) as latest_date
FROM journal_lines jl
JOIN journal_entries je ON jl.journal_entry_id = je.journal_entry_id
WHERE jl.account_id = 53
""")
result_check = cur.fetchone()
print(f" • 影響する仕訳: {result_check['journal_count']}")
print(f" • 最新の仕訳: {result_check['latest_date']}")
print(" → これらの仕訳の相手科目との整合性が乱れる可能性")
print("\n 【問題3】監査証跡の喪失")
print(" • 元の科目情報が失われます")
print(" → 修正履歴は保存されていません(一度背き不可)")
else:
print(" • account_id 53 の仕訳行は存在しません(置換は不要)")
print("\n" + "=" * 80)
print("【SQL 置換コマンド】")
print("=" * 80)
print("""
-- バックアップ推奨:先に元のデータをダンプしてください
-- pg_dump accounting_db > backup_before_replacement.sql
-- 置換実行SQL自己責任で実行してください
BEGIN; -- トランザクション開始
UPDATE journal_lines
SET account_id = 20
WHERE account_id = 53;
-- 確認用SQL実行前に確認してください
-- SELECT COUNT(*) FROM journal_lines WHERE account_id = 20;
COMMIT; -- コミット(このコマンドで確定)
-- ROLLBACK; -- ロールバック(中止したい場合)
""")
print("=" * 80)