56 lines
2.4 KiB
Python
56 lines
2.4 KiB
Python
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()
|
|
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_productplanitem(cls, productplanitem, client):
|
|
plan = cls.objects.create(client=client,
|
|
name=productplanitem.plan.product.name,
|
|
description=productplanitem.plan.product.description,
|
|
service_type=productplanitem.plan.product.product_type,
|
|
product=productplanitem.plan.product,
|
|
product_groups=productplanitem.plan.product.product_groups,
|
|
currency=productplanitem.plan.currency,
|
|
cycle=productplanitem.cycle,
|
|
count=productplanitem.count,
|
|
cost=productplanitem.cost,
|
|
taxable=productplanitem.taxable)
|
|
|
|
@property
|
|
def invoicable(self):
|
|
pass
|