diff --git a/budget/models.py b/budget/models.py
index 8f57ffd..1f3fa4f 100644
--- a/budget/models.py
+++ b/budget/models.py
@@ -45,21 +45,19 @@ class Project(db.Model):
for person in self.members:
balance = should_receive[person] - should_pay[person]
- balances[person] = round(balance, 2)
+ balances[person.id] = round(balance, 2)
return balances
- def settle_bill(self):
+ def settle_bills(self):
"""Return a list of transactions that could be made to settle the bill"""
- balances = self.balance
- credits, debts = list(), list()
- transactions = list()
+ credits, debts, transactions = [],[],[]
# Create lists of credits and debts
- for person in balances.keys():
- if balances[person] > 0:
- credits.append({"person": person, "balance": balances[person]})
- elif balances[person] < 0:
- debts.append({"person": person, "balance": -balances[person]})
+ for person in self.members:
+ if self.balance[person.id] > 0:
+ credits.append({"person": person, "balance": self.balance[person.id]})
+ elif self.balance[person.id] < 0:
+ debts.append({"person": person, "balance": -self.balance[person.id]})
# Try and find exact matches
for credit in credits:
match = self.exactmatch(credit["balance"], debts)
@@ -83,7 +81,7 @@ class Project(db.Model):
def exactmatch(self, credit, debts):
"""Recursively try and find subsets of 'debts' whose sum is equal to credit"""
if not debts:
- return []
+ return None
if debts[0]["balance"] > credit:
return self.exactmatch(credit, debts[1:])
elif debts[0]["balance"] == credit:
diff --git a/budget/templates/list_bills.html b/budget/templates/list_bills.html
index a39a78a..ee97624 100644
--- a/budget/templates/list_bills.html
+++ b/budget/templates/list_bills.html
@@ -61,20 +61,20 @@
{% set balance = g.project.balance %}
- {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %}
-
+ {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
+
{{ member.name }} |
{% if member.activated %}
- |
{% else %}
- |
{% endif %}
-
- {% if balance[member] > 0 %}+{% endif %}{{ balance[member] }}
+ |
+ {% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
|
{% endfor %}
diff --git a/budget/templates/settle_bill.html b/budget/templates/settle_bills.html
similarity index 71%
rename from budget/templates/settle_bill.html
rename to budget/templates/settle_bills.html
index d04ff63..309642a 100644
--- a/budget/templates/settle_bill.html
+++ b/budget/templates/settle_bills.html
@@ -5,7 +5,7 @@
{% endblock %}
{% block navbar %}
{{ _("Bills") }}
- {{ _("Settle") }}
+ {{ _("Settle") }}
{% endblock %}
{% block sidebar %}
@@ -14,11 +14,11 @@
{% set balance = g.project.balance %}
- {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member] != 0 %}
-
+ {% for member in g.project.members | sort(attribute='name') if member.activated or balance[member.id] != 0 %}
+
{{ member.name }} |
-
- {% if balance[member] > 0 %}+{% endif %}{{ balance[member] }}
+ |
+ {% if balance[member.id] > 0 %}+{% endif %}{{ balance[member.id] }}
|
{% endfor %}
diff --git a/budget/tests.py b/budget/tests.py
index d848112..983b352 100644
--- a/budget/tests.py
+++ b/budget/tests.py
@@ -436,9 +436,9 @@ class BudgetTestCase(TestCase):
balance = models.Project.query.get("raclette").balance
result = {}
- result[models.Project.query.get("raclette").members[0]] = 8.12
- result[models.Project.query.get("raclette").members[1]] = 0.0
- result[models.Project.query.get("raclette").members[2]] = -8.12
+ result[models.Project.query.get("raclette").members[0].id] = 8.12
+ result[models.Project.query.get("raclette").members[1].id] = 0.0
+ result[models.Project.query.get("raclette").members[2].id] = -8.12
self.assertDictEqual(balance, result)
def test_edit_project(self):
@@ -472,7 +472,7 @@ class BudgetTestCase(TestCase):
def test_settle_page(self):
self.post_project("raclette")
- response = self.app.get("/raclette/settle_bill")
+ response = self.app.get("/raclette/settle_bills")
self.assertEqual(response.status_code, 200)
def test_settle(self):
@@ -510,7 +510,7 @@ class BudgetTestCase(TestCase):
'amount': '10',
})
project = models.Project.query.get('raclette')
- transactions = project.settle_bill()
+ transactions = project.settle_bills()
members = defaultdict(int)
#We should have the same values between transactions and project balances
for t in transactions:
@@ -518,7 +518,7 @@ class BudgetTestCase(TestCase):
members[t['payer']]+=t['amount']
balance = models.Project.query.get("raclette").balance
for m, a in members.items():
- self.assertEqual(a, balance[m])
+ self.assertEqual(a, balance[m.id])
return
diff --git a/budget/web.py b/budget/web.py
index a9f70cb..f975587 100644
--- a/budget/web.py
+++ b/budget/web.py
@@ -383,11 +383,11 @@ def change_lang(lang):
return redirect(request.headers.get('Referer') or url_for('.home'))
-@main.route("//settle_bill")
+@main.route("//settle_bills")
def settle_bill():
"""Compute the sum each one have to pay to each other and display it"""
- bills = g.project.settle_bill()
- return render_template("settle_bill.html", bills=bills)
+ bills = g.project.settle_bills()
+ return render_template("settle_bills.html", bills=bills)
@main.route("//archives/create", methods=["GET", "POST"])