"""Betraege einer Buchung berechnen. Eigener Dienst, weil zwei Wege Buchungen anlegen: das Formular und der Eingang, der einen Vorschlag als Uebertrag bucht. Zwei Kopien derselben Umrechnung waeren genau die Stelle, an der Salden auseinanderlaufen. """ from __future__ import annotations from sqlalchemy.orm import Session from ..models import Account, Transaction, TxnType from . import currency def fx(txn: Transaction) -> float | None: return float(txn.fx_rate) if txn.fx_rate is not None else None def setze_betraege(db: Session, txn: Transaction, von: Account, nach: Account | None) -> None: """Setzt amount_eur_cents und (bei Uebertraegen) to_amount_cents neu.""" txn.amount_eur_cents = currency.compute_amount_eur_cents( db, txn.amount_cents, von.currency, txn.date, fx(txn) ) if txn.type == TxnType.transfer and nach is not None: if txn.to_amount_cents is None: if nach.currency == von.currency: txn.to_amount_cents = txn.amount_cents else: conv = currency.convert( db, txn.amount_cents, von.currency, nach.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