diff --git a/ihatemoney/models.py b/ihatemoney/models.py index d072c555..8385ae74 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -250,13 +250,37 @@ class Project(db.Model): def get_bills(self): """Return the list of bills related to this project""" + return self.order_bills(self.get_bills_unordered()) + + @staticmethod + def order_bills(query): return ( - self.get_bills_unordered() - .order_by(Bill.date.desc()) + query.order_by(Bill.date.desc()) .order_by(Bill.creation_date.desc()) .order_by(Bill.id.desc()) ) + def get_bill_weights(self): + """ + Return all bills for this project, along with the sum of weight for each bill. + Each line is a (float, Bill) tuple. + + Result is unordered. + """ + return ( + db.session.query(func.sum(Person.weight), Bill) + .options(orm.subqueryload(Bill.owers)) + .select_from(Person) + .join(billowers, Bill, Project) + .filter(Person.project_id == Project.id) + .filter(Project.id == self.id) + .group_by(Bill.id) + ) + + def get_bill_weights_ordered(self): + """Ordered version of get_bill_weights""" + return self.order_bills(self.get_bill_weights()) + def get_member_bills(self, member_id): """Return the list of bills related to a specific member""" return ( diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index dda16f2e..44aa1bb5 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -1,7 +1,7 @@ {% extends "sidebar_table_layout.html" %} -{%- macro bill_amount(bill, currency=bill.original_currency, amount=bill.amount) %} - {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=bill.pay_each_default(amount)|currency(currency)) }}) +{%- macro weighted_bill_amount(bill, weights, currency=bill.original_currency, amount=bill.amount) %} + {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=(amount / weights)|currency(currency)) }}) {% endmacro -%} {% block title %} - {{ g.project.name }}{% endblock %} @@ -109,7 +109,7 @@