fix: neutrale Kategorien gelten für Ausgabe UND Einnahme

Kategorien erben ihre Art von der Gruppe, und die ist entweder Ausgabe oder
Einnahme. Für einen Kontoübertrag passt beides nicht: Auf dem einen Konto geht
Geld raus, auf dem anderen kommt es rein – man musste also zwei Kategorien in
zwei Gruppen anlegen.

Als „nicht in Auswertungen einrechnen" markierte Kategorien werden jetzt in
beiden Richtungen angeboten (Buchungsformular und Import-Prüfansicht), und ihre
Schlagwörter greifen unabhängig vom Vorzeichen. Eine Kategorie „Kontoübertrag"
in einer Ausgabe-Gruppe genügt damit für beide Seiten.

Test für beide Richtungen; 94 grün.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Guido
2026-08-21 14:09:54 +02:00
co-authored by Claude Opus 4.8
parent 7674e47f43
commit af865a5c7b
5 changed files with 69 additions and 9 deletions
+11 -5
View File
@@ -222,7 +222,7 @@ def _match_rule(
purpose: str,
amount: int | None,
allowed_kind: CategoryKind | None,
cat_kind: dict[int, CategoryKind],
cat_kind: dict[int, CategoryKind | None],
) -> CategorizationRule | None:
"""Erste passende Regel (Muster + optionale Betragsbedingung). Kategorie-Regeln
greifen nur, wenn die Kategorie-Art (Einnahme/Ausgabe) zur Buchung passt;
@@ -241,7 +241,10 @@ def _match_rule(
continue
if not rule.category_id:
continue
if allowed_kind is not None and cat_kind.get(rule.category_id) != allowed_kind:
# Neutrale Kategorien („Kontoübertrag") passen in beide Richtungen: Geld
# geht auf dem einen Konto raus und kommt auf dem anderen rein.
art = cat_kind.get(rule.category_id)
if allowed_kind is not None and art is not None and art != allowed_kind:
continue
if _pattern_hit(rule, p, z):
return rule
@@ -410,9 +413,12 @@ def detect_rows(db: Session, user: User, account: Account, candidates: list[dict
rules = db.query(CategorizationRule).filter(CategorizationRule.owner_id == user.id).all()
rules.sort(key=_rule_sort_key, reverse=True) # längstes/​manuell zuerst
cat_kind: dict[int, CategoryKind] = {
cid: kind
for cid, kind in db.query(Category.id, Category.type).filter(Category.owner_id == user.id)
# None = neutral: zählt für Ausgabe UND Einnahme (siehe _match_rule).
cat_kind: dict[int, CategoryKind | None] = {
cid: (None if neutral else kind)
for cid, kind, neutral in db.query(
Category.id, Category.type, Category.excluded_from_stats
).filter(Category.owner_id == user.id)
}
# Duplikate als Multiset zählen: eine Zeile gilt nur dann als Duplikat, wenn
# bereits GENAU SO VIELE identische Buchungen in der DB stehen. So werden echte,
+32
View File
@@ -67,3 +67,35 @@ def test_neutral_category_out_of_stats_but_in_balance(admin):
# Häkchen entfernen -> zählt sofort wieder mit.
admin.patch(f"/api/categories/{uebertrag['id']}", json={"excluded_from_stats": False})
assert _ausgaben(admin) == vorher_ausgaben + 35000
def test_neutral_keyword_matches_both_directions(admin):
"""Ein Kontoübertrag geht auf dem einen Konto raus und kommt auf dem anderen
rein. Die neutrale Kategorie muss deshalb für BEIDE Richtungen greifen –
obwohl sie in einer Ausgabe-Gruppe liegt."""
import io, json
acc = admin.post("/api/accounts", json={
"name": "Giro Beide", "type": "girokonto", "currency": "EUR",
}).json()
uebertrag = _expense_cats(admin, 2)[1]
admin.patch(f"/api/categories/{uebertrag['id']}", json={"excluded_from_stats": True})
admin.post(f"/api/categories/{uebertrag['id']}/keywords", json={"word": "kontouebertrag"})
csv_text = (
"Datum;Betrag;Empfaenger;Zweck\n"
"05.03.2026;-250,00;Kontouebertrag raus;;\n"
"06.03.2026;250,00;Kontouebertrag rein;;\n"
)
rows = admin.post(
"/api/imports/preview",
files={"file": ("k.csv", io.BytesIO(csv_text.encode("utf-8")), "text/csv")},
data={
"account_id": str(acc["id"]), "delimiter": ";", "decimal_sep": ",",
"date_format": "%d.%m.%Y", "encoding": "utf-8", "has_header": "true",
"skip_rows": "0", "amount_mode": "signed",
"column_map": json.dumps({"date": 0, "amount": 1, "payee": 2, "purpose": 3}),
},
).json()["rows"]
assert rows[0]["category_id"] == uebertrag["id"], "Ausgabe-Richtung"
assert rows[1]["category_id"] == uebertrag["id"], "Einnahme-Richtung ebenso"