2024-01-08 13:54:00 +05:30
|
|
|
import os
|
2024-05-07 12:23:36 +05:30
|
|
|
from datetime import datetime
|
|
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
from django import apps
|
|
|
|
|
from django.db import models
|
2024-05-07 12:23:36 +05:30
|
|
|
from django.db.models.signals import post_delete, post_save
|
2024-01-31 16:25:07 +05:30
|
|
|
from django.forms import ValidationError
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-05-07 12:23:36 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
from base.horilla_company_manager import HorillaCompanyManager
|
2024-05-07 12:23:36 +05:30
|
|
|
from base.models import Company, Department, JobPosition, Tags
|
|
|
|
|
from employee.models import Employee
|
2024-03-29 10:06:23 +05:30
|
|
|
from horilla.models import HorillaModel
|
2024-01-08 13:54:00 +05:30
|
|
|
from horilla_audit.methods import get_diff
|
|
|
|
|
from horilla_audit.models import HorillaAuditInfo, HorillaAuditLog
|
|
|
|
|
|
|
|
|
|
PRIORITY = [
|
|
|
|
|
("low", "Low"),
|
|
|
|
|
("medium", "Medium"),
|
|
|
|
|
("high", "High"),
|
|
|
|
|
]
|
|
|
|
|
MANAGER_TYPES = [
|
|
|
|
|
("department", "Department"),
|
|
|
|
|
("job_position", "Job Position"),
|
|
|
|
|
("individual", "Individual"),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
TICKET_TYPES = [
|
|
|
|
|
("suggestion", "Suggestion"),
|
|
|
|
|
("complaint", "Complaint"),
|
|
|
|
|
("service_request", "Service Request"),
|
|
|
|
|
("meeting_request", "Meeting Request"),
|
|
|
|
|
("anounymous_complaint", "Anonymous Complaint"),
|
|
|
|
|
("others", "Others"),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
TICKET_STATUS = [
|
2024-03-10 19:37:46 +05:30
|
|
|
("new", "New"),
|
|
|
|
|
("in_progress", "In Progress"),
|
|
|
|
|
("on_hold", "On Hold"),
|
|
|
|
|
("resolved", "Resolved"),
|
|
|
|
|
("canceled", "Canceled"),
|
2024-01-08 13:54:00 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class DepartmentManager(HorillaModel):
|
2024-03-10 19:37:46 +05:30
|
|
|
manager = models.ForeignKey(
|
2024-01-08 13:54:00 +05:30
|
|
|
Employee,
|
2025-04-22 11:52:36 +05:30
|
|
|
verbose_name=_("Manager"),
|
2024-03-10 19:37:46 +05:30
|
|
|
related_name="dep_manager",
|
|
|
|
|
on_delete=models.CASCADE,
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
|
|
|
|
department = models.ForeignKey(
|
|
|
|
|
Department,
|
2025-04-22 11:52:36 +05:30
|
|
|
verbose_name=_("Department"),
|
2024-01-08 13:54:00 +05:30
|
|
|
related_name="dept_manager",
|
2024-03-10 19:37:46 +05:30
|
|
|
on_delete=models.CASCADE,
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
|
|
|
|
company_id = models.ForeignKey(
|
2024-03-10 19:37:46 +05:30
|
|
|
Company, null=True, editable=False, on_delete=models.PROTECT
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
2024-11-15 10:58:53 +05:30
|
|
|
objects = HorillaCompanyManager("manager__employee_work_info__company_id")
|
2024-01-08 13:54:00 +05:30
|
|
|
|
2024-10-11 12:28:26 +05:30
|
|
|
class Meta:
|
|
|
|
|
unique_together = ("department", "manager")
|
2025-04-22 11:52:36 +05:30
|
|
|
verbose_name = _("Department Manager")
|
|
|
|
|
verbose_name_plural = _("Department Managers")
|
2024-10-11 12:28:26 +05:30
|
|
|
|
|
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
|
super().clean(*args, **kwargs)
|
|
|
|
|
if not self.manager.get_department() == self.department:
|
|
|
|
|
raise ValidationError(_(f"This employee is not from {self.department} ."))
|
|
|
|
|
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class TicketType(HorillaModel):
|
2025-04-22 11:52:36 +05:30
|
|
|
title = models.CharField(max_length=100, unique=True, verbose_name=_("Title"))
|
|
|
|
|
type = models.CharField(choices=TICKET_TYPES, max_length=50, verbose_name=_("Type"))
|
|
|
|
|
prefix = models.CharField(max_length=3, unique=True, verbose_name=_("Prefix"))
|
2024-03-10 19:37:46 +05:30
|
|
|
company_id = models.ForeignKey(
|
|
|
|
|
Company, null=True, editable=False, on_delete=models.PROTECT
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
2024-03-10 19:37:46 +05:30
|
|
|
objects = HorillaCompanyManager(related_company_field="company_id")
|
2024-01-08 13:54:00 +05:30
|
|
|
|
|
|
|
|
def __str__(self):
|
2024-03-10 19:37:46 +05:30
|
|
|
return self.title
|
|
|
|
|
|
2025-04-22 11:52:36 +05:30
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("Ticket Type")
|
|
|
|
|
verbose_name_plural = _("Ticket Types")
|
|
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class Ticket(HorillaModel):
|
2024-01-08 13:54:00 +05:30
|
|
|
|
|
|
|
|
title = models.CharField(max_length=50)
|
|
|
|
|
employee_id = models.ForeignKey(
|
2024-03-10 19:37:46 +05:30
|
|
|
Employee, on_delete=models.PROTECT, related_name="ticket", verbose_name="Owner"
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
|
|
|
|
ticket_type = models.ForeignKey(
|
|
|
|
|
TicketType,
|
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
|
verbose_name="Ticket Type",
|
|
|
|
|
)
|
2024-02-14 12:59:41 +05:30
|
|
|
description = models.TextField(max_length=255)
|
2024-03-10 19:37:46 +05:30
|
|
|
priority = models.CharField(choices=PRIORITY, max_length=100, default="low")
|
2024-01-08 13:54:00 +05:30
|
|
|
created_date = models.DateField(auto_now_add=True)
|
|
|
|
|
resolved_date = models.DateField(blank=True, null=True)
|
2025-04-22 11:52:36 +05:30
|
|
|
assigning_type = models.CharField(
|
|
|
|
|
choices=MANAGER_TYPES, max_length=100, verbose_name=_("Assigning Type")
|
|
|
|
|
)
|
|
|
|
|
raised_on = models.CharField(max_length=100, verbose_name=_("Forward To"))
|
2024-03-10 19:37:46 +05:30
|
|
|
assigned_to = models.ManyToManyField(
|
|
|
|
|
Employee, blank=True, related_name="ticket_assigned_to"
|
|
|
|
|
)
|
|
|
|
|
deadline = models.DateField(null=True, blank=True)
|
|
|
|
|
tags = models.ManyToManyField(Tags, blank=True, related_name="ticket_tags")
|
|
|
|
|
status = models.CharField(choices=TICKET_STATUS, default="new", max_length=50)
|
2024-01-08 13:54:00 +05:30
|
|
|
history = HorillaAuditLog(
|
|
|
|
|
related_name="history_set",
|
|
|
|
|
bases=[
|
|
|
|
|
HorillaAuditInfo,
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
objects = HorillaCompanyManager(
|
2024-11-15 10:58:53 +05:30
|
|
|
related_company_field="employee_id__employee_work_info__company_id"
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-10-11 12:28:26 +05:30
|
|
|
class Meta:
|
|
|
|
|
ordering = ["-created_date"]
|
2025-04-22 11:52:36 +05:30
|
|
|
verbose_name = _("Ticket")
|
|
|
|
|
verbose_name_plural = _("Tickets")
|
2024-10-11 12:28:26 +05:30
|
|
|
|
2024-01-31 16:25:07 +05:30
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
|
super().clean(*args, **kwargs)
|
|
|
|
|
deadline = self.deadline
|
|
|
|
|
today = datetime.today().date()
|
|
|
|
|
if deadline < today:
|
|
|
|
|
raise ValidationError(_("Deadline should be greater than today"))
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def get_raised_on(self):
|
|
|
|
|
obj_id = self.raised_on
|
2024-03-10 19:37:46 +05:30
|
|
|
if self.assigning_type == "department":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = Department.objects.get(id=obj_id).department
|
2024-03-10 19:37:46 +05:30
|
|
|
elif self.assigning_type == "job_position":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = JobPosition.objects.get(id=obj_id).job_position
|
2024-03-10 19:37:46 +05:30
|
|
|
elif self.assigning_type == "individual":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = Employee.objects.get(id=obj_id).get_full_name()
|
|
|
|
|
return raised_on
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def get_raised_on_object(self):
|
|
|
|
|
obj_id = self.raised_on
|
2024-03-10 19:37:46 +05:30
|
|
|
if self.assigning_type == "department":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = Department.objects.get(id=obj_id)
|
2024-03-10 19:37:46 +05:30
|
|
|
elif self.assigning_type == "job_position":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = JobPosition.objects.get(id=obj_id)
|
2024-03-10 19:37:46 +05:30
|
|
|
elif self.assigning_type == "individual":
|
2024-01-08 13:54:00 +05:30
|
|
|
raised_on = Employee.objects.get(id=obj_id)
|
|
|
|
|
return raised_on
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def __str__(self):
|
|
|
|
|
return self.title
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def tracking(self):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to return the tracked history of the instance
|
|
|
|
|
"""
|
|
|
|
|
return get_diff(self)
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
|
2024-10-11 12:28:26 +05:30
|
|
|
class ClaimRequest(HorillaModel):
|
|
|
|
|
ticket_id = models.ForeignKey(
|
|
|
|
|
Ticket,
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
null=True,
|
|
|
|
|
blank=True,
|
|
|
|
|
)
|
|
|
|
|
employee_id = models.ForeignKey(
|
|
|
|
|
Employee,
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
null=True,
|
|
|
|
|
blank=True,
|
|
|
|
|
)
|
|
|
|
|
is_approved = models.BooleanField(default=False)
|
|
|
|
|
is_rejected = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
unique_together = ("ticket_id", "employee_id")
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return f"{self.ticket_id}|{self.employee_id}"
|
|
|
|
|
|
|
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
|
super().clean(*args, **kwargs)
|
|
|
|
|
if not self.ticket_id:
|
|
|
|
|
raise ValidationError({"ticket_id": _("This field is required.")})
|
|
|
|
|
if not self.employee_id:
|
|
|
|
|
raise ValidationError({"employee_id": _("This field is required.")})
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class Comment(HorillaModel):
|
2024-04-09 16:04:37 +05:30
|
|
|
comment = models.TextField(null=True, blank=True)
|
2024-01-08 13:54:00 +05:30
|
|
|
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, related_name="comment")
|
2024-03-10 19:37:46 +05:30
|
|
|
employee_id = models.ForeignKey(
|
|
|
|
|
Employee, on_delete=models.DO_NOTHING, related_name="employee_comment"
|
|
|
|
|
)
|
2024-01-08 13:54:00 +05:30
|
|
|
date = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.comment
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class Attachment(HorillaModel):
|
2024-01-08 13:54:00 +05:30
|
|
|
file = models.FileField(upload_to="Tickets/Attachment")
|
|
|
|
|
description = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
format = models.CharField(max_length=50, blank=True, null=True)
|
|
|
|
|
ticket = models.ForeignKey(
|
|
|
|
|
Ticket,
|
2024-03-10 19:37:46 +05:30
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
null=True,
|
|
|
|
|
blank=True,
|
|
|
|
|
related_name="ticket_attachment",
|
|
|
|
|
)
|
2024-01-08 13:54:00 +05:30
|
|
|
comment = models.ForeignKey(
|
|
|
|
|
Comment,
|
2024-03-10 19:37:46 +05:30
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
null=True,
|
|
|
|
|
blank=True,
|
|
|
|
|
related_name="comment_attachment",
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def get_file_format(self):
|
2024-03-10 19:37:46 +05:30
|
|
|
image_format = [".jpg", ".jpeg", ".png", ".svg"]
|
|
|
|
|
audio_format = [".m4a", ".mp3"]
|
2024-01-08 13:54:00 +05:30
|
|
|
file_extension = os.path.splitext(self.file.url)[1].lower()
|
|
|
|
|
if file_extension in audio_format:
|
2024-03-10 19:37:46 +05:30
|
|
|
self.format = "audio"
|
2024-01-08 13:54:00 +05:30
|
|
|
elif file_extension in image_format:
|
2024-03-10 19:37:46 +05:30
|
|
|
self.format = "image"
|
|
|
|
|
else:
|
|
|
|
|
self.format = "file"
|
2024-01-08 13:54:00 +05:30
|
|
|
|
2024-03-10 19:37:46 +05:30
|
|
|
def save(self, *args, **kwargs):
|
2024-01-08 13:54:00 +05:30
|
|
|
self.get_file_format()
|
|
|
|
|
|
2024-03-10 19:37:46 +05:30
|
|
|
super().save(self, *args, **kwargs)
|
|
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
def __str__(self):
|
|
|
|
|
return os.path.basename(self.file.name)
|
|
|
|
|
|
|
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class FAQCategory(HorillaModel):
|
2024-01-08 13:54:00 +05:30
|
|
|
title = models.CharField(max_length=30)
|
2024-03-10 19:37:46 +05:30
|
|
|
description = models.TextField(blank=True, null=True, max_length=255)
|
2024-01-08 13:54:00 +05:30
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.title
|
|
|
|
|
|
2025-04-22 11:52:36 +05:30
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("FAQ Category")
|
|
|
|
|
verbose_name_plural = _("FAQ Categories")
|
|
|
|
|
|
2024-01-08 13:54:00 +05:30
|
|
|
|
2024-03-29 10:06:23 +05:30
|
|
|
class FAQ(HorillaModel):
|
2024-01-08 13:54:00 +05:30
|
|
|
question = models.CharField(max_length=255)
|
2024-03-10 19:37:46 +05:30
|
|
|
answer = models.TextField(max_length=255)
|
2024-01-08 13:54:00 +05:30
|
|
|
tags = models.ManyToManyField(Tags)
|
2024-03-10 19:37:46 +05:30
|
|
|
category = models.ForeignKey(FAQCategory, on_delete=models.PROTECT)
|
|
|
|
|
company_id = models.ForeignKey(
|
|
|
|
|
Company, null=True, editable=False, on_delete=models.PROTECT
|
2024-01-08 13:54:00 +05:30
|
|
|
)
|
2024-03-10 19:37:46 +05:30
|
|
|
objects = HorillaCompanyManager(related_company_field="company_id")
|
2024-01-08 13:54:00 +05:30
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.question
|
|
|
|
|
|
2025-04-22 11:52:36 +05:30
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("FAQ")
|
|
|
|
|
verbose_name_plural = _("FAQs")
|