better report generation

This commit is contained in:
Felix Breidenstein 2020-06-17 22:01:21 +02:00
parent 72fba9facd
commit dc3c2b309c
2 changed files with 19 additions and 71 deletions

View file

@ -8,6 +8,7 @@ from sqlalchemy import func
from weasyprint import HTML
from datetime import date
from helper import format_amount
from pprint import pprint
@click.command(name="report")
@ -18,45 +19,18 @@ def command(profile, year):
year = date.today().year
session = get_session(profile)
result = (
session.query(
Transaction.category_id,
Category.name,
func.sum(Transaction.amount).label("total"),
)
.group_by(Transaction.category_id)
.join(Category, Transaction.category_id == Category.id)
.all()
)
main_categories = session.query(Category).filter(Category.parent_id == None).all()
sum_of_categories = []
for r in result:
tmp = {}
tmp["name"] = r.name
tmp["total"] = r.total
tmp["total_formatted"] = format_amount(r.total)
tmp["full_name"] = session.query(Category).get(r.category_id).full_name()
sum_of_categories.append(tmp)
data = {}
categories = sorted(sum_of_categories, key=lambda i: i["name"])
einnahmen = [c for c in categories if c["total"] > 0]
ausgaben = [c for c in categories if c["total"] < 0]
sum_einnahmen = sum(c["total"] for c in einnahmen)
sum_ausgaben = sum(c["total"] for c in ausgaben)
total = {
"einnahmen": format_amount(sum_einnahmen),
"ausgaben": format_amount(sum_ausgaben),
"total": format_amount(sum_einnahmen + sum_ausgaben),
}
context = {
"einnahmen": einnahmen,
"ausgaben": ausgaben,
"year": year,
"total": total,
}
for mc in main_categories:
data[mc.name] = {}
sub_categories = session.query(Category).filter(Category.parent_id == mc.id).all()
for sc in sub_categories:
transactions = session.query(Transaction).filter(Transaction.category_id == sc.id).all()
data[mc.name][sc.name] = format_amount(sum([x.amount for x in transactions]))
context = {"year": year, "data": data}
tpl = template("views/report.tpl", context)
filename = "output/report_{}_{}.pdf".format(profile, year)

View file

@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>EUR {{ year }}</title>
<title>Report {{ year }}</title>
<style>
@page {
size: 21cm 29.7cm;
@ -60,47 +60,21 @@
<body>
<h2>Einnahmenüberschussrechnung {{ year }}</h2>
<h2>Kontoübersicht {{ year }}</h2>
<hr />
<div class="content">
<h4>Einnahmen</h4>
% for name, entries in data.items():
<h4>{{ name }}</h4>
<table>
% for c in einnahmen:
% for sc, amount in entries.items():
<tr>
<td class="category-name">{{ c["name"] }}</td>
<td class="amount"> {{ c["total_formatted"] }} €</td>
<td class="category-name">{{ sc }}</td>
<td class="amount"> {{ amount }} €</td>
</tr>
% end
<tr class="sum-column">
<td class="category-name">Summe</td>
<td class="amount text-green"> {{ total["einnahmen"] }} €</td>
</tr>
</table>
<h4>Ausgaben</h4>
<table>
% for c in ausgaben:
<tr>
<td class="category-name">{{ c["name"] }}</td>
<td class="amount"> {{ c["total_formatted"] }} €</td>
</tr>
% end
<tr class="sum-column">
<td class="category-name">Summe</td>
<td class="amount text-red"> {{ total["ausgaben"] }} €</td>
</tr>
</table>
<br>
<table>
<tr class="sum-column-total">
<td class="category-name"><b>Summe</b></td>
<td class="amount text-green"> {{ total["total"] }} €</td>
</tr>
</table>
</div>
% end
</body>