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

@@ -2,6 +2,7 @@ from fastapi import APIRouter, HTTPException, Query
from decimal import Decimal
from typing import Dict, Any, List
from app.core.database import get_connection
from app.modules.trial_balance.service import fetch_trial_balance
router = APIRouter(prefix="/trial-balance", tags=["試算表"])
@@ -10,12 +11,23 @@ def D(x) -> Decimal:
@router.get("", summary="試算表(全科目)")
def get_trial_balance(
fiscal_year: int = Query(..., description="会計年度(例: 2025"),
date_from: str = Query(..., description="YYYY-MM-DD"),
date_to: str = Query(..., description="YYYY-MM-DD"),
):
if date_from > date_to:
raise HTTPException(status_code=400, detail="期間指定が不正です。")
result = fetch_trial_balance(date_from, date_to)
return {
"period": {
"from": date_from,
"to": date_to
},
**result
}
with get_connection() as conn, conn.cursor() as cur:
# 全科目取得
@@ -37,15 +49,18 @@ def get_trial_balance(
for acc in accounts:
aid = acc["account_id"]
# 期首残高
# 期首残高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
""", (aid, date_from))
opening = D(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, aid))
row = cur.fetchone()
opening = D(row["opening"]) if row else D(0)
# 当期
cur.execute("""

View File

@@ -0,0 +1,81 @@
# backend/app/modules/trial_balance/service.py
from decimal import Decimal
from typing import Dict, Any, List
from app.core.database import get_connection
def D(x) -> Decimal:
return Decimal(str(x or 0))
def fetch_trial_balance(date_from: str, date_to: str):
with get_connection() as conn, conn.cursor() as cur:
cur.execute("""
SELECT account_id, account_code, account_name, account_type
FROM accounts
WHERE is_active = true
ORDER BY account_code
""")
accounts = cur.fetchall()
result_accounts: List[Dict[str, Any]] = []
total_opening = Decimal("0")
total_debit = Decimal("0")
total_credit = Decimal("0")
total_closing = Decimal("0")
for acc in accounts:
aid = acc["account_id"]
# 期首
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
""", (aid, date_from))
opening = D(cur.fetchone()["opening"])
# 当期
cur.execute("""
SELECT
COALESCE(SUM(l.debit), 0) AS debit,
COALESCE(SUM(l.credit), 0) AS credit
FROM journal_lines l
JOIN journal_entries j ON j.journal_id = l.journal_id
WHERE l.account_id = %s
AND j.journal_date BETWEEN %s AND %s
""", (aid, date_from, date_to))
row = cur.fetchone()
debit = D(row["debit"])
credit = D(row["credit"])
closing = opening + debit - credit
total_opening += opening
total_debit += debit
total_credit += credit
total_closing += closing
result_accounts.append({
"account_id": aid,
"account_code": acc["account_code"],
"account_name": acc["account_name"],
"account_type": acc["account_type"],
"opening_balance": str(opening),
"debit": str(debit),
"credit": str(credit),
"closing_balance": str(closing),
})
return {
"totals": {
"opening_balance": str(total_opening),
"debit": str(total_debit),
"credit": str(total_credit),
"closing_balance": str(total_closing)
},
"accounts": result_accounts
}

View File

@@ -0,0 +1,84 @@
# backend/app/modules/trial_balance/sql.py
TRIAL_BALANCE_SQL = """
WITH accounts_base AS (
SELECT
a.account_id,
a.account_code,
a.account_name,
a.account_type
FROM accounts a
WHERE a.is_active = true
),
opening AS (
SELECT
jl.account_id,
SUM(jl.debit) AS opening_debit,
SUM(jl.credit) AS opening_credit
FROM journal_entries je
JOIN journal_lines jl ON jl.journal_id = je.journal_id
WHERE je.description = '前期残高'
AND je.journal_date = %(start_date)s
GROUP BY jl.account_id
),
period AS (
SELECT
jl.account_id,
SUM(jl.debit) AS period_debit,
SUM(jl.credit) AS period_credit
FROM journal_entries je
JOIN journal_lines jl ON jl.journal_id = je.journal_id
WHERE je.journal_date BETWEEN %(start_date)s AND %(end_date)s
AND je.description <> '前期残高'
GROUP BY jl.account_id
)
SELECT
a.account_code,
a.account_name,
a.account_type,
COALESCE(o.opening_debit, 0) AS opening_debit,
COALESCE(o.opening_credit, 0) AS opening_credit,
COALESCE(p.period_debit, 0) AS period_debit,
COALESCE(p.period_credit, 0) AS period_credit,
CASE
WHEN (
COALESCE(o.opening_debit, 0)
- COALESCE(o.opening_credit, 0)
+ COALESCE(p.period_debit, 0)
- COALESCE(p.period_credit, 0)
) > 0
THEN (
COALESCE(o.opening_debit, 0)
- COALESCE(o.opening_credit, 0)
+ COALESCE(p.period_debit, 0)
- COALESCE(p.period_credit, 0)
)
ELSE 0
END AS closing_debit,
CASE
WHEN (
COALESCE(o.opening_debit, 0)
- COALESCE(o.opening_credit, 0)
+ COALESCE(p.period_debit, 0)
- COALESCE(p.period_credit, 0)
) < 0
THEN ABS(
COALESCE(o.opening_debit, 0)
- COALESCE(o.opening_credit, 0)
+ COALESCE(p.period_debit, 0)
- COALESCE(p.period_credit, 0)
)
ELSE 0
END AS closing_credit
FROM accounts_base a
LEFT JOIN opening o ON o.account_id = a.account_id
LEFT JOIN period p ON p.account_id = a.account_id
ORDER BY a.account_code;
"""