2 Calculate the current state of the books
7 from bluechips.lib.base import *
8 from bluechips.lib.totals import *
12 from datetime import date, timedelta
14 from bluechips.model.types import Currency
16 from pylons import request
18 log = logging.getLogger(__name__)
20 class StatusController(BaseController):
23 c.settle = settle(c.debts)
25 c.total = self._total(sqlalchemy.text('1=1'))
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)
32 c.year_total, c.this_year_total, c.this_month_total =\
33 [self._total(model.expenditures.c.date >= i)
34 for i in [year, this_year, this_month]]
37 c.last_month_total = self._total(sqlalchemy.and_(
38 model.expenditures.c.date >= last_month,
39 model.expenditures.c.date < this_month))
41 c.expenditures = meta.Session.query(model.Expenditure).\
42 filter(model.Expenditure.spender==request.environ['user']).\
44 c.transfers = meta.Session.query(model.Transfer).\
45 filter(sqlalchemy.or_(
46 model.Transfer.debtor==request.environ['user'],
47 model.Transfer.creditor==request.environ['user'])).\
50 return render('/status/index.mako')
52 def _total(self, where):
53 return Currency(meta.Session.execute(sqlalchemy.sql.select([
54 sqlalchemy.func.sum(model.expenditures.c.amount).\
56 where(where)).scalar() or 0)