Skip to content

Commit

Permalink
Initial montly expenses (spiral-project#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dutchy- committed Feb 9, 2020
1 parent 997bab6 commit dfd3deb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ihatemoney/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def members_stats(self):
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
Expand Down
9 changes: 8 additions & 1 deletion ihatemoney/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions ihatemoney/templates/statistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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 %}
5 changes: 5 additions & 0 deletions ihatemoney/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
get_members,
same_bill,
)
from datetime import datetime
from dateutil.relativedelta import relativedelta

main = Blueprint("main", __name__)

Expand Down Expand Up @@ -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",
)

Expand Down

0 comments on commit dfd3deb

Please sign in to comment.