diff --git a/employee/forms.py b/employee/forms.py index 66355b1d5..4b0f7dae6 100644 --- a/employee/forms.py +++ b/employee/forms.py @@ -26,9 +26,10 @@ import re from django import forms from django.db.models import Q from django.contrib.auth.models import User -from django.forms import DateInput, ModelChoiceField, TextInput +from django.forms import DateInput, TextInput from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy as trans +from django.template.loader import render_to_string from base import thread_local_middleware from horilla.decorators import logger from employee.models import ( @@ -589,7 +590,16 @@ class DisciplinaryActionForm(ModelForm): self.fields["action"].queryset.values_list("id", "title") ) self.fields["action"].choices = action_choices - self.fields["action"].choices += [("create", _("Create new action type "))] + if self.instance.pk is None: + self.fields["action"].choices += [("create", _("Create new action type "))] + + def as_p(self): + """ + Render the form fields as HTML table rows with Bootstrap styling. + """ + context = {"form": self} + table_html = render_to_string("common_form.html", context) + return table_html class ActiontypeForm(ModelForm): diff --git a/employee/models.py b/employee/models.py index 02745754a..75c3d4102 100644 --- a/employee/models.py +++ b/employee/models.py @@ -7,7 +7,7 @@ This module is used to register models for employee app import datetime as dtime from datetime import date, datetime, timedelta -from typing import Any +from typing import Any, Iterable from django.conf import settings from django.db import models from django.contrib.auth.models import User, Permission @@ -19,6 +19,7 @@ from django.core.exceptions import ValidationError from django.core.files.storage import default_storage from simple_history.models import HistoricalRecords from base import thread_local_middleware +from base.models import validate_time_format from horilla_audit.models import HorillaAuditLog, HorillaAuditInfo from horilla_audit.methods import get_diff from base.models import ( @@ -344,7 +345,7 @@ class Employee(models.Model): This method is used to get last send mail """ from base.models import EmailLog - + return ( EmailLog.objects.filter(to__icontains=self.get_mail()) .order_by("-created_at") @@ -591,7 +592,7 @@ class EmployeeNote(models.Model): related_name="employee_name", ) title = models.CharField(max_length=50, null=True, verbose_name=_("Title")) - description = models.TextField(verbose_name=_("Description"),max_length=255) + description = models.TextField(verbose_name=_("Description"), max_length=255) created_at = models.DateTimeField( auto_now_add=True, verbose_name=_("Created At"), @@ -655,7 +656,7 @@ class BonusPoint(models.Model): max_length=100, choices=CONDITIONS, blank=True, null=True ) redeeming_points = models.IntegerField(blank=True, null=True) - reason = models.TextField(blank=True, null=True,max_length=255) + reason = models.TextField(blank=True, null=True, max_length=255) history = HorillaAuditLog( related_name="history_set", bases=[ @@ -678,7 +679,6 @@ class BonusPoint(models.Model): BonusPoint.objects.create(employee_id=instance) - class Actiontype(models.Model): """ Action type model @@ -702,10 +702,18 @@ class DisciplinaryAction(models.Model): Disciplinary model """ - employee_id = models.ManyToManyField(Employee) + choices = [("days", "Days"), ("hours", "In Hours")] + employee_id = models.ManyToManyField(Employee, verbose_name="Employee") action = models.ForeignKey(Actiontype, on_delete=models.CASCADE) description = models.TextField(max_length=255) - days = models.IntegerField(null=True, blank=True) + unit_in = models.CharField(max_length=10, choices=choices,default="days") + days = models.IntegerField(null=True, default=2) + hours = models.CharField( + max_length=6, + default="00:00", + null=True, + validators=[validate_time_format], + ) start_date = models.DateField(null=True) attachment = models.FileField( upload_to="employee/discipline", null=True, blank=True diff --git a/employee/policies.py b/employee/policies.py index ac8ad71ad..14b41a88a 100644 --- a/employee/policies.py +++ b/employee/policies.py @@ -171,7 +171,7 @@ def create_actions(request): """ Method is used to create Disciplinaryaction """ - form = DisciplinaryActionForm(request.GET) + form = DisciplinaryActionForm(initial=request.GET) employees = [] if request.method == "POST": form = DisciplinaryActionForm(request.POST, request.FILES) diff --git a/employee/templates/disciplinary_actions/disciplinary_records.html b/employee/templates/disciplinary_actions/disciplinary_records.html index 4e57f6c56..c69023878 100644 --- a/employee/templates/disciplinary_actions/disciplinary_records.html +++ b/employee/templates/disciplinary_actions/disciplinary_records.html @@ -53,13 +53,20 @@ {% if i.action.action_type == 'suspension' %}
{{ i.action }} -

{% trans "Suspended for" %} {{ i.days }} {% trans "days." %}

+

{% trans "Suspended for" %} + {% if i.unit_in == "days" %} + {{ i.days }} {% trans "days" %}. + {% else %} + {{ i.hours }} {% trans "hours" %}. + {% endif %} +

+
{% else %}
{{ i.action }}
{% endif %} - {% if i.action.action_type == 'suspension' %} + {% if i.action.action_type == 'suspension' and i.unit_in == "days" %}
{{ i.start_date }}   to  {{ i.start_date | add_days:i.days }} @@ -187,7 +194,7 @@ function actionTypeChange(selectElement) { var selectedActiontype = selectElement.val(); var parentForm = selectElement.parents().closest("form"); - if (selectedActiontype !== "create") { + if (selectedActiontype !== "create" && selectedActiontype) { $.ajax({ type: "post", url: "{% url 'action-type-details' %}", @@ -196,17 +203,34 @@ "action_type": selectedActiontype, }, success: function (response) { - console.log(response.action_type); + // Check if the response.action_type is "suspension" + $("#id_unit_in").change(function (e) { + e.preventDefault(); + if (this.value == "days") { + parentForm.find('#id_days').parent().show(); + parentForm.find('#id_hours').parent().hide(); + parentForm.find('[name=days]').prop('required', true); + }else{ + parentForm.find('#id_hours').parent().show(); + parentForm.find('#id_days').parent().hide(); + parentForm.find('[name=days]').prop('required', false); + } + }); if (response.action_type === "suspension") { // Show the 'days' field - parentForm.find('#daysDiv').show(); - parentForm.find('[name=days]').prop('required', true); - } else { + parentForm.find('#id_unit_in').parent().show(); + parentForm.find('#id_hours').parent().show(); + $("#id_unit_in").change() + } else { // Hide the 'days' field - parentForm.find('#daysDiv').hide(); + $("#id_unit_in").change() + parentForm.find('#id_days').parent().hide(); + parentForm.find('#id_unit_in').parent().hide(); + parentForm.find('#id_hours').parent().hide(); parentForm.find('[name=days]').prop('required', false); } + }, }); }else{ diff --git a/employee/templates/disciplinary_actions/form.html b/employee/templates/disciplinary_actions/form.html index 2dea00d13..ab32706d5 100644 --- a/employee/templates/disciplinary_actions/form.html +++ b/employee/templates/disciplinary_actions/form.html @@ -23,43 +23,7 @@ > {% csrf_token %} -
- - {{ form.employee_id }} -
-
- - {{ form.action }} -
- -
- - {{ form.start_date }} -
- -
- - {{ form.description }} -
-
- - {{ form.attachment }} -
+ {{form.as_p}} + {{form.as_p}} + \ No newline at end of file diff --git a/employee/templates/employee/view/individual.html b/employee/templates/employee/view/individual.html index 13062d8b7..62b00547a 100644 --- a/employee/templates/employee/view/individual.html +++ b/employee/templates/employee/view/individual.html @@ -112,6 +112,27 @@ /> {% endif %} + {% if perms.change_employee %} + {% if employee.employee_user_id.is_active %} +
+ {% csrf_token %} + +
+ {% else %} +
+ {% csrf_token %} + +
+ {% endif %} + {% endif %}
diff --git a/employee/urls.py b/employee/urls.py index e199acb27..9d5bd88e5 100644 --- a/employee/urls.py +++ b/employee/urls.py @@ -104,6 +104,11 @@ urlpatterns = [ views.save_employee_bulk_update, name="save-employee-bulk-update", ), + path( + "employee-account-block-unblock//", + views.employee_account_block_unblock, + name="employee-account-block-unblock", + ), path( "employee-bulk-delete", views.employee_bulk_delete, name="employee-bulk-delete" ),