修正
This commit is contained in:
@@ -98,15 +98,29 @@ def _find_retained_earnings(cur) -> Optional[Tuple[int, str]]:
|
||||
def save_opening_balances(req: OpeningBalanceRequest):
|
||||
|
||||
with get_connection() as conn, conn.cursor() as cur:
|
||||
# 年度ロック
|
||||
cur.execute("""
|
||||
SELECT is_locked
|
||||
FROM fiscal_year_locks
|
||||
WHERE fiscal_year = %s
|
||||
""", (req.fiscal_year,))
|
||||
row = cur.fetchone()
|
||||
if row and row["is_locked"]:
|
||||
raise HTTPException(status_code=400, detail="この会計年度の期首残高は既に確定されています。")
|
||||
# テーブルの存在確認(例外処理を避けるため)
|
||||
table_exists = False
|
||||
try:
|
||||
cur.execute("""
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_name = 'fiscal_year_locks'
|
||||
)
|
||||
""")
|
||||
table_exists = cur.fetchone()[0]
|
||||
except:
|
||||
pass
|
||||
|
||||
# 年度ロック(テーブルが存在する場合のみチェック)
|
||||
if table_exists:
|
||||
cur.execute("""
|
||||
SELECT is_locked
|
||||
FROM fiscal_year_locks
|
||||
WHERE fiscal_year = %s
|
||||
""", (req.fiscal_year,))
|
||||
row = cur.fetchone()
|
||||
if row and row["is_locked"]:
|
||||
raise HTTPException(status_code=400, detail="この会計年度の期首残高は既に確定されています。")
|
||||
|
||||
# 全 BS 科目取得(存在性/種別チェック用)
|
||||
cur.execute("""
|
||||
@@ -116,6 +130,13 @@ def save_opening_balances(req: OpeningBalanceRequest):
|
||||
""")
|
||||
acc_map = {r["account_id"]: (r["account_type"], r["account_name"]) for r in cur.fetchall()}
|
||||
|
||||
# ★ 重要:空白データ保存防止(ユーザーが読込せずに保存した場合の対策)
|
||||
if not req.balances:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="保存するデータがありません。「読込」ボタンで期首残高を読み込んでください。"
|
||||
)
|
||||
|
||||
# 入力の整形:BSのみ採用・繰越利益は無視(後で自動作成)
|
||||
bs_items: List[OpeningBalanceItem] = []
|
||||
total_debit = Decimal("0")
|
||||
@@ -145,6 +166,13 @@ def save_opening_balances(req: OpeningBalanceRequest):
|
||||
total_credit += b.credit
|
||||
bs_items.append(b)
|
||||
|
||||
# ★ 重要:0/0の行だけで実質的にデータなし(削除防止)
|
||||
if not bs_items:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="全科目の残高が0です。期首残高データを入力してください。"
|
||||
)
|
||||
|
||||
# 繰越利益(剰余金)を自動計算
|
||||
diff = total_debit - total_credit # 借方合計 − 貸方合計
|
||||
re_account = _find_retained_earnings(cur)
|
||||
|
||||
Reference in New Issue
Block a user