diff --git a/pfbudget/__main__.py b/pfbudget/__main__.py index b8f312e..df8dd3a 100644 --- a/pfbudget/__main__.py +++ b/pfbudget/__main__.py @@ -1,3 +1,6 @@ +from decimal import Decimal +from typing import Sequence + from pfbudget.cli.runnable import argparser from pfbudget.common.types import Operation from pfbudget.core.manager import Manager @@ -26,6 +29,10 @@ def interactive(manager: Manager): next = False quit = True + case "split": + manager.action(Operation.Split, split(transaction, categories)) + next = False + case "tag": tag = input("tag: ") if tag not in [t.name for t in tags]: @@ -48,10 +55,46 @@ def interactive(manager: Manager): ) next = False + session.commit() if quit: break +def split( + original: type.Transaction, categories: Sequence[type.Category] +) -> list[type.Transaction]: + + total = original.amount + splitted: list[type.Transaction] = [] + + while True: + if abs(sum(t.amount for t in splitted)) > abs(total): + print( + "The total amount from the splitted transactions exceeds the original transaction amount, please try again..." + ) + splitted.clear() + + if sum(t.amount for t in splitted) == total: + break + + while (category := input("New transaction category: ")) not in [ + c.name for c in categories + ]: + print(f"{category} is not a valid category") + + amount = input("amount: ") + + split = type.Transaction(original.date, original.description, Decimal(amount)) + split.category = type.TransactionCategory( + category, type.CategorySelector(type.Selector_T.manual) + ) + + splitted.append(split) + + splitted.insert(0, original) + return splitted + + if __name__ == "__main__": argparser = argparser() args = vars(argparser.parse_args()) diff --git a/pfbudget/core/categorizer.py b/pfbudget/core/categorizer.py index 1e0e60a..c15ebe0 100644 --- a/pfbudget/core/categorizer.py +++ b/pfbudget/core/categorizer.py @@ -35,6 +35,7 @@ class Categorizer: @Timer(name="nullify") def _nullify(self, transactions: Sequence[t.BankTransaction]): + print(f"Nullifying {len(transactions)} transactions") count = 0 matching = [] for transaction in transactions: @@ -72,6 +73,7 @@ class Categorizer: transactions: Sequence[t.BankTransaction], categories: Sequence[t.Category], ): + print(f"Categorizing {len(transactions)} transactions") d = {} for category in [c for c in categories if c.rules]: for rule in category.rules: @@ -112,6 +114,7 @@ class Categorizer: def _rule_based_tags( self, transactions: Sequence[t.BankTransaction], tags: Sequence[t.Tag] ): + print(f"Tagging {len(transactions)} transactions") d = {} for tag in [t for t in tags if len(t.rules) > 0]: for rule in tag.rules: