feat: Transfer-Schlagwort mit festem Zielkonto (z. B. REVOLUT -> Konto Revolut)

- Transfer-Regel kann direkt ein Zielkonto tragen (target_account_id); ohne ->
  weiterhin Auto-Erkennung aus dem Buchungstext (Konto-Texte)
- Migration 0010; Endpunkt aktualisiert bestehendes Wort statt Konflikt
- Import bevorzugt das feste Zielkonto, sonst _guess_target_account
- Konten-Seite: Transfer-Erkennung mit Zielkonto-Auswahl + Anzeige am Chip
- Test: festes Zielkonto greift ohne Konto-Texte (75 grün)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Guido
2026-08-20 19:06:24 +02:00
co-authored by Claude Opus 4.8
parent 794a6aeb91
commit c44892ff72
9 changed files with 96 additions and 12 deletions
+5
View File
@@ -551,6 +551,11 @@ class CategorizationRule(Base):
amount_cents: Mapped[int | None] = mapped_column(Integer, nullable=True)
# Wenn True: passende Buchung wird zum Transfer (category_id bleibt leer).
is_transfer: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
# Optionales festes Zielkonto für Transfer-Regeln. Ohne -> Zielkonto wird aus
# dem Buchungstext (Konto-Erkennungs-Texte) erraten.
target_account_id: Mapped[int | None] = mapped_column(
ForeignKey("accounts.id", ondelete="SET NULL"), nullable=True
)
# Art der Regel: "expense" | "income" | "transfer". Erlaubt, dasselbe Wort je
# einmal für eine Ausgabe- UND eine Einnahme-Kategorie zu verwenden (das
# Matching filtert ohnehin nach Buchungsart).
+15 -6
View File
@@ -9,7 +9,7 @@ from sqlalchemy.orm import Session
from ..database import get_db
from ..deps import get_current_user, get_owned
from ..models import CategorizationRule, Category, CategoryGroup, MatchField, RuleSource, User
from ..models import Account, CategorizationRule, Category, CategoryGroup, MatchField, RuleSource, User
from ..schemas import (
CategoryCreate,
CategoryGroupCreate,
@@ -353,7 +353,10 @@ def add_keyword(
def _transfer_keyword_out(rule: CategorizationRule) -> TransferKeywordOut:
return TransferKeywordOut(id=rule.id, word=rule.pattern, amount_cents=rule.amount_cents)
return TransferKeywordOut(
id=rule.id, word=rule.pattern, amount_cents=rule.amount_cents,
target_account_id=rule.target_account_id,
)
@router.get("/transfer-keywords", response_model=list[TransferKeywordOut])
@@ -378,15 +381,21 @@ def add_transfer_keyword(
pattern = normalize_text(payload.word)[:255]
if not pattern:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Leeres Schlagwort")
target_id = None
if payload.target_account_id is not None:
target_id = get_owned(db, Account, payload.target_account_id, user).id
# Gleiches Wort existiert schon als Transfer-Regel -> aktualisieren (Betrag/Zielkonto).
existing = _find_any_rule(db, user, pattern, "transfer")
if existing is not None:
if existing.is_transfer and existing.amount_cents == payload.amount_cents:
return _transfer_keyword_out(existing)
raise _conflict(db, existing)
existing.amount_cents = payload.amount_cents
existing.target_account_id = target_id
db.commit()
db.refresh(existing)
return _transfer_keyword_out(existing)
rule = CategorizationRule(
owner_id=user.id, match_field=MatchField.any, pattern=pattern,
category_id=None, amount_cents=payload.amount_cents, is_transfer=True,
kind="transfer", source=RuleSource.manual, priority=10,
target_account_id=target_id, kind="transfer", source=RuleSource.manual, priority=10,
)
db.add(rule)
db.commit()
+2
View File
@@ -441,12 +441,14 @@ class KeywordOut(BaseModel):
class TransferKeywordCreate(BaseModel):
word: str = Field(min_length=1, max_length=255)
amount_cents: int | None = Field(default=None, gt=0)
target_account_id: int | None = None
class TransferKeywordOut(BaseModel):
id: int
word: str
amount_cents: int | None = None
target_account_id: int | None = None
class FlaggedKeywordOut(BaseModel):
+5 -1
View File
@@ -300,7 +300,11 @@ def detect_rows(db: Session, user: User, account: Account, candidates: list[dict
matched_rule_id = rule.id
if rule.is_transfer:
ttype = "transfer"
to_account_id = _guess_target_account(text_idents, payee or "", purpose or "", account.id)
# Festes Zielkonto der Regel bevorzugen, sonst aus dem Text raten.
if rule.target_account_id and rule.target_account_id != account.id:
to_account_id = rule.target_account_id
else:
to_account_id = _guess_target_account(text_idents, payee or "", purpose or "", account.id)
else:
category_id = rule.category_id