phase in configuration - move address to base

This commit is contained in:
Nico Schottelius 2020-10-11 22:32:08 +02:00
commit bbc7625550
22 changed files with 668 additions and 300 deletions

View file

@ -18,9 +18,8 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.conf import settings
import uncloud_pay.stripe
from uncloud_pay import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS, COUNTRIES
from uncloud.models import UncloudModel, UncloudStatus
from uncloud_pay import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
from uncloud.models import UncloudAddress
# Used to generate bill due dates.
BILL_PAYMENT_DELAY=datetime.timedelta(days=10)
@ -69,16 +68,6 @@ class Currency(models.TextChoices):
EUR = 'EUR', _('Euro')
USD = 'USD', _('US Dollar')
class CountryField(models.CharField):
def __init__(self, *args, **kwargs):
kwargs.setdefault('choices', COUNTRIES)
kwargs.setdefault('default', 'CH')
kwargs.setdefault('max_length', 2)
super().__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
def get_balance_for_user(user):
bills = reduce(
@ -276,12 +265,6 @@ class RecurringPeriod(models.Model):
class BillingAddress(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
organization = models.CharField(max_length=100, blank=True, null=True)
name = models.CharField(max_length=100)
street = models.CharField(max_length=100)
city = models.CharField(max_length=50)
postal_code = models.CharField(max_length=50)
country = CountryField(blank=True)
vat_number = models.CharField(max_length=100, default="", blank=True)
active = models.BooleanField(default=False)
@ -360,7 +343,7 @@ class VATRate(models.Model):
###
# Products
class Product(UncloudModel):
class Product(models.Model):
"""
A product is something a user can order. To record the pricing, we
create order that define a state in time.
@ -1061,6 +1044,24 @@ class Bill(models.Model):
bill_records = BillRecord.objects.filter(bill=self)
return sum([ br.sum for br in bill_records ])
@property
def vat_rate(self):
"""
Handling VAT is a tricky business - thus we only implement the cases
that we clearly now and leave it open to fellow developers to implement
correct handling for other cases.
Case CH:
- If the customer is in .ch -> apply standard rate
- If the customer is in EU AND private -> apply country specific rate
- If the customer is in EU AND business -> do not apply VAT
- If the customer is outside EU and outside CH -> do not apply VAT
"""
provider_country = UncloudProvider.objects.get()
@classmethod
def create_bills_for_all_users(cls):
"""
@ -1218,30 +1219,3 @@ class ProductToRecurringPeriod(models.Model):
def __str__(self):
return f"{self.product} - {self.recurring_period} (default: {self.is_default})"
###
# Who is running / providing this instance of uncloud?
class UncloudProvider(models.Model):
"""
A class resembling who is running this uncloud instance.
This might change over time so we allow starting/ending dates
This also defines the taxation rules.
starting/ending date define from when to when this is valid. This way
we can model address changes and have it correct in the bills.
"""
valid_from = models.DateField()
valid_to = models.DateField(blank=True)
billing_address = models.ForeignKey(BillingAddress, on_delete=models.CASCADE)
@classmethod
def populate_db_defaults(cls):
ba = BillingAddress.objects.get_or_create()
# obj, created = cls.objects.get_or_create(
# valid_from=timezone.now()
# defaults={ 'duration_seconds': seconds })