2021-01-01 11:41:54 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.db import transaction
|
|
|
|
from .models import *
|
|
|
|
|
2021-07-30 07:04:32 +00:00
|
|
|
def get_deposit_payments_for_user(user):
|
|
|
|
payments = [ payment.amount for payment in Payment.objects.filter(owner=user, type='deposit')]
|
2021-01-01 11:41:54 +00:00
|
|
|
return sum(payments)
|
|
|
|
|
|
|
|
def get_spendings_for_user(user):
|
2021-07-30 07:04:32 +00:00
|
|
|
spendings = [payment.amount for payment in Payment.objects.filter(owner=user, type='withdraw')]
|
|
|
|
return sum(spendings)
|
2021-01-01 11:41:54 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def get_balance_for_user(user):
|
2021-07-30 07:04:32 +00:00
|
|
|
return get_deposit_payments_for_user(user) - get_spendings_for_user(user)
|
2021-01-17 14:53:30 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def has_enough_balance(user, due_amount):
|
|
|
|
balance = get_balance_for_user(user)
|
|
|
|
if balance >= due_amount:
|
|
|
|
return True
|
|
|
|
return False
|
2021-06-20 09:51:27 +00:00
|
|
|
|
2021-07-19 14:36:10 +00:00
|
|
|
def get_billing_address_for_user(user):
|
|
|
|
return BillingAddress.objects.filter(owner=user, active=True).first()
|