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
13 from decimal import Decimal
15 from pylons import request
17 log = logging.getLogger(__name__)
19 class StatusController(BaseController):
22 c.settle = settle(c.debts)
24 c.total = self._total(True)
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)
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]]
36 c.last_month_total = self._total(sqlalchemy.and_(
37 model.expenditures.c.date >= last_month,
38 model.expenditures.c.date < this_month))
40 c.expenditures = meta.Session.query(model.Expenditure).\
41 filter(model.Expenditure.spender==request.environ['user']).\
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'])).\
49 return render('/status/index.mako')
51 def _total(self, where):
52 return (meta.Session.execute(sqlalchemy.sql.select([
53 sqlalchemy.func.sum(model.expenditures.c.amount).\
55 where(where)).scalar() or Decimal("0.00")) / 100