]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/status.py
d19dd4ad6139853300356c57afc891b63b05ed32
[bluechips.git] / bluechips / controllers / status.py
1 """
2 Calculate the current state of the books
3 """
4
5 import logging
6
7 from bluechips.lib.base import *
8 from bluechips.lib.totals import *
9
10 import sqlalchemy
11
12 from datetime import date, timedelta
13
14 from bluechips.model.types import Currency
15
16 from pylons import request
17
18 log = logging.getLogger(__name__)
19
20 class StatusController(BaseController):
21     def index(self):
22         c.debts = debts()
23         c.settle = settle(c.debts)
24         
25         c.total = self._total()
26         
27         year = date.today() - timedelta(days=365)
28         this_year = date.today().replace(month=1, day=1)
29         this_month = date.today().replace(day=1)
30         last_month = (date.today() - timedelta(days=30)).replace(day=1)
31         
32         c.year_total, c.this_year_total, c.this_month_total = \
33             [self._total(model.Expenditure.date >= i)
34              for i in [year, this_year, this_month]]
35         
36
37         c.last_month_total = self._total(sqlalchemy.and_(
38                     model.Expenditure.date >= last_month,
39                     model.Expenditure.date < this_month))
40         
41         c.expenditures = meta.Session.query(model.Expenditure).\
42                 filter(sqlalchemy.or_(
43                     model.Expenditure.spender == request.environ['user'],
44                     model.Expenditure.splits.any(
45                         sqlalchemy.and_(
46                             model.Split.user == request.environ['user'],
47                             model.Split.share != 0)))).\
48                 limit(10).all()
49         c.transfers = meta.Session.query(model.Transfer).\
50             filter(sqlalchemy.or_(
51                 model.Transfer.debtor==request.environ['user'],
52                 model.Transfer.creditor==request.environ['user'])).\
53                 limit(10).all()
54         
55         return render('/status/index.mako')
56     
57     def _total(self, conditions=None):
58         q = meta.Session.query(sqlalchemy.func.SUM(
59             model.Expenditure.amount))
60         if conditions is not None:
61             q = q.filter(conditions)
62         return q.scalar()