import psycopg2
from datetime import datetime

# 数据库连接
conn = psycopg2.connect(
    host="192.168.0.61",
    port=55432,
    database="njts_acct",
    user="njts_app",
    password="njts_app2025"
)

cur = conn.cursor()

# 查询 2025年8月 关于"遠達ビル 事務所費用"的仕訳
cur.execute("""
    SELECT 
        je.journal_entry_id,
        je.entry_date,
        je.description,
        je.revision_count,
        je.is_latest,
        je.is_deleted,
        jl.account_id,
        a.account_code,
        a.account_name,
        jl.debit,
        jl.credit
    FROM journal_entries je
    LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id
    LEFT JOIN accounts a ON jl.account_id = a.account_id
    WHERE je.entry_date >= '2025-08-01' 
      AND je.entry_date < '2025-09-01'
      AND je.description LIKE '%遠達ビル 事務所費用%'
    ORDER BY je.journal_entry_id, je.entry_date DESC
""")

results = cur.fetchall()

print("=" * 100)
print("2025年8月 遠達ビル 事務所費用 検索結果")
print("=" * 100)

if not results:
    print("該当データなし")
else:
    current_id = None
    for row in results:
        entry_id, entry_date, description, rev_count, is_latest, is_deleted, account_id, account_code, account_name, debit, credit = row
        
        if entry_id != current_id:
            current_id = entry_id
            print(f"\n📝 仕訳ID: {entry_id}")
            print(f"   日付: {entry_date}")
            print(f"   摘要: {description}")
            print(f"   版本: v{rev_count}, is_latest={is_latest}, is_deleted={is_deleted}")
            print(f"   科目明細:")
        
        if account_code:
            print(f"      - {account_code} {account_name}: 借={debit}, 貸={credit}")

# 查询普通預金 2025年8月的所有仕訳
print("\n" + "=" * 100)
print("普通預金 2025年8月 全仕訳")
print("=" * 100)

cur.execute("""
    SELECT 
        je.journal_entry_id,
        je.entry_date,
        je.description,
        je.revision_count,
        je.is_latest,
        je.is_deleted,
        SUM(CASE WHEN jl.debit > 0 THEN jl.debit ELSE 0 END) as total_debit,
        SUM(CASE WHEN jl.credit > 0 THEN jl.credit ELSE 0 END) as total_credit
    FROM journal_entries je
    LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id
    WHERE je.entry_date >= '2025-08-01' 
      AND je.entry_date < '2025-09-01'
      AND jl.account_id = (SELECT account_id FROM accounts WHERE account_code = '111')
    GROUP BY je.journal_entry_id, je.entry_date, je.description, je.revision_count, je.is_latest, je.is_deleted
    ORDER BY je.entry_date DESC, je.journal_entry_id DESC
""")

results = cur.fetchall()

if results:
    total_debit = 0
    total_credit = 0
    for entry_id, entry_date, description, rev_count, is_latest, is_deleted, debit, credit in results:
        debit = debit or 0
        credit = credit or 0
        total_debit += debit
        total_credit += credit
        deleted_mark = "【削除】" if is_deleted else ""
        latest_mark = "✓" if is_latest else "✗"
        print(f"{entry_date} {deleted_mark} [{latest_mark}v{rev_count}] {entry_id}: {description}")
        print(f"           借={debit:>15,.0f}  貸={credit:>15,.0f}")
    
    print(f"\n小計: 借={total_debit:>15,.0f}  貸={total_credit:>15,.0f}")

cur.close()
conn.close()
