feat: Vorschlaege buchen und Telefon-Dateien nur ablegen
Zwei Luecken geschlossen: Mail-Vorschlaege standen als "pending" im Eingang, aber es gab keinen Knopf zum Buchen – der Weg war hinten offen. Jetzt je Eintrag "Buchen" und ein "Alle buchen". Unklare bleiben liegen: Bei ihnen fehlt etwas oder die Erkennung war unsicher, die soll man ansehen statt im Stapel durchzuwinken. Der Auszug vom Telefon bucht nicht mehr sofort, sondern wird nur abgelegt. Am Telefon prueft man keine 500 Zeilen; die Datei wartet in der Import-Ansicht und laeuft dort durch die normale Vorschau mit Saldo-Abgleich, Zeilenfilter und Duplikatpruefung. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,9 +10,18 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, get_owned
|
||||
from ..models import Account, ApiToken, InboxItem, InboxStatus, Transaction, User
|
||||
from ..models import (
|
||||
Account,
|
||||
ApiToken,
|
||||
InboxItem,
|
||||
InboxStatus,
|
||||
Transaction,
|
||||
TxnType,
|
||||
User,
|
||||
)
|
||||
from ..schemas import ApiTokenCreate, ApiTokenCreated, ApiTokenOut, InboxItemOut
|
||||
from ..security import generate_session_token, hash_session_token
|
||||
from ..services import currency
|
||||
|
||||
router = APIRouter(prefix="/inbox", tags=["inbox"])
|
||||
|
||||
@@ -77,6 +86,68 @@ def list_inbox(
|
||||
)
|
||||
|
||||
|
||||
def _buche(db: Session, user: User, eintrag: InboxItem) -> Transaction | None:
|
||||
"""Aus einem Vorschlag eine Buchung machen. Ohne Konto geht es nicht."""
|
||||
if eintrag.status == InboxStatus.booked or eintrag.transaction_id is not None:
|
||||
return None
|
||||
if eintrag.account_id is None:
|
||||
return None
|
||||
konto = db.get(Account, eintrag.account_id)
|
||||
if konto is None or konto.owner_id != user.id:
|
||||
return None
|
||||
betrag = abs(eintrag.amount_cents)
|
||||
txn = Transaction(
|
||||
owner_id=user.id,
|
||||
type=TxnType.expense if eintrag.amount_cents < 0 else TxnType.income,
|
||||
date=eintrag.date,
|
||||
account_id=konto.id,
|
||||
amount_cents=betrag,
|
||||
amount_eur_cents=currency.compute_amount_eur_cents(
|
||||
db, betrag, konto.currency, eintrag.date),
|
||||
category_id=eintrag.category_id,
|
||||
payee=eintrag.payee,
|
||||
description=eintrag.note,
|
||||
)
|
||||
db.add(txn)
|
||||
db.flush()
|
||||
eintrag.transaction_id = txn.id
|
||||
eintrag.status = InboxStatus.booked
|
||||
return txn
|
||||
|
||||
|
||||
@router.post("/{item_id}/book", response_model=InboxItemOut)
|
||||
def book_item(item_id: int, db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
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")
|
||||
if _buche(db, user, eintrag) 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.
|
||||
|
||||
Unklare bleiben liegen: Bei ihnen fehlt etwas oder die Erkennung war
|
||||
unsicher – die soll man ansehen, nicht im Stapel durchwinken.
|
||||
"""
|
||||
offen = (
|
||||
db.query(InboxItem)
|
||||
.filter(InboxItem.owner_id == user.id,
|
||||
InboxItem.status == InboxStatus.pending,
|
||||
InboxItem.account_id.isnot(None))
|
||||
.all()
|
||||
)
|
||||
gebucht = sum(1 for e in offen if _buche(db, user, e) is not None)
|
||||
db.commit()
|
||||
return {"gebucht": gebucht}
|
||||
|
||||
|
||||
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def discard_item(item_id: int, db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
"""Eintrag verwerfen – samt der Buchung, die daraus entstanden ist.
|
||||
|
||||
Reference in New Issue
Block a user