feat: CRUD-API Konten/Buchungen/Kategorien/Regeln
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
20
finance/app/services/categorize.py
Normal file
20
finance/app/services/categorize.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.tables import CategoryRule, Transaction
|
||||
|
||||
|
||||
def apply_rules(session: Session, transactions: list[Transaction]) -> int:
|
||||
rules = session.execute(
|
||||
select(CategoryRule).order_by(CategoryRule.priority)).scalars().all()
|
||||
hits = 0
|
||||
for tx in transactions:
|
||||
if tx.category_id is not None:
|
||||
continue
|
||||
haystack = f"{tx.purpose} {tx.counterparty}".lower()
|
||||
for rule in rules:
|
||||
if rule.pattern.lower() in haystack:
|
||||
tx.category_id = rule.category_id
|
||||
hits += 1
|
||||
break
|
||||
return hits
|
||||
Reference in New Issue
Block a user