2 Calculate the current state of the books
7 from bluechips.lib.base import *
8 from bluechips.lib.totals import *
11 from sqlalchemy import orm
13 from datetime import date, timedelta
15 from bluechips.model.types import Currency
17 from pylons import request
19 log = logging.getLogger(__name__)
21 class StatusController(BaseController):
24 c.settle = settle(c.debts)
26 c.total = self._total()
28 year = date.today() - timedelta(days=365)
29 this_year = date.today().replace(month=1, day=1)
30 this_month = date.today().replace(day=1)
31 last_month = (date.today() - timedelta(days=30)).replace(day=1)
33 c.year_total, c.this_year_total, c.this_month_total = \
34 [self._total(model.Expenditure.date >= i)
35 for i in [year, this_year, this_month]]
38 c.last_month_total = self._total(sqlalchemy.and_(
39 model.Expenditure.date >= last_month,
40 model.Expenditure.date < this_month))
42 c.expenditures = meta.Session.query(model.Expenditure).\
43 filter(sqlalchemy.or_(
44 model.Expenditure.spender == request.environ['user'],
45 model.Expenditure.splits.any(
47 model.Split.user == request.environ['user'],
48 model.Split.share != 0)))).\
49 options(orm.eagerload('splits')).\
51 c.transfers = meta.Session.query(model.Transfer).\
52 filter(sqlalchemy.or_(
53 model.Transfer.debtor==request.environ['user'],
54 model.Transfer.creditor==request.environ['user'])).\
57 return render('/status/index.mako')
59 def _total(self, conditions=None):
60 q = meta.Session.query(sqlalchemy.func.SUM(
61 model.Expenditure.amount))
62 if conditions is not None:
63 q = q.filter(conditions)