2015-10-02 11:08:23 +00:00
|
|
|
from functools import lru_cache
|
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
from django.core.validators import ValidationError
|
|
|
|
from django.db import models
|
2015-03-27 19:50:54 +00:00
|
|
|
from django.db.models import Q
|
2015-04-07 15:14:49 +00:00
|
|
|
from django.utils.functional import cached_property
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-11-18 13:59:21 +00:00
|
|
|
|
|
|
|
from orchestra.core.validators import validate_name
|
|
|
|
from orchestra.models import queryset
|
2015-05-09 15:37:35 +00:00
|
|
|
from orchestra.utils.python import import_class
|
2014-11-18 13:59:21 +00:00
|
|
|
|
2015-05-09 15:37:35 +00:00
|
|
|
from . import settings
|
2014-11-18 13:59:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Plan(models.Model):
|
|
|
|
name = models.CharField(_("name"), max_length=32, unique=True, validators=[validate_name])
|
|
|
|
verbose_name = models.CharField(_("verbose_name"), max_length=128, blank=True)
|
2015-04-08 14:41:09 +00:00
|
|
|
is_active = models.BooleanField(_("active"), default=True,
|
|
|
|
help_text=_("Designates whether this account should be treated as active. "
|
|
|
|
"Unselect this instead of deleting accounts."))
|
2014-11-18 13:59:21 +00:00
|
|
|
is_default = models.BooleanField(_("default"), default=False,
|
|
|
|
help_text=_("Designates whether this plan is used by default or not."))
|
|
|
|
is_combinable = models.BooleanField(_("combinable"), default=True,
|
|
|
|
help_text=_("Designates whether this plan can be combined with other plans or not."))
|
|
|
|
allow_multiple = models.BooleanField(_("allow multiple"), default=False,
|
|
|
|
help_text=_("Designates whether this plan allow for multiple contractions."))
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2015-03-27 19:50:54 +00:00
|
|
|
return self.get_verbose_name()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
def clean(self):
|
|
|
|
self.verbose_name = self.verbose_name.strip()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
def get_verbose_name(self):
|
|
|
|
return self.verbose_name or self.name
|
|
|
|
|
|
|
|
|
|
|
|
class ContractedPlan(models.Model):
|
2021-04-22 08:28:00 +00:00
|
|
|
plan = models.ForeignKey(Plan, on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("plan"), related_name='contracts')
|
|
|
|
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("account"), related_name='plans')
|
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = _("plans")
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-11-18 13:59:21 +00:00
|
|
|
return str(self.plan)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-07 15:14:49 +00:00
|
|
|
@cached_property
|
|
|
|
def active(self):
|
|
|
|
return self.plan.is_active and self.account.is_active
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
def clean(self):
|
2015-03-27 19:50:54 +00:00
|
|
|
if not self.pk and not self.plan.allow_multiple:
|
2014-11-18 13:59:21 +00:00
|
|
|
if ContractedPlan.objects.filter(plan=self.plan, account=self.account).exists():
|
|
|
|
raise ValidationError("A contracted plan for this account already exists.")
|
|
|
|
|
|
|
|
|
|
|
|
class RateQuerySet(models.QuerySet):
|
|
|
|
group_by = queryset.group_by
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
def by_account(self, account):
|
|
|
|
# Default allways selected
|
|
|
|
return self.filter(
|
|
|
|
Q(plan__is_default=True) |
|
|
|
|
Q(plan__contracts__account=account)
|
2015-05-14 13:28:54 +00:00
|
|
|
).order_by('plan', 'quantity').select_related('plan', 'service')
|
2014-11-18 13:59:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Rate(models.Model):
|
2021-04-22 08:28:00 +00:00
|
|
|
service = models.ForeignKey('services.Service', on_delete=models.CASCADE,
|
|
|
|
verbose_name=_("service"), related_name='rates')
|
|
|
|
plan = models.ForeignKey(Plan, on_delete=models.SET_NULL, null=True, blank=True,
|
|
|
|
verbose_name=_("plan"), related_name='rates')
|
2015-04-24 14:03:42 +00:00
|
|
|
quantity = models.PositiveIntegerField(_("quantity"), null=True, blank=True,
|
|
|
|
help_text=_("See rate algorihm help text."))
|
2014-11-18 13:59:21 +00:00
|
|
|
price = models.DecimalField(_("price"), max_digits=12, decimal_places=2)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
objects = RateQuerySet.as_manager()
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('service', 'plan', 'quantity')
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
def __str__(self):
|
2014-11-18 13:59:21 +00:00
|
|
|
return "{}-{}".format(str(self.price), self.quantity)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
@classmethod
|
2015-10-02 11:08:23 +00:00
|
|
|
@lru_cache()
|
2015-03-31 12:39:08 +00:00
|
|
|
def get_methods(cls):
|
2015-05-09 15:37:35 +00:00
|
|
|
return dict((method, import_class(method)) for method in settings.PLANS_RATE_METHODS)
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-03-31 12:39:08 +00:00
|
|
|
@classmethod
|
2015-10-02 11:08:23 +00:00
|
|
|
@lru_cache()
|
2015-03-31 12:39:08 +00:00
|
|
|
def get_choices(cls):
|
|
|
|
choices = []
|
2015-05-09 15:37:35 +00:00
|
|
|
for name, method in cls.get_methods().items():
|
2015-03-31 12:39:08 +00:00
|
|
|
choices.append((name, method.verbose_name))
|
|
|
|
return choices
|
2021-04-22 08:28:00 +00:00
|
|
|
|
2015-05-09 15:37:35 +00:00
|
|
|
@classmethod
|
|
|
|
def get_default(cls):
|
|
|
|
return settings.PLANS_DEFAULT_RATE_METHOD
|