2019-06-10 21:56:21 +02:00
|
|
|
from django.db import models
|
2020-01-07 22:20:07 +01:00
|
|
|
|
|
|
|
from core.models import Category
|
2020-02-16 20:15:47 +01:00
|
|
|
from core.utils import td_format
|
2020-03-01 11:16:36 +01:00
|
|
|
from customers.models import Customer
|
2020-01-07 22:20:07 +01:00
|
|
|
|
2020-01-12 16:21:12 +01:00
|
|
|
from .device import Device
|
2019-06-10 21:56:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WarrantyType(Category):
|
|
|
|
description = models.TextField()
|
|
|
|
|
|
|
|
class Meta:
|
2020-07-07 22:12:57 +02:00
|
|
|
ordering = ['name']
|
2019-06-10 21:56:21 +02:00
|
|
|
verbose_name_plural = "Warranty Types"
|
|
|
|
|
|
|
|
|
|
|
|
class Warranty(models.Model):
|
2020-03-01 11:16:36 +01:00
|
|
|
customer = models.ForeignKey(Customer,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True)
|
2019-06-10 21:56:21 +02:00
|
|
|
device = models.ForeignKey(Device, on_delete=models.CASCADE)
|
|
|
|
valid_from = models.DateField()
|
|
|
|
valid_until = models.DateField()
|
2020-03-01 11:16:36 +01:00
|
|
|
warranty_type = models.ForeignKey(WarrantyType,
|
|
|
|
models.SET_NULL,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.customer = self.device.customer
|
|
|
|
super(Warranty, self).save(*args, **kwargs)
|
2019-06-10 21:56:21 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-02-16 13:00:25 +01:00
|
|
|
return str(self.device)
|
2019-06-10 21:56:21 +02:00
|
|
|
|
|
|
|
class Meta:
|
2020-07-07 22:12:57 +02:00
|
|
|
ordering = ['customer']
|
2019-06-10 21:56:21 +02:00
|
|
|
verbose_name_plural = "Warranties"
|
2019-12-01 22:51:54 +01:00
|
|
|
|
2020-02-16 20:15:47 +01:00
|
|
|
@property
|
|
|
|
def duration_in_years(self):
|
|
|
|
delta = self.valid_until - self.valid_from
|
|
|
|
return td_format(delta)
|