Compute VAT rate and amount on bill generation

This commit is contained in:
fnux 2020-04-18 11:43:55 +02:00
commit b3afad5d5d
3 changed files with 54 additions and 38 deletions

View file

@ -469,6 +469,28 @@ class BillingAddress(models.Model):
self.name, self.street, self.postal_code, self.city,
self.country)
# Populated with the import-vat-numbers django command.
class VATRate(models.Model):
start_date = models.DateField(blank=True, null=True)
stop_date = models.DateField(blank=True, null=True)
territory_codes = models.TextField(blank=True, default='')
currency_code = models.CharField(max_length=10)
rate = models.FloatField()
rate_type = models.TextField(blank=True, default='')
description = models.TextField(blank=True, default='')
@staticmethod
def get_for_country(country_code):
vat_rate = None
try:
vat_rate = VATRate.objects.get(
territory_codes=country_code, start_date__isnull=False, stop_date=None
)
return vat_rate.rate
except VATRate.DoesNotExist as dne:
logger.debug(str(dne))
logger.debug("Did not find VAT rate for %s, returning 0" % country_code)
return 0
class Bill(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
@ -506,9 +528,17 @@ class Bill(models.Model):
return bill_records
@property
def total(self):
def amount(self):
return reduce(lambda acc, record: acc + record.amount, self.records, 0)
@property
def vat_amount(self):
return reduce(lambda acc, record: acc + record.vat_amount, self.records, 0)
@property
def total(self):
return self.amount + self.vat_amount
@property
def final(self):
# A bill is final when its ending date is passed.
@ -752,37 +782,20 @@ class BillRecord():
format(record.recurring_period))
@property
def vat(self):
return 0
def vat_rate(self):
return Decimal(VATRate.get_for_country(self.bill.billing_address.country))
@property
def vat_amount(self):
return self.amount * self.vat_rate
@property
def amount(self):
return Decimal(float(self.recurring_price) * self.recurring_count) + self.one_time_price
# Populated with the import-vat-numbers django command.
class VATRate(models.Model):
start_date = models.DateField(blank=True, null=True)
stop_date = models.DateField(blank=True, null=True)
territory_codes = models.TextField(blank=True, default='')
currency_code = models.CharField(max_length=10)
rate = models.FloatField()
rate_type = models.TextField(blank=True, default='')
description = models.TextField(blank=True, default='')
@staticmethod
def get_for_country(country_code):
vat_rate = None
try:
vat_rate = VATRates.objects.get(
territory_codes=country, start_date__isnull=False, stop_date=None
)
logger.debug("VAT rate for %s is %s" % (country, vat_rate.rate))
return vat_rate.rate
except VATRates.DoesNotExist as dne:
logger.debug(str(dne))
logger.debug("Did not find VAT rate for %s, returning 0" % country)
return 0
@property
def total(self):
return self.amount + self.vat_amount
###
# Orders.