forked from uncloud/uncloud
implement balance getting
This commit is contained in:
parent
1b06d8ee03
commit
6c15d2086e
7 changed files with 53 additions and 65 deletions
23
uncloud_pay/selectors.py
Normal file
23
uncloud_pay/selectors.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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):
|
||||
orders = Order.objects.filter(owner=user)
|
||||
|
||||
amount = 0
|
||||
for order in orders:
|
||||
amount += order.one_time_price
|
||||
amount += order.recurring_price * order.count_used(when=timezone.now())
|
||||
|
||||
return amount
|
||||
|
||||
@transaction.atomic
|
||||
def get_balance_for_user(user):
|
||||
return get_payments_for_user(user) - get_spendings_for_user(user)
|
||||
Loading…
Add table
Add a link
Reference in a new issue