From 7751500eb785a5d3a80f350881d1a684068914b0 Mon Sep 17 00:00:00 2001 From: Horilla Date: Wed, 1 Nov 2023 12:19:04 +0530 Subject: [PATCH] [ADD] EMPLOYEE: New bulk selection feature in employee view --- employee/static/employee/actions.js | 111 ++++++++-- .../templates/employee/profile/profile.html | 4 +- employee/templates/employee_nav.html | 71 +++--- .../employee_personal_info/employee_list.html | 202 +++++++++++++++--- .../employee_personal_info/employee_view.html | 6 +- employee/templates/tabs/attendance-tab.html | 2 +- employee/urls.py | 3 + employee/views.py | 54 +++++ 8 files changed, 362 insertions(+), 91 deletions(-) diff --git a/employee/static/employee/actions.js b/employee/static/employee/actions.js index 4a20ef8c2..54d793a80 100644 --- a/employee/static/employee/actions.js +++ b/employee/static/employee/actions.js @@ -29,6 +29,16 @@ var norowMessages = { en: "No rows have been selected.", fr: "Aucune ligne n'a été sélectionnée.", }; + +var selectedemployees = { + ar: " موظفون محددون", + de: " Ausgewählte Mitarbeiter", + es: " Empleados seleccionados", + en: " Selected Employees", + fr: " Employés sélectionnés", +}; + + function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== "") { @@ -63,15 +73,70 @@ $(".all-employee").change(function (e) { } else { $(".all-employee-row").prop("checked", false); } + addingIds() }); -$(".all-employee").change(function (e) { - var is_checked = $(this).is(":checked"); - if (is_checked) { - $(".all-employee-row").prop("checked", true); - } else { - $(".all-employee-row").prop("checked", false); + + +$(".all-employee-row").change(function(){ + addingIds() +}) + + +function addingIds(){ + var ids = JSON.parse($("#selectedInstances").attr("data-ids") || "[]"); + var selectedCount = 0; + + $(".all-employee-row").each(function() { + if ($(this).is(":checked")) { + ids.push(this.id); + } else { + var index = ids.indexOf(this.id); + if (index > -1) { + ids.splice(index, 1); + } + } + }); + var ids = makeListUnique(ids); + var selectedCount = ids.length; + + getCurrentLanguageCode(function(code){ + languageCode = code; + var message = selectedemployees[languageCode]; + + $("#selectedInstances").attr("data-ids", JSON.stringify(ids)); + $('#selectedshow').text(selectedCount + ' -' + message); + }) + +} + + +function tickCheckboxes(uniqueIds) { + + click = $("#selectedInstances").attr("data-clicked") + if ( click === '1'){ + $(".all-employee").prop('checked',true) } -}); + + uniqueIds.forEach(function(id) { + $('#' + id).prop('checked', true); + }); + var selectedCount = uniqueIds.length; + getCurrentLanguageCode(function(code){ + languageCode = code; + var message = selectedemployees[languageCode]; + $('#selectedshow').text(selectedCount + ' -' + message); + }) + +} + + +function makeListUnique(list) { + return Array.from(new Set(list)); +} + +var ids = JSON.parse($("#selectedInstances").attr("data-ids") || "[]"); +tickCheckboxes(ids); + $("#archiveEmployees").click(function (e) { e.preventDefault(); var languageCode = null; @@ -97,11 +162,12 @@ $("#archiveEmployees").click(function (e) { }).then(function (result) { if (result.isConfirmed) { e.preventDefault(); + ids = []; - checkedRows.each(function () { - ids.push($(this).attr("id")); - }); - + + ids.push($("#selectedInstances").attr("data-ids")) + ids = JSON.parse($("#selectedInstances").attr("data-ids")); + $.ajax({ type: "POST", url: "/employee/employee-bulk-archive?is_active=False", @@ -147,12 +213,13 @@ $("#unArchiveEmployees").click(function (e) { confirmButtonText: "Confirm", }).then(function (result) { if (result.isConfirmed) { - var checkedRows = $(".all-employee-row").filter(":checked"); + e.preventDefault(); + ids = []; - checkedRows.each(function () { - ids.push($(this).attr("id")); - }); - + + ids.push($("#selectedInstances").attr("data-ids")) + ids = JSON.parse($("#selectedInstances").attr("data-ids")); + $.ajax({ type: "POST", url: "/employee/employee-bulk-archive?is_active=True", @@ -198,13 +265,13 @@ $("#deleteEmployees").click(function (e) { confirmButtonText: "Confirm", }).then(function (result) { if (result.isConfirmed) { - var checkedRows = $(".all-employee-row").filter(":checked"); + e.preventDefault(); + ids = []; - checkedRows.each(function () { - ids.push($(this).attr("id")); - }); - - $.ajax({ + ids.push($("#selectedInstances").attr("data-ids")) + ids = JSON.parse($("#selectedInstances").attr("data-ids")); + + $.ajax({ type: "POST", url: "/employee/employee-bulk-delete", data: { diff --git a/employee/templates/employee/profile/profile.html b/employee/templates/employee/profile/profile.html index e9fbd0b5f..11e6d8bb7 100644 --- a/employee/templates/employee/profile/profile.html +++ b/employee/templates/employee/profile/profile.html @@ -46,7 +46,9 @@ // Active tab script function activeCandTab() { var activeTab = localStorage.getItem("activeCandTab") - if(activeTab != null){ + if (!$(activeTab).length && $(`[data-target="#personal"]`).length) { + $(`[data-target="#personal"]`)[0].click() + }else if(activeTab != null){ $(".oh-general__tab-link--active").removeClass("oh-general__tab-link--active"); $(`[data-target='${activeTab}']`).addClass("oh-general__tab-link--active"); $(".oh-general__tab-target").addClass("d-none"); diff --git a/employee/templates/employee_nav.html b/employee/templates/employee_nav.html index a7ad42e35..3947e3d38 100644 --- a/employee/templates/employee_nav.html +++ b/employee/templates/employee_nav.html @@ -165,7 +165,7 @@
- {% if data %} + {% if emp %}
- {% endif %} {% if data %} + {% endif %} {% if emp %}
diff --git a/employee/templates/employee_personal_info/employee_list.html b/employee/templates/employee_personal_info/employee_list.html index 0b722ab06..3841e3cce 100644 --- a/employee/templates/employee_personal_info/employee_list.html +++ b/employee/templates/employee_personal_info/employee_list.html @@ -2,32 +2,38 @@ {% load static %} {% load i18n %} -
- {% if messages %} -
- {% for message in messages %} -
{{ message }}
- {% endfor %} -
- {% endif %} - {% include 'filter_tags.html' %} +
+ {% if messages %} +
+ {% for message in messages %} +
{{ message }}
+ {% endfor %} +
+ {% endif %} + {% include 'filter_tags.html' %} +
+ {% trans "Select All Employees" %} +
+
+ {% trans "Unselect All Employees" %} +
+
+
-
-
-
- -
-
- {% trans "Employee" %} -
-
+
+ +
+
+ {% trans "Employee" %}
{% trans "Email" %}
{% trans "Phone" %}
@@ -48,14 +54,16 @@
+
+ +
+
+
-
- -
{% if emp.employee_profile %} @@ -185,7 +193,7 @@ class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100" title="{% trans 'Un-Archive' %}" > - + {% endif %} @@ -240,6 +248,7 @@ hx-target="#view-container" hx-get="{% url 'employee-view-list' %}?{{pd}}&page=1" class="oh-pagination__link" + onclick=tickCheckboxes() >{% trans "First" %} @@ -248,6 +257,7 @@ hx-target="#view-container" hx-get="{% url 'employee-view-list' %}?{{pd}}&page={{ data.previous_page_number }}" class="oh-pagination__link" + onclick=tickCheckboxes() >{% trans "Previous" %} @@ -257,6 +267,7 @@ hx-target="#view-container" hx-get="{% url 'employee-view-list' %}?{{pd}}&page={{ data.next_page_number }}" class="oh-pagination__link" + onclick=tickCheckboxes() >{% trans "Next" %} @@ -265,6 +276,7 @@ hx-target="#view-container" hx-get="{% url 'employee-view-list' %}?{{pd}}&page={{ data.paginator.num_pages }}" class="oh-pagination__link" + onclick=tickCheckboxes() >{% trans "Last" %} @@ -277,3 +289,133 @@ + \ No newline at end of file diff --git a/employee/templates/employee_personal_info/employee_view.html b/employee/templates/employee_personal_info/employee_view.html index 4209da4c3..2809ccaa8 100644 --- a/employee/templates/employee_personal_info/employee_view.html +++ b/employee/templates/employee_personal_info/employee_view.html @@ -1,5 +1,8 @@ -{% extends "index.html" %} {% block content %} {% include 'employee_nav.html' %} +{% extends "index.html" %} {% load static %} {% load i18n %} {% block content %} {% include 'employee_nav.html' %}
+
{% if data %} {% if view_type == 'list' %} @@ -12,4 +15,5 @@ {% endif %}
+ {% endblock %} diff --git a/employee/templates/tabs/attendance-tab.html b/employee/templates/tabs/attendance-tab.html index 7ac58968e..231e59236 100644 --- a/employee/templates/tabs/attendance-tab.html +++ b/employee/templates/tabs/attendance-tab.html @@ -184,7 +184,7 @@
{% with month=ot.month|capfirst %}{% trans month %}{% endwith %}
{{ot.year}}
-
{{ot.hour_account}}
+
{{ot.worked_hours}}
{{ot.overtime}}
diff --git a/employee/urls.py b/employee/urls.py index 982a7de70..52cc05c83 100644 --- a/employee/urls.py +++ b/employee/urls.py @@ -163,4 +163,7 @@ urlpatterns = [ path("attendance-tab/", views.attendance_tab, name="attendance-tab"), path("shift-tab/", views.shift_tab, name="shift-tab"), path("contract-tab/", views.contract_tab, name="contract-tab"), + path('employee-select/', views.employee_select, name='employee-select'), + path('employee-select-filter/', views.employee_select_filter, name='employee-select-filter'), + ] diff --git a/employee/views.py b/employee/views.py index af6bd4f27..f359c1fe0 100755 --- a/employee/views.py +++ b/employee/views.py @@ -18,6 +18,7 @@ from datetime import datetime, timedelta, date from collections import defaultdict from urllib.parse import parse_qs import pandas as pd +from django.db.models import Q from django.db.models import F, ProtectedError from django.conf import settings from django.contrib import messages @@ -382,6 +383,7 @@ def employee_view(request): ) data_dict = parse_qs(previous_data) get_key_instances(Employee, data_dict) + emp = Employee.objects.all() return render( request, "employee_personal_info/employee_view.html", @@ -393,6 +395,7 @@ def employee_view(request): "export_form": export_form, "view_type": view_type, "filter_dict": data_dict, + "emp":emp }, ) @@ -1648,3 +1651,54 @@ def widget_filter(request): """ ids = EmployeeFilter(request.GET).qs.values_list("id", flat=True) return JsonResponse({"ids": list(ids)}) + + +@login_required +def employee_select(request): + page_number = request.GET.get('page') + + if page_number == 'all': + employees = Employee.objects.all() + else: + employees = Employee.objects.all().order_by('employee_first_name') + paginator = Paginator(employees, per_page=50) + page_obj = paginator.get_page(page_number) + + employee_ids = [str(emp.id) for emp in employees] + total_count = employees.count() + + context = { + 'employee_ids': employee_ids, + 'total_count': total_count + } + + return JsonResponse(context, safe=False) + + +@login_required +def employee_select_filter(request): + page_number = request.GET.get('page') + filtered = request.GET.get('filter') + filters = json.loads(filtered) if filtered else {} + + if page_number == 'all': + query = Q() + + for key, value in filters.items(): + if value != '' and key != 'is_active': # Add check for is_active and non-empty values + kwargs = {key: value} + query &= Q(**kwargs) + elif key == 'is_active' and value in ['true', 'false']: # Check if is_active is valid + kwargs = {key: value == 'true'} # Convert 'true' to True, 'false' to False + query &= Q(**kwargs) + + employees = Employee.objects.filter(query) + + employee_ids = [str(emp.id) for emp in employees] + total_count = employees.count() + + context = { + 'employee_ids': employee_ids, + 'total_count': total_count + } + return JsonResponse(context)