diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 250f009b..8a7e2734 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -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 diff --git a/ihatemoney/static/css/main.css b/ihatemoney/static/css/main.css index 43df23d8..1a7dc1fb 100644 --- a/ihatemoney/static/css/main.css +++ b/ihatemoney/static/css/main.css @@ -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; +} diff --git a/ihatemoney/templates/statistics.html b/ihatemoney/templates/statistics.html index 0cfe1f2f..539781ff 100644 --- a/ihatemoney/templates/statistics.html +++ b/ihatemoney/templates/statistics.html @@ -21,6 +21,7 @@ {% block content %} +

{{ _("Balance") }}

@@ -33,5 +34,17 @@ {% endfor %}
{{ _("Who?") }}{{ _("Paid") }}{{ _("Spent") }}
+

{{ _("Expenses by Month") }}

+ + + + {% for month in months %} + + + + + {% endfor %} + +
{{ _("Period") }}{{ _("Spent") }}
{{ _(month.strftime("%B")) }} {{ month.year }}{{ "%0.2f"|format(monthly_stats[month.year][month.month]) }}
{% endblock %} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index be39feb8..d45190c8 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -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("//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", )