]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/status.py
Add history controller to display old transactions
[bluechips.git] / bluechips / controllers / status.py
index 491493e1ec48cda16d8434bb28181411af4d22ed..37992e59964d7b9d077644e56624b662c82c7bdc 100644 (file)
@@ -5,9 +5,41 @@ Calculate the current state of the books
 import logging
 
 from bluechips.lib.base import *
+from bluechips.lib.totals import *
+
+import sqlalchemy
+
+from datetime import date, timedelta
+from decimal import Decimal
 
 log = logging.getLogger(__name__)
 
 class StatusController(BaseController):
     def index(self):
-        return 'Hello World'
+        c.debts = debts()
+        c.settle = settle(c.debts)
+        
+        
+        c.total = self._total(True)
+        
+        year = date.today() - timedelta(days=365)
+        this_year = date.today().replace(month=1, day=1)
+        this_month = date.today().replace(day=1)
+        last_month = (date.today() - timedelta(days=30)).replace(day=1)
+        
+        c.year_total, c.this_year_total, c.this_month_total =\
+            [self._total(model.expenditures.c.date >= i)
+             for i in [year, this_year, this_month]]
+        
+
+        c.last_month_total = self._total(sqlalchemy.and_(
+                    model.expenditures.c.date >= last_month,
+                    model.expenditures.c.date < this_month))
+        
+        return render('/status/index.mako')
+    
+    def _total(self, where):
+        return (meta.Session.execute(sqlalchemy.sql.select([
+                sqlalchemy.func.sum(model.expenditures.c.amount).\
+                    label('total')]).\
+                    where(where)).scalar() or Decimal("0.00")) / 100