X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Ftotals.py;h=e8b2985a07446718079eca982f2baef6fff3a6e8;hb=74d234f880ee6b3c510227ea94459b81a79755db;hp=3f18a32d115d46ddee1fce3017412074a6a5a6d5;hpb=9bff8b8b19f3579d1ff654255e98826e7340a40e;p=bluechips.git diff --git a/bluechips/lib/totals.py b/bluechips/lib/totals.py index 3f18a32..e8b2985 100644 --- a/bluechips/lib/totals.py +++ b/bluechips/lib/totals.py @@ -5,9 +5,9 @@ Calculate the total state of the books from bluechips import model from bluechips.model import meta -import sqlalchemy +from bluechips.model.types import Currency -from decimal import Decimal +import sqlalchemy class DirtyBooks(Exception): """ @@ -21,11 +21,11 @@ def debts(): # house users = meta.Session.query(model.User) - debts = {} + debts_dict = {} # First, credit everyone for expenditures they've made for user in users: - debts[user] = -sum(map((lambda x: x.amount), user.expenditures)) + debts_dict[user] = Currency(-sum(map((lambda x: x.amount), user.expenditures))) # Next, debit everyone for expenditures that they have an # investment in (i.e. splits) @@ -35,7 +35,7 @@ def debts(): group_by(model.Split.user_id) for split, total_cents in total_splits: - debts[split.user] += (total_cents / 100) + debts_dict[split.user] += total_cents # Finally, move transfers around appropriately # @@ -48,11 +48,11 @@ def debts(): total_credits = transfer_q.group_by(model.Transfer.creditor_id) for transfer, total_amount in total_debits: - debts[transfer.debtor] -= (total_amount / 100) + debts_dict[transfer.debtor] -= total_amount for transfer, total_amount in total_credits: - debts[transfer.creditor] += (total_amount / 100) + debts_dict[transfer.creditor] += total_amount - return debts + return debts_dict def settle(debts_dict): # This algorithm has been shamelessly stolen from Nelson Elhage's @@ -65,7 +65,7 @@ def settle(debts_dict): owes_list = [debt for debt in debts_list if debt['amount'] > 0] owed_list = [debt for debt in debts_list if debt['amount'] < 0] - settle = [] + settle_list = [] while len(owes_list) > 0 and len(owed_list) > 0: owes = owes_list[0] @@ -88,13 +88,13 @@ def settle(debts_dict): owes_list.pop(0) val = owes['amount'] - settle.append((owes['who'], owed['who'], val)) + settle_list.append((owes['who'], owed['who'], val)) if len(owes_list) > 0: raise DirtyBooks, ("People still owe money", owes_list) if len(owed_list) > 0: raise DirtyBooks, ("People are still owed money", owed_list) - return settle + return settle_list __all__ = ['debts', 'settle']