51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from app.core.database import get_connection
|
|
from pydantic import BaseModel
|
|
from typing import List
|
|
from decimal import Decimal
|
|
from datetime import date
|
|
|
|
router = APIRouter(prefix="/journal-entries", tags=["仕訳"])
|
|
|
|
|
|
class JournalLine(BaseModel):
|
|
account_id: int
|
|
debit: Decimal = Decimal("0")
|
|
credit: Decimal = Decimal("0")
|
|
|
|
|
|
class JournalEntryRequest(BaseModel):
|
|
entry_date: date
|
|
description: str
|
|
lines: List[JournalLine]
|
|
|
|
|
|
@router.post("")
|
|
def create_journal_entry(req: JournalEntryRequest):
|
|
with get_connection() as conn:
|
|
with conn.cursor() as cur:
|
|
# 仕訳ヘッダ
|
|
cur.execute("""
|
|
INSERT INTO journal_entries (entry_date, description)
|
|
VALUES (%s, %s)
|
|
RETURNING id
|
|
""", (req.entry_date, req.description))
|
|
entry_id = cur.fetchone()[0]
|
|
|
|
# 明細
|
|
for line in req.lines:
|
|
cur.execute("""
|
|
INSERT INTO journal_lines
|
|
(journal_entry_id, account_id, debit, credit)
|
|
VALUES (%s, %s, %s, %s)
|
|
""", (
|
|
entry_id,
|
|
line.account_id,
|
|
line.debit,
|
|
line.credit
|
|
))
|
|
|
|
conn.commit()
|
|
|
|
return {"status": "ok", "journal_entry_id": entry_id}
|