From b890d13e6e31d63abcbf3083b964b83384e5c87f Mon Sep 17 00:00:00 2001 From: fleaz Date: Fri, 24 Feb 2023 00:05:01 +0100 Subject: [PATCH] stats: Automatic selection of valid tags. Show withdrawals --- schmeckels/stats.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/schmeckels/stats.py b/schmeckels/stats.py index 8f83737..b58dc17 100644 --- a/schmeckels/stats.py +++ b/schmeckels/stats.py @@ -13,16 +13,20 @@ from dateutil.relativedelta import relativedelta def command(): outgoing = {} incoming = {} + withdrawal = {} session = get_session() tags = session.query(Tag).filter_by(reporting=True).all() # Get start and end of timerange - year = 2021 + year = 2022 start_date = date(year=year,month=1,day=1) end_date = date(year=year,month=12,day=31) for tag in tags: + if tag.name.find(":") < 0 and tag.name != "Privatentnahme": + continue + tag_sum = ( session.query(func.sum(Transaction.amount)) .filter(and_(Transaction.date >= start_date, Transaction.date <= end_date)) @@ -30,29 +34,47 @@ def command(): .first()[0] ) + # No transactions with this tag in the given timeframe + if not tag_sum: + continue + if tag_sum < 0: - outgoing[tag] = {"sum": tag_sum} + if tag.name == "Privatentnahme": + withdrawal[tag] = {"sum": tag_sum} + else: + outgoing[tag] = {"sum": tag_sum} else: incoming[tag] = {"sum": tag_sum} sum_outgoing = sum(x["sum"] for x in outgoing.values()) sum_incoming = sum(x["sum"] for x in incoming.values()) + sum_withdrawal = sum(x["sum"] for x in withdrawal.values()) outgoing = {k: format_amount(v) for k, v in sorted(outgoing.items(), key=lambda item: float(item[1]["sum"]))} incoming = {k: format_amount(v) for k, v in sorted(incoming.items(), key=lambda item: float(item[1]["sum"]))} + withdrawawl = {k: format_amount(v) for k, v in sorted(withdrawal.items(), key=lambda item: float(item[1]["sum"]))} print(f"============== Report for {year} =============\n") + + # Ausgaben for name, data in outgoing.items(): print(" {:<30} {:>10} €".format(name.name, data['sum'])) print("-"*44) print(" {:<30} {:>10} €".format("AUSGABEN", format_amount(sum_outgoing))) - print("\n") + # Einnahmen for name, data in incoming.items(): print(" {:<30} {:>10} €".format(name.name, data['sum'])) print("-"*44) print(" {:<30} {:>10} €".format("EINNAHMEN", format_amount(sum_incoming))) + print("\n") + + # Entnahmen + for name, data in withdrawal.items(): + print(" {:<30} {:>10} €".format(name.name, data['sum'])) + print("-"*44) + print(" {:<30} {:>10} €".format("ENTNAHME", format_amount(sum_withdrawal))) print("\n") print("="*44)