feat: Kontouebertraege lassen sich im Eingang buchen
Bisher blieb im Eingang nur, eine Umbuchung als Ausgabe oder Einnahme zu kategorisieren. Sie zaehlte dann als Ausgabe, obwohl kein Geld weg ist – nur verschoben. Jetzt gibt es je Vorschlag den Weg 'Als Uebertrag buchen': Zielkonto waehlen, fertig. Es entsteht EINE Buchung, die beide Salden bewegt und in keiner Auswertung zaehlt. Die Richtung kommt aus dem Vorzeichen – ein Abgang geht von hier weg, ein Zugang kommt vom Gegenkonto her; entsprechend fragt der Dialog 'wohin' oder 'woher'. Bei verschiedenen Waehrungen wird die Seite exakt gesetzt, die wir wirklich kennen, und die andere umgerechnet. Der teuerste Fehler an dieser Stelle waere ein Zielkonto, das selbst an einer Bank haengt: Es meldet den Zugang von allein, eine Gegenbuchung hier haette das Geld zweimal. Der Server lehnt das mit einer Begruendung ab; wer es besser weiss, bestaetigt und kommt durch. Das Wissen bleibt an einer Stelle – die Oberflaeche fragt nicht selbst nach, sie zeigt die Antwort des Servers. Die Betragslogik ist dafuer aus dem Buchungs-Router in einen Dienst gewandert. Zwei Kopien derselben Umrechnung waeren genau die Stelle, an der Salden auseinanderlaufen. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ gegen sie ab, damit dieselbe Zahlung nicht ein zweites Mal verbucht wird.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
@@ -101,6 +102,49 @@ def book_item(item_id: int, db: Session = Depends(get_db), user: User = Depends(
|
||||
return eintrag
|
||||
|
||||
|
||||
class InboxTransferIn(BaseModel):
|
||||
to_account_id: int
|
||||
# Nur setzen, wenn man es besser weiss als Certain – siehe unten.
|
||||
trotzdem: bool = False
|
||||
|
||||
|
||||
@router.post("/{item_id}/transfer", response_model=InboxItemOut)
|
||||
def book_as_transfer(item_id: int, payload: InboxTransferIn,
|
||||
db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
"""Einen Vorschlag als Kontoübertrag buchen statt als Ausgabe oder Einnahme.
|
||||
|
||||
Es entsteht EINE Buchung, die das Geld vom einen Konto ins andere bewegt.
|
||||
Sie zählt auf beiden Seiten im Saldo und in keiner Auswertung – ein
|
||||
Übertrag ist keine Ausgabe.
|
||||
"""
|
||||
eintrag = get_owned(db, InboxItem, item_id, user)
|
||||
if eintrag.account_id is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Kein Konto zugeordnet")
|
||||
ziel = get_owned(db, Account, payload.to_account_id, user)
|
||||
if ziel.id == eintrag.account_id:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Ein Übertrag braucht ein anderes Zielkonto")
|
||||
|
||||
# Der teuerste Fehler an dieser Stelle: Das Zielkonto hängt selbst an einer
|
||||
# Bank, liefert die Gegenbuchung also von allein. Wer hier trotzdem eine
|
||||
# anlegt, hat das Geld zweimal auf dem Zielkonto.
|
||||
if not payload.trotzdem and eingang.ist_angebunden(db, user, ziel):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=(f"'{ziel.name}' ist selbst an eine Bank angebunden und meldet "
|
||||
f"den Zugang von allein. Buche das hier als Ausgabe – beide "
|
||||
f"Seiten werden danach automatisch als Übertrag verbunden."),
|
||||
)
|
||||
|
||||
if eingang.buche_als_uebertrag(db, user, eintrag, ziel) is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Lässt sich nicht buchen")
|
||||
db.commit()
|
||||
db.refresh(eintrag)
|
||||
return eintrag
|
||||
|
||||
|
||||
@router.post("/book-all")
|
||||
def book_all(db: Session = Depends(get_db), user: User = Depends(get_current_user)) -> dict:
|
||||
"""Alle offenen Vorschläge buchen, die ein Konto haben.
|
||||
|
||||
@@ -14,34 +14,12 @@ from ..database import get_db
|
||||
from ..deps import get_current_user, get_owned
|
||||
from ..models import Account, Category, Transaction, TxnType, User
|
||||
from ..schemas import TransactionCreate, TransactionOut, TransactionUpdate
|
||||
from ..services import currency
|
||||
from ..services import buchung, currency
|
||||
from .accounts import base_currency
|
||||
|
||||
router = APIRouter(prefix="/transactions", tags=["transactions"])
|
||||
|
||||
|
||||
def _fx(txn: Transaction) -> float | None:
|
||||
return float(txn.fx_rate) if txn.fx_rate is not None else None
|
||||
|
||||
|
||||
def _recompute(db: Session, txn: Transaction, from_acc: Account, to_acc: Account | None) -> None:
|
||||
"""Setzt amount_eur_cents und (bei Transfers) to_amount_cents neu."""
|
||||
txn.amount_eur_cents = currency.compute_amount_eur_cents(
|
||||
db, txn.amount_cents, from_acc.currency, txn.date, _fx(txn)
|
||||
)
|
||||
if txn.type == TxnType.transfer and to_acc is not None:
|
||||
if txn.to_amount_cents is None:
|
||||
if to_acc.currency == from_acc.currency:
|
||||
txn.to_amount_cents = txn.amount_cents
|
||||
else:
|
||||
conv = currency.convert(
|
||||
db, txn.amount_cents, from_acc.currency, to_acc.currency, txn.date, _fx(txn)
|
||||
)
|
||||
txn.to_amount_cents = conv if conv is not None else txn.amount_cents
|
||||
else:
|
||||
txn.to_amount_cents = None
|
||||
|
||||
|
||||
def to_out(db: Session, txn: Transaction, base: str, ccy_by_account: dict[int, str]) -> TransactionOut:
|
||||
acc_ccy = ccy_by_account.get(txn.account_id, "EUR")
|
||||
if base == "EUR":
|
||||
@@ -59,7 +37,7 @@ def to_out(db: Session, txn: Transaction, base: str, ccy_by_account: dict[int, s
|
||||
account_currency=acc_ccy,
|
||||
amount_cents=txn.amount_cents,
|
||||
to_amount_cents=txn.to_amount_cents,
|
||||
fx_rate=_fx(txn),
|
||||
fx_rate=buchung.fx(txn),
|
||||
amount_eur_cents=txn.amount_eur_cents,
|
||||
amount_base_cents=amount_base,
|
||||
category_id=txn.category_id,
|
||||
@@ -168,7 +146,7 @@ def create_transaction(
|
||||
description=(payload.description or None),
|
||||
notes=(payload.notes or None),
|
||||
)
|
||||
_recompute(db, txn, from_acc, to_acc)
|
||||
buchung.setze_betraege(db, txn, from_acc, to_acc)
|
||||
db.add(txn)
|
||||
db.commit()
|
||||
db.refresh(txn)
|
||||
@@ -222,7 +200,7 @@ def update_transaction(
|
||||
txn.category_id = None
|
||||
else:
|
||||
txn.to_account_id = None
|
||||
_recompute(db, txn, from_acc, to_acc)
|
||||
buchung.setze_betraege(db, txn, from_acc, to_acc)
|
||||
|
||||
db.commit()
|
||||
db.refresh(txn)
|
||||
|
||||
Reference in New Issue
Block a user