2020-05-27 10:36:49 +00:00
|
|
|
from core.models.billable import CycleChoices
|
|
|
|
from core.models.products import ProductGroup
|
2020-06-04 06:03:35 +00:00
|
|
|
from core.fields.numbers import CostField
|
2020-05-27 10:36:49 +00:00
|
|
|
|
2020-06-04 06:03:35 +00:00
|
|
|
from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, OneToOneField
|
2020-05-27 10:36:49 +00:00
|
|
|
|
|
|
|
from importlib import import_module
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
class Service(Model):
|
|
|
|
name = CharField(max_length=255)
|
|
|
|
description = TextField(null=True, blank=True)
|
|
|
|
handler_module = CharField(max_length=255, null=True, blank=True)
|
|
|
|
product_groups = ManyToManyField(ProductGroup)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def handler(self):
|
|
|
|
if self.handler_module:
|
|
|
|
try:
|
|
|
|
handler_module = import_module(self.handler_module)
|
|
|
|
return handler_module.ProductRouter(self.id)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Could not load product handler {self.handler_module} for product {self.id}: {e}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
class ServicePlan(Model):
|
|
|
|
service = OneToOneField(Service, on_delete=CASCADE)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_productplan(cls, productplan, service):
|
|
|
|
plan = cls.objects.create(service=service)
|
|
|
|
|
|
|
|
for item in productplan.serviceplanitem_set:
|
|
|
|
ServicePlanItem.objects.create(plan=plan, cycle=item.cycle, count=item.count, cost=item.cost)
|
|
|
|
|
|
|
|
class ServicePlanItem(Model):
|
|
|
|
plan = ForeignKey(ServicePlan, on_delete=CASCADE)
|
|
|
|
cycle = PositiveIntegerField(choices=CycleChoices.choices)
|
|
|
|
count = PositiveIntegerField()
|
2020-06-04 06:03:35 +00:00
|
|
|
cost = CostField()
|