diff --git a/budget/models.py b/budget/models.py index 9538670..0670d6e 100644 --- a/budget/models.py +++ b/budget/models.py @@ -61,9 +61,9 @@ class Project(db.Model): credits, debts, transactions = [],[],[] # Create lists of credits and debts for person in self.members: - if balance[person.id] > 0: + if round(balance[person.id], 2) > 0: credits.append({"person": person, "balance": balance[person.id]}) - elif balance[person.id] < 0: + elif round(balance[person.id], 2) < 0: debts.append({"person": person, "balance": -balance[person.id]}) # Try and find exact matches for credit in credits: diff --git a/budget/templates/list_bills.html b/budget/templates/list_bills.html index f081334..72b9032 100644 --- a/budget/templates/list_bills.html +++ b/budget/templates/list_bills.html @@ -65,7 +65,7 @@
{% set balance = g.project.balance %} - {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %} + {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id]|round(2) != 0 %} {% endif %} - {% endfor %} diff --git a/budget/templates/settle_bills.html b/budget/templates/settle_bills.html index 4066b16..16c60b3 100644 --- a/budget/templates/settle_bills.html +++ b/budget/templates/settle_bills.html @@ -11,11 +11,11 @@
{{ member.name }} (x{{ member.weight|minimal_round(1) }}) @@ -82,8 +82,8 @@
- {% if balance[member.id] > 0 %}+{% endif %}{{ "%.2f" | format(balance[member.id]) }} + + {% if balance[member.id]|round(2) > 0 %}+{% endif %}{{ "%.2f" | format(balance[member.id]) }}
{% set balance = g.project.balance %} - {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %} + {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id]|round(2) != 0 %} - {% endfor %} diff --git a/budget/tests.py b/budget/tests.py index eea7537..f0a5ea7 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -580,6 +580,46 @@ class BudgetTestCase(TestCase): self.assertEqual(a, balance[m.id]) return + def test_settle_zero(self): + self.post_project("raclette") + + # add members + self.app.post("/raclette/members/add", data={'name': 'alexis'}) + self.app.post("/raclette/members/add", data={'name': 'fred'}) + self.app.post("/raclette/members/add", data={'name': 'tata'}) + + # create bills + self.app.post("/raclette/add", data={ + 'date': '2016-12-31', + 'what': u'fromage à raclette', + 'payer': 1, + 'payed_for': [1, 2, 3], + 'amount': '10.0', + }) + + self.app.post("/raclette/add", data={ + 'date': '2016-12-31', + 'what': u'red wine', + 'payer': 2, + 'payed_for': [1, 3], + 'amount': '20', + }) + + self.app.post("/raclette/add", data={ + 'date': '2017-01-01', + 'what': u'refund', + 'payer': 3, + 'payed_for': [2], + 'amount': '13.33', + }) + project = models.Project.query.get('raclette') + transactions = project.get_transactions_to_settle_bill() + members = defaultdict(int) + # There should not be any zero-amount transfer after rounding + for t in transactions: + rounded_amount = round(t['amount'], 2) + self.assertNotEqual(0.0, rounded_amount, + msg='%f is equal to zero after rounding' % t['amount']) class APITestCase(TestCase):
{{ member.name }} - {% if balance[member.id] > 0 %}+{% endif %}{{ "%.2f" | format(balance[member.id]) }} + + {% if balance[member.id]|round(2) > 0 %}+{% endif %}{{ "%.2f" | format(balance[member.id]) }}