Allows using rules for the nullying step

This commit is contained in:
Luís Murta 2023-02-23 23:24:01 +00:00
parent 1a774e3769
commit ed2dda63e9
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94

View File

@ -9,7 +9,7 @@ class Categorizer:
options = {} options = {}
def __init__(self): def __init__(self):
self.options["null_days"] = 4 self.options["null_days"] = 3
def rules( def rules(
self, self,
@ -28,14 +28,20 @@ class Categorizer:
tags (Sequence[Tag]): currently available tags tags (Sequence[Tag]): currently available tags
""" """
self._nullify(transactions) try:
null = next(cat for cat in categories if cat.name == "null")
print("Nullifying")
self._nullify(transactions, null)
categories = [cat for cat in categories if cat.name != "null"]
except StopIteration:
print("Null category not defined")
self._rule_based_categories(transactions, categories) self._rule_based_categories(transactions, categories)
self._rule_based_tags(transactions, tags) self._rule_based_tags(transactions, tags)
@Timer(name="nullify") @Timer(name="nullify")
def _nullify(self, transactions: Sequence[t.BankTransaction]): def _nullify(self, transactions: Sequence[t.BankTransaction], null: t.Category):
print(f"Nullifying {len(transactions)} transactions")
count = 0 count = 0
matching = [] matching = []
for transaction in transactions: for transaction in transactions:
@ -46,11 +52,13 @@ class Categorizer:
transaction.date - timedelta(days=self.options["null_days"]) transaction.date - timedelta(days=self.options["null_days"])
<= cancel.date <= cancel.date
<= transaction.date + timedelta(days=self.options["null_days"]) <= transaction.date + timedelta(days=self.options["null_days"])
and transaction not in matching
and cancel not in matching
and cancel != transaction and cancel != transaction
and cancel.bank != transaction.bank and cancel.bank != transaction.bank
and cancel.amount == -transaction.amount and cancel.amount == -transaction.amount
and transaction not in matching
and cancel not in matching
and all(r.matches(transaction) for r in null.rules)
and all(r.matches(cancel) for r in null.rules)
) )
): ):
transaction.category = t.TransactionCategory( transaction.category = t.TransactionCategory(
@ -65,7 +73,7 @@ class Categorizer:
count += 2 count += 2
break break
print(f"Nullified {count} transactions") print(f"Nullified {count} of {len(transactions)} transactions")
@Timer(name="categoryrules") @Timer(name="categoryrules")
def _rule_based_categories( def _rule_based_categories(
@ -87,12 +95,14 @@ class Categorizer:
continue continue
# passed all conditions, assign category # passed all conditions, assign category
if transaction.category:
if transaction.category.name == category.name:
continue
if ( if (
transaction.category input(
and transaction.category.name == category.name f"Overwrite {transaction} with {category.name}? (y/n)"
): )
if (
input(f"Overwrite {transaction} with {category}? (y/n)")
== "y" == "y"
): ):
transaction.category.name = category.name transaction.category.name = category.name