stats: Automatic selection of valid tags. Show withdrawals

This commit is contained in:
fleaz 2023-02-24 00:05:01 +01:00
parent d9a1f637f8
commit b890d13e6e
No known key found for this signature in database
GPG key ID: AED15F8FDD04D083

View file

@ -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)