partly calculate analysis and improve amount formatting
This commit is contained in:
@@ -49,12 +49,24 @@
|
|||||||
<h2>Analysis</h2>
|
<h2>Analysis</h2>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>avg outgoing transactions / month</td>
|
||||||
|
<td>{{ analysis.avg_monthly_out|euro }}</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>avg incoming transactions / month</td>
|
<td>avg incoming transactions / month</td>
|
||||||
|
<td>{{ analysis.avg_monthly_in|euro }}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>irregular expenses / month</td>
|
||||||
<td>0</td>
|
<td>0</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>avg outgoing transactions / month</td>
|
<td>irregular expenses / week</td>
|
||||||
|
<td>0</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>irregular expenses / day</td>
|
||||||
<td>0</td>
|
<td>0</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ from decimal import Decimal
|
|||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
|
from financeplanner.utils import format_price
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name="euro")
|
@register.filter(name="euro")
|
||||||
def euro(value):
|
def euro(value):
|
||||||
assert isinstance(value, Decimal)
|
assert isinstance(value, Decimal)
|
||||||
return str(value) + "€"
|
return format_price(value) or "-"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ def round_with_dec_places(number, places):
|
|||||||
def format_price(number):
|
def format_price(number):
|
||||||
if number is None:
|
if number is None:
|
||||||
return None
|
return None
|
||||||
return f"{round_with_dec_places(number, 2)}€"
|
return f"{round_with_dec_places(number, 2)} €"
|
||||||
|
|
||||||
|
|
||||||
def _count_rates(start, end, months_delta):
|
def _count_rates(start, end, months_delta):
|
||||||
|
|||||||
@@ -176,6 +176,27 @@ def _build_graph_data(range_start, range_end, calculation_start, balance_list, a
|
|||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
|
def _calculate_analysis(user):
|
||||||
|
result = {}
|
||||||
|
|
||||||
|
today = datetime.now().date()
|
||||||
|
booked = Q(booking_date__lte=today)
|
||||||
|
outgoing = Q(amount__gt=0)
|
||||||
|
incoming = Q(amount__lt=0)
|
||||||
|
recurring = Q(recurring_months__isnull=False) & (
|
||||||
|
Q(not_recurring_after__isnull=True) | Q(not_recurring_after__gte=today))
|
||||||
|
|
||||||
|
out_trans = user.transactions.filter(booked & outgoing & recurring)
|
||||||
|
out_trans_per_month = [t.amount / t.recurring_months for t in out_trans]
|
||||||
|
result["avg_monthly_out"] = sum(out_trans_per_month)
|
||||||
|
|
||||||
|
in_trans = user.transactions.filter(booked & incoming & recurring)
|
||||||
|
in_trans_per_month = [t.amount / t.recurring_months for t in in_trans]
|
||||||
|
result["avg_monthly_in"] = -sum(in_trans_per_month)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def index(request):
|
def index(request):
|
||||||
user = request.user
|
user = request.user
|
||||||
@@ -190,6 +211,7 @@ def index(request):
|
|||||||
graph_data = _build_graph_data(range_start, range_end, calculation_start, balance_list, actual_transactions)
|
graph_data = _build_graph_data(range_start, range_end, calculation_start, balance_list, actual_transactions)
|
||||||
else:
|
else:
|
||||||
graph_data = None
|
graph_data = None
|
||||||
|
analysis = _calculate_analysis(user)
|
||||||
context = {
|
context = {
|
||||||
"range_start": range_start,
|
"range_start": range_start,
|
||||||
"range_end": range_end,
|
"range_end": range_end,
|
||||||
@@ -198,5 +220,6 @@ def index(request):
|
|||||||
"transaction_list": transaction_list,
|
"transaction_list": transaction_list,
|
||||||
"actual_transactions": sorted(actual_transactions, key=lambda t: t[0]),
|
"actual_transactions": sorted(actual_transactions, key=lambda t: t[0]),
|
||||||
"graph_data": graph_data,
|
"graph_data": graph_data,
|
||||||
|
"analysis": analysis,
|
||||||
}
|
}
|
||||||
return render(request, "financeplanner/index.html", context)
|
return render(request, "financeplanner/index.html", context)
|
||||||
|
|||||||
Reference in New Issue
Block a user