Initial montly expenses (#526)

This commit is contained in:
Edwin Smulders 2020-02-09 13:03:52 +01:00 committed by GitHub
parent bf691660ee
commit 02242f2e12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 1 deletions

View file

@ -94,6 +94,18 @@ class Project(db.Model):
for member in self.active_members
]
@property
def monthly_stats(self):
"""Compute expenses by month
:return: a dict of years mapping to a dict of months mapping to the amount
:rtype dict:
"""
monthly = defaultdict(lambda: defaultdict(float))
for bill in self.get_bills().all():
monthly[bill.date.year][bill.date.month] += bill.amount
return monthly
@property
def uses_weights(self):
return len([i for i in self.members if i.weight != 1]) > 0

View file

@ -311,8 +311,9 @@ footer .footer-left {
background: url("../images/see.png") no-repeat right;
}
#bill_table {
#bill_table, #monthly_stats {
margin-top: 30px;
margin-bottom: 30px;
}
.project-actions > .delete,
@ -487,3 +488,9 @@ footer .icon svg {
.icon.icon-white {
fill: white;
}
/* align the first column */
#monthly_stats tr *:first-child {
text-align: right;
width: 200px;
}

View file

@ -21,6 +21,7 @@
{% block content %}
<h2>{{ _("Balance") }}</h2>
<table id="bill_table" class="split_bills table table-striped">
<thead><tr><th>{{ _("Who?") }}</th><th>{{ _("Paid") }}</th><th>{{ _("Spent") }}</th></tr></thead>
<tbody>
@ -33,5 +34,17 @@
{% endfor %}
</tbody>
</table>
<h2>{{ _("Expenses by Month") }}</h2>
<table id="monthly_stats" class="table table-striped">
<thead><tr><th>{{ _("Period") }}</th><th>{{ _("Spent") }}</th></tr></thead>
<tbody>
{% for month in months %}
<tr>
<td>{{ _(month.strftime("%B")) }} {{ month.year }}</td>
<td>{{ "%0.2f"|format(monthly_stats[month.year][month.month]) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

View file

@ -55,6 +55,8 @@ from ihatemoney.utils import (
get_members,
same_bill,
)
from datetime import datetime
from dateutil.relativedelta import relativedelta
main = Blueprint("main", __name__)
@ -737,9 +739,12 @@ def settle_bill():
@main.route("/<project_id>/statistics")
def statistics():
"""Compute what each member has paid and spent and display it"""
today = datetime.now()
return render_template(
"statistics.html",
members_stats=g.project.members_stats,
monthly_stats=g.project.monthly_stats,
months=[today - relativedelta(months=i) for i in range(12)],
current_view="statistics",
)