Skip to content

Commit

Permalink
[UPDT] LEAVE: Add leave type with infinite number of days and apply a…
Browse files Browse the repository at this point in the history
…s default to all employees option
  • Loading branch information
horilla-opensource committed Aug 28, 2024
1 parent 9a61036 commit fd18cd1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 22 deletions.
38 changes: 38 additions & 0 deletions leave/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
including leave type, leave request, leave allocation request, holidays and company leaves.
"""

import math
import re
import uuid
from datetime import date, datetime
Expand Down Expand Up @@ -160,6 +161,18 @@ def __init__(self, *args, **kwargs):

class LeaveTypeForm(ConditionForm):

employee_id = HorillaMultiSelectField(
queryset=Employee.objects.all(),
widget=HorillaMultiSelectWidget(
filter_route_name="employee-widget-filter",
filter_class=EmployeeFilter,
filter_instance_contex_name="f",
filter_template_path="employee_filters.html",
required=False,
),
label="Employee",
)

class Meta:
model = LeaveType
fields = "__all__"
Expand All @@ -179,8 +192,22 @@ def clean(self):
del self.errors["employee_id"]
if "exceed_days" in self.errors:
del self.errors["exceed_days"]
cleaned_data["total_days"] = round(cleaned_data["total_days"] * 2) / 2
if not cleaned_data["limit_leave"]:
cleaned_data["total_days"] = math.inf
return cleaned_data

def save(self, *args, **kwargs):
leave_type = super().save(*args, **kwargs)
if employees := self.data.getlist("employee_id"):
for employee_id in employees:
employee = Employee.objects.get(id=employee_id)
AvailableLeave(
leave_type_id=leave_type,
employee_id=employee,
available_days=leave_type.total_days,
).save()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -217,8 +244,19 @@ def clean(self):
cleaned_data = super().clean()
if "exceed_days" in self.errors:
del self.errors["exceed_days"]
cleaned_data["count"] = round(cleaned_data["count"] * 2) / 2
if not cleaned_data["limit_leave"]:
cleaned_data["count"] = math.inf

return cleaned_data

def save(self, *args, **kwargs):
leave_type = super().save(*args, **kwargs)

AvailableLeave.objects.filter(leave_type_id=leave_type).update(
total_leave_days=leave_type.count
)


def cal_effective_requested_days(start_date, end_date, leave_type_id, requested_days):
requested_dates = leave_requested_dates(start_date, end_date)
Expand Down
5 changes: 3 additions & 2 deletions leave/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ class LeaveType(HorillaModel):
name = models.CharField(max_length=30, null=False)
color = models.CharField(null=True, max_length=30)
payment = models.CharField(max_length=30, choices=PAYMENT, default="unpaid")
count = models.IntegerField(null=True, default=1)
count = models.FloatField(null=True, default=1)
period_in = models.CharField(max_length=30, choices=TIME_PERIOD, default="day")
total_days = models.IntegerField(null=True, default=1)
limit_leave = models.BooleanField(default=True)
total_days = models.FloatField(null=True, default=1)
reset = models.BooleanField(default=False)
is_encashable = models.BooleanField(default=False, verbose_name=_("Is encashable"))
reset_based = models.CharField(
Expand Down
2 changes: 1 addition & 1 deletion leave/templates/leave/job_position_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
{% endif %}
</label>
{{form.job_position}} {{form.job_position.errors}}
</div>
</div>
51 changes: 35 additions & 16 deletions leave/templates/leave/leave_type/leave_type_creation.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
{{form.payment.errors}}
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-6">
<div class="oh-input-group oh-label__info justify-content-between">
<div class="d-flex align-items-center">
<label for="id_reset" class="oh-label">{% trans "Limit Leave Days" %}</label>
<span class=" oh-info me-3" title="{% trans "Do you want to limit leave days for this leave type?" %}"></span>
</div>
<div class="oh-switch p-3 ">
{{form.limit_leave}}
{{form.limit_leave.errors}}
</div>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-12 totalDaysContainer">
<div class="oh-input-group">
<label for="" class="oh-label">{% trans "Total Days" %}</label>
{{form.count}}
Expand All @@ -76,7 +88,7 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
{{form.total_days.errors}}
</div>
</div>
<div class="col-sm-12 col-md-6">
<div class="col-sm-12 col-md-6 resetContainer">
<div class="oh-input-group oh-label__info justify-content-between">
<div class="d-flex align-items-center">
<label for="id_reset" class="oh-label">{% trans "Reset" %}</label>
Expand All @@ -94,8 +106,8 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
<label for="id_reset_based" class="oh-label">{% trans "Reset Period" %}</label>
<span id="reset_based_id" class=" oh-info me-3"title="{% trans "The time period for the leave to reset." %}"></span>
</div>
{{ form.reset_based }}
{{ form.reset_based.errors }}
{{ form.reset_based }}
{{ form.reset_based.errors }}
</div>
</div>
<div class="col-sm-12 col-md-12 ">
Expand Down Expand Up @@ -219,6 +231,14 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
</div>
</div>
</div>
<div class="col-sm-12 col-md-12">
<div class="oh-input-group oh-label__info">
<label for="id_employee_id" class="oh-label">{% trans "Assign to Employees" %}</label>
<span class=" oh-info me-3" title="{% trans 'Choose employees to assign this leave type' %}"></span>
</div>
{{form.employee_id}}
{{form.employee_id.errors}}
</div>
<div hidden>{{form.require_approval}}</div>
<div hidden>{{form.require_attachment}}</div>
<div hidden>{{form.exclude_company_leave}}</div>
Expand Down Expand Up @@ -256,8 +276,10 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
var carryforward_expire_in = $("#id_carryforward_expire_in");
var carryforward_expire_period_label = $("label[for='id_carryforward_expire_period']");
var carryforward_expire_period = $("#id_carryforward_expire_period");


var totalDaysContainer = $(".totalDaysContainer");
var resetContainer = $(".resetContainer");
var totalDays = $("#id_total_days");
var limitLeave = $("#id_limit_leave");

//Making all fields which are not needed first was hidden and not required
reset_based_label.hide();
Expand All @@ -276,13 +298,16 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
carryforward_expire_period_label.hide();
carryforward_expire_period.hide().prop('required', false);


// Check value of reset change field and trigger action
reset.change(function () {
(this.checked) ? (reset_based.show().prop('required', true), reset_based_label.show()) : (reset_based_label.hide(), reset_based.hide().prop('required', false), reset_based.val('')),reset_based_span.show()
reset_based.is(':hidden') ? (reset_month_label.hide(), reset_month.hide().prop('required', false), reset_month.val(''), reset_day_label.hide(), reset_day.val(''), reset_day.hide().prop('required', false), reset_weekend_label.hide(),reset_based_span.hide(), reset_weekend.hide().prop('required', false), reset_weekend.val('')) : ""
});

limitLeave.change(function (){
(this.checked) ? (totalDaysContainer.show(), resetContainer.show()) : (totalDaysContainer.hide(), resetContainer.hide())
})

// Check value of boolean change field and trigger action
reset_based.change(function (e) {
var value = $(this).val();
Expand Down Expand Up @@ -324,8 +349,6 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
reset_day_label.hide()
reset_day.val('')
}


});

carryforward_type.change(function () {
Expand All @@ -344,23 +367,18 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
else if ($("#id_carryforward_type").val() == "carryforward expire") {
$("#id_carryforward_max,#id_carryforward_expire_in,#id_carryforward_expire_period").parent().show();
}

}else{

$("#id_carryforward_type,#id_carryforward_max").parent().hide();
$("#id_carryforward_max,#id_carryforward_expire_in,#id_carryforward_expire_period").parent().hide();


}
}

$("select,input[type=checkbox]").change(function (e) {
toggleFields()
//check the yes or no fields
});

});
toggleFields()

toggleFields()

// leave total days calculation
$('#id_period_in, #id_count').change(function () {
Expand All @@ -382,6 +400,7 @@ <h2 class="oh-onboarding-card__section-title">{% trans "Create Leave Type" %}</h
});

});

function checkYesOrNoFields(){

if ($('#id_approval').is(":checked")) {
Expand Down
25 changes: 23 additions & 2 deletions leave/templates/leave/leave_type/leave_type_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ <h2 class="oh-onboarding-card__section-title">
{{form.payment.errors}}
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="col-sm-12 col-md-6">
<div class="oh-input-group oh-label__info justify-content-between">
<div class="d-flex align-items-center">
<label for="id_reset" class="oh-label">{% trans "Limit Leave Days" %}</label>
<span class=" oh-info me-3" title="{% trans "Do you want to limit leave days for this leave type?" %}"></span>
</div>
<div class="oh-switch p-3 ">
{{form.limit_leave}}
{{form.limit_leave.errors}}
</div>
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-12 totalDaysContainer">
<div class="oh-input-group">
<label for="" class="oh-label">{% trans "Total Days" %}</label>
{{form.count}}
Expand All @@ -72,7 +84,7 @@ <h2 class="oh-onboarding-card__section-title">
{{form.total_days.errors}}
</div>
</div>
<div class="col-sm-12 col-md-6">
<div class="col-sm-12 col-md-6 resetContainer">
<div class="oh-input-group oh-label__info justify-content-between">
<div class="d-flex align-items-center">
<label for="id_reset" class="oh-label">{% trans "Reset" %}</label>
Expand Down Expand Up @@ -258,6 +270,10 @@ <h2 class="oh-onboarding-card__section-title">
"label[for='id_carryforward_expire_period']"
);
var carryforward_expire_period = $("#id_carryforward_expire_period");
var totalDaysContainer = $(".totalDaysContainer");
var resetContainer = $(".resetContainer");
var totalDays = $("#id_total_days");
var limitLeave = $("#id_limit_leave");

var hiddenLabelsFields = $("[data-hidden]");
for (let index = 0; index < hiddenLabelsFields.length; index++) {
Expand All @@ -274,7 +290,12 @@ <h2 class="oh-onboarding-card__section-title">
else{
reset_based_span.hide()
}
(limitLeave.checked) ? (totalDaysContainer.show(), resetContainer.show()) : (totalDaysContainer.hide(), resetContainer.hide(), $("#id_count").val(0))
console.log($("#id_count").val())

limitLeave.change(function (){
(this.checked) ? (totalDaysContainer.show(), resetContainer.show()) : (totalDaysContainer.hide(), resetContainer.hide(), $("#id_count").val(0))
})

// Check value of reset change field and trigger action
reset.change(function () {
Expand Down
2 changes: 1 addition & 1 deletion leave/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ def user_leave_request(request, id):
form.add_error(
None, _("There is already a leave request for this date range..")
)
elif requested_days <= available_total_leave:
elif not leave_type.limit_leave or requested_days <= available_total_leave:
if form.is_valid():
leave_request = form.save(commit=False)
save = True
Expand Down

0 comments on commit fd18cd1

Please sign in to comment.