2020-06-04 06:03:35 +00:00
|
|
|
from core.fields.numbers import CostField
|
2020-06-05 07:38:55 +00:00
|
|
|
from core.models.local import Currency
|
|
|
|
from core.models.profiles import ClientProfile
|
2020-06-06 11:25:54 +00:00
|
|
|
from core.models.billable import BaseBillable
|
2020-06-06 11:58:36 +00:00
|
|
|
from core.mixins.billable import RecurMixin
|
2020-05-27 10:36:49 +00:00
|
|
|
|
2020-06-05 07:38:55 +00:00
|
|
|
from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, OneToOneField, BooleanField, SET_NULL
|
2020-05-27 10:36:49 +00:00
|
|
|
|
|
|
|
from importlib import import_module
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
class Service(Model):
|
2020-06-06 05:33:42 +00:00
|
|
|
from core.models.products import ProductGroup
|
2020-06-05 05:52:52 +00:00
|
|
|
|
2020-05-27 10:36:49 +00:00
|
|
|
name = CharField(max_length=255)
|
|
|
|
description = TextField(null=True, blank=True)
|
2020-06-06 05:37:50 +00:00
|
|
|
service_type = CharField(max_length=255, null=True, blank=True)
|
2020-05-27 10:36:49 +00:00
|
|
|
product_groups = ManyToManyField(ProductGroup)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def handler(self):
|
2020-06-06 05:37:50 +00:00
|
|
|
if self.service_type:
|
2020-05-27 10:36:49 +00:00
|
|
|
try:
|
2020-06-06 05:37:50 +00:00
|
|
|
service_type = import_module(self.service_type)
|
|
|
|
return service_type.ServiceRouter(self)
|
2020-05-27 10:36:49 +00:00
|
|
|
except Exception as e:
|
2020-06-06 05:37:50 +00:00
|
|
|
logger.error(f"Could not load product handler {self.service_type} for product {self.id}: {e}")
|
2020-05-27 10:36:49 +00:00
|
|
|
return None
|
|
|
|
|
2020-06-06 05:33:42 +00:00
|
|
|
class ServicePlan(Model):
|
|
|
|
service = OneToOneField(Service, on_delete=CASCADE)
|
|
|
|
|
2020-05-27 10:36:49 +00:00
|
|
|
@classmethod
|
2020-06-06 05:33:42 +00:00
|
|
|
def from_productplan(cls, productplan, service):
|
|
|
|
plan = cls.objects.create(service=service)
|
2020-05-27 10:36:49 +00:00
|
|
|
|
2020-06-06 05:33:42 +00:00
|
|
|
for item in productplan.serviceplanitem_set:
|
|
|
|
ServicePlanItem.objects.create(plan=plan, cycle=item.cycle, count=item.count, cost=item.cost)
|
|
|
|
|
2020-06-06 11:58:36 +00:00
|
|
|
class ServicePlanItem(RecurMixin, BaseBillable):
|
2020-06-06 05:33:42 +00:00
|
|
|
plan = ForeignKey(ServicePlan, on_delete=CASCADE)
|