from core.fields.numbers import CostField from core.models.local import Currency from core.models.profiles import ClientProfile from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, OneToOneField, BooleanField, SET_NULL from importlib import import_module from logging import getLogger logger = getLogger(__name__) class Service(Model): from core.models.products import ProductGroup, Product from core.models.billable import CycleChoices client = ForeignKey(ClientProfile, on_delete=CASCADE) name = CharField(max_length=255) description = TextField(null=True, blank=True) service_type = CharField(max_length=255, null=True, blank=True) product = ForeignKey(Product, on_delete=SET_NULL, null=True) product_groups = ManyToManyField(ProductGroup) currency = ForeignKey(Currency, on_delete=CASCADE) cycle = PositiveIntegerField(choices=CycleChoices.choices) count = PositiveIntegerField() cost = CostField() setup = CostField() taxable=BooleanField() @property def handler(self): if self.service_type and self.product: try: service_type = import_module(self.service_type) return service_type.ProductRouter(self.product, self) except Exception as e: logger.error( f"Could not load product handler {self.service_type} for product {self.id}: {e}") return None @classmethod 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) @property def invoicable(self): pass