update VAT importer

This commit is contained in:
Nico Schottelius 2020-10-08 19:54:04 +02:00
commit e03cdf214a
6 changed files with 141 additions and 79 deletions

View file

@ -1,28 +1,26 @@
import logging
import itertools
import datetime
from math import ceil
from calendar import monthrange
from decimal import Decimal
from functools import reduce
from django.db import models
from django.db.models import Q
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
import logging
from functools import reduce
import itertools
from math import ceil
import datetime
from calendar import monthrange
from decimal import Decimal
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 decimal import Decimal
import decimal
# Used to generate bill due dates.
BILL_PAYMENT_DELAY=datetime.timedelta(days=10)
@ -102,22 +100,6 @@ class StripeCustomer(models.Model):
on_delete=models.CASCADE)
stripe_id = models.CharField(max_length=32)
###
# Hosting company configuration
class HostingProvider(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
WIP.
"""
starting_date = models.DateField()
ending_date = models.DateField()
###
# Payments and Payment Methods.
@ -267,7 +249,6 @@ class RecurringPeriod(models.Model):
obj, created = cls.objects.get_or_create(name=name,
defaults={ 'duration_seconds': seconds })
@staticmethod
def secs_to_name(secs):
name = ""
@ -290,14 +271,11 @@ class RecurringPeriod(models.Model):
return f"{self.name} ({duration})"
###
# Bills.
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)
@ -314,6 +292,32 @@ class BillingAddress(models.Model):
name='one_active_billing_address_per_user')
]
@classmethod
def populate_db_defaults(cls):
"""
Ensure we have at least one billing address that is associated with the uncloud-admin.
This way we are sure that an UncloudProvider can be created.
Cannot use get_or_create as that looks for exactly one.
"""
owner = get_user_model().objects.get(username=settings.UNCLOUD_ADMIN_NAME)
billing_address = cls.objects.filter(owner=owner).first()
if not billing_address:
billing_address = cls.objects.create(owner=owner,
organization="uncloud admins",
name="Uncloud Admin",
street="Uncloudstreet. 42",
city="Luchsingen",
postal_code="8775",
country="CH",
active=True)
@staticmethod
def get_address_for(user):
return BillingAddress.objects.get(owner=user, active=True)
@ -349,6 +353,10 @@ class VATRate(models.Model):
logger.debug("Did not find VAT rate for %s, returning 0" % country_code)
return 0
def __str__(self):
return f"{self.territory_codes}: {self.starting_date} - {self.ending_date}: {self.rate_type}"
###
# Products
@ -1205,3 +1213,30 @@ 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 })