403Webshell
Server IP : 65.108.144.40  /  Your IP : 216.73.217.165
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64
User : dev ( 1000)
PHP Version : 8.2.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/erp/employee/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/erp/employee/__pycache__/views.cpython-310.pyc
o

hͦh���	@s dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	mZm
Z
ddlmZddl
ZddlmZddlmZddlmZddlmZdd	lmZdd
lmZmZddlmZmZddlm Z dd
l!m"Z"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+m,Z,ddl-m.Z.ddl/m0Z0ddl1m2Z3ddl1m4Z5ddl6m7Z7ddl8m9Z9ddl:m;Z;ddl<m=Z=ddl>m?Z?ddl@mAZAddlBmCZCmDZDmEZEmFZFmGZGmHZHddlImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVddlWmXZXmYZYmZZZddl[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhddlimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZuddlvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~dd lm�Z�m�Z�m�Z�m�Z�m�Z�m�Z�dd!l�m�Z�dd"l�m�Z�dd#l�m�Z�dd$l�m�Z�dd%l�m�Z�m�Z�dd&l�m�Z�m�Z�m�Z�m�Z�dd'l�m�Z�m�Z�dd(l�m�Z�d)d*�Z�ej�ej�ej�ej�ej�ej�ej�e�d+�Z�d,d-d.�id,d/d.�id,d0d.�id,d1d.�id2�Z�d3d4�Z�e�d5d6��Z�e�d7d8��Z�e�e9d9d:d;�d<d=���Z�d>d?�Z�e�e9d@dAe�dB�dCdD���Z�e�e�dEdF���Z�e�e�e�dGex�dHdI����Z�e�e�dJ�dKdL���Z�e�e�e�dJ�dMdN����Z�e�e�e�dO�dPdQ����Z�e�e�e�dR�dSdT����Z�e�e�e�dUex�dVdW����Z�e�e�e�dXex�dYdZ����Z�e�d[d\��Z�e�e�d]d^���Z�e�e�d_d`���Z�e�e�dadb���Z�dcdd�Z�e�e�e�dX�dedf����Z�e�e�e�dX�dgdh����Z�e�e�dX�didj���Z�e�e�dX�dkdl���Z�e�e7dmg�dndo���Z�e�e�dp�dqdr���Z�e�e�ds�e7dmg�dtdu����Z�dvdw�Z�e�e9dxdAe�dB�dydx���Z�e�e�d:�dzd{���Z�e�e�d:�d|d}���Z�e�e�d:�d~d���Z�e�e�d��d�d����Z�e�e�d:�d�d����Z�e�e7dmg�e�d:�d�d�����Z�e�e7dmg�d�d����Z�e�e7d�g�e�d:�d�d�����Z�e�e7d�g�d�d����Z�e�e�d:�e7dmg��dd�d�����Z�e�e�d��e7dmg��dd�d�����Z�e�e�d��e7dmg��dd�d�����Z�e�e�e9dxdAe�dB�d�d�����Z�e�e�dA�e�d�d�����Z�e�e�dA�e�d�d�����Z�e�e�e�dA�d�d�����Z�e�e�d��e7dmg�d�d�����Z�e�e�d��d�d����Z�e�e�d��e7dmg�d�d�����Z�e�e�e�d��d�d�����Z�e�e�d:�d�d����Z�e�e�dA�d�d����Z�e�e9dxdAe�dB�d�d����Z�e�e�d��e7dmg�d�d�����Z�e�e�d��e7dmg�d�d�����Z�e�e�d��e7dmg�d�d�����Z�e�e�d��e7dmg�d�d�����Z�e�e�d��e7dmd�g�d�d�����Z�e�e�d��d�d����Z�e�e�d��d�d����Z�d�d��Zse�e�d��d�d����Z�e�e�dA�d�dÄ��Z�d�dńZ�e�d�dDŽ�Z�e�e�dA�d�dɄ��Z�e�d�d˄�Z�e�d�d̈́�Z�e�d�dτ�Z�e�d�dф�Z�e�d�dӄ�Z�e�d�dՄ�Z�e�d�dׄ�Z�e�d�dل�Z�e�e�dA�d�dۄ��Z�e�e�e�d�dݍd�d߄���Z�e�e�e�d�dݍ�dd�d����Z�e�e�d�dݍd�d���Z�e�e�d�dݍd�d���Z�e�e�e�d�dݍd�d����Z�e�e�e�d�dݍd�d����Z�e�e�e�d�ex�d�d����Z�e�e�d�dݍd�d���Z�e�e�d�ex�d�d����Z�e�d�d���Z�e�e�d��d�d����Z�e�e�d��d�d����Z�e�e�dA�d��d���Z�e�e�e�dA��d�d����Z�e��d�d��Z�e��d�d��Z�e�e��d��d�d	���Z�e�e�e��d
��d�d����Z�e�e�e��d
��d
�d����Z�dS(a�
views.py

This module contains the view functions for handling HTTP requests and rendering
responses in your application.

Each view function corresponds to a specific URL route and performs the necessary
actions to handle the request, process data, and generate a response.

This module is part of the recruitment project and is intended to
provide the main entry points for interacting with the application's functionality.
�N)�date�datetime�	timedelta)�parse_qs)�apps)�settings)�messages)�User)�cache)�models�transaction)�F�ProtectedError)�QuerySet)�	DateInput�Select)�HttpResponse�HttpResponseRedirect�JsonResponse�	QueryDict)�get_object_or_404�redirect�render)�reverse)�timezone)�gettext)�gettext_lazy)�require_http_methods)�enter_if_accessible)�#update_employee_accessibility_cache)�ACCESSIBILITY_CACHE_USER_KEYS)�DefaultAccessibility)�	ModelForm)�choosesubordinates�filtersubordinates�filtersubordinatesemployeemodel�get_key_instances�get_pagination�sortby)
�Company�
Department�EmailLog�
EmployeeShift�EmployeeType�JobPosition�JobRole�RotatingShiftAssign�RotatingWorkTypeAssign�ShiftRequest�WorkType�WorkTypeRequest�clear_messages)�DocumentRequestFilter�EmployeeFilter�EmployeeReGroup)
�BonusPointAddForm�BonusPointRedeemForm�BulkUpdateFieldForm�EmployeeBankDetailsForm�EmployeeBankDetailsUpdateForm�EmployeeExportExcelForm�EmployeeForm� EmployeeGeneralSettingPrefixForm�EmployeeNoteForm�EmployeeTagForm�EmployeeWorkInformationForm�!EmployeeWorkInformationUpdateForm�
excel_columns)�bulk_create_department_import�bulk_create_employee_import�bulk_create_employee_types�bulk_create_job_position_import�bulk_create_job_role_import�bulk_create_shifts�bulk_create_user_import�bulk_create_work_info_import�bulk_create_work_types�convert_nan�get_ordered_badge_ids�set_initial_password)�
BonusPoint�Employee�EmployeeBankDetails�EmployeeGeneralSetting�EmployeeNote�EmployeeTag�EmployeeWorkInformation�	NoteFiles)�hx_request_required�logger�login_required�manager_can_enter�owner_can_enter�permission_required)�HorillaPaginator)�group_by_queryset)�HORILLA_DATE_FORMATS)�get_horilla_model_class)�AccountBlockUnblock�HistoryTrackingFields)�DocumentForm�DocumentRejectForm�DocumentRequestForm�DocumentUpdateForm)�Document�DocumentRequest)�notifycCsdS�N�)�a�brnrn�/var/www/erp/employee/views.py�return_none�srr)�equal�notequal�lt�gt�le�ge�	icontains�range�filtercC�||jjdd�S)NT)�employee_id�work_type_id__id�attendance_validated)�work_type_id�id��employee�	allowancernrnrq�<lambda>���r�cCr|)NT)r}�shift_id__idr)�shift_idr�r�rnrnrqr��r�cCs|ddd�S)NT)r}�attendance_overtime_approverrnr�rnrnrqr��s�cCs
|dd�S)NT)r}rrnr�rnrnrqr��s�)r�r��overtime�
attendancecCs|jj}|j��Srm)�user�employee_get�reporting_manager�exists)�requestr�rnrnrq�_check_reporting_manager�s
r�cCs|j}td|i�S)z�
    Retrieve the language code for the current request.

    This view function extracts the LANGUAGE_CODE from the request object and
    returns it as a JSON response. This function requires the user to be logged in.
    �
language_code)�
LANGUAGE_CODEr)r�r�rnrnrq�get_language_code�sr�cCs,|jj}t��}t��}t|d|||d��S)z>
    This method is used to view own profile of employee.
    z"employee/profile/profile_view.html)r��current_date�now)r�r�r�todayrr�r)r�r�r�r�rnrnrq�employee_profile�s��r��profile_edit�employee.change_employee)�feature�permcCs>|j}tjj|d���}|j}ttjj|d���d�}ttjj|d���d�}|j	r�|j	�
d�duretjj|jd���}t|j	|d�}|��rd|jdd�}||_
|jdurX||_|��t�|td��n1|j	�
d	�dur�tjj|d���}t|j	|d�}|��r�|jdd�}||_|��t�|td
��t|d||d��S)
zC
    This method is used to update own profile of an employee.
    ��employee_user_id�r}��instance�employee_first_nameNF��commit�Profile updated.�any_other_code1zBank details updated.zemployee/profile/profile.html)�form�	bank_form)r�rS�objectsr{�first�badge_idr<rTr?�POST�get�is_valid�saver�r�success�_r}r)r�r�r�r�r�r�r�rnrnrq�self_info_update�sD	�
���r�cCs�|j�dd�}tjj|d���}|rMtjj|d�}|��}rM||j��vr-|j�	|�n|j�
|�t�|��jj
d�}|rMt�|d�t|d|�t|j�dd��S)Nr�)r��r�����HTTP_REFERER�/)�GETr�r!r�r{r�rS�	employees�all�remove�addr r�r�r
�deleterr�META)r��emp_idr��
accessibilityr�r��user_cache_keyrnrnrq�profile_edit_accesss�r��employee_detailed_view�employee.view_employee)r�r��methodcKsDtjj|d�}t�d�r|j��nd}tj��otj�	�j
}|j�dg�}tjj|d�}t
�dd�t||j�d��jD��}t|t�rHt�|�n|}	|j}
d}d}t|	�D](\}
}||
kr}|
t|	�d	krhd}n|	|
d	}|
d
krud}n|	|
d	}nqU||||	t��t
�g�|d�}|jj|kr�||d<n||d
<t|d|�S)�=
    This method is used to view profile of an employee.
    r��leaveN�filtered_employees��id__incS�g|]}|j�qSrnr���.0r�rnrnrq�
<listcomp>1s��z,employee_view_individual.<locals>.<listcomp>�page�r)r��previous�next�requests_idsr��leave_request_ids�enabled_block_unblock�user_leaves�employee_leaveszemployee/view/individual.html)rSr�r�r�is_installed�available_leaver�rdr�r��
is_enabled�sessionr{�json�dumps�
paginator_qryr��object_list�
isinstance�str�ast�literal_evalr��	enumerate�lenrr�r�r�r)r��obj_id�kwargsr�r�r��filtered_employee_idsr��request_ids_strr�r}�previous_id�next_id�index�req_id�contextrnrnrq�employee_view_individualsb
�

�������
�

�r�cKsRtjj|d�}t�d�r|j��nd}t�d�r|j��nd}t|d|||d��S)r�r��payrollNr�ztabs/personal_tab.html)r�r��	contracts)	rSr�r�rr��contract_setr�r�r)r�r�r�r�r�r�rnrnrq�	about_tabgs���r�zperms.employee.view_employeecCs�tjj|d�}tjj|d�}t�dd�|D��}tjj|d�}t�dd�|D��}tjj|d�}t�dd�|D��}t	jj|d�}	t�dd�|	D��}
|||||||	|
||d�
}t
|d	|d
�S)z�
    This function is used to view shift tab of an employee in employee individual & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return shift-tab template
    r�r�cSr�rnr�r�rnrnrqr���zshift_tab.<locals>.<listcomp>cSr�rnr�r�rnrnrqr��r�cSr�rnr�r�rnrnrqr��r�cSr�rnr�r�rnrnrqr��r�)
�	work_data�work_type_requests_ids�
rshift_assign�rshift_assign_ids�rwork_type_assign�rwork_type_assign_ids�
shift_data�shift_requests_idsr�r�ztabs/shift-tab.html�r�)rSr�r�r4r{r�r�r0r1r2r)r�r�r��work_type_requestsr�r�r�r�r��shift_requestsr�r�rnrnrq�	shift_tab}s.
��r��'horilla_documents.view_documentrequestscCs||j��}t�}tj��}tjjdd�}t|d|d�}t	|d|j�
d�d�}t|�}tt|�|||||d�}t
|d|d	�S)
z�
    This function is used to view documents requests of employees.

    Parameters:
    request (HttpRequest): The HTTP request object.

    Returns: return document_request template
    F��document_request_id__isnullr)r�r��queryset�document_request_idr�)�document_requests�	documents�f�pd�filter_dictz documents/document_requests.htmlr�)r��	urlencoder6rkr�r�rjr{r$rar�rr&r)r��
previous_data�filter_classrr�	data_dictr�rnrnrq�document_request_view�s*

��
�rc	Csxtj��}|j��}t|j�j}|jdd��d�}t	|d|j�
d�d�}t|�}tt
|�t|d|t|j�|||d��S)�1
    This method is used to filter employee.
    Trz-document_request_idrr�zdocuments/requests.html)rrrr	r)rkr�r�r�r
r6�qs�exclude�order_byrar�rr&rjrr7)r�rrrr
rnrnrq�document_filter_view�s*

��
��rz&horilla_documents.add_documentrequestscCs�t�}t||d�}|jdkrct|j�}t||d�}|��rc|��}t�|td��dd�|j	�
�D�}tj|j
j||j
j�d�d|j
j�d�|j
j�d	�|j
j�d
�|j
j�d�td�d
d�	td�Sd|i}t|d|d�S)z�
    This function is used to create document requests of an employee in employee requests view.

    Parameters:
    request (HttpRequest): The HTTP request object.

    Returns: return document_request_create_form template
    z%horilla_documents.add_documentrequestr�z%Document request created successfullycSr�rnr�)r�r�rnrnrqr��r�z+document_request_create.<locals>.<listcomp>z requested a document.uطلب u مستنداً.z hat ein Dokument angefordert.u solicitó un documento.u a demandé un document.�employee-profile�chatbox-ellipses��	recipient�verb�verb_ar�verb_de�verb_es�verb_frr�icon�*<script>window.location.reload();</script>r��+documents/document_request_create_form.htmlr�)rhr#r�r�r�r�rr�r�r}r�rl�sendr�r�rrr)r�r�r�r�rnrnrq�document_request_create�s8

����r!z)horilla_documents.change_documentrequestscCs�tt|d�}tjj|jd�}t|d�}|jdkrEt|j|d�}|�	�rE|�
�}|j�t
jj|j�d�d��|j|j��d���td�S||d	�}t|d
|d�S)z�
    This function is used to update document requests of an employee in employee requests view.

    Parameters:
    request (HttpRequest): The HTTP request object.

    Returns: return document_request_create_form template
    r�)rr�r�r}r���employee_id__inr)r��document_requestrr�)rrkrjr�r{r�rhr�r�r�r�r}�setrS�data�getlistrr�r�rr)r�r�r$rr��doc_objr�rnrnrq�document_request_updates$

���r)zhorilla_documents.view_documentcCs6t|j|j�}tjj|d�}|||d�}t|d|d�S)a	
    This function is used to view documents tab of an employee in employee individual
    & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return document_tab template
    r�)rr�r�ztabs/document_tab.htmlr�)rir��FILESrjr�r{r)r�r�r�rr�rnrnrq�document_tab8s�r+zhorilla_documents.add_documentcCsvtjj|d�}t|dd�d�}|jdkr/t|j|j�}|��r/|��t	�
|td��td�S||d�}t
|d	|d
�S)z�
    This function is used to create documents from employee individual & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee

    Returns: return document_tab template
    r�N)r}�expiry_date��initialr�zDocument created successfully.r�r�r�z#tabs/htmx/document_create_form.htmlr�)rSr�r�rfr�r�r*r�r�rr�r�rr)r�r�r}r�r�rnrnrq�document_createRs

�r0cCs`tt|d�}|j�d�}|jdkr$||_|��t�|t	d��td�St�
|t	d��td�S)z�
    This function is used to create documents from employee individual & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.

    Returns: return document_tab template
    r��titler�z#Document title updated successfullyzInvalid request�)rrjr�r�r�r1r�rr�r��errorr)r�r��document�namernrnrq�update_document_titleos

�r6c	Cszwtjj|d�}|j�d�s|j|jd�jdd�}|rk|��}|��t�	|t
d|�d|j�d���|j�
d	d
�}dd�|�d�dd
��}|�d�sR|�d�rgtjj|jd�}|sgtd|jj�d��WSt�WSt�|t
d��Wtd�Sty�t�|t
d��Ytd�Sw)a�
    Handle the deletion of a document, with permissions and error handling.

    This view function attempts to delete a document specified by its ID.
    If the user does not have the "delete_document" permission, it restricts
    deletion to documents owned by the user. It provides appropriate success
    or error messages based on the outcome. If the document is protected and
    cannot be deleted, it handles the exception and informs the user.
    r�z!horilla_documents.delete_document)�employee_id__employee_user_idFrzDocument request z for z deleted successfullyr�r2r��Nz/employee/employee-view/z/employee/employee-profile/r�zB
                            <span hx-get='/employee/document-tab/z�?employee_view=true'
                            hx-target='#document_target' hx-trigger='load'></span>
                        zDocument not foundz You cannot delete this document.r)rjr�r{r��has_permrr�r�rr�r�r}r�r��join�split�
startswith�endswithrr�r3r)r�r�r4�document_first�referrer�existing_documentsrnrnrq�document_delete�sL����������rAcCs�tjj|d�}t|d�}|jdkrpt|j|j|d�}|��rp|��t	�
|td��z=tj
|jj|jj��j|jj�d�d|jj�d�|jj�d�|jj�d	�|jj�d
�tdd|jjjid
�dd�	Wtd�SYtd�S||d�}t|d|d�S)a
    This function is used to upload documents of an employee in employee individual & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    id (int): The id of the document.

    Returns: return document_form template
    r�r�r�zDocument uploaded successfullyz uploaded a documentuقام u بتحميل مستندz hat ein Dokument hochgeladenu subió un documentou a téléchargé un documentzemployee-view-individualr�)r�rrr)r�r4ztabs/htmx/document_form.htmlr�)rjr�r�rir�r�r*r�r�rr�r�rlr r�r��get_reporting_managerr�rr�rr)r�r��
document_itemr�r�rnrnrq�file_upload�s8

���
rDc	Cs�tjj|d���}d|i}|jrT|jj}tj�|�ddd���}t	|�}zt
|d��}|��}Wd�n1s;wYWnd}Y||d<||d<||d<t|d	|�S)
z�
    This function used to view the uploaded document in the modal.
    Parameters:

    request (HttpRequest): The HTTP request object.
    id (int): The id of the document.

    Returns: return view_file template
    r�r4r�N�rb�file_content�file_extension�content_typeztabs/htmx/view_file.html)
rjr�r{r�r4�path�os�splitext�lower�get_content_type�open�readr)	r�r��document_objr��	file_pathrGrH�filerFrnrnrq�	view_file�s,
���
��rScCs dddddddd�}|�|d�S)	z}
    This function retuns the content type of a file
    parameters:

    file_extension: The file extension of the file
    zapplication/pdfz
text/plainzGapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentzAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetz
image/jpegz	image/png)�pdf�txt�docx�xlsx�jpg�png�jpegzapplication/octet-stream)r�)rG�
content_typesrnrnrqrMs	�rMcCsPtt|d�}|jrd|_|��t�|td��t	d�St�|td��t	d�S)z�
    This function used to view the approve uploaded document.
    Parameters:

    request (HttpRequest): The HTTP request object.
    id (int): The id of the document.

    Returns:
    r��approvedzDocument request approved�No document uploadedr)
rrjr4�statusr�rr�r�r3r)r�r�rPrnrnrq�document_approves�r_cCs�tt|d�}t�}|jr4|jdkr3t|j|d�}|��r3|��}d|_|��t	�
|td��td�Snt	�
|td��td�St
|d||d	��S)
z�
    This function used to view the reject uploaded document.
    Parameters:

    request (HttpRequest): The HTTP request object.
    id (int): The id of the document.

    Returns:
    r�r�r��rejected�Document request rejectedrr]ztabs/htmx/reject_form.html)r�rP)rrjrgr4r�r�r�r�r^rr3r�rr)r�r�rPr��testrnrnrq�document_reject6s&

��rccCsP|j�d�}tjj|d�jdd�}|jdd�t�|t	t
|��d���td�S)	z�
    This function used to view the approve uploaded document.
    Parameters:

    request (HttpRequest): The HTTP request object.

    Returns:
    �idsr�r2)r4r\)r^z Document request approvedr�)r�r'rjr�r{r�updaterr�r�r�r)r�rdrPrnrnrq�document_bulk_approveZs��rfcCsL|j�d�}|j�d�}tjj|d�}|jd|d�t�|t	d��t
d�S)z�
    This function used to view the reject uploaded document.
    Parameters:

    request (HttpRequest): The HTTP request object.

    Returns:
    rd�reasonr�r`)r^�
reject_reasonrar�)r�r'r�rjr�r{rerr�r�r)r�rdrgrPrnrnrq�document_bulk_rejectosrir�cCsj|jj}tjj|d���}t|j|d�}|��r-|j	dd�}||_
|�	�t�|t
d��t|j�d��S)z7
    This method is used to fill self bank details
    r�r�Fr�zBank details updatedr�)r�r�rTr�r{r�r=r�r�r�r}rr�r�rr�r�)r�r�r�r��	bank_infornrnrq�employee_profile_bank_details�srkzemployee.view_profilecCsb|j}tjj|d�}|�d�r-|jdkr-t|j|j|d�}|�	�r-|�
�t�|t
d��td�S)zJ
    This method is used update own profile of the requested employee
    r�zemployee.change_profiler�r�r�z/employee/employee-profile)r�rSr�r�r9r�r?r�r*r�r�rr�r�r)r��
employee_userr�r�rnrnrq�employee_profile_update�s

rm�delete_groupcCs tjj|d�}|j��td�S)z9
    This method is used to delete user group assign
    r�z)/employee/employee-user-group-assign-view)r	r�r��groups�clearr)r�r�r�rnrnrq�!employee_user_group_assign_delete�s
rqcCst|t��}|�|�}|S)z3
    This method is used to paginate query set
    )r`r'�get_page)�qryset�page_number�	paginatorrnrnrqr��s
r��
employee_viewc
Cs�|j�d�}|j��}|j�d�}|j�d�}|j�dd�}|dkr*tjjd|d�ntjjdd	�}t|j|d
�}t	�}t
|�}	tt|	�tj��}
dd�|D�|jd
<t|dt
|j|�|||||	|
tj|d�	�S)zF
    This method is used to render template for view all employee
    �viewr��selected_company�
error_messageNr�T)�	is_active�employee_work_info__company_id�rz�rcSr�rnr��r�r�rnrnrqr��r�z!employee_view.<locals>.<listcomp>r�z)employee_personal_info/employee_view.html)	r&rr�update_fields_form�	view_typer	�emp�	gp_fieldsry)r�r�r
r��poprSr�r{r7r;rr&rr�rr8�fields)r�r�rrtrxryr�
filter_obj�
update_fieldsr
r�rnrnrqrv�s>

���


��cs|jdkr�|j�d��|j�d�}|rt�|�nd}|r�G��fdd�dt��G��fdd�dt��G��fd	d
�d
t����}��}��}|j��}t	|�}g}|D]}	d|	}	|�
|	�qS|D]}	|	�vrl��
|	�qat���}
||||
|d�}t|d
|d�St�
|td��tt�SdS)Nr�r��bulk_employee_idsr2c�0eZdZG�fdd�d�Z��fdd�Z�ZS)z9view_employee_bulk_update.<locals>.EmployeeBulkUpdateFormcs�eZdZeZgZiZiZ�D]KZzDej	�
e�Zejdvr=d�vr.e�
d�eddid�ed<e�
e�eddid�ee<ne�
e�eeej�rSedddd�d�ee<WqYqd	S)
z>view_employee_bulk_update.<locals>.EmployeeBulkUpdateForm.Meta��country�stater��requiredT��attrsrF)�typer�zdata-ppN)�__name__�
__module__�__qualname__rS�modelr��widgets�labels�field�_meta�	get_field�	field_objr5�appendrr�r�	DateFieldrrn�r�rnrq�Meta�s8


�

�
���r�c�4t�|�j|i|��|j��D]\}}d|_qdS�NT��super�__init__r��itemsr���self�argsr��
field_namer�)�EmployeeBulkUpdateForm�	__class__rnrqr���zBview_employee_bulk_update.<locals>.EmployeeBulkUpdateForm.__init__�r�r�r�r�r��
__classcell__rn)r�r��r�rqr��sr�cr�)z9view_employee_bulk_update.<locals>.WorkInfoBulkUpdateFormcs�eZdZeZgZiZiZ�D]�Zz�e	e��
d�Zedr�eddkr�ej�
ed�Zeddks<eddks<eddkr�d	�vrNe�d�ed
did�ed<d
�vr`e�d�ed
did�ed<d�vrre�d�ed
did�ed<e�ed�ed
did�ee<e�ed�dd�eD�Zeeej�r�eddid�eed<eddvr�ejdkr�ed�ned�eed<WqYqdS)z>view_employee_bulk_update.<locals>.WorkInfoBulkUpdateForm.Meta�__r�r�employee_work_infor��
department_id�job_position_id�job_role_id�!employee_work_info__department_idr�Tr��#employee_work_info__job_position_id�employee_work_info__job_role_idcSsg|]	}t|t�s|�qSrn)r��list�r��itemrnrnrqr�Ns��zIview_employee_bulk_update.<locals>.WorkInfoBulkUpdateForm.Meta.<listcomp>r�r)�email�mobiler�z
Work Emailz
Work PhoneN)r�r�r�rXr�r�r�r�r�r�r;�partsr�r�r�r�rr�rr�rr5r�rnr�rnrqr�st��

��

��

�
���
�
���r�csxt�|�j|i|��d|jvr|jdjj�ddi�d|jvr-|jdjj�ddi�|j��D]\}}d|_q2dS)Nr��onchangezdepChange($(this))r�zjobChange($(this))T)r�r�r��widgetr�rer�r�r�)�WorkInfoBulkUpdateFormr�rnrqr�as
��
���zBview_employee_bulk_update.<locals>.WorkInfoBulkUpdateForm.__init__r�rn)r�r�r�rqr�sFr�cr�)z9view_employee_bulk_update.<locals>.BankInfoBulkUpdateFormcs
eZdZeZgZiZiZ�D]tZzme	e��
d�Zedr|eddkr|ej�
ed�Ze�ed�eeej�rBeddid�eed<ejdvr|d	�vrYe�d	�ed
did�ed	<e�ed�ed
did�eed<ejd	krted�ned
�eed<WqYqdS)z>view_employee_bulk_update.<locals>.BankInfoBulkUpdateForm.Metar�r�r�employee_bank_detailsr�rr�r�r�r�TzBank Countryz
Bank StateN)r�r�r�rTr�r�r�r�r�r�r;r�r�r�r�r�r�rr�rr5rr�rnr�rnrqr�ssH��


��
�
���r�cr�r�r�r�)�BankInfoBulkUpdateFormr�rnrqr��r�zBview_employee_bulk_update.<locals>.BankInfoBulkUpdateForm.__init__r�rn)r�r�r�rqr�rs&r��employee_work_info__)r��form1�form2r�r�z'employee_personal_info/bulk_update.htmlr�z0There are no employees selected for bulk update.)r�r�r'r�r�r�r"r��keysr�r�rr�warningr�rrv)r�r��bulk_employee_ids_strr�r�r�r��	keys_list�fields_list�i�update_fields_strr�rn)r�r�r�r�rq�view_employee_bulk_update�sP
�$X,

�
�����r�c
Cs�|jdkr�|j�dd�}|rt�|�ng}|jd}|j�dd�}|r(t�|�ng}t�|�}|D]/}ztj	jt
|�d�}tj	j|d�\}	}
t
j	j|d�\}}
Wq1ttfy`|�|�Yq1w|D]j}t|��d�}
|
d	r�|
d
dkr�tj	j|d�}|�|
d	�}|jdi|
d	|i��qc|
d
d
kr�|D]}t
j	j|d�}|�|
d	�}|jdi|
d	|i��q�qctj	j|d�}|�|�}|jdi||i��qct|�d
kr�t�|td�t|����td�S)Nr�r�r2�_postr�r�r�r�r�rr�r"r�r�z-{} employees information updated successfullyz"/employee/employee-view/?view=listrn)r�r�r�r��loads�__dict__r�r�rSr��intrX�
get_or_createrT�
ValueError�
OverflowErrorr�r�r;r{rer�rr�r��formatr)r�r�r��
dict_valuer�r��
employee_listr��employee_instancer��created�
employee_bankr�r��employee_queryset�valuernrnrq�save_employee_bulk_update�st

�
����
����
����r�cCs�tt|d�}|st�|td��tt�Stt|jj	d�}|s*t�|td��tt�S|j
sO|j|_|js9td�ntd�}|��t�
|td�j||d��nt�|td�j|d��tt|d	�S)
Nr�zEmployee not found�blocked�	unblockedz3{employee}'s account {action_message} successfully!)r��action_messagez0{employee} is a superuser and cannot be blocked.)r�)r�)rrSr�infor�rrvr	r�r��is_superuserrzr�r�r�r�)r�r�r�r�r�rnrnrq�employee_account_block_unblocks2
����r�zemployee.add_employeecCs8t�}t�}t�}ttj��d�}t|d||||d��S)zF
    This method is used to render form to create a new employee.
    r}�#employee/create_form/form_view.html)r��	work_formr�r)r?rCr<r7rSr�r�r)r�r�r�r�r�rnrnrq�employee_view_new"s�r�cKs�|jd}tjj|jd���}tj��}d}|r|jdkrd}tjj|d���}tj��}|j|d���}	|dur^|	}t	j��}
t
jj|d�}|
j|d���}|dkrZ||_|�
�|�
�|ri|jj|d���sp|j�d	��rNt|d
�}
tt	jj|d���d
�}ttjj|d���d
�}|j�rB|j�d�dkr�t|j|d
�}
|
��r�|
�
�t�|td
��n�|j�d�dk�rt	jj|d���}t|j|d
�}|���r|j
dd�}||_|�
�|j�|j�d��tj |jj!|jj"dddddt#d�dd�	t�|td��tt	jj|d���d
�}n3|j�d�dk�rBtjj|d���}t$|j|d
�}|���rB|j
dd�}||_|�
�t�|td��t%|d||
|||d��St&|j'�dd��S) zA
    This method is used to render update form for employee.
    rxr�FTr�Nr�r�r�r�r��personalz&Employee personal information updated.�workr��tagsz#Your work details has been updated.u&تم تحديث تفاصيل عملك.z(Ihre Arbeitsdetails wurden aktualisiert.z.Se han actualizado los detalles de su trabajo.u:Vos informations professionnelles ont été mises à jour.r�	briefcaserz"Employee work information updated.�bankzEmployee bank details updated.z#employee/update_form/form_view.html)r�r�r�r��work_info_historyr�z/employee/employee-view)(r�rSr�r{r�r�re�work_info_track�get_allrXr)r��
company_idr�r�r�r9r?rCr<rTr�r�rr�r�rDr}r�r%r'rlr r�r�rr=rrr�)r�r�r��companyr��	work_infor�r��
all_employeesr��
all_work_info�cmpnyr�r�r�r�r�rnrnrq�employee_view_update3s�



��
�
��������
������
���r�cCs|ztjj|d�}|jd}||_|��t�|td��Wnt	y.t�
|td��Ynwt|d�}t|j
�d�d�S)z7
    This method is used to upload a profile image
    r�r��Profile image updated.zNo image chosen.�#employee/profile/profile_modal.html�utf-8�#<script>location.reload();</script>)rSr�r�r*r�r�rr�r��	Exceptionr3rr�content�decode)r�r�r��img�responsernrnrq�update_profile_image�s 
���r�cCsP|jj}|j�d�}||_|��t�|td��t	|d�}t
|j�d�d�S)zP
    This method is used to update own profile image from profile view form
    r�r�r�r�r�)
r�r�r*r�r�r�rr�r�rrr�r�)r�r�r�r�rnrnrq�update_own_profile_image�s��r��DELETEcCs�tjj|d�}|jjdkr$t�|td��t|d�}t	|j
�d�d�S|jj}t
j�tj|�}t
�|�d|_|��t�|td��t|d�}t	|j
�d�d�S)	zd
    This method is used to remove uploaded image
    Args: obj_id : Employee model instance id
    r�r2�No profile image to remove.r�r�r�N�Profile image removed.)rSr�r�r�r5rr�r�rrr�r�rIrJr:r�
MEDIA_ROOTr�r�r�)r�r�r�r�rQ�
absolute_pathrnrnrq�remove_profile_image�s.��
��rcCs�|jj}|jjdkr!t�|td��t|d�}t|j	�
d�d�S|jj}tj�
tj|�}t�|�d|_|��t�|td��t|d�}t|j	�
d�d�S)z9
    This method is used to remove own profile image
    r2rr�r�r�Nr)r�r�r�r5rr�r�rrr�r�rIrJr:rrr�r�r�)r�r�r�rQrrnrnrq�remove_own_profile_image�s.��
��rcs�tjj|d���}t|j|d�����rX���|durTt�	|t
d��t|j�jd��tt
jj|d���d�}ttjj|d���d�}td�jj�d��||d�d	�Std
�S|durdt|dd�i�Sd
��fdd��j��D��}td|�d��S)zA
    This method is used to update employee's personal info.
    r�r�NzNew Employee Added.r�zemployee-view-update/r��r�r�r��r&z�
                <div class="oh-alert-container">
                    <div class="oh-alert oh-alert--animated oh-alert--success">
                        Personal Info updated
                    </div>
                </div>

        r�r��
c�4g|]\}}d�j�||�j�dd�|��d��qS�z<li>z: z, z</li>�r�r��labelr:�r�r��errors�r�rnrqr�8�"��z8employee_create_update_personal_info.<locals>.<listcomp>�<ul class="alert alert-danger">�</ul>)rSr�r{r�r?r�r�r�rr�r�r�rCrXr<rTrr�rrr:rr�)r�r�r�r�r�rrnrrq�$employee_create_update_personal_info	sN����
��
��
��rz'employee.change_employeeworkinformationcs�tjj|d���}t|jtjj|d���d��d�jd_|�_	��
�r9|dur9�jdd�}||_	|��td�Sd	�
�fd
d��j��D��}td|�d
��S)z:
    This method is used to update employee work info
    r�r�r�Fr}Nr�z�

                <div class="oh-alert-container">
                    <div class="oh-alert oh-alert--animated oh-alert--success">
                        Personal Info updated
                    </div>
                </div>

        rcr	r
rr
rrnrqr�^rz-employee_update_work_info.<locals>.<listcomp>rr)rSr�r{r�rCr�rXr�r�r}r�r�rr:rr�)r�r�r�r�rrnrrq�employee_update_work_info@s(��
��rz#employee.change_employeebankdetailscs�tjj|d���}t|jtjj|d���d�����r0|dur0�jdd�}||_	|��t
d�Sd��fd	d
��j�
�D��}t
d|�d��S)
zS
    This method is used to render form to create employee's bank information.
    r�r�r�NFr�z�
            <div class="oh-alert-container">
                <div class="oh-alert oh-alert--animated oh-alert--success">
                    Bank details updated
                </div>
            </div>
        rcr	r
rr
rrnrqr��rz0employee_update_bank_details.<locals>.<listcomp>rr)rSr�r{r�r<r�rTr�r�r}rr:rr�)r�r�r�rjrrnrrq�employee_update_bank_detailsfs$��	
��rc
	Cs"|j��}|j�d�}tj��}|j�d�}t|j|d�j}|j�d�dkr,|jdd�}|j�d�d	kr>|d
kr>|j|d�}|j�d�}|j�d
�}t	|�}t
t|�d}	|dkr[d}	|dkrm|d	urmt|||d�}d}	nt||d�}t
||�}dd�|D�|jd<t||	|t|j�|||d��S)rr�rxr}rz�FalseTr|r{Nr�)r{r�rw�)employee_personal_info/employee_card.htmlr��)employee_personal_info/employee_list.htmlr2z$employee_personal_info/group_by.html�orderbycSr�rnr�r~rnrnrqr��r�z(employee_filter_view.<locals>.<listcomp>r�)r&rrr�r	)r�r
r�rSr�r{r�r7rrr&rar(r�r)
r�rr�rrxr�rtrwr
�templaternrnrq�employee_filter_view�sB



��rcCs�|j��}|j�d�}t|td��rd}t|tj��d�}|j�d�dur2t	|j|j
|dd�d�}nt	|j|j
|d	�d�}|j�d
�}t||jd�}t
|dt||�||d
��S)zB
    This method renders card template to view all employees.
    �searchNr2r�rzT��employee_first_name__icontainsrzr}�rr�rr�r&rr)r�r
r�r�r�r%rSr�r�r7r{r(rrr�)r�rrr�r�rtrnrnrq�
employee_card�s8
���
���r!cCs�|j��}|j�d�}t|td��rd}|j�d�dur*t|jtjj|dd�d�}nt|jtjj|d�d�}t	||j
d	�}t||d
�}|j�d�}t|dt
||�||d
��S)z<
    This method renders template to view all employees
    rNr2rzTrr}rr�rr�rr )r�r
r�r�r�r7rSr�r{r%rr(rr�)r�rrr�r�rtrnrnrqr��s8
������r�cCs�tjj|d�}t|d�}tjj|d���}tjj|d���}t�}t	�}|dur-t|d�}|dur6t	|d�}|j
dkrZ|j�d�rZt|j
|j|d�}|��rZ|��t�|td��t|d|||d	��S)
zl
    This method is used to update employee if the form is valid
    args:
        obj_id : employee id
    r�r�r�Nr�r�zEmployee updated.�0employee_personal_info/employee_update_form.htmlr)rSr�r�r?rXr{r�rTrCr=r�r�r9r�r*r�r�rr�r�r)r�r�r�r�r�rjr�r�rnrnrq�employee_updates*	




�r#zemployee.delete_employeec
CsTzM|j�d�}tjj|d�}t�d�r-|j����r-|j��}|D]}|j	dkr,|�
�q!|j}z|�
�Wn
tyC|�
�Ynwt
�|td��WnPtjy_t
�|td��Yn?ty�}z3t�}|jD]
}	|�t|	jj����qld�|�}
td�|
��}t|�}||jd	<tt�WYd
}~Sd
}~wwt |j!�dd|����S)
zT
    This method is used to delete employee
    args:
        id  : employee id
    rwr�r��activezEmployee deleted�Employee not found.z, - z- {}.ryNr�z/view=)"r�r�rSr�rr�r�r�r��contract_statusr�r��AttributeErrorrr�r��DoesNotExistr3rr%�protected_objectsr�r�r��verbose_name�
capitalizer:r�r�r�rrvrr�)r�r�rwr�r��contractr��e�model_verbose_names_set�obj�model_names_strryrnrnrq�employee_delete+s>



��


��	r1c
Cs�|jd}t�|�}|D]c}z;tjj|d�}t�d�r5|j�	��
�r5|j�	�}|D]}|jdkr4|��q)|j
}|��t�|td�d|i�Wqtjy[t�|td��Yqtyot�|td�d|i�Yqwtd	d
i�S)zA
    This method is used to delete set of Employee instances
    rdr�r�r$z%(employee)s deleted.r�r%zYou cannot delete %(employee)s.�message�Success)r�r�r�rSr�r�rr�r�r�r�r&r�r�rr�r�r(r3rr)r�rdr}r�r�r,r�rnrnrq�employee_bulk_deleteSs2




����r4c
Cs |jd}t�|�}d}|j�d�dkrd}|D]q}tjj|d�}tjj|d�}|jjrW|j	rWd}tjj
dd�}|D]
}|jjrD|d	}q:|d	krWt�|t
d
��td�S||_	||j_	|��dur~|��t
d�}	|rrt
d
�}	t�||�d|	���qt�|t
d��|��qtddi�S)zC
    This method is used to archive bulk of Employee instances
    rdFrz�TrueTr�rr|r��%You can't archive the last superuser.�.<script>$('#filterEmployee').click();</script>�archivedzun-archivedz is zRelated data found for {}.r2r3)r�r�r�r�r�rSr�r�r�rzr{rr3r�r�get_archive_conditionr�r�r�r�r)
r�rdrzr}r�r��countr��	super_empr2rnrnrq�employee_bulk_archivess8

�r<c	Cs tjj|d�}|j|_|j|j_d}d}|jsWtjj|d�}|jjrJd}tjjdd�}|D]
}|jjr9|d}q/|dkrJt�|t	d��t
d�S|��}	|	rSd	}nt	d
�}|ry|��t�
||�d}
|
|j��vrut|j�dd
��St
d�St|d||	�d�|	�d�|	�d�t	d�d��S)zn
    This method is used to archive employee instance
    Args:
            obj_id : Employee instance id
    r�T�Employee un-archivedrr|r�r6r7F�Employee archived�HTTP_HX_REQUESTr�r��related_models.html�related_models�related_model_fields�employee_choices�
Can't Archive�r�rArBrCr1)rSr�r�rzr�r�r{rr3r�rr9r�r�r�r�rr)r�r�r�r�r2r�r:r�r;�result�keyrnrnrq�employee_archive�sL	
���rHcCs�|j�d�}tjj|d���}|r|���dd�nd}|�r|�r|D]�}|�dd�}|�r|j�|�}tjj|d���}|dkrSt|�|krSt	jj|d�j
|d�}	q#t�d�r�|d	kr�t|�|kr�t
ddd
�}
|
jj|d�}|r�|D]}|j�|�|j�|�qsq#t�d�r�|dkr�t|�|kr�t
dd
d
�}
|
jj|d�}|r�|D]}|j�|�|j�|�q�q#t�d�r�|dkr�t|�|kr�t
ddd
�}|jj|d�}|r�|D]}|j�|�|j�|�q�q#t�d��r|dk�rt|�|k�rt
ddd
�}|jj|d�}|�r|D]}|j�|�|j�|��qq#	q#|��}|dk�r/t�|td��td�S|du�rK|dk�rKd|_|��t�|td��|��tt�S)Nr1r�rAr2r��reporting_manager_id)rI�recruitment�recruitment_managers��	app_labelr�)rK�recruitment_stage_managers�stage)�stage_managers�
onboarding�onboarding_stage_manager�onboardingstager��onboarding_task_manager�onboardingtask�Change the DesignationszDesignation changed.z!/offboarding/offboarding-pipelineFz{} archived successfully)r�r�rSr�r{r�r9r�r�rXrerr�rcrKr�r�rPr}rr�r�rrzr�r�rv)r�r�r1r�rArr��replace_emp_id�replace_empr��Recruitment�recruitment_queryrJ�Stage�recruitment_stage_queryrO�OnboardingStage�onboarding_stage_query�OnboardingTask�onboarding_task_query�taskrnrnrq�replace_employee�s�����������������
����
rbc		Cs�|j�d�}tjj|d���}|j�d�}|rtd�}ntd�}|j|_|j|j_d}d}|js?|�	�}|r;d}ntd	�}|ra|�
�t�||�d
}||j
��vr]t|j
�dd��Std
�St|d||�d�|�d�|�d�|d��S)zA
    This method is used to get the manager in records model
    r}r��offboardingrVrDTr=Fr>r?r�r��)<script>window.location.reload()</script>r@rArBrCrE)r�r�rSr�r{r�r�rzr�r9r�rr�r�r�rrr)	r�r}r�rcr1r�r2rFrGrnrnrq�get_manager_in.s@

��recCs�|jd}|jd}|j��}t|j�j}|dkr|jdd�}|j�d�}d}|dkr-d	}t||d
�}t||d�}t|�}t	t
|�t||t||�||d��S)
z0
    This method is used to search employee
    rrwr2Tr|r�rr�rr�r)r&rr	)
r�r
r7rr{r�r%r(rr&rSrr�)r�rrwrr�rtrr
rnrnrq�employee_search[s0



�
��rfz$employee.add_employeeworkinformationcCs�tjj|d�}t|d�}t|j�}t�}tjj|d��	�}|dur't|j
d�}|��r@|jdd�}||_
|��t�|td��t|d|||d	��S)
z�
    This method is used to create employee work information from employee single view template
    args:
        obj_id : employee instance id
    r�r�r�NFr�zCreated work informationr"r)rSr�r�r?rDr�r=rTr{r�r�r�r�r}rr�r�r)r�r�r�r�r�r��bank_form_instancer�rnrnrq�employee_work_info_view_create�s.


���
�rhcCsjtjj|d�}t|jd�}t|jjd�}t|j|d�}|�	�r+|�
�t�|t
d��t|d|||d��S)z�
    This method is used to update employee work information from single view template
    args:
        obj_id  : employee work information id
    r�r�z%Work Information Updated Successfullyr"r)rXr�r�r?r}r=r�rDr�r�r�rr�r�r)r�r��work_informationr�r�r�rnrnrq�employee_work_info_view_update�s"
��
�rjz employee.add_employeebankdetailscCs�tjj|d�}t|d�}t|j�}tjj|d���}t	�}|dur&t	|d�}|�
�r?|jdd�}||_|��t
�|td��t|d|||d	��S)
z
    This method used to create bank details object from the view template
    args:
        obj_id : employee instance id
    r�r�r�NFr�z!Bank Details Created Successfullyr"r)rSr�r�r?r=r�rXr{r�rDr�r�r}rr�r�r)r�r�r�r�r��work_form_instancer��
bank_instancernrnrq�!employee_bank_details_view_create�s*	

��

�rmcCs~tjj|d�}t|jd�}t|jjd�}t|j|d�}|�	�r5|j
dd�}|j|_|�
�t�|t
d��t|d|||d��S)z>
    This method is used to update employee bank details.
    r�r�Fr�z!Bank Details Updated Successfullyr"r)rTr�r�r?r}rDr�r=r�r�r�rr�r�r)r�r��employee_bank_instancer�r�r�rlrnrnrq�!employee_bank_details_view_update�s$��
�roz'employee.delete_employeeworkinformationcCs�ztjj|d�}|��t�|td��Wt
d�Stjy-t�|td��Yt
d�St	y@t�|td��Yt
d�Sw)zy
    This method is used to delete employee work information
    args:
        obj_id : employee work information id
    r�z!Employee work information deletedz$Employee work information not found.z0You cannot delete this Employee work informationz(/employee/employee-work-information-view)rXr�r�r�rr�r�r(r3rr)r�r��
employee_workrnrnrq� employee_work_information_delete�s	���rqc	Cs,|jdkr||jd}t�|�}|�d�}g}|D]_}zO|d}|d}|d}tjj|d���}	|	durh|}
d	}d
|vrD|�	d
d�\}
}tjj
||t|���dd
�}t
�}
||
_|
|
_||
_||
_||
_|
��Wqtyw|�|�Yqwtd�Stjgd�d�}tdd�}d|d<|j|dd�|S)zH
    This method is used to create employee and corresponding user.
    r�rR�records�phoner��employee_full_name)�usernameNr2� r�F)rur��passwordr�zz
    <div class='alert-success p-3 border-rounded'>
        Employee data has been imported successfully.
    </div>

    )rtr�rs��columns�application/ms-excel�rHz-attachment; filename="employee_template.xlsx"�Content-Disposition�r�)r�r*r�
read_excel�to_dictr	r�r{r�r;�create_userr��striprSr�r��employee_last_namer�rsr�r�r�r�	DataFrame�to_excel)r�rR�
data_frame�employee_dicts�
error_list�
employee_dictrsr�rt�
existing_userr�r�r�r�r�rnrnrq�employee_import	sX




�
����
r�cCs�dd�tj��D�}|�d�|�d�|�d�|�d�|�d�|�d�tjj|�}tjt|�|d	�}t	d
d�}d|d
<|j
|dd�|S)�=
    This method is used to export employee data to xlsx
    cSsg|]}|js|j�qSrn)�auto_createdr5)r�rrnrnrqr�R	�z#employee_export.<locals>.<listcomp>r�r��additional_info�is_from_onboarding�is_directly_convertedrzrxrzr{�+attachment; filename="employee_export.xlsx"r|Fr})rSr��
get_fieldsr�r��values_listrr�r�rr�)r��field_names�
employee_datar�r�rnrnrq�employee_exportK	s






r�cCs0|�|�}zt|�WdSty|YSw)z4
    This method is returns None or field value
    N)r��floatr�)r��dicts�field_valuernrnrqrOg	s
�rOc%Cs�tjgd�d�}idg�dg�dg�dg�dg�dg�d	g�d
g�dg�dg�d
g�dg�dg�dg�dg�dg�dg�ggggggggggggd��}tdd�}d|d<|j|dd�d}|j�d�dkrhd}|jdk�r�|j�d�d u�r�d!}g}g}d}|jd}	|	j�	d"�d#�
�}
|
d$kr�t�|	�nt�|	�}|�
d%�}ttjjd&dd'��}ttjjd(dd'��}
ttj�d)d*d+��}g}|D�]!}d}�z|d}|d}td|�}td|�}|d}|d}|d}td,|�}td-|�}d.}zt�|��st�||��sd/|d0<d}Wn
d}d/|d0<Yzt�|�Wnt�y+d1|d2<d}Ynwzt�|�Wnt�yCd3|d4<d}Ynwt�|��rPd5|d6<d}t�|��r\d7|d8<d}|||f}||v�rmd9|d:<d}n|�|�z	t�|���Wn
d;|d<<d}Yz	t�|���Wn
d;|d=<d}Y||v�r�d>|d?<d}n|�|�||
v�r�d@|dA<d}n|
�|�|�r�|�|�n|�|�Wq�t�y�}z
d}t�|�WYd }~q�d }~ww|�s�|�s=z6t |�}t!|�}t"j#t$|fdB�}|�%�t&|�}t'|�t(|�t)|�t*|�t+|�t,|�t-|�Wnt�y<}z
d}t�|�WYd }~nd }~ww|�rit.�|dC�tjdDgdEgd�}t&|�} tdd�}dF|d<|j|dd�| |dG<|S|�r�|D] }!|�/�D]\}"}#|"|!v�r�|#�|!|"��qt|#�d ��qt�qndHdI�|�/�D�}$|$D]}"||"=�q�tj||�0�d�}t&|�} tdd�}dF|d<|j|dd�| |dG<|St1dJ|dK��S|S)LzV
    This method is used to import Employee instances and creates related objects
    )�Badge id�
First Name�	Last Name�Phone�Email�Genderr*�Job Position�Job Role�	Work Type�Shift�
Employee Type�Reporting Managerr)�Location�Date joining�Contract End Date�Basic Salary�Salary Hourrxr�r�r�r�r�r�r*r�r�r�r�r�r�r)r�r�r�)r�r��Email Error�First Name error�Name and Email Error�Phone error�Joining Date Error�Contract Error�Badge ID Error�Basic Salary Error�Salary Hour Error�
User ID Errorrzr{z.attachment; filename="work_info_template.xlsx"r|Fr}�create_work_info�trueTr�rRNr�.r��csvrrr���flatrur�r�r�r�r�z0^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$zInvalid Email addressr�zBasic Salary must be a numberr�zSalary Hour must be a numberr�zFirst Name can't be emptyr�zPhone Number can't be emptyr�zFAn employee with this first name, last name, and email already exists.r�z5Invalid Date format. Please use the format YYYY-MM-DDr�r�z,An Employee with the badge ID already existsr�z%User with the email ID already existsr�)�targetr�zsomething went wrong....z3The provided titles don't match the default titles.zTitle Errorz'attachment; filename="ImportError.xlsx"z
X-Error-CountcSs&g|]\}}tdd�|D��r|�qS)css�|]}|duVqdSrmrn)r��vrnrnrq�	<genexpr>W
s�z.work_info_import.<locals>.<listcomp>.<genexpr>)r�)r�rGr�rnrnrqr�T
s��z$work_info_import.<locals>.<listcomp>zEmployees Imported Succefully)r3�
success_count)2rr�rr�r�r�r�r*r5r;rL�read_csvr~rr%rSr�r�r	rO�isna�re�match�
to_numericr�r��to_datetimerr�r�r[r3rLrG�	threading�ThreadrQ�startr�rFrIrJrNrKrHrMrr�r�r)%r�r��
error_datar�r��total_count�error_lists�
success_lists�
error_occuredrRrG�work_info_dicts�existing_badge_ids�existing_usernames�existing_name_emails�usersr�r3r�rs�
first_name�	last_namer��date_joining�contract_end_date�basic_salary�salary_hour�pattern�name_email_tupler-r��thread�error_countr�rGr��keys_to_removernrnrq�work_info_imports	s����������	�
���
������
!
�
��




�
��

�
��
�

�

�������

��

��r�cCs |j�d�rt�t�d�}t|d|�Si}g}t�}t|j�j}t||d�}|j�d�}|sF|j	dj
}|j�d�}t�|�}t
jj|d�}tD]}	|	d}
|	d	}|
|vr]|�|
|f�qH|D]�\}}
|�d
�}g||
<|D]�}|}
|D]}t|
|d�}
|
dur�nqu|
dur�t|
�nd}t|
�tkr�|j}|j}tjj|d
�}|��r�|D]}|j}q�tjj|d�}|��}|r�|jnd}nd}t� t|
�d���}t!�"�D]
\}}||kr�|�#|�}q�|dkr�t$d�}n|dkr�t$d�}||
�|�qoq`t%j&|d�}t'dd�}d|d<|j(|dd�|S)r�r?)�
export_filter�export_formzemployee_export_filter.htmlr��selected_fieldsrdr�rr�r�Nr2r�)r�zMMM. D, YYYYz%Y-%m-%dr5�Yesr�Norrzr{r�r|Fr}))r�r�r7r>rr�rr%r'r�r.r�r�rSr�r{rEr�r;�getattrr�r�rr�r�rXr�r�r)r��date_formatr�strptimerbr��strftimer�rr�rr�)r�r��employees_data�selected_columnsr�r�r�rd�id_listr�r�rG�column_value�column_name�nested_attributesr��attrr&r�r�r�r��employee_company�company_name�emp_companyr��
start_date�format_name�
format_stringr�r�rnrnrq�work_info_exportn
s~��
�
��
�
�(
r�cCsht����}t�|j|j�d}tjj	d|j
|j|d��td�j
dd��}|D]
}|jj
|j
|_q'|S)zP
    This method is used to find upcoming birthday and returns the queryset
    r�T)rz�
dob__day__gte�
dob__month�
dob__day__lte�dob__day)�
nulls_last)rr�r�calendar�
monthrange�year�monthrSr�r{�dayrr
�asc�dob�days_until_birthday)r��last_day_of_monthr�r�rnrnrq�birthday�
s��rcs,t�}d��fdd�|D�}t|dd|i�S)zJ
    Render all upcoming birthday employee details for the dashboard.
    z3https://ui-avatars.com/api/?background=random&name=c	s�g|]Q}t|d�r
|��n
��|j�d|j��|j�d|j��|j�d�|jdkr-td�n|jdkr6td�nd|j�d	�|��rE|��j	nd
|�
�rO|�
�jnd
d��qS)�
get_avatar�+rvz%d %b %Yr�Todayr��TomorrowzIn z Daysr2)�profiler5r�daysUntilBirthday�
department�job_position)�hasattrrr�r�rr�rr��get_departmentr
�get_job_positionr�r�r���default_avatar_urlrnrqr��
s �
�

�
���z*get_employees_birthday.<locals>.<listcomp>zbirthdays_container.html�	birthdays)rr)r�r�rrnrrq�get_employees_birthday�
s
�rc
Cs�t�}tj��}t||d�}|jdd�}|jdd�}d}d}|��r:t|�t|�dd�}t|�t|�dd�}t|d|t|�t|�t|�||d	��S)
zP
    This method is used to render individual dashboard for employee module
    r�Tr|Fr�dz.1fz*employee/dashboard/dashboard_employee.html)r�active_employees�inactive_employees�total_employees�active_ratio�inactive_ratio)	rrSr�r�r$r{r�r�r)r��upcoming_birthdaysr�rrrrrnrnrq�	dashboard�
s*
��rcCstjjdd���}t|�S)NTr|)rSr�r{r:r)r�r�rnrnrq�total_employees_countsrcCsLd}t�d�r"tddd�}|jjt��t��tdd�gdd���}t	|�S)	NrrJ�	candidaterLr���daysT)�joining_date__rangerz)
rr�rcr�r{rr�rr:r)r��
newbies_today�	Candidaternrnrq�joining_today_counts
��r#cCsld}t�d�r2tddd�}|jjt��tt����d�t��tdt����d�gddd��	�}t
|�S)	NrrJrrLr�T)r rz�hired)rr�rcr�r{rr�r�weekdayr:r)r��newbies_weekr"rnrnrq�joining_week_count's
���r(cCsTtd�td�g}tj��}td�t|jdd��t|jdd��gd�g|d�}t|�S)	z1
    Active and in-active employee dashboard
    �Activez	In-Active�	EmployeesTr|F�rr&��dataSetr��r�rSr�r{r�r�r�r�r�r�rnrnrq�dashboard_employee7s�
���	�r0cCsltd�td�td�g}tjjdd�}td�t|jdd��t|jd	d��t|jd
d��gd�g|d�}t|�S)
zA
    This method is used to filter out gender vise employees
    �Male�Female�OtherTr|r*�male)�gender�female�otherr+r,r.r/rnrnrq�dashboard_employee_genderPs���
�
r8cCsvg}g}tj��}|D]}ttjj|dd��r*|�|j�|�ttjj|dd���qd|d�g|td�d�}t	|�S)z]
    This method is used to find the count of employees corresponding to the departments
    T)�-employee_work_info__department_id__departmentrzr*r+zNo Data Found...)r-r�r2)
r*r�r�r�rSr{r�r
r�r)r�r�r:�departments�deptr�rnrnrq�dashboard_employee_departmenths0
������
	�r<cCs&t|j�jjddd�}tdt|�i�S)zD
    This method is used to return all the ids of the employees
    r�Tr�rd)r7r�rr�rr�)r�rdrnrnrq�
widget_filter�sr=cCsX|j�d�}tj��}|dkrtjjdd�}dd�|D�}|��}||d�}t|dd	�S)
z^
    This method is used to return all the id of the employees to select the employee row
    r�r�Tr|cS�g|]}t|j��qSrn�r�r�rrnrnrqr���z#employee_select.<locals>.<listcomp>��employee_idsr�F)�safe)r�r�rSr�r{r:r)r�rtr�rBr�r�rnrnrq�employee_select�s

rDcCsd|j�d�}|dkr0t|jtj��d�}t||jdd�}dd�|D�}|��}||d�}t	|�Sd	S)
zM
    This method is used to return all the ids of the filtered employees
    r�r�r}r�)r�rr�cSr>rnr?rrnrnrqr��r@z*employee_select_filter.<locals>.<listcomp>rAN)
r�r�r7rSr�r{r%rr:r)r�rt�employee_filterr�rBr�r�rnrnrq�employee_select_filter�s��
�rFzemployee.view_employeenote)r�cCs4tjj|d�}tjj|d��d�}t|d||d��S)a
    This function is used to view note tab of an employee in employee individual
    & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return note-tab template

    r�r�z-idztabs/note_tab.html)r��notes)rSr�r�rVr{rr)r�r��employee_objrGrnrnrq�note_tab�s�rIzemployee.add_employeenotecCs�td|id�}|jdkrOt|j|j�}|��rO|jdd�\}}tjj|d�}||_	|j
j|_|��|j
�|�t�|td��t|dd	|i�}td
|���Stjj|d�}t|d||d��S)zH
    This method renders template component to add candidate remark
    r}r-r�Fr�r�zNote added successfully..ztabs/add_note.htmlr��/employee/note-tab/)r�r�)rAr�r�r*r�r�rSr�r�r}r�r��
updated_by�
note_filesr%rr�r�rr)r�r�r��note�attachment_idsr�r�rHrnrnrq�add_note�s0
�
��rOzemployee.change_employeenotecCs�tjj|d�}t|d�}|jr8t|j|d�}|��r8|��t�|t	d��t
|dd|i�}t|j�
d�d�St
|dd|i�S)z^
    This method is used to update the note
    Args:
        id : stage note instance id
    r�r�zNote updated successfully...ztabs/update_note.htmlr�r�r�)rVr�r�rAr�r�r�rr�r�rrr�r�)r��note_idrMr�r�rnrnrq�employee_note_update�s*	
����rQzemployee.delete_employeenotecCs,tjj|d�}|��t�|td��t�S)z^
    This method is used to delete the note
    Args:
        id : stage note instance id
    r�zNote deleted successfully.)rVr�r�r�rr�r�r)r�rPrMrnrnrq�employee_note_deletes	rRzemployee.add_notefilescCsptjj|d�}|jj}|jdkr1|j�d�}g}|D]}tjj	|d�}|�
|j�|j�|j�qt
d|���S)zs
    This method is used to Add more files to the Employee note.
    Args:
        id : stage note instance id
    r�r��files)rSrJ)rVr�r�r}r�r�r*r'rY�creater�rLr�r)r�rPrMr}rS�	files_idsrRr�rnrnrq�add_more_employee_files)s	
rVzemployee.delete_notefilescCstjj|d�}|��t�S)zi
    This method is used to delete the stage note file
    Args:
        id : stage file instance id
    r�)rYr�r�r�r)r��note_file_idrRrnrnrq�delete_employee_note_file?s	rXzemployee.view_bonuspointc
Cstjj|d�}tjj|d�}t�d�r#tddd�}|jj|ddd�}nt��	�}|�
�}g}|D]5}|�|d	|d
dj|d
dj
|d
dj
ttjj|d
djd���d
d�|d
djd��q0|D]}	|�d|	j|	j|jdd��qht|dd�dd�}|||d�}
t|d|
�S)a

    This function is used to view Bonus Points tab of an employee in employee individual
    & profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return bonus_points template

    r�r�r��
reimbursementrL�bonus_encashment�	requested)r}r�r^r��pairrr�r�N)r�r�pointsr�rgzRedeemed pointscSs|dS)Nrrn)�xrnrnrqr��sz"bonus_points_tab.<locals>.<lambda>T)rGr)r�r]�
activity_listztabs/bonus_points.html)rSr�r�rRrr�rcr{r�none�trackingr��history_dater]r�r	�history_user_idr�rg�
created_at�bonus_to_encashr��sortedr)r�r�rHr]�
Reimbursement�requested_bonus_points�	trackingsr_�historyr[r�rnrnrq�bonus_points_tabMsX
��
���
��	��rkzemployee.add_bonuspointcCs�tjj|d�}t�}|jdkrKt|j|j�}|��rK|jdd�|j	|j
d7_	|j
d|_|��t�
|td��|j
d��t|j�dd	��St|d
||d��S)z�
    This function is used to add bonus points to an employee

    Args:
        request (HttpRequest): The HTTP request object.
        emp_id (int): The id of the employee.

    Returns: returns add_points form
    r�r�Fr�r]rgz$Added {} points to the bonus accountr�r�ztabs/forms/add_points.htmlr/)rRr�r�r9r�r�r*r�r�r]�cleaned_datargrr�r�r�rr�r)r�r��bonus_pointr�rnrnrq�add_bonus_points�s4

�����rnc

Cs$tjj|d�}d}tjj|d���rtjj|d���j}td|id�}||j	_
d}t�d�rAt
ddd�}|j��r?|j��jnd	}|jd
kr�t|j�}||j	_
|��r�|jdd�|jd}||}t�d�r�t
dd
d�}	|	jjd|��d||||�d|�d�t��d�td�St|d||d��S)z�
    This function is used to redeem bonus points for an employee

    Args:
        request (HttpRequest): The HTTP request object.
        emp_id (int): The id of the employee.

    Returns: returns redeem_points_form form
    r�rr�r]r-r��encashmentgeneralsettingsrLr�r�Fr�rYzBonus point Redeem for rZz want to redeem z points)r1r�r}re�amount�description�allowance_onrz"tabs/forms/redeem_points_form.html)r�r�)rSr�r�rRr{r�r�r]r:r�r}rr�rc�bonus_amountr�r�r�r�rlrTrr�rr)
r�r�r��avialable_pointsr��amount_for_bonus_point�EncashmentGeneralSettingsr]rprgrnrnrq�
redeem_points�sX�
���



��	��rwcs�|j�d�}|j�d�dkr|dkrtjjdd|d���}n
tjjddd���}d	d
�|D��g����fdd��|j�d�}|j�d�dkrW|dkrWtjjdd|d���}n
tjjddd���}|jj}t	|�d
krni}n|d
j
td�i��}|jdkr�|j
�d�r�t|j
�d��}tjj|d�}|��t|��dtd���|�d�}d|i}t|d|d�S|��t|��dtd���|�d�}|||j
d�}t|d|d�S)z7
    This method is used to view oganisation chart
    rxr{Nr�TF)rz�reporting_manager__isnullr{)rzrxcSsi|]}|j|���qSrn)r��
get_full_namer�rnrnrq�
<dictcomp>

r�z&organisation_chart.<locals>.<dictcomp>c	s�g}|j���vr��|�tjjd|d�j|jd�}|D]?}|�vr$q|j���vrF|�|��t|�	�dt
d���|�d����|�q|�|��t|�	�dt
d��d�|�d��q|S)	z,
        Hierarchy generator method
        T)rz�(employee_work_info__reporting_manager_idr�r�Not set�r5r1�childrenzmiddle-level)r5r1�	classNamer~)r�r�r�rSr�r{rryr�rr�)�manager�nodes�subordinatesr���create_hierarchy�entered_req_managers�result_dictrnrqr�
s>
�����	���
z,organisation_chart.<locals>.create_hierarchyrzMy viewr��
manager_idr�rr|r}�act_datasourcezorganisation_chart/chart.htmlr�)r��reporting_manager_dict�act_manager_idz!organisation_chart/org_chart.html)r�r�r�rSr�r{�distinctr�r�r�r�r�r�r�r�ryr�rr)r�rx�reporting_managersr��new_dictr��noder�rnr�rq�organisation_chart�sr����,����
���r�z%payroll.add_encashmentgeneralsettingscCs�t�d�rSddlm}tddd�}t�d�r|j��nt���}|j	dkrF||j
|d�}|��rE|��t
�|td��t|j�d	d
��Sn||d�}t|dd|i�St
�|td
��t|j�d	d
��S)zJ
    Handle the creation and updating of encashment general settings.
    r�r)�EncashmentGeneralSettingsFormrorLr�r�zSettings updated.r�r�z!settings/encashment_settings.html�encashment_formzPayroll app not installed)rr��payroll.forms.formsr�rcr�r�rr`r�r�r�r�rr�r�rr�r�rr�)r�r�rvr�r�rnrnrq�encashment_condition_createn
s4
���
��
�r�z#employee.add_employeegeneralsettingcCs�tj��}|s
t�}|jdkr4t|j|d�}|��r-|��t�	|d�t
|j�dd��St�
|d�nt|d�}t|dd|i�S)	zE
    This method is used to set the initial prefix using a form.
    r�r�z$Initial prefix updated successfully.r�r�z'There was an error updating the prefix.zsettings/settings.html�prefix_form)rUr�r�r�r@r�r�r�rr�rr�r�r3r)r�r�r�rnrnrq�initial_prefix�
s


r�cCst�}t|dd|i�S)zV
    This method is used to return the first last badge ids in grouped and ordere
    z,employee_personal_info/first_last_badge.html�	badge_ids)rPr)r�r�rnrnrq�first_last_badge�
s�r�cCsf|jd}tjj|d�}tjj|jd�}|jr&|jjr&|tjj|jjd�B}|�d�}t	|dd|i�S)zG
    This method is used to track mails sent along with the status
    r�r�)�
to__icontainsz-created_atztabs/mail_log.html�
tracked_mails)
r�rSr�r�r+r{r�r�rr)r�r}r�r�rnrnrq�employee_get_mail_log�
s
�
r�cCs:|j�d�}|rtjj|d��dd�ng}tdt|�i�S)Nr�)r�r�r�
job_positions)r�r�r.r�r{r�r�dict)r�r�r�rnrnrq�get_job_positions�
s���r�cCs2|j�d�}tjj|d��dd�}tdt|�i�S)a
    Retrieve job roles associated with a specific job position.

    This view function extracts the job_id from the GET request, queries the
    JobRole model for job roles that match the provided job_position_id, and
    returns the results as a JSON response.
    �job_id)r�r��job_role�	job_roles)r�r�r/r�r{r�rr�)r�r�r�rnrnrq�
get_job_roles�
s
	�r�zemployee.view_employeetagcCstj��}t|dd|i�S)z.
    This method is used to Employee tags
    zbase/tags/employee_tags.html�employeetags)rWr�r�r)r�r�rnrnrq�employee_tag_view�
s
�r�zemployee.add_employeetagcCsPt�}|jdkr t|j�}|��r |��t�}t�|td��t|dd|i�S)�E
    This method renders form and template to create Ticket type
    r�z"Tag has been created successfully!�(base/employee_tag/employee_tag_form.htmlr�)	rBr�r�r�r�rr�r�r)r�r�rnrnrq�employee_tag_create�
s

��r�cCsptjj|d�}t|d�}|jdkr/t|j|d�}|��r/|��t�}t�	|t
d��td�St|d||d��S)r�r�r�r�z"Tag has been updated successfully!rdr�)r��tag_id)
rWr�r�rBr�r�r�r�rr�r�rr)r�r��tagr�rnrnrq�employee_tag_updates

�r�rm(�__doc__r�r�r��operatorrJr�r�rrr�urllib.parser�pandasr�django.appsr�django.confr�django.contribr�django.contrib.auth.modelsr	�django.core.cacher
�	django.dbrr�django.db.modelsr
r�django.db.models.queryr�django.formsrr�django.httprrrr�django.shortcutsrrr�django.urlsr�django.utilsr�django.utils.translationrr�rr��django.views.decorators.httpr�accessibility.decoratorsr�accessibility.methodsr�accessibility.middlewaresr �accessibility.modelsr!�
base.formsr"�base.methodsr#r$r%r&r'r(�base.modelsr)r*r+r,r-r.r/r0r1r2r3r4r5�employee.filtersr6r7r8�employee.formsr9r:r;r<r=r>r?r@rArBrCrDrE�employee.methods.methodsrFrGrHrIrJrKrLrMrNrOrPrQ�employee.modelsrRrSrTrUrVrWrXrY�horilla.decoratorsrZr[r\r]r^r_�horilla.filtersr`�horilla.group_byra�horilla.horilla_settingsrb�horilla.methodsrc�horilla_audit.modelsrdre�horilla_documents.formsrfrgrhri�horilla_documents.modelsrjrk�notifications.signalsrlrr�eq�nerurvrwrx�contains�operator_mapping�filter_mappingr�r�r�r�r�r�r�r�rrr!r)r+r0r6rArDrSrMr_rcrfrirkrmrqr�rvr�r�r�r�r�r�r�rrrrrrr!r�r#r1r4r<rHrbrerfrhrjrmrorqr�r�r�r�rrrrr#r(r0r8r<r=rDrFrIrOrQrRrVrXrkrnrwr�r�r�r�r�r�r�r�r�r�rnrnrnrq�<module>s�
 <<8( 
������

�(�G%!*
0)$!		�+X=g4#�.$$%$3\+�!
:zO
$







" 
>)<
v%


Youez - 2016 - github.com/yon3zu
LinuXploit