]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/model/expenditure.py
f7e69344dc2cf88bf09cb7f0fa6ea9a81fdc4cb9
[bluechips.git] / bluechips / model / expenditure.py
1 from bluechips.model.user import User
2 from bluechips.model.split import Split
3 from bluechips.model import meta
4 from bluechips.model.types import Currency
5 from decimal import Decimal
6 from datetime import datetime
7 import random
8
9 class Expenditure(object):
10     def __init__(self, spender=None, amount=Currency(0), description=u"",
11                  date=None):
12         self.spender = spender
13         self.amount = amount
14         self.description = description
15         if self.date == None:
16             self.date = datetime.now()
17     
18     def __repr__(self):
19         return '<Expenditure: spender: %s spent: %s>' % (self.spender,
20                                                          self.amount)
21
22     def even_split(self):
23         """
24         Split up an expenditure evenly among the resident users
25         """
26         
27         residents = meta.Session.query(User).filter(User.resident==True)
28         split_percentage = Decimal(100) / Decimal(residents.count())
29         self.split(dict((resident, split_percentage) for resident in residents))
30     
31     def update_split(self):
32         """
33         Re-split an expenditure using the same percentages as what is
34         currently in the database
35         """
36         
37         old_splits = meta.Session.query(Split).filter(Split.expenditure==self)
38         split_dict = dict((s.user, Decimal(int(s.share))) for s in old_splits)
39         self.split(split_dict)
40     
41     def split(self, split_dict):
42         """
43         Split up an expenditure.
44         
45         split_dict should be a dict mapping from bluechips.model:User
46         objects to a decimal:Decimal object representing the percentage
47         that user is responsible for.
48         
49         Percentages will be normalized to sum to 100%.
50         
51         If the split leaks or gains money due to rounding errors, the
52         pennies will be randomly distributed to one of the users.
53         
54         I mean, come on. You're already living together. Are you really
55         going to squabble over a few pennies?
56         """
57         
58         map(meta.Session.delete, meta.Session.query(Split).\
59                 filter_by(expenditure_id=self.id))
60         
61         total = sum(split_dict.itervalues())
62         
63         for user, share in split_dict.items():
64             if share == 0:
65                 del split_dict[user]
66             else:
67                 split_dict[user] = share / total
68             
69         amounts_dict = dict()
70         
71         for user, share in split_dict.iteritems():
72             amounts_dict[user] = Currency(split_dict[user] * self.amount)
73         
74         difference = self.amount - sum(amounts_dict.itervalues())
75         
76         if difference > 0:
77             for i in xrange(difference):
78                 winner = random.choice(amounts_dict.keys())
79                 amounts_dict[winner] += Currency(1)
80         elif difference < 0:
81             for i in xrange(-difference):
82                 winner = random.choice(amounts_dict.keys())
83                 amounts_dict[winner] -= Currency(1)
84         
85         for user, share in amounts_dict.iteritems():
86             s = Split(self, user, share)
87             meta.Session.add(s)
88
89 __all__ = ['Expenditure']