import os
os.environ['DB_HOST'] = '192.168.0.61'
os.environ['DB_PORT'] = '55432'
os.environ['DB_NAME'] = 'njts_acct'
os.environ['DB_USER'] = 'njts_app'
os.environ['DB_PASSWORD'] = 'njts_app2025'

from app.core.database import get_connection

with get_connection() as conn:
    with conn.cursor() as cur:
        # 602科目の前期残高を登録
        cur.execute("""
            INSERT INTO opening_balances (fiscal_year, account_id, opening_debit, opening_credit)
            VALUES (2025, (SELECT account_id FROM accounts WHERE account_code = '602'), 0, 5206146)
            ON CONFLICT (fiscal_year, account_id) 
            DO UPDATE SET opening_credit = 5206146
        """)
        conn.commit()
        print('✓ 602科目（繰越利益剰余金）の前期残高 5,206,146円 を登録しました')
        
        # 確認
        cur.execute("""
            SELECT a.account_code, a.account_name, ob.opening_debit, ob.opening_credit
            FROM opening_balances ob
            JOIN accounts a ON ob.account_id = a.account_id
            WHERE ob.fiscal_year = 2025 AND a.account_code = '602'
        """)
        row = cur.fetchone()
        if row:
            print(f"\n【確認】")
            print(f"{row['account_code']} {row['account_name']}: 貸方 {row['opening_credit']:,}円")
