]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
Allow Splits to be mathematical expressions
[bluechips.git] / bluechips / model / expenditure.py
index 9d0fc7e0f981ad2719f2435789199a934b5c5bca..8fd706bfd5c358046c66ec2e68d80852c1365fb2 100644 (file)
@@ -28,17 +28,7 @@ class Expenditure(object):
         split_percentage = Decimal(100) / Decimal(residents.count())
         self.split(dict((resident, split_percentage) for resident in residents))
     
-    def update_split(self):
-        """
-        Re-split an expenditure using the same percentages as what is
-        currently in the database
-        """
-        
-        old_splits = meta.Session.query(Split).filter(Split.expenditure==self)
-        split_dict = dict((s.user, Decimal(int(s.share))) for s in old_splits)
-        self.split(split_dict)
-    
-    def split(self, split_dict):
+    def split(self, split_dict, split_text_dict):
         """
         Split up an expenditure.
         
@@ -63,13 +53,11 @@ class Expenditure(object):
         for user, share in split_dict.items():
             if share == 0:
                 del split_dict[user]
-            else:
-                split_dict[user] = share / total
             
         amounts_dict = dict()
         
         for user, share in split_dict.iteritems():
-            amounts_dict[user] = Currency(split_dict[user] * self.amount)
+            amounts_dict[user] = Currency((share * self.amount) / total)
         
         difference = self.amount - sum(amounts_dict.itervalues())
         
@@ -83,13 +71,14 @@ class Expenditure(object):
                 amounts_dict[winner] -= Currency(1)
         
         for user, share in amounts_dict.iteritems():
-            s = Split(self, user, share)
+            s = Split(self, user, share, split_text_dict[user])
             meta.Session.add(s)
 
     def involves(self, user):
         "Returns True if ``user`` is involved in this expenditure."
-        return any((split.user == user) and (split.share != 0)
-                   for split in self.splits)
+        return (any((split.user == user) and (split.share != 0)
+                    for split in self.splits) or
+                (self.spender == user))
 
     def share(self, user):
         "Return the share corresponding to ``user``."