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()
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.Expenditure.date >= i)
34 for i in [year, this_year, this_month]]
37 c.last_month_total = self._total(sqlalchemy.and_(
38 model.Expenditure.date >= last_month,
39 model.Expenditure.date < this_month))
41 c.expenditures = meta.Session.query(model.Expenditure).\
42 filter(sqlalchemy.or_(
43 model.Expenditure.spender == request.environ['user'],
44 model.Expenditure.splits.any(
46 model.Split.user == request.environ['user'],
47 model.Split.share != 0)))).\
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'])).\
55 return render('/status/index.mako')
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)