from fastapi import HTTPException
from app.core.database import get_connection

def check_fiscal_year_unlocked(journal_date):
    fiscal_year = journal_date.year

    with get_connection() as conn, conn.cursor() as cur:
        cur.execute("""
            SELECT is_locked
              FROM fiscal_year_locks
             WHERE fiscal_year = %s
        """, (fiscal_year,))
        row = cur.fetchone()

        if row and row["is_locked"]:
            raise HTTPException(
                status_code=400,
                detail="この会計年度は既に確定されているため、仕訳を変更できません。"
            )
