[ADD] EMPLOYEE: New feature for employee individual data export from employee view

This commit is contained in:
Horilla
2024-08-27 17:32:51 +05:30
parent 1bb45a5f2b
commit 2078776d36
4 changed files with 360 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import json
from datetime import date
from django import template
from django.apps import apps
from django.contrib import messages
from django.core.mail import EmailMessage
from django.core.paginator import Paginator
@@ -91,6 +92,168 @@ def send_mail(request, emp_id=None):
)
from attendance.filters import AttendanceFilters
from attendance.forms import AttendanceExportForm
from attendance.models import Attendance
from base.methods import export_data
from leave.filters import LeaveRequestFilter
from leave.forms import LeaveRequestExportForm
from leave.models import LeaveRequest
from payroll.filters import PayslipFilter
from payroll.forms.component_forms import PayslipExportColumnForm
@login_required
@manager_can_enter("employee.change_employee")
def employee_data_export(request, emp_id=None):
"""
This method used send mail to the employees
"""
resolver_match = request.resolver_match
if (
resolver_match
and resolver_match.url_name
and resolver_match.url_name == "export-data-employee"
):
employee = None
if emp_id:
employee = Employee.objects.get(id=emp_id)
context = {"employee": employee}
# IF LEAVE IS INSTALLED
if apps.is_installed("leave"):
excel_column = LeaveRequestExportForm()
export_filter = LeaveRequestFilter()
context.update(
{
"leave_excel_column": excel_column,
"leave_export_filter": export_filter.form,
}
)
# IF ATTENDANCE IS INSTALLED
if apps.is_installed("attendance"):
excel_column = AttendanceExportForm()
export_filter = AttendanceFilters()
context.update(
{
"attendance_excel_column": excel_column,
"attendance_export_filter": export_filter.form,
}
)
# IF PAYROLL IS INSTALLED
if apps.is_installed("payroll"):
context.update(
{
"payroll_export_column": PayslipExportColumnForm(),
"payroll_export_filter": PayslipFilter(request.GET),
}
)
return render(request, "employee/export_data_employee.html", context=context)
return export_data(
request=request,
model=Attendance,
filter_class=AttendanceFilters,
form_class=AttendanceExportForm,
file_name="Attendance_export",
)
# @login_required
# def payslip_export(request):
# """
# This view exports payslip data based on selected fields and filters,
# and generates an Excel file for download.
# """
# choices_mapping = {
# "draft": _("Draft"),
# "review_ongoing": _("Review Ongoing"),
# "confirmed": _("Confirmed"),
# "paid": _("Paid"),
# }
# selected_columns = []
# payslips_data = {}
# payslips = PayslipFilter(request.GET).qs
# today_date = date.today().strftime("%Y-%m-%d")
# file_name = f"Payslip_excel_{today_date}.xlsx"
# selected_fields = request.GET.getlist("selected_fields")
# form = forms.PayslipExportColumnForm()
# if not selected_fields:
# selected_fields = form.fields["selected_fields"].initial
# ids = request.GET.get("ids")
# id_list = json.loads(ids)
# payslips = Payslip.objects.filter(id__in=id_list)
# for field in forms.excel_columns:
# value = field[0]
# key = field[1]
# if value in selected_fields:
# selected_columns.append((value, key))
# for column_value, column_name in selected_columns:
# nested_attributes = column_value.split("__")
# payslips_data[column_name] = []
# for payslip in payslips:
# value = payslip
# for attr in nested_attributes:
# value = getattr(value, attr, None)
# if value is None:
# break
# data = str(value) if value is not None else ""
# if column_name == "Status":
# data = choices_mapping.get(value, "")
# if type(value) == date:
# user = request.user
# employee = user.employee_get
# # Taking the company_name of the user
# info = EmployeeWorkInformation.objects.filter(employee_id=employee)
# if info.exists():
# for i in info:
# employee_company = i.company_id
# company_name = Company.objects.filter(company=employee_company)
# emp_company = company_name.first()
# # Access the date_format attribute directly
# date_format = (
# emp_company.date_format if emp_company else "MMM. D, YYYY"
# )
# else:
# date_format = "MMM. D, YYYY"
# # Convert the string to a datetime.date object
# start_date = datetime.strptime(str(value), "%Y-%m-%d").date()
# # Print the formatted date for each format
# for format_name, format_string in HORILLA_DATE_FORMATS.items():
# if format_name == date_format:
# data = start_date.strftime(format_string)
# else:
# data = str(value) if value is not None else ""
# payslips_data[column_name].append(data)
# data_frame = pd.DataFrame(data=payslips_data)
# response = HttpResponse(
# content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
# )
# response["Content-Disposition"] = f'attachment; filename="{file_name}"'
# writer = pd.ExcelWriter(response, engine="xlsxwriter")
# data_frame.style.map(lambda x: "text-align: center").to_excel(
# writer, index=False, sheet_name="Sheet1"
# )
# worksheet = writer.sheets["Sheet1"]
# worksheet.set_column("A:Z", 20)
# writer.close()
# return response
@login_required
def get_template(request, emp_id):
"""

View File

@@ -0,0 +1,166 @@
{% load i18n %} {% load horillafilters %}
<div class="oh-profile-section ">
{% if employee %}
<div class="oh-timeoff-modal__profile-content">
<div class="oh-profile mb-2">
<div class="oh-profile__avatar">
<img src="{{employee.get_avatar}}"
class="oh-profile__image me-2">
</div>
<div class="oh-timeoff-modal__profile-info">
<span class="oh-timeoff-modal__user fw-bold">{{employee.get_full_name}}</span>
<span class="oh-timeoff-modal__user m-0" style="font-size: 18px; color: #4d4a4a">
{{employee.get_job_position}} /
{{employee.get_department}}</span>
</div>
</div>
</div>
{% endif %}
{% if "attendance"|app_installed %}
{% comment %} -----ATTENDANCE RECORDS----- {% endcomment %}
<div id="ack-message-{{employee.id}}"></div>
<form action="{%url 'attendance-info-export' %}"
method="get"
onsubmit="event.stopPropagation();$(this).parents().find('.oh-modal--show').last().toggleClass('oh-modal--show');"
id="attendanceExportForm"
class="pt-2">
{% csrf_token %}
<div class="modal-body">
<h6 class="oh-timeoff-modal__user fw-bold">
<span class="material-symbols-outlined"style="font-size: 14px;">radio_button_checked</span>
{% trans "Attendance Records" %}</h6>
<div class="row d-none">
{% for field in attendance_excel_column.selected_fields %}
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="oh-input-group">
<label class="oh-label"> {{ field }} </label>
</div>
</div>
{% endfor %}
</div>
<div class="oh-input-group">
<input type="text" name="employee_id" value="{{ employee.id }}" readonly hidden>
</div>
<div class="form-group mt-2 d-flex">
<label for="start_date" class="me-2 mt-3">
<h6>{% trans "From" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="attendance_date__gte" value="{{ attendance_export.form.attendance_date__gte.value }}" required>
<label for="end_date" class="me-2 mt-3 ms-4">
<h6>{% trans "To" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="attendance_date__lte" value="{{ attendance_export.form.attendance_date__lte.value }}" required>
<div class="mt-1">
<button class="ms-4 d-flex" style="color:white; background-color:hsl(8,77%,56%);border:none;height:40px;">
<ion-icon name="download-outline" style="font-size:17px;" class='me-1 mt-2'></ion-icon>
<p style="margin-top:10px;">{% trans "Export Data" %}</p>
</button>
</div>
</div>
</form>
{% endif %}
{% if "leave"|app_installed %}
{% comment %} -----LEAVE RECORDS----- {% endcomment %}
<div id="ack-message-{{employee.id}}"></div>
<form action="{%url 'leave-requests-info-export' %}"
method="get"
onsubmit="event.stopPropagation();$(this).parents().find('.oh-modal--show').last().toggleClass('oh-modal--show');"
class="mt-4">
{% csrf_token %}
<div class="modal-body">
<h6 class="oh-timeoff-modal__user fw-bold">
<span class="material-symbols-outlined"style="font-size: 14px;">radio_button_checked</span>
{% trans "Leave Records" %}</h6>
<div class="row d-none">
{% for field in leave_excel_column.selected_fields %}
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="oh-input-group">
<label class="oh-label"> {{ field|capfirst }} </label>
</div>
</div>
{% endfor %}
</div>
<div class="oh-input-group">
<input type="text" name="employee_id" value="{{ employee.id }}" readonly hidden>
</div>
<div class="form-group mt-2 d-flex">
<label for="start_date" class="me-2 mt-3">
<h6>{% trans "From" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="from_date" value="{{ leave_export.form.from_date.value }}" required>
<label for="end_date" class="me-2 mt-3 ms-4">
<h6>{% trans "To" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="to_date" value="{{ leave_export.form.to_date.value }}" required>
<div class="mt-1">
<button class="ms-4 d-flex" style="color:white; background-color:hsl(8,77%,56%);border:none;height:40px;">
<ion-icon name="download-outline" style="font-size:17px;" class='me-1 mt-2'></ion-icon>
<p style="margin-top:10px;">{% trans "Export Data" %}</p>
</button>
</div>
</div>
</form>
{% endif %}
{% if "payroll"|app_installed %}
{% comment %} -----PAYROLL RECORDS----- {% endcomment %}
<div id="ack-message-{{employee.id}}"></div>
<form action="{%url 'payslip-info-export' %}"
method="get"
onsubmit="event.stopPropagation();$(this).parents().find('.oh-modal--show').last().toggleClass('oh-modal--show');"
class="mt-4">
{% csrf_token %}
<div class="modal-body">
<h6 class="oh-timeoff-modal__user fw-bold">
<span class="material-symbols-outlined"style="font-size: 14px;">radio_button_checked</span>
{% trans "Payroll Records" %}</h6>
<div class="row d-none">
{% for field in payroll_export_column.selected_fields %}
<div class="col-sm-4 col-md-4 col-lg-4">
<div class="oh-input-group">
<label class="oh-label"> {{ field|capfirst }} </label>
</div>
</div>
{% endfor %}
</div>
<div class="oh-input-group">
<input type="text" name="employee_id" value="{{ employee.id }}" readonly hidden>
</div>
<div class="form-group mt-2 d-flex">
<label for="start_date" class="me-2 mt-3">
<h6>{% trans "From" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="start_date_from" value="{{ payroll_export.form.start_date_from.value }}" required>
<label for="end_date" class="me-2 mt-3 ms-4">
<h6>{% trans "To" %}</h6>
</label>
<input type="date" class="oh-input w-25" name="end_date_till" value="{{ payroll_export.form.end_date_till.value }}" required>
<div class="mt-1">
<button class="ms-4 d-flex" style="color:white; background-color:hsl(8,77%,56%);border:none;height:40px;">
<ion-icon name="download-outline" style="font-size:17px;" class='me-1 mt-2'></ion-icon>
<p style="margin-top:10px;">{% trans "Export Data" %}</p>
</button>
</div>
</div>
</form>
{% endif %}
</div>

View File

@@ -1,5 +1,5 @@
{% include 'filter_tags.html' %}
{% load basefilters %}
{% load basefilters %} {% load horillafilters %}
{% load static %} {% load i18n %} {% if messages %}
<div class="oh-alert-container">
{% for message in messages %}
@@ -224,6 +224,21 @@
>
<div class="oh-btn-group">
{% if "leave"|app_installed or "payroll"|app_installed or "attendance"|app_installed %}
<button
type="button"
hx-get="{% url 'export-data-employee' emp.id %}"
title="{% trans "Export Individual Data" %}"
hx-target="#export-content"
hx-swap="innerHTML"
class="oh-btn oh-btn--light-bkg w-100"
data-toggle="oh-modal-toggle"
data-target="#dataExport"
>
<ion-icon name="download-outline"></ion-icon>
</button>
{% endif %}
<button
type="button"
hx-get="{% url 'send-mail-employee' emp.id %}"
@@ -378,6 +393,16 @@
</div>
</div>
<div class="oh-modal" id="dataExport" role="dialog" aria-labelledby="dataExport" aria-hidden="true">
<div class="oh-modal__dialog">
<div class="oh-modal__dialog-header">
<span class="oh-modal__dialog-title" id="dataExport"><h5>{% trans 'Export Data' %}</h5></span>
<button class="oh-modal__close" aria-label="Close"><ion-icon name="close-outline"></ion-icon></button>
</div>
<div class="oh-modal__dialog-body" id="export-content"></div>
</div>
</div>
<script src="{% static 'employee/actions.js' %}"></script>
<script>

View File

@@ -234,6 +234,11 @@ urlpatterns = [
not_in_out_dashboard.send_mail,
name="send-mail-employee",
),
path(
"export-data-employee/<int:emp_id>/",
not_in_out_dashboard.employee_data_export,
name="export-data-employee",
),
path(
"employee-bulk-mail", not_in_out_dashboard.send_mail, name="employee-bulk-mail"
),