partly calculate analysis and improve amount formatting
This commit is contained in:
@@ -49,12 +49,24 @@
|
||||
<h2>Analysis</h2>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>avg outgoing transactions / month</td>
|
||||
<td>{{ analysis.avg_monthly_out|euro }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>avg incoming transactions / month</td>
|
||||
<td>{{ analysis.avg_monthly_in|euro }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>irregular expenses / month</td>
|
||||
<td>0</td>
|
||||
</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>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
||||
@@ -2,10 +2,12 @@ from decimal import Decimal
|
||||
|
||||
from django import template
|
||||
|
||||
from financeplanner.utils import format_price
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter(name="euro")
|
||||
def euro(value):
|
||||
assert isinstance(value, Decimal)
|
||||
return str(value) + "€"
|
||||
return format_price(value) or "-"
|
||||
|
||||
@@ -176,6 +176,27 @@ def _build_graph_data(range_start, range_end, calculation_start, balance_list, a
|
||||
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
|
||||
def index(request):
|
||||
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)
|
||||
else:
|
||||
graph_data = None
|
||||
analysis = _calculate_analysis(user)
|
||||
context = {
|
||||
"range_start": range_start,
|
||||
"range_end": range_end,
|
||||
@@ -198,5 +220,6 @@ def index(request):
|
||||
"transaction_list": transaction_list,
|
||||
"actual_transactions": sorted(actual_transactions, key=lambda t: t[0]),
|
||||
"graph_data": graph_data,
|
||||
"analysis": analysis,
|
||||
}
|
||||
return render(request, "financeplanner/index.html", context)
|
||||
|
||||
Reference in New Issue
Block a user