forked from uncloud/uncloud
amal
b7aa1c6971
- Implement a complete cycle for buying a Matrix Chat Host - Refactor the Payement cycle and stripe related methods
31 lines
807 B
Python
31 lines
807 B
Python
from django.utils import timezone
|
|
from django.db import transaction
|
|
from .models import *
|
|
|
|
def get_payments_for_user(user):
|
|
payments = [ payment.amount for payment in Payment.objects.filter(owner=user) ]
|
|
|
|
return sum(payments)
|
|
|
|
def get_spendings_for_user(user):
|
|
bills = Bill.objects.filter(owner=user)
|
|
|
|
amount = 0
|
|
for bill in bills:
|
|
amount += bill.sum
|
|
|
|
return amount
|
|
|
|
@transaction.atomic
|
|
def get_balance_for_user(user):
|
|
return get_payments_for_user(user) - get_spendings_for_user(user)
|
|
|
|
@transaction.atomic
|
|
def has_enough_balance(user, due_amount):
|
|
balance = get_balance_for_user(user)
|
|
if balance >= due_amount:
|
|
return True
|
|
return False
|
|
|
|
def get_billing_address_for_user(user):
|
|
return BillingAddress.objects.filter(owner=user, active=True).first()
|