Commits

Robert Brewer committed a0fc96d

Better budget data.

Comments (0)

Files changed (1)

flowrate/__init__.py

             months = range(1, 13)
 
         if dategroup == 'year':
-            budget_dates = [datetime.date(y, 12, 31) for y in years]
             dategroups = [str(y) for y in years]
             pgdatefmt = 'YYYY'
         elif dategroup == 'day':
-            budget_dates = []
+            dategroups = []
             for y in years:
                 for m in months:
                     for d in days or xrange(1, calendar.monthrange(y, m)[1] + 1):
-                        budget_dates.append(datetime.date(y, m, d))
-            dategroups = [d.isoformat() for d in budget_dates]
+                        dategroups.append(datetime.date(y, m, d).isoformat())
             pgdatefmt = 'YYYY-MM-DD'
         else:
             # dategroup == 'month' or other
-            budget_dates = [datetime.date(y, m, calendar.monthrange(y, m)[1])
-                            for y in years for m in months]
-            dategroups = ['%04d-%02d' % (y, m)
-                          for y in years for m in months]
+            dategroups = ['%04d-%02d' % (y, m) for y in years for m in months]
             pgdatefmt = 'YYYY-MM'
 
         # Calculate the budget of each requested account for each
         if accounts:
             whereclause.append("ARRAY[account] <@ %(accounts)s")
             args['accounts'] = accounts
-        for bd, dg in zip(budget_dates, dategroups):
+        for dg in dategroups:
             wc = whereclause + [
                 "to_char(postdate, %(df)s) = %(dg)s"]
             args['df'] = pgdatefmt
             args['dg'] = dg
-            # TODO: this isn't quite right; we should show amount over budget,
-            # but obligations.remaining is currently not allowed to go under 0
-            sql = ("SELECT account, SUM(total) AS budget, "
-                   "SUM(total - amount) AS fulfilled "
+            sql = ("SELECT account, SUM(total) AS budget "
                    "FROM flowledger WHERE " +  " AND ".join(wc) +
                    " GROUP BY 1 ORDER BY 1;")
-            print sql
             for row in db.execute(sql, args).fetchall():
-                budgets[row.account][dg] = [row.budget, row.fulfilled]
+                budgets[row.account][dg][0] = row.budget
+            sql = ("SELECT account, SUM(amount) AS spent "
+                   "FROM ledger WHERE " +  " AND ".join(wc) +
+                   " GROUP BY 1 ORDER BY 1;")
+            for row in db.execute(sql, args).fetchall():
+                budgets[row.account][dg][1] = row.spent
 
         b['data'] = budgets