This commit is contained in:
admin
2026-02-21 10:51:25 +09:00
parent 584530937b
commit 3b028b8fc0
25 changed files with 1852 additions and 244 deletions

View File

@@ -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)

View File

@@ -9,14 +9,17 @@ def D(x) -> Decimal:
# 科目グループ定義(表示順序を制御)- PDFフォーマットに準拠
ACCOUNT_GROUPS = [
# 資産の部
("現金・預金合計", ["101", "102", "103", "104"]),
("流動資産合計", ["101", "102", "103", "104", "201", "202", "203", "204", "205"]),
("現金・預金合計", ["101", "102", "103", "104", "105"]),
("売上債権合計", ["201", "202"]),
("有価証券合計", ["301"]),
("棚卸資産合計", ["210"]),
("他流動資産合計", ["203", "204", "205", "206", "207", "208"]),
("流動資産合計", ["101", "102", "103", "104", "105", "201", "202", "203", "204", "205", "206", "207", "208", "210", "301"]),
("有形固定資産合計", ["401", "402", "403", "404", "405", "406", "407"]),
("無形固定資産合計", ["408"]),
("投資その他の資産合計", ["301", "410"]),
("固定資産合計", ["301", "401", "402", "403", "404", "405", "406", "407", "408", "410"]),
("資産合計", ["101", "102", "103", "104", "201", "202", "203", "204", "205", "301", "401", "402", "403", "404", "405", "406", "407", "408", "410"]),
("資産合計", ["101", "102", "103", "104", "105", "201", "202", "203", "204", "205", "206", "207", "208", "210", "301", "401", "402", "403", "404", "405", "406", "407", "408", "410"]),
# 負債の部
("仕入債務合計", ["501"]),
("流動負債合計", ["501", "502", "503", "504", "505", "506", "507", "508"]),
@@ -169,7 +172,7 @@ def fetch_trial_balance(date_from: str, date_to: str):
# 繰越利益の行を追加
result_accounts.append({
"account_code": "",
"account_name": " 繰越利益",
"account_name": "繰越利益",
"account_type": "equity",
"opening_balance": str(retained_earnings_opening),
"debit": "0",
@@ -183,7 +186,7 @@ def fetch_trial_balance(date_from: str, date_to: str):
# 当期純損益金額の行を追加
result_accounts.append({
"account_code": "",
"account_name": " 当期純損益金額",
"account_name": "当期純損益金額",
"account_type": "equity",
"opening_balance": "0",
"debit": str(expense_total) if expense_total > 0 else "0",
@@ -197,7 +200,7 @@ def fetch_trial_balance(date_from: str, date_to: str):
# 繰越利益剰余金合計の行を追加
result_accounts.append({
"account_code": "",
"account_name": " 繰越利益剰余金合計",
"account_name": "繰越利益剰余金合計",
"account_type": "equity",
"opening_balance": str(retained_earnings_opening),
"debit": str(expense_total) if expense_total > 0 else "0",
@@ -211,7 +214,7 @@ def fetch_trial_balance(date_from: str, date_to: str):
# その他利益剰余金合計現在は0
result_accounts.append({
"account_code": "",
"account_name": " その他利益剰余金合計",
"account_name": "その他利益剰余金合計",
"account_type": "equity",
"opening_balance": "0",
"debit": "0",