expephalon/core/models/local.py

45 lines
No EOL
1.4 KiB
Python

from django.db.models import Model, CharField, BooleanField, DecimalField, ForeignKey, CASCADE
from django_countries.fields import CountryField
from core.fields.base import LongCharField
class Currency(Model):
name = LongCharField(unique=True)
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)
def __str__(self):
return f"{self.name} ({self.code})"
class TaxPolicy(Model):
name = LongCharField(blank=True)
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
def __str__(self):
return self.name
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)
def __str__(self):
return str(self.destination_country)