aboutsummaryrefslogtreecommitdiff
path: root/budget/models.py
diff options
context:
space:
mode:
authorAlexis Metaireau <alexis@notmyidea.org>2011-07-31 23:55:18 +0200
committerAlexis Metaireau <alexis@notmyidea.org>2011-07-31 23:55:18 +0200
commit769bcbf3f10f59a1bce952c336adeb1804a5cc77 (patch)
tree46e7c4266b3a93d4ffa67d77d03e24cf2c5f998a /budget/models.py
parent548101d8bbd5adff4887cfad8fc70d1a4cf82370 (diff)
downloadihatemoney-mirror-769bcbf3f10f59a1bce952c336adeb1804a5cc77.zip
ihatemoney-mirror-769bcbf3f10f59a1bce952c336adeb1804a5cc77.tar.gz
ihatemoney-mirror-769bcbf3f10f59a1bce952c336adeb1804a5cc77.tar.bz2
Makes the computation working.
Diffstat (limited to 'budget/models.py')
-rw-r--r--budget/models.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/budget/models.py b/budget/models.py
index bf9ad92..efb5a3d 100644
--- a/budget/models.py
+++ b/budget/models.py
@@ -1,3 +1,5 @@
+from collections import defaultdict
+
from datetime import datetime
from flaskext.sqlalchemy import SQLAlchemy
@@ -12,6 +14,28 @@ class Project(db.Model):
contact_email = db.Column(db.String)
members = db.relationship("Person", backref="project")
+ @property
+ def active_members(self):
+ return [m for m in self.members if m.activated]
+
+ def get_balance(self):
+
+ balances, should_pay, should_receive = defaultdict(int), defaultdict(int), defaultdict(int)
+
+ # for each person
+ for person in self.members:
+ # get the list of bills he has to pay
+ bills = Bill.query.filter(Bill.owers.contains(person))
+ for bill in bills.all():
+ if person != bill.payer:
+ should_pay[person] += bill.pay_each()
+ should_receive[bill.payer] += bill.pay_each()
+
+ for person in self.members:
+ balances[person] = should_receive[person] - should_pay[person]
+
+ return balances
+
def __repr__(self):
return "<Project %s>" % self.name
@@ -22,7 +46,7 @@ class Person(db.Model):
bills = db.relationship("Bill", backref="payer")
name = db.Column(db.UnicodeText)
- # activated = db.Column(db.Boolean, default=True)
+ activated = db.Column(db.Boolean, default=True)
def __str__(self):
return self.name