]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/status.py
Add history controller to display old transactions
[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 log = logging.getLogger(__name__)
16
17 class StatusController(BaseController):
18     def index(self):
19         c.debts = debts()
20         c.settle = settle(c.debts)
21         
22         
23         c.total = self._total(True)
24         
25         year = date.today() - timedelta(days=365)
26         this_year = date.today().replace(month=1, day=1)
27         this_month = date.today().replace(day=1)
28         last_month = (date.today() - timedelta(days=30)).replace(day=1)
29         
30         c.year_total, c.this_year_total, c.this_month_total =\
31             [self._total(model.expenditures.c.date >= i)
32              for i in [year, this_year, this_month]]
33         
34
35         c.last_month_total = self._total(sqlalchemy.and_(
36                     model.expenditures.c.date >= last_month,
37                     model.expenditures.c.date < this_month))
38         
39         return render('/status/index.mako')
40     
41     def _total(self, where):
42         return (meta.Session.execute(sqlalchemy.sql.select([
43                 sqlalchemy.func.sum(model.expenditures.c.amount).\
44                     label('total')]).\
45                     where(where)).scalar() or Decimal("0.00")) / 100