#!/usr/bin/env python3
"""
查看 test 元的完整明细
"""
import psycopg

try:
    conn = psycopg.connect(
        host='192.168.0.61',
        port=55432,
        dbname='njts_acct',
        user='njts_app',
        password='njts_app2025'
    )
    
    with conn.cursor() as cur:
        print("=" * 100)
        print("【test 元 - 完整明細查詢】")
        print("=" * 100)
        
        # 查詢所有test元記錄的明細
        cur.execute("""
            SELECT 
                je.journal_entry_id,
                je.description,
                je.is_latest,
                jl.account_id,
                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.description LIKE '%test%'
            AND je.is_deleted = false
            ORDER BY je.journal_entry_id, jl.journal_line_id
        """)
        
        rows = cur.fetchall()
        
        current_je_id = None
        for row in rows:
            je_id, je_desc, is_latest, acc_id, acc_name, debit, credit = row
            
            if je_id != current_je_id:
                status = "✓" if is_latest else "✗"
                print(f"\n{status} ID={je_id} {je_desc}")
                current_je_id = je_id
            
            if acc_id:
                print(f"     {acc_id:>4d} {acc_name:20s} 借={debit:>10.0f} 貸={credit:>10.0f}")
        
        conn.close()

except Exception as e:
    print(f'錯誤: {e}')
    import traceback
    traceback.print_exc()
