import psycopg2 from psycopg2.extras import RealDictCursor conn = psycopg2.connect('host=192.168.0.61 port=55432 dbname=njts_acct user=njts_app password=njts_app2025') cur = conn.cursor(cursor_factory=RealDictCursor) print("=" * 100) print("6月の修復方法を確認(Entry#357 vs Entry#360,371,373)") print("=" * 100) cur.execute(''' SELECT je.journal_entry_id, je.entry_date, je.description, je.parent_entry_id, je.is_deleted, (SELECT COUNT(*) FROM journal_entries child WHERE child.parent_entry_id = je.journal_entry_id) as child_count, SUM(CASE WHEN jl.debit::numeric > 0 THEN jl.debit::numeric ELSE 0 END) as debit, SUM(CASE WHEN jl.credit::numeric > 0 THEN jl.credit::numeric ELSE 0 END) as credit FROM journal_entries je LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id WHERE je.journal_entry_id IN (357, 360, 371, 373) AND jl.account_id = 41 -- 売上高 GROUP BY je.journal_entry_id, je.entry_date, je.description, je.parent_entry_id, je.is_deleted ORDER BY je.journal_entry_id ''') rows = cur.fetchall() print("\n【6月のEntry詳細】") for row in rows: deleted_str = "削除済み" if row['is_deleted'] else "有効" has_child = f"({row['child_count']})" if row['child_count'] > 0 else "" parent_str = f"parent={row['parent_entry_id']}" if row['parent_entry_id'] else "parent=None" print(f"Entry#{row['journal_entry_id']}: {deleted_str} | {parent_str} | {has_child} 借: {row['debit']:>10,.0f} 貳: {row['credit']:>10,.0f}") print(f" {row['description'][:80]}") print("\n【6月修復の結果】") print(" Entry#357: 有効 | parent=None | 1.4M貳(売上)- 原始交易") print(" Entry#360: 有効 | parent=357 | 対冲交易") print(" Entry#371: 削除 | 不要な重複") print(" Entry#373: 削除 | 不要な重複") print("\n【6月の試算表結果】") cur.execute(''' SELECT SUM(CASE WHEN jl.debit::numeric > 0 THEN jl.debit::numeric ELSE 0 END) as debit, SUM(CASE WHEN jl.credit::numeric > 0 THEN jl.credit::numeric ELSE 0 END) as credit FROM journal_lines jl JOIN journal_entries je ON jl.journal_entry_id = je.journal_entry_id LEFT JOIN journal_entries child ON child.parent_entry_id = je.journal_entry_id WHERE jl.account_id = 41 AND EXTRACT(YEAR FROM je.entry_date) = 2025 AND EXTRACT(MONTH FROM je.entry_date) = 6 AND je.is_deleted = false AND child.journal_entry_id IS NULL -- フィルー ''') result = cur.fetchone() print(f" 試算表: 借: {result['debit'] if result['debit'] else 0:,.0f} | 貳: {result['credit'] if result['credit'] else 0:,.0f}") conn.close()