feat: Mail-Abruf sagt, was mit den Treffern passiert ist – und jede Buchung,
woher sie kommt Die Meldung nach einem Abruf zaehlte geprueft, neu und unklar – aber nicht "schon bekannt". Wurden alle Treffer beim vorigen Abruf schon uebernommen, stand da "0 neu", und es sah aus, als waere nichts passiert, obwohl die Eintraege im Eingang liegen. Die Zahl steht jetzt dabei. Und die uebergangenen Mails wurden aufgelistet, die unklaren nicht: nur eine Zahl, ohne zu sagen, welche Mail gemeint ist oder was ihr fehlt. Jetzt stehen sie mit Grund da – kein Konto zugeordnet, kein Betrag erkannt, Richtung unklar, Zahlung noch nicht ausgefuehrt. Ausserdem traegt jede Buchung jetzt ihre Herkunft: Bank, Mail, Kurzbefehl, Datei-Import oder von Hand. In der Buchungsliste steht sie als kleines Kennzeichen unter dem Text. Ermittelt wird sie mit einer Sammelabfrage, nicht je Zeile. Bei einem Uebertrag zeigen zwei Eingangs-Eintraege auf dieselbe Buchung; die Gegenseite hat Certain selbst angelegt, gemeint ist die echte Quelle. Genau dafuer gibt es einen Test. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,7 +30,33 @@ from .accounts import base_currency
|
||||
router = APIRouter(prefix="/transactions", tags=["transactions"])
|
||||
|
||||
|
||||
def to_out(db: Session, txn: Transaction, base: str, ccy_by_account: dict[int, str]) -> TransactionOut:
|
||||
def _herkunft(db: Session, user: User, txns: list[Transaction]) -> dict[int, str]:
|
||||
"""Woher jede Buchung stammt.
|
||||
|
||||
Der Eingangs-Eintrag sagt es für alles, was über Bank, Mail oder Kurzbefehl
|
||||
kam; sonst entscheidet der Import-Stapel: mit ihm aus einer Datei, ohne ihn
|
||||
von Hand. Als Sammelabfrage, nicht je Zeile – die Liste geht bis 500.
|
||||
"""
|
||||
ids = [t.id for t in txns]
|
||||
if not ids:
|
||||
return {}
|
||||
aus_eingang: dict[int, str] = {}
|
||||
for txn_id, quelle in db.query(InboxItem.transaction_id, InboxItem.source).filter(
|
||||
InboxItem.owner_id == user.id, InboxItem.transaction_id.in_(ids)
|
||||
):
|
||||
# Bei einem Übertrag zeigen ZWEI Einträge auf dieselbe Buchung. Die
|
||||
# Gegenseite hat Certain selbst angelegt; gemeint ist die echte Quelle.
|
||||
if txn_id in aus_eingang and quelle.value == "transfer":
|
||||
continue
|
||||
aus_eingang[txn_id] = quelle.value
|
||||
return {
|
||||
t.id: aus_eingang.get(t.id, "csv" if t.import_batch_id is not None else "manuell")
|
||||
for t in txns
|
||||
}
|
||||
|
||||
|
||||
def to_out(db: Session, txn: Transaction, base: str, ccy_by_account: dict[int, str],
|
||||
herkunft: dict[int, str] | None = None) -> TransactionOut:
|
||||
acc_ccy = ccy_by_account.get(txn.account_id, "EUR")
|
||||
if base == "EUR":
|
||||
amount_base = txn.amount_eur_cents
|
||||
@@ -54,6 +80,7 @@ def to_out(db: Session, txn: Transaction, base: str, ccy_by_account: dict[int, s
|
||||
budget_treatment=txn.budget_treatment,
|
||||
budget_category_id=txn.budget_category_id,
|
||||
import_batch_id=txn.import_batch_id,
|
||||
herkunft=(herkunft or {}).get(txn.id, "manuell"),
|
||||
payee=txn.payee,
|
||||
description=txn.description,
|
||||
notes=txn.notes,
|
||||
@@ -117,7 +144,8 @@ def list_transactions(
|
||||
rows = q.order_by(Transaction.date.desc(), Transaction.id.desc()).limit(limit).offset(offset).all()
|
||||
base = base_currency(db, user)
|
||||
ccy = _currency_map(db, user)
|
||||
return [to_out(db, t, base, ccy) for t in rows]
|
||||
woher = _herkunft(db, user, rows)
|
||||
return [to_out(db, t, base, ccy, woher) for t in rows]
|
||||
|
||||
|
||||
@router.post("", response_model=TransactionOut, status_code=status.HTTP_201_CREATED)
|
||||
@@ -161,7 +189,8 @@ def create_transaction(
|
||||
db.add(txn)
|
||||
db.commit()
|
||||
db.refresh(txn)
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user))
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user),
|
||||
_herkunft(db, user, [txn]))
|
||||
|
||||
|
||||
@router.get("/{txn_id}", response_model=TransactionOut)
|
||||
@@ -169,7 +198,8 @@ def get_transaction(
|
||||
txn_id: int, db: Session = Depends(get_db), user: User = Depends(get_current_user)
|
||||
):
|
||||
txn = get_owned(db, Transaction, txn_id, user)
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user))
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user),
|
||||
_herkunft(db, user, [txn]))
|
||||
|
||||
|
||||
@router.patch("/{txn_id}", response_model=TransactionOut)
|
||||
@@ -215,7 +245,8 @@ def update_transaction(
|
||||
|
||||
db.commit()
|
||||
db.refresh(txn)
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user))
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user),
|
||||
_herkunft(db, user, [txn]))
|
||||
|
||||
|
||||
class TransactionTransferIn(BaseModel):
|
||||
@@ -246,7 +277,8 @@ def make_transfer(txn_id: int, payload: TransactionTransferIn,
|
||||
) from e
|
||||
db.commit()
|
||||
db.refresh(txn)
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user))
|
||||
return to_out(db, txn, base_currency(db, user), _currency_map(db, user),
|
||||
_herkunft(db, user, [txn]))
|
||||
|
||||
|
||||
@router.delete("/{txn_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
Reference in New Issue
Block a user