From ebef0fed6f07d48d1e2b787ec46b7ce1ab061a5b Mon Sep 17 00:00:00 2001 From: Flo Ha Date: Fri, 16 Sep 2022 09:50:41 +0200 Subject: [PATCH] add detailed finance stuff and reuse euro teplate tag --- gaehsnitz/admin.py | 8 +--- gaehsnitz/management/commands/drink_stats.py | 3 +- .../management/commands/total_balance.py | 13 +++--- gaehsnitz/management/commands/user_stats.py | 17 ++++---- gaehsnitz/static/gaehsnitz/style.css | 21 ++-------- gaehsnitz/templates/gaehsnitz/finance.html | 40 ++++++++++++++++++- gaehsnitz/templatetags/__init__.py | 0 gaehsnitz/templatetags/money.py | 11 +++++ gaehsnitz/views.py | 22 +++++++++- 9 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 gaehsnitz/templatetags/__init__.py create mode 100644 gaehsnitz/templatetags/money.py diff --git a/gaehsnitz/admin.py b/gaehsnitz/admin.py index e7a84dd..0595644 100644 --- a/gaehsnitz/admin.py +++ b/gaehsnitz/admin.py @@ -1,20 +1,14 @@ -from decimal import Decimal -from typing import Union - from django.contrib import admin from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.utils.safestring import mark_safe from gaehsnitz.models import Donation, Payment, Drink, Consumption, Payback +from gaehsnitz.templatetags.money import euro User = get_user_model() -def euro(value: Union[int, float, Decimal]) -> str: - return f"{value:.2f} €" - - class DonationInline(admin.TabularInline): model = Donation extra = 0 diff --git a/gaehsnitz/management/commands/drink_stats.py b/gaehsnitz/management/commands/drink_stats.py index e88196b..6bc5a55 100644 --- a/gaehsnitz/management/commands/drink_stats.py +++ b/gaehsnitz/management/commands/drink_stats.py @@ -2,6 +2,7 @@ from django.core.management import BaseCommand from django.db.models import Sum from gaehsnitz.models import Drink +from gaehsnitz.templatetags.money import euro class Command(BaseCommand): @@ -29,4 +30,4 @@ class Command(BaseCommand): remaining = bought - consumed print(f" übrig: {remaining}") purchase_value = remaining * drink.purchase_price_per_bottle - print(f" Wert: {purchase_value:.2f} €") + print(f" Wert: {euro(purchase_value)}") diff --git a/gaehsnitz/management/commands/total_balance.py b/gaehsnitz/management/commands/total_balance.py index 97a973d..bd3ef15 100644 --- a/gaehsnitz/management/commands/total_balance.py +++ b/gaehsnitz/management/commands/total_balance.py @@ -3,6 +3,7 @@ from django.core.management import BaseCommand from django.db.models import Sum from gaehsnitz.models import Payment, Donation +from gaehsnitz.templatetags.money import euro User = get_user_model() @@ -10,23 +11,23 @@ User = get_user_model() class Command(BaseCommand): def handle(self, *args, **options): all_donations_sum = Donation.objects.all().aggregate(sum=Sum("amount"))["sum"] - print(f"Alle Spenden/Zahlungen: {all_donations_sum:.2f} €") + print(f"Alle Spenden/Zahlungen: {euro(all_donations_sum)}") all_payments_sum = Payment.objects.all().aggregate(sum=Sum("amount"))["sum"] - print(f"Alle Ausgaben: {all_payments_sum:.2f} €") + print(f"Alle Ausgaben: {euro(all_payments_sum)}") balance = all_donations_sum - all_payments_sum print("-------------------------") - print(f"Bilanz: {balance:.2f} €") + print(f"Bilanz: {euro(balance)}") print() print("Leute mit Schulden:") awaited_sum = 0 for user in User.objects.all().order_by("username"): if (value := user.balance) < 0: - print(f"{user}: {-value:.2f} €") + print(f"{user}: {euro(-value)}") awaited_sum -= value print("-------------------------") - print(f"gesamt: {awaited_sum:.2f} €") + print(f"gesamt: {euro(awaited_sum)}") balance_after_payments = balance + awaited_sum print("-------------------------") - print(f"Bilanz danach: {balance_after_payments:.2f} €") + print(f"Bilanz danach: {euro(balance_after_payments)}") print() diff --git a/gaehsnitz/management/commands/user_stats.py b/gaehsnitz/management/commands/user_stats.py index 8f93d62..6a01920 100644 --- a/gaehsnitz/management/commands/user_stats.py +++ b/gaehsnitz/management/commands/user_stats.py @@ -3,6 +3,7 @@ from django.core.management import BaseCommand from django.db.models import Sum from gaehsnitz.models import Drink +from gaehsnitz.templatetags.money import euro User = get_user_model() @@ -21,26 +22,26 @@ class Command(BaseCommand): amount = drink_dict["amount"] drink_list.append(f"{amount}x {name}") drink_list_str = ", ".join(drink_list) - print(f"zu zahlen: {to_pay:.2f} € ({drink_list_str})") + print(f"zu zahlen: {euro(to_pay)} ({drink_list_str})") donated = user.donations_made if donated != 0: - don_list = [f"{don.date:%d.%m.} {don.amount:.2f} €" for don in user.donations.order_by("date")] + don_list = [f"{don.date:%d.%m.} {euro(don.amount)}" for don in user.donations.order_by("date")] don_list_str = ", ".join(don_list) - print(f"gespendet: {donated:.2f} € ({don_list_str})") + print(f"gespendet: {euro(donated)} ({don_list_str})") paid = user.other_payments_made if paid != 0: - pay_list = [f"{pay.purpose} {pay.amount:.2f} €" for pay in user.payments.order_by("date")] + pay_list = [f"{pay.purpose} {euro(pay.amount)}" for pay in user.payments.order_by("date")] pay_list_str = ", ".join(pay_list) - print(f"bezahlt: {paid:.2f} € ({pay_list_str})") + print(f"bezahlt: {euro(paid)} ({pay_list_str})") paid_back = user.paybacks_received if paid_back != 0: - pb_list = [f"{pb.date:%d.%m.} {pb.amount:.2f} €" for pb in user.paybacks.order_by("date")] + pb_list = [f"{pb.date:%d.%m.} {euro(pb.amount)}" for pb in user.paybacks.order_by("date")] pb_list_str = ", ".join(pb_list) - print(f"zurückbekommen: {paid_back:.2f} € ({pb_list_str})") + print(f"zurückbekommen: {euro(paid_back)} ({pb_list_str})") balance = donated + paid - to_pay - paid_back - print(f"==> STAND: {balance:.2f} €") + print(f"==> STAND: {euro(balance)}") print() diff --git a/gaehsnitz/static/gaehsnitz/style.css b/gaehsnitz/static/gaehsnitz/style.css index d412f6f..c681616 100644 --- a/gaehsnitz/static/gaehsnitz/style.css +++ b/gaehsnitz/static/gaehsnitz/style.css @@ -114,8 +114,8 @@ p, ul { } table { - margin-top: 12px; - margin-bottom: 12px; + margin-top: 16px; + margin-bottom: 16px; border-spacing: 1px; } @@ -132,7 +132,8 @@ thead { } td { - padding: 3px 8px; + padding: 3px 12px; + text-align: right; } hr { @@ -172,20 +173,6 @@ hr { max-width: 800px; } -#drinks-table { - font-size: 0.9rem; -} - -#drinks-table td { - padding: 2px 5px; - text-align: right; - white-space: nowrap; -} - -#drinks-table tfoot { - color: #EE9933; -} - .bandbox { display: flex; flex-direction: row; diff --git a/gaehsnitz/templates/gaehsnitz/finance.html b/gaehsnitz/templates/gaehsnitz/finance.html index 230cd87..afadd09 100644 --- a/gaehsnitz/templates/gaehsnitz/finance.html +++ b/gaehsnitz/templates/gaehsnitz/finance.html @@ -1,4 +1,5 @@ {% extends "gaehsnitz/base.html" %} +{% load money %} {% block content %} @@ -40,9 +41,46 @@ ganz transparent eine Übersicht, von wo nach wo eigentlich wie viel Kohle geflossen ist. Nur keine Klarnamen. ;)

+ +

Zusammenfassung

+ + + + + + + + + + + + + +
Summe aller Spenden/Zahlungen{{ total_donations|euro }}
Summe aller Ausgaben{{ total_payments|euro }}
Stand{{ total_balance|euro }}
+ +

Ausgaben

+ + + + + + + + + {% for payment in payments %} + + + + + + {% endfor %} +
ZweckBetragDatum
{{ payment.purpose }}{{ payment.amount|euro }}{% if payment.date %}{{ payment.date|date:"d.m." }}{% else %}-{% endif %}
+

- ... dynamische Listen kommen zeitnah 😎 ... + Details zu den Spenden und Rückzahlungen an die Leute, die die Ausgaben geleistet haben, + lassen wir hier erstmal weg. Wer's ganz genau wissen will, kann ja fragen.

+

Nicht aufgelistet sind kurzfristige Dinge für die Vorbereitungsaktionen, also z.B. Suff und Sprit, den wir für die Arbeitseinsätze gekauft und auch direkt vernichtet haben. Danke an dieser Stelle nochmal allen für die diff --git a/gaehsnitz/templatetags/__init__.py b/gaehsnitz/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gaehsnitz/templatetags/money.py b/gaehsnitz/templatetags/money.py new file mode 100644 index 0000000..fde8a70 --- /dev/null +++ b/gaehsnitz/templatetags/money.py @@ -0,0 +1,11 @@ +from decimal import Decimal +from typing import Union + +from django.template.defaultfilters import register + + +@register.filter(name="euro") +def euro(value: Union[int, float, Decimal]): + if not value: + return "-" + return f"{value:.0f} €" diff --git a/gaehsnitz/views.py b/gaehsnitz/views.py index aac1c3e..82110fd 100644 --- a/gaehsnitz/views.py +++ b/gaehsnitz/views.py @@ -1,7 +1,10 @@ from datetime import date +from django.db.models import Sum from django.views.generic import TemplateView +from gaehsnitz.models import Donation, Payment + festival_start_date, festival_end_date = date(2022, 8, 25), date(2022, 8, 28) @@ -34,8 +37,25 @@ class FinanceView(GaehsnitzTemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - # TODO reimplement + total_donations = Donation.objects.all().aggregate(sum=Sum("amount"))["sum"] + total_payments = Payment.objects.all().aggregate(sum=Sum("amount"))["sum"] + total_balance = total_donations - total_payments + band_sum, displayed_payments = 0, [] + for pay in Payment.objects.order_by("date"): + if pay.purpose.startswith("Band"): + band_sum += pay.amount + else: + displayed_payments.append(pay) + + displayed_payments.append(Payment(purpose="Bands", amount=band_sum)) + + context.update({ + "total_donations": total_donations, + "total_payments": total_payments, + "total_balance": total_balance, + "payments": displayed_payments, + }) return context