63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
from dataclasses import dataclass
|
|
from typing import Dict, Any, List
|
|
import config
|
|
from models import Receipt, Transaction
|
|
|
|
@dataclass
|
|
class AIRule:
|
|
name: str
|
|
condition: str
|
|
action: str
|
|
source: str
|
|
status: str = "active"
|
|
|
|
class AIRulesEngine:
|
|
def __init__(self):
|
|
self.rules: List[AIRule] = []
|
|
self._load_default_rules()
|
|
|
|
def _load_default_rules(self):
|
|
self.rules = [
|
|
AIRule("exact_amount_match", "amount_diff <= 0.01", "auto_approve", "system"),
|
|
AIRule("same_vendor_same_date", "vendor_match and date_diff <= 1", "high_confidence", "system"),
|
|
AIRule("gas_station_pattern", "vendor contains 'gas' or 'fuel'", "categorize_transport", "system")
|
|
]
|
|
|
|
def apply_rules(self, receipt: Receipt, transaction: Transaction) -> Dict[str, Any]:
|
|
results = {"auto_approve": False, "confidence_boost": 0, "category": None}
|
|
|
|
for rule in self.rules:
|
|
if rule.status != "active":
|
|
continue
|
|
|
|
if self._evaluate_condition(rule.condition, receipt, transaction):
|
|
self._execute_action(rule.action, results, receipt, transaction)
|
|
|
|
return results
|
|
|
|
def _evaluate_condition(self, condition: str, receipt: Receipt, transaction: Transaction) -> bool:
|
|
amount_diff = abs(receipt.amount - transaction.amount)
|
|
date_diff = abs((receipt.receipt_date - transaction.transaction_date).days)
|
|
vendor_match = receipt.vendor.lower() in transaction.vendor.lower() or transaction.vendor.lower() in receipt.vendor.lower()
|
|
|
|
return eval(condition, {
|
|
"amount_diff": amount_diff,
|
|
"date_diff": date_diff,
|
|
"vendor_match": vendor_match,
|
|
"receipt": receipt,
|
|
"transaction": transaction
|
|
})
|
|
|
|
def _execute_action(self, action: str, results: Dict[str, Any], receipt: Receipt, transaction: Transaction):
|
|
if action == "auto_approve":
|
|
results["auto_approve"] = True
|
|
elif action == "high_confidence":
|
|
results["confidence_boost"] += 0.2
|
|
elif action == "categorize_transport":
|
|
results["category"] = "Transportation"
|
|
|
|
def add_rule(self, rule: AIRule):
|
|
self.rules.append(rule)
|
|
|
|
def remove_rule(self, rule_name: str):
|
|
self.rules = [r for r in self.rules if r.name != rule_name] |