def cash_totals(trip_id): """ Returns dict of cash totals by currency """ incomes = db_session.query(MoneyExchange.income_currency_id, func.sum(MoneyExchange.income_amount)).\ filter_by(trip_id=trip_id).\ group_by(MoneyExchange.income_currency_id) exchanges = db_session.query(MoneyExchange.expense_currency_id, func.sum(MoneyExchange.expense_amount)).\ filter_by(trip_id=trip_id).\ filter_by(expense_account_id=CASH_ACCOUNT_ID).\ group_by(MoneyExchange.expense_currency_id) cash_by_currency = collections.defaultdict(Decimal) for i in incomes: cash_by_currency[i[0]] += i[1] for e in exchanges: cash_by_currency[e[0]] -= e[1] expenses = db_session.query(Expense.currency_id, func.sum(Expense.amount)).\ filter_by(trip_id=trip_id).\ filter_by(account_id=CASH_ACCOUNT_ID).\ filter(~Expense.category_id.in_([4,5])).\ group_by(Expense.currency_id) for e in expenses: cash_by_currency[e[0]] -= e[1] return dict(cash_by_currency)
def total_spent_by(trip_id, group_by): """ Return total spent for trip in dict indexed by {group_by} """ expenses = db_session.query(getattr(Expense, group_by), func.sum(Expense.preferred_currency_amount)).\ filter_by(trip_id=trip_id).\ group_by(getattr(Expense, group_by)) return expenses
def total_spent_trip(trip_id): return db_session.query(func.sum(Expense.preferred_currency_amount)).\ filter_by(trip_id=trip_id)[0][0]