]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/status.py
5d92d2f26194aa670ce2a887ee9732f35310ea2b
[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 from decimal import Decimal
14
15 from pylons import request
16
17 log = logging.getLogger(__name__)
18
19 class StatusController(BaseController):
20     def index(self):
21         c.debts = debts()
22         c.settle = settle(c.debts)
23         
24         c.total = self._total(sqlalchemy.text('1=1'))
25         
26         year = date.today() - timedelta(days=365)
27         this_year = date.today().replace(month=1, day=1)
28         this_month = date.today().replace(day=1)
29         last_month = (date.today() - timedelta(days=30)).replace(day=1)
30         
31         c.year_total, c.this_year_total, c.this_month_total =\
32             [self._total(model.expenditures.c.date >= i)
33              for i in [year, this_year, this_month]]
34         
35
36         c.last_month_total = self._total(sqlalchemy.and_(
37                     model.expenditures.c.date >= last_month,
38                     model.expenditures.c.date < this_month))
39         
40         c.expenditures = meta.Session.query(model.Expenditure).\
41             filter(model.Expenditure.spender==request.environ['user']).\
42             limit(10).all()
43         c.transfers = meta.Session.query(model.Transfer).\
44             filter(sqlalchemy.or_(
45                 model.Transfer.debtor==request.environ['user'],
46                 model.Transfer.creditor==request.environ['user'])).\
47                 limit(10).all()
48         
49         return render('/status/index.mako')
50     
51     def _total(self, where):
52         return (meta.Session.execute(sqlalchemy.sql.select([
53                 sqlalchemy.func.sum(model.expenditures.c.amount).\
54                     label('total')]).\
55                     where(where)).scalar() or Decimal("0.00")) / 100