2020-02-28 07:59:32 +00:00
|
|
|
from functools import reduce
|
2020-02-28 08:10:36 +00:00
|
|
|
from .models import Bill, Payment, PaymentMethod
|
2020-02-28 07:59:32 +00:00
|
|
|
|
|
|
|
def sum_amounts(entries):
|
2020-02-28 08:10:36 +00:00
|
|
|
return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
|
2020-02-28 07:59:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_balance_for(user):
|
2020-02-28 08:10:36 +00:00
|
|
|
bills = sum_amounts(Bill.objects.filter(owner=user))
|
|
|
|
payments = sum_amounts(Payment.objects.filter(owner=user))
|
|
|
|
return payments - bills
|
2020-02-28 07:59:32 +00:00
|
|
|
|
2020-02-28 08:10:36 +00:00
|
|
|
def get_payment_method_for(user):
|
|
|
|
methods = PaymentMethod.objects.filter(owner=user)
|
|
|
|
for method in methods:
|
|
|
|
# Do we want to do something with non-primary method?
|
|
|
|
if method.primary:
|
|
|
|
return method
|
|
|
|
|
|
|
|
return None
|