Late commits

This commit is contained in:
Nico Schottelius 2021-06-20 11:51:27 +02:00
commit a463bcf7bd
9 changed files with 161 additions and 14 deletions

View file

@ -233,6 +233,7 @@ class RecurringPeriod(models.Model):
class BillingAddress(UncloudAddress):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
vat_number = models.CharField(max_length=100, default="", blank=True)
vat_number_verified = models.BooleanField(default=False)
active = models.BooleanField(default=False)
class Meta:

View file

@ -1,6 +1,9 @@
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):
@ -24,3 +27,32 @@ def get_balance_for_user(user):
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

View file

@ -42,6 +42,13 @@ class BillingAddressSerializer(serializers.ModelSerializer):
exclude = [ "owner" ]
class VATRateSerializer(serializers.ModelSerializer):
class Meta:
model = VATRate
fields = '__all__'
################################################################################
# Unchecked code

View file

@ -5,6 +5,7 @@ from django.utils import timezone
from .models import *
from uncloud_service.models import GenericServiceProduct
from uncloud.models import UncloudProvider
import json
@ -463,3 +464,26 @@ class BillingAddressTestCase(TestCase):
self.assertRaises(uncloud_pay.models.BillingAddress.DoesNotExist,
BillingAddress.get_address_for,
self.user)
class VATRatesTestCase(TestCase):
def setUp(self):
self.user = get_user_model().objects.create(
username='random_user',
email='jane.random@domain.tld')
self.user_addr = BillingAddress.objects.create(
owner=self.user,
organization = 'Test org',
street="unknown",
city="unknown",
postal_code="unknown",
active=True)
UncloudProvider.populate_db_defaults()
def test_get_rate_for_user(self):
"""
Raise an error, when there is no address
"""

View file

@ -295,6 +295,11 @@ class OrderViewSet(viewsets.ReadOnlyModelViewSet):
def get_queryset(self):
return Order.objects.filter(owner=self.request.user)
class VATRateViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = VATRateSerializer
permission_classes = [permissions.IsAuthenticated]
queryset = VATRate.objects.all()
class BillingAddressViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,