diff options
| -rw-r--r-- | ihatemoney/models.py | 12 | ||||
| -rw-r--r-- | ihatemoney/static/css/main.css | 9 | ||||
| -rw-r--r-- | ihatemoney/templates/statistics.html | 13 | ||||
| -rw-r--r-- | ihatemoney/web.py | 5 |
4 files changed, 38 insertions, 1 deletions
diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 250f009..8a7e273 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -95,6 +95,18 @@ class Project(db.Model): ] @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 43df23d..1a7dc1f 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 0cfe1f2..539781f 100644 --- a/ihatemoney/templates/statistics.html +++ b/ihatemoney/templates/statistics.html @@ -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 %} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index be39feb..d45190c 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("/<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", ) |
