desktop修正
This commit is contained in:
@@ -4,6 +4,8 @@ from typing import List
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from psycopg.errors import ForeignKeyViolation, CheckViolation
|
||||
from app.core.database import get_connection
|
||||
from app.core.fiscal_lock import check_fiscal_year_unlocked
|
||||
from datetime import date
|
||||
|
||||
router = APIRouter(prefix="/journals", tags=["仕訳"])
|
||||
|
||||
@@ -109,3 +111,74 @@ def create_journal(data: JournalCreate):
|
||||
raise HTTPException(status_code=400, detail="科目IDが不正です。")
|
||||
except CheckViolation:
|
||||
raise HTTPException(status_code=400, detail="金額の指定が不正です。")
|
||||
|
||||
|
||||
@router.put("/{journal_id}", summary="仕訳修正")
|
||||
def update_journal(journal_id: int, data: JournalCreate):
|
||||
|
||||
with get_connection() as conn, conn.cursor() as cur:
|
||||
|
||||
# ① 先取原仕訳の日付
|
||||
cur.execute(
|
||||
"SELECT journal_date FROM journal_entries WHERE journal_id = %s",
|
||||
(journal_id,)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="仕訳が存在しません。")
|
||||
|
||||
# 🔒 ② 这里!!年度锁定检查
|
||||
check_fiscal_year_unlocked(row["journal_date"])
|
||||
|
||||
# ③ 删除旧明细
|
||||
cur.execute(
|
||||
"DELETE FROM journal_lines WHERE journal_id = %s",
|
||||
(journal_id,)
|
||||
)
|
||||
|
||||
# ④ 更新ヘッダ
|
||||
cur.execute(
|
||||
"UPDATE journal_entries SET journal_date=%s, description=%s WHERE journal_id=%s",
|
||||
(data.journal_date, data.description, journal_id)
|
||||
)
|
||||
|
||||
# ⑤ 插入新明细
|
||||
for l in data.lines:
|
||||
cur.execute(
|
||||
"""INSERT INTO journal_lines (journal_id, account_id, debit, credit)
|
||||
VALUES (%s, %s, %s, %s)""",
|
||||
(journal_id, l.account_id, l.debit, l.credit)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
return {"message": "仕訳を修正しました。"}
|
||||
|
||||
@router.delete("/{journal_id}", summary="仕訳削除")
|
||||
def delete_journal(journal_id: int):
|
||||
|
||||
with get_connection() as conn, conn.cursor() as cur:
|
||||
|
||||
# ① 先取日付
|
||||
cur.execute(
|
||||
"SELECT journal_date FROM journal_entries WHERE journal_id = %s",
|
||||
(journal_id,)
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="仕訳が存在しません。")
|
||||
|
||||
# 🔒 ② 年度锁定检查(就在这里)
|
||||
check_fiscal_year_unlocked(row["journal_date"])
|
||||
|
||||
# ③ 删除
|
||||
cur.execute(
|
||||
"DELETE FROM journal_lines WHERE journal_id = %s",
|
||||
(journal_id,)
|
||||
)
|
||||
cur.execute(
|
||||
"DELETE FROM journal_entries WHERE journal_id = %s",
|
||||
(journal_id,)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
return {"message": "仕訳を削除しました。"}
|
||||
Reference in New Issue
Block a user