import sys
sys.path.insert(0, '/app')

from app.core.database import get_connection

print("=" * 80)
print("【期間内（2025-06-01～06-30）のすべての仕訳と科目】")
print("=" * 80)

from_date = "2025-06-01"
to_date = "2025-06-30"

with get_connection() as conn:
    with conn.cursor() as cur:
        # 期間内のすべての仕訳
        cur.execute("""
            SELECT je.journal_entry_id, je.entry_date, je.description
            FROM journal_entries je
            WHERE je.is_deleted = false
                AND je.is_latest = true
                AND je.entry_date >= %s
                AND je.entry_date <= %s
            ORDER BY je.entry_date DESC, je.journal_entry_id DESC
            LIMIT 10
        """, [from_date, to_date])
        
        rows = cur.fetchall()
        print(f"\n期間内の仕訳（最新版のみ）：{len(rows)} 件")
        
        for i, row in enumerate(rows, 1):
            print(f"\n{i}. 仕訳 ID {row['journal_entry_id']}: {row['entry_date']} - {row['description']}")
            
            # この仕訳の科目を表示
            cur.execute("""
                SELECT 
                    a.account_id, 
                    a.account_code,
                    a.account_name,
                    jl.debit,
                    jl.credit
                FROM journal_lines jl
                JOIN accounts a ON jl.account_id = a.account_id
                WHERE jl.journal_entry_id = %s
                ORDER BY a.account_id
            """, [row['journal_entry_id']])
            
            for line in cur.fetchall():
                print(f"   • ID {line['account_id']} ({line['account_code']}) {line['account_name']}: 借方 {line['debit']}, 貸方 {line['credit']}")

print("\n" + "=" * 80)
