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

@@ -0,0 +1,28 @@
from fastapi import APIRouter, HTTPException, Query
from app.modules.general_ledger.service import fetch_general_ledger
router = APIRouter(
prefix="/general-ledger",
tags=["元帳"]
)
@router.get("", summary="元帳取得(科目別)")
def get_general_ledger(
account_id: int = Query(...),
date_from: str = Query(...),
date_to: str = Query(...)
):
if date_from > date_to:
raise HTTPException(status_code=400, detail="期間指定が不正です。")
result = fetch_general_ledger(account_id, date_from, date_to)
if result is None:
raise HTTPException(status_code=400, detail="科目が存在しません。")
return {
"period": {
"from": date_from,
"to": date_to
},
**result
}

View File

@@ -0,0 +1,53 @@
from decimal import Decimal
from app.core.database import get_connection
from app.modules.general_ledger.sql import GENERAL_LEDGER_SQL
def D(x) -> Decimal:
return Decimal(str(x or 0))
def fetch_general_ledger(account_id: int, date_from: str, date_to: str):
with get_connection() as conn, conn.cursor() as cur:
# ① 科目确认
cur.execute("""
SELECT account_code, account_name
FROM accounts
WHERE account_id = %s
AND is_active = true
""", (account_id,))
acc = cur.fetchone()
if not acc:
return None
# ② 元帳明细
cur.execute(
GENERAL_LEDGER_SQL,
{
"account_id": account_id,
"date_from": date_from,
"date_to": date_to
}
)
rows = cur.fetchall()
balance = Decimal("0")
lines = []
for r in rows:
balance += D(r["debit"]) - D(r["credit"])
lines.append({
"journal_date": r["journal_date"],
"description": r["description"],
"debit": str(D(r["debit"])),
"credit": str(D(r["credit"])),
"balance": str(balance)
})
return {
"account": {
"account_code": acc["account_code"],
"account_name": acc["account_name"]
},
"lines": lines
}

View File

@@ -0,0 +1,14 @@
# sql.py
GENERAL_LEDGER_SQL = """
SELECT
j.journal_date,
j.description,
l.debit,
l.credit
FROM journal_lines l
JOIN journal_entries j
ON j.journal_id = l.journal_id
WHERE l.account_id = %(account_id)s
AND j.journal_date BETWEEN %(date_from)s AND %(date_to)s
ORDER BY j.journal_date, l.line_id;
"""