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-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__)
|
|
|
|
|
2020-06-05 07:38:55 +00:00
|
|
|
|
2020-05-27 10:36:49 +00:00
|
|
|
class Service(Model):
|
2020-06-05 07:38:55 +00:00
|
|
|
from core.models.products import ProductGroup, Product
|
|
|
|
from core.models.billable import CycleChoices
|
2020-06-05 05:52:52 +00:00
|
|
|
|
2020-06-05 07:38:55 +00:00
|
|
|
client = ForeignKey(ClientProfile, on_delete=CASCADE)
|
2020-05-27 10:36:49 +00:00
|
|
|
name = CharField(max_length=255)
|
|
|
|
description = TextField(null=True, blank=True)
|
2020-06-05 07:38:55 +00:00
|
|
|
service_type = CharField(max_length=255, null=True, blank=True)
|
|
|
|
product = ForeignKey(Product, on_delete=SET_NULL, null=True)
|
2020-05-27 10:36:49 +00:00
|
|
|
product_groups = ManyToManyField(ProductGroup)
|
2020-06-05 07:38:55 +00:00
|
|
|
currency = ForeignKey(Currency, on_delete=CASCADE)
|
|
|
|
cycle = PositiveIntegerField(choices=CycleChoices.choices)
|
|
|
|
count = PositiveIntegerField()
|
|
|
|
cost = CostField()
|
2020-06-06 04:33:52 +00:00
|
|
|
setup = CostField()
|
2020-06-05 07:38:55 +00:00
|
|
|
taxable=BooleanField()
|
2020-05-27 10:36:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def handler(self):
|
2020-06-05 07:38:55 +00:00
|
|
|
if self.service_type and self.product:
|
2020-05-27 10:36:49 +00:00
|
|
|
try:
|
2020-06-05 07:38:55 +00:00
|
|
|
service_type = import_module(self.service_type)
|
|
|
|
return service_type.ProductRouter(self.product, self)
|
2020-05-27 10:36:49 +00:00
|
|
|
except Exception as e:
|
2020-06-05 07:38:55 +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
|
|
|
|
|
|
|
|
@classmethod
|
2020-06-06 04:33:52 +00:00
|
|
|
def from_productplan(cls, productplan, client):
|
|
|
|
cls.objects.create(client=client,
|
|
|
|
name=productplan.product.name,
|
|
|
|
description=productplan.product.description,
|
|
|
|
service_type=productplan.product.product_type,
|
|
|
|
product=productplan.product,
|
|
|
|
product_groups=productplan.product.product_groups,
|
|
|
|
currency=productplan.currency,
|
|
|
|
cycle=productplan.cycle,
|
|
|
|
count=productplan.count,
|
|
|
|
cost=productplan.cost,
|
|
|
|
taxable=productplan.taxable)
|
2020-05-27 10:36:49 +00:00
|
|
|
|
2020-06-05 07:38:55 +00:00
|
|
|
@property
|
|
|
|
def invoicable(self):
|
|
|
|
pass
|