]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/lib/totals.py
Added UI for working with tags
[bluechips.git] / bluechips / lib / totals.py
index 730ca648ea1b228c3f10ab712d0b77868858cd29..94a62a127bbe73d63f70bec4f8c9e6928414a18e 100644 (file)
@@ -21,11 +21,14 @@ def debts():
     # house
     users = meta.Session.query(model.User)
     
-    debts_dict = {}
+    debts_dict = dict((u, Currency(0)) for u in users)
     
     # First, credit everyone for expenditures they've made
-    for user in users:
-        debts_dict[user] = Currency(-sum(map((lambda x: x.amount), user.expenditures)))
+    total_expenditures = meta.Session.query(model.Expenditure).\
+        add_column(sqlalchemy.func.sum(model.Expenditure.amount).label('total_spend')).\
+        group_by(model.Expenditure.spender_id)
+    for expenditure, total_spend in total_expenditures:
+        debts_dict[expenditure.spender] -= total_spend
     
     # Next, debit everyone for expenditures that they have an
     # investment in (i.e. splits)
@@ -94,9 +97,9 @@ def settle(debts_dict):
         settle_list.append((owes['who'], owed['who'], val))
     
     if len(owes_list) > 0:
-        raise DirtyBooks, ("People still owe money", owes_list) #pragma:nocover
+        raise DirtyBooks, ("People still owe money", owes_list)
     if len(owed_list) > 0:
-        raise DirtyBooks, ("People are still owed money", owed_list) #pragma:nocover
+        raise DirtyBooks, ("People are still owed money", owed_list)
     
     return settle_list