forked from uncloud/uncloud
- Added PricingPlan Model
- Implement a complete cycle for buying a Matrix Chat Host - Refactor the Payement cycle and stripe related methods
This commit is contained in:
parent
e205d8d07c
commit
b7aa1c6971
81 changed files with 5079 additions and 810 deletions
|
|
@ -1,9 +1,5 @@
|
|||
from django.utils import timezone
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
|
||||
from uncloud.selectors import filter_for_when
|
||||
from uncloud.models import UncloudProvider
|
||||
from .models import *
|
||||
|
||||
def get_payments_for_user(user):
|
||||
|
|
@ -12,12 +8,11 @@ def get_payments_for_user(user):
|
|||
return sum(payments)
|
||||
|
||||
def get_spendings_for_user(user):
|
||||
orders = Order.objects.filter(owner=user)
|
||||
bills = Bill.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())
|
||||
for bill in bills:
|
||||
amount += bill.sum
|
||||
|
||||
return amount
|
||||
|
||||
|
|
@ -25,34 +20,12 @@ def get_spendings_for_user(user):
|
|||
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.get(owner=user, active=True)
|
||||
|
||||
def get_vat_rate(billing_address, when=None):
|
||||
"""
|
||||
Returns the VAT rate for business to customer.
|
||||
|
||||
B2B is always 0% with the exception of trading within the own country
|
||||
"""
|
||||
|
||||
country = billing_address.country
|
||||
|
||||
# Need to have a provider country
|
||||
uncloud_provider = filter_for_when(UncloudProvider.objects.all()).get()
|
||||
vatrate = filter_for_when(VATRate.objects.filter(territory_codes=country), when).first()
|
||||
|
||||
# By default we charge VAT. This affects:
|
||||
# - Same country sales (VAT applied)
|
||||
# - B2C to EU (VAT applied)
|
||||
rate = vatrate.rate
|
||||
|
||||
# Exception: if...
|
||||
# - the billing_address is in EU,
|
||||
# - the vat_number has been set
|
||||
# - the vat_number has been verified
|
||||
# Then we do not charge VAT
|
||||
|
||||
if uncloud_provider.country != country and billing_address.vat_number and billing_address.vat_number_verified:
|
||||
rate = 0
|
||||
|
||||
return rate
|
||||
return BillingAddress.objects.filter(owner=user, active=True).first()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue