desktop修正

This commit is contained in:
admin
2025-12-14 23:54:48 +09:00
parent f9f5eec35e
commit 74dcb88fe6
25 changed files with 1068 additions and 157 deletions

View File

@@ -1,109 +1,107 @@
from fastapi import APIRouter, HTTPException, Query
from typing import Optional, List, Dict, Any
from decimal import Decimal
from datetime import date
from app.core.database import get_connection
router = APIRouter(prefix="/ledger", tags=["元帳"])
router = APIRouter(
prefix="/general-ledger",
tags=["元帳"]
)
def to_decimal(x) -> Decimal:
if isinstance(x, Decimal):
return x
def D(x) -> Decimal:
return Decimal(str(x or 0))
@router.get("/{account_id}", summary="科目別元帳(残高推移)")
def get_ledger(
account_id: int,
date_from: Optional[str] = Query(None, description="YYYY-MM-DD"),
date_to: Optional[str] = Query(None, description="YYYY-MM-DD"),
@router.get("", summary="元帳取得(期首残高対応)")
def get_general_ledger(
account_id: int = Query(..., description="科目ID"),
fiscal_year: int = Query(..., description="会計年度2025"),
date_from: str = Query(..., description="YYYY-MM-DD"),
date_to: str = Query(..., description="YYYY-MM-DD"),
):
# 1) 科目存在チェック
if date_from > date_to:
raise HTTPException(status_code=400, detail="期間指定が不正です。")
with get_connection() as conn, conn.cursor() as cur:
# ----------------------------
# 科目存在確認
# ----------------------------
cur.execute("""
SELECT account_id, account_code, account_name, account_type
SELECT account_code, account_name
FROM accounts
WHERE account_id = %s
AND is_active = true
""", (account_id,))
account = cur.fetchone()
if not account:
raise HTTPException(status_code=404, detail="指定した科目が見つかりません。")
acc = cur.fetchone()
# 2) 期間デフォルト(最小〜最大)
if not date_from:
cur.execute("""
SELECT MIN(j.journal_date) AS min_d
FROM journal_entries j
JOIN journal_lines l ON l.journal_id = j.journal_id
WHERE l.account_id = %s
""", (account_id,))
row = cur.fetchone()
date_from = (row["min_d"] or date.today()).isoformat()
if not acc:
raise HTTPException(status_code=400, detail="指定された科目は存在しません。")
if not date_to:
cur.execute("""
SELECT MAX(j.journal_date) AS max_d
FROM journal_entries j
JOIN journal_lines l ON l.journal_id = j.journal_id
WHERE l.account_id = %s
""", (account_id,))
row = cur.fetchone()
date_to = (row["max_d"] or date.today()).isoformat()
# 3) 期首残高date_from より前)
# ----------------------------
# 期首残高opening_balances
# ----------------------------
cur.execute("""
SELECT COALESCE(SUM(l.debit - l.credit), 0) AS opening
FROM journal_lines l
JOIN journal_entries j ON j.journal_id = l.journal_id
WHERE l.account_id = %s
AND j.journal_date < %s
""", (account_id, date_from))
opening = to_decimal(cur.fetchone()["opening"])
SELECT
COALESCE(opening_debit, 0) - COALESCE(opening_credit, 0) AS opening
FROM opening_balances
WHERE fiscal_year = %s
AND account_id = %s
""", (fiscal_year, account_id))
# 4) 期間内明細
row = cur.fetchone()
opening_balance = D(row["opening"]) if row else D(0)
# ----------------------------
# 期間内仕訳取得
# ----------------------------
cur.execute("""
SELECT j.journal_date, j.journal_id, j.description,
l.debit, l.credit
FROM journal_lines l
JOIN journal_entries j ON j.journal_id = l.journal_id
SELECT
j.journal_date,
j.description,
l.debit,
l.credit
FROM journal_entries j
JOIN journal_lines l
ON l.journal_id = j.journal_id
WHERE l.account_id = %s
AND j.journal_date BETWEEN %s AND %s
ORDER BY j.journal_date, j.journal_id, l.line_id
ORDER BY j.journal_date, l.line_id
""", (account_id, date_from, date_to))
rows = cur.fetchall()
# 5) 残高推移の計算(借方−貸方)
balance = opening
out_rows: List[Dict[str, Any]] = []
total_debit = Decimal("0")
total_credit = Decimal("0")
# ----------------------------
# 累計残高計算
# ----------------------------
balance = opening_balance
lines = []
for r in rows:
d = to_decimal(r["debit"])
c = to_decimal(r["credit"])
total_debit += d
total_credit += c
balance += (d - c)
out_rows.append({
"date": r["journal_date"].isoformat(),
"journal_id": r["journal_id"],
"description": r["description"],
"debit": str(d),
"credit": str(c),
"balance": str(balance) # 借方残を正とする(借方−貸方)
})
for r in rows:
debit = D(r["debit"])
credit = D(r["credit"])
balance += debit - credit
result = {
lines.append({
"journal_date": r["journal_date"],
"description": r["description"],
"debit": str(debit),
"credit": str(credit),
"balance": str(balance),
})
return {
"account": {
"id": account["account_id"],
"code": account["account_code"],
"name": account["account_name"],
"type": account["account_type"],
"account_id": account_id,
"account_code": acc["account_code"],
"account_name": acc["account_name"],
},
"period": {"from": date_from, "to": date_to},
"opening_balance": str(opening),
"total_debit": str(total_debit),
"total_credit": str(total_credit),
"fiscal_year": fiscal_year,
"period": {
"from": date_from,
"to": date_to
},
"opening_balance": str(opening_balance),
"lines": lines,
"closing_balance": str(balance),
"rows": out_rows
}
return result