desktop修正
This commit is contained in:
0
backend/app/modules/general_ledger/__init__.py
Normal file
0
backend/app/modules/general_ledger/__init__.py
Normal file
28
backend/app/modules/general_ledger/router.py
Normal file
28
backend/app/modules/general_ledger/router.py
Normal 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
|
||||
}
|
||||
53
backend/app/modules/general_ledger/service.py
Normal file
53
backend/app/modules/general_ledger/service.py
Normal 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
|
||||
}
|
||||
14
backend/app/modules/general_ledger/sql.py
Normal file
14
backend/app/modules/general_ledger/sql.py
Normal 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;
|
||||
"""
|
||||
Reference in New Issue
Block a user