aboutsummaryrefslogtreecommitdiff
path: root/budget/models.py
diff options
context:
space:
mode:
authorA.Avenel <avenel.alexandre@gmail.com>2013-04-07 20:25:25 +0200
committerA.Avenel <avenel.alexandre@gmail.com>2013-04-07 20:25:25 +0200
commit1fa0cff180d668a8d93d24413fe7832cdbd3e826 (patch)
tree5bd62decbb5b56ea82d881ef5b307d2ff10f8503 /budget/models.py
parentb410e48851b440c279366de1cda151bfef09d9b8 (diff)
downloadihatemoney-mirror-1fa0cff180d668a8d93d24413fe7832cdbd3e826.zip
ihatemoney-mirror-1fa0cff180d668a8d93d24413fe7832cdbd3e826.tar.gz
ihatemoney-mirror-1fa0cff180d668a8d93d24413fe7832cdbd3e826.tar.bz2
use "member.id" instead of "member"
Diffstat (limited to 'budget/models.py')
-rw-r--r--budget/models.py20
1 files changed, 9 insertions, 11 deletions
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: