aboutsummaryrefslogtreecommitdiff
path: root/budget/models.py
diff options
context:
space:
mode:
authorAlexis Metaireau <alexis@notmyidea.org>2016-06-17 10:06:19 +0200
committerGitHub <noreply@github.com>2016-06-17 10:06:19 +0200
commit5084cafe6bcd266bd1e676fc6921a7dba3c48a57 (patch)
tree7c2d5f915a5f37d086e31b61cb71fd20dbf4b385 /budget/models.py
parent789196721584ca4800e4236eee36955e78761346 (diff)
parentc49a355eb082cff208806f785d52567ddd043c03 (diff)
downloadihatemoney-mirror-5084cafe6bcd266bd1e676fc6921a7dba3c48a57.zip
ihatemoney-mirror-5084cafe6bcd266bd1e676fc6921a7dba3c48a57.tar.gz
ihatemoney-mirror-5084cafe6bcd266bd1e676fc6921a7dba3c48a57.tar.bz2
Merge pull request #131 from JocelynDelalande/members-weights
Added Members weights handling
Diffstat (limited to 'budget/models.py')
-rw-r--r--budget/models.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/budget/models.py b/budget/models.py
index 727200f..852b3e1 100644
--- a/budget/models.py
+++ b/budget/models.py
@@ -40,8 +40,9 @@ class Project(db.Model):
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()
+ share = bill.pay_each() * person.weight
+ should_pay[person] += share
+ should_receive[bill.payer] += share
for person in self.members:
balance = should_receive[person] - should_pay[person]
@@ -49,6 +50,10 @@ class Project(db.Model):
return balances
+ @property
+ def uses_weights(self):
+ return len([i for i in self.members if i.weight != 1]) > 0
+
def get_transactions_to_settle_bill(self):
"""Return a list of transactions that could be made to settle the bill"""
#cache value for better performance
@@ -152,13 +157,14 @@ class Person(db.Model):
query_class = PersonQuery
- _to_serialize = ("id", "name", "activated")
+ _to_serialize = ("id", "name", "weight", "activated")
id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.String(64), db.ForeignKey("project.id"))
bills = db.relationship("Bill", backref="payer")
name = db.Column(db.UnicodeText)
+ weight = db.Column(db.Float, default=1)
activated = db.Column(db.Boolean, default=True)
def has_bills(self):
@@ -217,9 +223,10 @@ class Bill(db.Model):
archive = db.Column(db.Integer, db.ForeignKey("archive.id"))
def pay_each(self):
- """Compute what each person has to pay"""
+ """Compute what each share has to pay"""
if self.owers:
- return self.amount / len(self.owers)
+ # FIXME: SQL might dot that more efficiently
+ return self.amount / sum(i.weight for i in self.owers)
else:
return 0