2020-05-22 16:13:23 +00:00
|
|
|
from django.db.models import Model, CharField, BooleanField, DecimalField, ForeignKey, CASCADE
|
|
|
|
|
|
|
|
from django_countries.fields import CountryField
|
|
|
|
|
2020-05-27 13:06:38 +00:00
|
|
|
from core.fields.base import LongCharField
|
|
|
|
|
2020-05-22 16:13:23 +00:00
|
|
|
class Currency(Model):
|
2020-05-27 13:06:38 +00:00
|
|
|
name = LongCharField(unique=True)
|
2020-05-22 16:13:23 +00:00
|
|
|
code = CharField(max_length=16, unique=True)
|
|
|
|
symbol = CharField(max_length=8)
|
|
|
|
base = BooleanField(default=False)
|
|
|
|
rate = DecimalField(default=1, max_digits=30, decimal_places=10)
|
|
|
|
|
|
|
|
def set_base(self):
|
|
|
|
type(self).get_base().update(base=False)
|
|
|
|
self.update(base=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_base(cls):
|
|
|
|
return cls.objects.get(base=True)
|
|
|
|
|
2020-06-02 15:58:20 +00:00
|
|
|
def __str__(self):
|
2020-06-03 16:25:59 +00:00
|
|
|
return f"{self.name} ({self.code})"
|
2020-06-02 15:58:20 +00:00
|
|
|
|
2020-05-22 16:13:23 +00:00
|
|
|
class TaxPolicy(Model):
|
2020-05-27 13:06:38 +00:00
|
|
|
name = LongCharField(blank=True)
|
2020-05-22 16:13:23 +00:00
|
|
|
default_rate = DecimalField(default=0, max_digits=10, decimal_places=5)
|
|
|
|
|
|
|
|
def get_applicable_rate(self, country, reverse_charge=False):
|
|
|
|
rule = self.taxrule_set.get(destination_country=country)
|
|
|
|
if reverse_charge:
|
|
|
|
return rule.tax_rate if not rule.reverse_charge else 0
|
|
|
|
return rule.tax_rate
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2020-05-22 16:13:23 +00:00
|
|
|
|
|
|
|
class TaxRule(Model):
|
|
|
|
policy = ForeignKey(TaxPolicy, on_delete=CASCADE)
|
|
|
|
destination_country = CountryField()
|
|
|
|
tax_rate = DecimalField(max_digits=10, decimal_places=5)
|
|
|
|
reverse_charge = BooleanField(default=False)
|
2020-06-02 15:58:20 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.destination_country)
|