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/payroll/views/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

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

hͦh�:�	@s�dZddlZddlZddlmZddlmZmZddlmZddl	m
Z
ddlZddl
mZddlmZdd	lmZmZdd
lmZddlmZmZmZmZddlmZmZdd
lmZddl m!Z!ddl"m#Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+ddl,m-Z-ddl.Z/ddl0m1Z1ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8ddl9m:Z:ddl;m<Z<m=Z=ddl>m?Z?m@Z@mAZAmBZBddlCmDZDddlEmFZFddlGmHZHmIZImJZJddlKmLZLddlMmNZNmOZOmPZPmQZQmRZRmSZSddlTmUZVddlWmXZXddlYmZZZm[Z[m\Z\m]Z]dd l^m_Z_m`Z`maZambZbmcZcmdZdmeZedd!lfmgZgdd"l.mhZhmiZimjZjmkZkmlZlmmZmmnZnmoZodd#lpmqZqdd$lrmsZsd%d&�Ztejuejvejwejxejyejzej{etd'�Z|d(d)�Z}e@e?d*d+���Z~e@eBd,�d-d.���Ze@eBd/�d0d1���Z�e@e?d2d3���Z�e@e?eBd/�d4d5����Z�e@eBd6�d7d8���Z�e@e?eBd9�d:d;����Z�e@eBd<�d=d>���Z�e@eBd/�d?d@���Z�e@e?dAdB���Z�e@e?eBd/�dCdD����Z�e@eBdE�dFdG���Z�e@e?eBdH�d�dIdJ����Z�e@eBdK�dLdM���Z�e@e?dNdO���Z�e@eBdK�d�dPdQ���Z�e@eBdK�dRdS���Z�e@eBdT�dUdV���Z�e@dWdX��Z�e@e?dYdZ���Z�e@eBd[�d\d]���Z�e@eBd,�d^d_���Z�e@eBdK�d`da���Z�e@eBd,�dbdc���Z�e@eBd<�ddde���Z�e@eBdf�dgdh���Z�e@e?didj���Z�e@eBdf�dkdl���Z�e@eBdm�dndo���Z�e@eBdf�dpdq���Z�e@e?eBdf�drds����Z�e@eBdt�dudv���Z�e@eBdw�dxdy���Z�e@e?dzd{���Z�e@e?d|d}���Z�e@d~d��Z�e@eBd��d�d����Z�e@eBd��d�d����Z�e@eAdwemd��d�d����Z�e@eAdwemd��d�d����Z�e@eAd�emd��d�d����Z�e@eBdT�d�d����Z�d�d��Z�e@d�d���Z�e@eBd[�d�d����Z�dS)�zf
component_views.py

This module is used to write methods to the component_urls patterns respectively
�N)�defaultdict)�date�datetime)�groupby)�parse_qs)�apps)�messages)�ObjectDoesNotExist�ValidationError)�Sum)�HttpResponse�HttpResponseRedirect�JsonResponse�	QueryDict)�redirect�render)�reverse)�format_html)�gettext_lazy)�Workbook)�	Alignment�Border�Font�Side)�get_column_letter)�ConfiguredEmailBackend)�closest_numbers�
eval_validate�filter_own_records�get_key_instances�get_next_month_same_date�sortby)�Company)�Employee�EmployeeWorkInformation)�hx_request_required�login_required�owner_can_enter�permission_required)�group_by_queryset)�HORILLA_DATE_FORMATS)�dynamic_attr�get_horilla_model_class�
get_urlencode)�notify)�AllowanceFilter�DeductionFilter�LoanAccountFilter�
PayslipFilter�PayslipReGroup�ReimbursementFilter)�component_forms)�update_compensation_deduction)�calculate_employer_contribution�compute_salary_on_period�
paginator_qry�save_payslip)�calculate_allowance�calculate_gross_pay�calculate_net_pay_deduction�calculate_post_tax_deduction�calculate_pre_tax_deduction�calculate_tax_deduction�calculate_taxable_gross_pay)�calculate_taxable_amount)�	Allowance�Contract�	Deduction�LoanAccount�Payslip�
Reimbursement�ReimbursementMultipleAttachment�_create_deductions)�MailSendThread)�view_created_payslipcCsdS�N�)�a�brNrN�-/var/www/erp/payroll/views/component_views.py�return_none\srR)�equal�notequal�lt�gt�le�ge�	icontains�rangec'	Cs�t|||�}|d}|d}|d}|d}|d}|d}	|d}
t||d||�}|d}|d	}|js9t|�nd
}
||
}|||||
d�}td+i|��}tdd
�|dD��}||d<||d<td+i|��}|d}|d	}||d<td+i|��}td+i|��}|d|dB}t	d+i|��}t
d+i|��}td+i|��}tdd
�|dD��}tdd
�|dD��}tdd
�|dD��}tdd
�|dD��}|||||
}||}t||d||�}|d}|d	} t||dfi|��}!|!d}"| D]}#|"�
|#�q�||!d}id|�d|�d|�d|�d|d�d|�d|d�d|�d|	�d|�d |�d|d�d|d�d|d�d!|"�d"|�d|�||||�d#��d$|�d#���d%��}$|$��}%|j|%d<|�d&�|%d'<|�d&�|%d(<t�|%�}&|&|$d)<||$d<|$S),a�
    Calculate payroll components for the specified employee within the given date range.


    Args:
        employee (Employee): The employee for whom the payroll is calculated.
        start_date (date): The start date of the payroll period.
        end_date (date): The end date of the payroll period.


    Returns:
        dict: A dictionary containing the calculated payroll components:
    �contract�
contract_wage�	basic_pay�loss_of_pay�	paid_days�unpaid_days�
month_data�compensation_amount�
deductionsr)�employee�
start_date�end_dater]�day_dictcs��|]}|dVqdS��amountNrN��.0�	allowancerNrNrQ�	<genexpr>���z&payroll_calculation.<locals>.<genexpr>�
allowances�total_allowance�	gross_pay�installmentscsrhrirN�rl�itemrNrNrQrn�rocsrhrirNrtrNrNrQrn���
��pretax_deductionscsrhrirNrtrNrNrQrn�rv�post_tax_deductionscsrhrirNrtrNrNrQrn�rv�tax_deductions�net_pay�net_pay_deduction�net_pay_deductions�
net_deductionrd�taxable_gross_pay�basic_pay_deductions�gross_pay_deductions�net_deductions�total_deductionsz%b %d %Yz - )�federal_taxrerfrZ�%Y-%m-%drerf�	json_dataNrN)r8r6�deduct_leave_from_basic_pay�floatr;�sumr<r?r>rAr@rBr=�append�strftime�copy�id�json�dumps)'rdrerf�basic_pay_detailsr[r\r]r^r_r`�working_days_details�updated_basic_pay_datar�loss_of_pay_amount�kwargsrprq�updated_gross_pay_datarrr�rwrxrsr~ryr��total_pretax_deduction�total_post_tax_deduction�total_tax_deductionsr�rz�updated_net_pay_data�update_net_pay_deductionsr|�net_pay_deduction_list�	deduction�payslip_data�data_to_jsonr�rNrNrQ�payroll_calculationls�
������������
�����������	�
���
������

r�c	sfg}g}tjj|d�}|jjdd���}|r|jnd��r�tjj|d�tjjdd�j|d�Btjjdd	�j|d�B}|D]J}d}|j	rt
|j�d
dd��}	|	�
|j|j|j���d
d�f�|	D]\}
}}t||
�}
|
duszt�|�|
t|
�|��s~d}nq`|r�|�
|�q<�fdd�|D�}tjj|d�tjjdd�j|d�Btjjdd	�j|d�B}|D]J}d}|j	r�t
|j�d
dd��}	|	�
|j|j|j���d
d�f�|	D]\}
}}t||
�}
|
dus�t�|�|
t|
�|��s�d}nq�|r�|�
|�q�|�rt�dd�|D��nd}|�rt�dd�|D��nd}|�|�r|nd||�r&|nd||d�}t|d|d�S)a�
    Retrieve and render the allowances and deductions applicable to an employee.

    This view function retrieves the active contract, basic pay, allowances, and
    deductions for a specified employee. It filters allowances and deductions
    based on various conditions, including specific employee assignments and
    condition-based rules. The results are then rendered in the allowance and
    deduction tab template.
    �r��active)�contract_statusN)�specific_employeesT)�is_condition_based)�exclude_employees)�include_active_employees�field�	condition�value� �_Fcs2g|]}t�|j�|jdkr�nd|j�r|�qS)r]r)�operator_mapping�get�if_condition�	if_choice�	if_amountrk�r]rNrQ�
<listcomp>)s
���z-allowances_deductions_tab.<locals>.<listcomp>cS�g|]}|j�qSrNr��rl�instancerNrNrQr�X�cSr�rNr�r�rNrNrQr�]r�)�active_contractsr]rp�
allowance_idsrc�
deduction_idsrdz!tabs/allowance_deduction-tab.html)�context)r#�objectsr��contract_set�filter�first�wagerC�excluder��list�other_conditions�values_listr�r�r�r��lower�replacer+r��typerEr�r�r)�request�emp_id�employee_deductions�employee_allowancesrdr�rprm�
applicable�
conditionsr��operatorr��valrcr�r�r�r�rNr�rQ�allowances_deductions_tab�s������
����
��
�
�������������
��
������	r�zpayroll.add_allowancecCs^t��}|jdkr't�|j�}|��r'|��t��}t�|td��t	t
�St|dd|i�S)zD
    This method is used to create allowance condition template
    �POSTzAllowance created.�payroll/common/form.html�form)�forms�
AllowanceForm�methodr��is_valid�saver�successr�r�view_allowancer�r�r�rNrNrQ�create_allowancems
r�zpayroll.view_allowancecC�Ttjjdd�}t|j�}t||j�d��}t�dd�|j	D��}t
|d|||d��S)zQ
    This method is used render template to view all the allowance instances
    T��only_show_under_employee�pagecSr�rNr�r�rNrNrQr��r�z"view_allowance.<locals>.<listcomp>z%payroll/allowance/view_allowance.html)rp�fr�)rCr�r�r/�GETr9r�r�r��object_listr)r�rp�allowance_filterr�rNrNrQr�~s
��r�c	Csnt|�}t�|�}|j�d�}d|i}|r-t�|�}t||�\}}||d<||d<||d<||d<t|d|�S)zV
    This method is used render template to view the selected allowance instances
    �
instances_idsrm�next�previousr��pdz,payroll/allowance/view_single_allowance.html)	r-rC�findr�r�r��loadsrr)	r��allowance_id�
previous_datarm�allowance_ids_jsonr�r��previous_id�next_idrNrNrQ�view_single_allowance�s"
�
�r�cC�|j��}t|j�jjdd�}d}d}|}|j�d�dkr|}t||d�}t||j�d��}t�	d	d
�|j
D��}t|�}tt
|�t||||||d��S)zZ
    Filter and retrieve a list of allowances based on the provided query parameters.
    Tr�z%payroll/allowance/list_allowance.htmlz%payroll/allowance/card_allowance.html�viewr�r!r�cSr�rNr�r�rNrNrQr��r�z$filter_allowance.<locals>.<listcomp>)rpr��filter_dictr�)r��	urlencoder/�qsr�r�r!r9r�r�r�rrrCr)r��query_stringrp�	list_view�	card_view�templater��	data_dictrNrNrQ�filter_allowance��*

��r�zpayroll.change_allowancecK�ltjj|d�}tj|d�}|jdkr.tj|j|d�}|��r.|��t	�
|td��tt
�St|dd|i�S)zb
    This method is used to update the allowance
    Args:
        id : allowance instance id
    r��r�r�zAllowance updated.r�r�)rCr�r�r�r�r�r�r�r�rr�r�rr�r)r�r�r�r�r�rNrNrQ�update_allowance�s
r�zpayroll.delete_allowancec
Cs&t|�}z"tjj|d���}|r|��t�|td��nt�	|td��Wn"t
yH}zt�	|td��t�	|t|��WYd}~nd}~ww|j�
d�ddksXtj��s\td	�S|j�d
�}|r�t�|�}t||�\}}||vr�|�|�d|��}	d|�d
|��}
t|	|
�Std|���S)z>
    This method is used to delete the allowance instance
    r�zAllowance deleted successfullyzAllowance not foundz.An error occurred while deleting the allowanceN�/�zdelete-employee-allowance�*<script>window.location.reload();</script>r�z/payroll/single-allowance-view/�?�&instances_ids=z/payroll/filter-allowance?)r-rCr�r�r��deleterr�r��error�	Exception�str�path�split�existsrr�r�r�r�r�remover)r�r�r�rm�er��instances_list�previous_instance�
next_instance�url�paramsrNrNrQ�delete_allowance�s6����


rzpayroll.add_deductioncCsVt��}|jdkr#t�|j�}|��r#|��t�|td��t	t
�St|dd|i�S)z1
    This method is used to create deduction
    r�zDeduction created.r�r�)r��
DeductionFormr�r�r�r�rr�r�r�view_deductionrr�rNrNrQ�create_deductions
rcCr�)zQ
    This method is used render template to view all the deduction instances
    Tr�r�cSr�rNr�r�rNrNrQr�r�z"view_deduction.<locals>.<listcomp>z%payroll/deduction/view_deduction.html)rcr�r�)rEr�r�r0r�r9r�r�r�r�r)r�rc�deduction_filterr�rNrNrQrs
��rc	Cs0t|�}tjj|d���}||d�}|j�d�}|r.t�|�}t	||�\|d<|d<||d<|j
�dd�}|�d	��d	�}d
|vrM|�
d|��dd
��nE|dd�dt|d�gkrvz|�
dt|d���dd
��Wn%tyuYnw|�d�r�|�
d|jjj��dd
��n|�
ddd
��t|d|�S)zN
    Render template to view a single deduction instance with navigation.
    r�)r�r�r�r�r�r��HTTP_REFERER�r�zview-deduction�/payroll/filter-deduction?z#payroll-deduction-container)�load_hx_url�load_hx_target���Nz
employee-view����#/payroll/allowances-deductions-tab/z#allowance_deductionzemployee-profile/z,payroll/deduction/view_single_deduction.html)r-rEr�r�r�r�r�r�r�r�META�rstripr�updater�int�
ValueError�endswith�user�employee_getr�r)	r��deduction_idr�r�r��deduction_ids_jsonr�r�
referer_partsrNrNrQ�view_single_deduction+sH

������
��r)cCr�)z2
    This method is used search the deduction
    Tr�z%payroll/deduction/list_deduction.htmlz%payroll/deduction/card_deduction.htmlr�r�r!r�cSr�rNr�r�rNrNrQr�pr�z$filter_deduction.<locals>.<listcomp>)rcr�r�r�)r�r�r0r�r�r�r!r9r�r�r�rrrEr)r�r�rcr�r�r�r�r�rNrNrQ�filter_deduction`r�r*zpayroll.change_deductioncKr�)z>
    This method is used to update the deduction instance
    r�r�r�zDeduction updated.r�r�)rEr�r�r�rr�r�r�r�rr�r�rrr)r�r&r�r�r�rNrNrQ�update_deductions
r+zpayroll.delete_deductionc
Cs|j�d�}d}d}d}|r#t|�}t�|�}t||�\}}|�|�tjj	|d��
�}|r;|��t�
|td��nt�|td��d|j����d|��d|�d	|�d
|��d�}	|j�d�}
|	�|
�}|
r{|
d
krutj�	�sutd�S|r{t|�S|
r�|jn|j�dd�}t|�S)Nr�rr�zDeduction deleted successfullyzDeduction not foundrrz/payroll/single-deduction-view/rr)�payroll-deduction-container�allowance_deduction�objectDetailsModalTarget�HTTP_HX_TARGETr,rrr�)r�r�r-r�r�rr
rEr�r�r�rrr�r�rr�rrrrr
)
r�r&r�r�rrr�r
r��paths�http_hx_target�redirected_path�default_redirectrNrNrQ�delete_deduction�s<

�
��r4zpayroll.add_payslipc
Cs�|j�d�r|j�d�dkrt��}t|dd|i�Sg}g}t��}|jdkr�t�|j�}|��r�g}|jd}|jd}|jd	}|jd
}	|D]�}
t	j
j|
dd���}||j
kr]|j
}t|
||�}|�|�|�|d
�||d<i}
|
|
d<|	|
d
<|d|
d<|d	|
d	<d|
d<|d|
d<|d|
d<|d|
d<|d|
d<|d|
d<t�|d
�|
d<t|
�|d|
d<td)i|
��}|�|�tj|jj|
jdddddtdd |jid!�d"d#�	qIt�||���d$��td%|	���St|d&d'|i�S)*z�
    Generate payslips for selected employees within a specified date range.

    Requires the user to be logged in and have the 'payroll.add_payslip' permission.

    �HTTP_HX_REQUESTr/�objectCreateModalTargetz(payroll/payslip/bulk_create_payslip.html�	bulk_formr��employee_idrerf�
group_namer��r8r�r��paysliprd�draft�statusr\r]rrr�r�rz�pay_datars�#Payslip has been generated for you.�%تم إصدار كشف راتب لك.�*Gehaltsabrechnung wurde für Sie erstellt.�%Se ha generado la nómina para usted.�.La fiche de paie a été générée pour vous.�view-created-payslip�
payslip_id�r��close��	recipient�verb�verb_ar�verb_de�verb_es�verb_frr�iconz payslip saved as draftz7/payroll/view-payslip?group_by=group_name&active_group=r�r�NrN)rr�r��GeneratePayslipFormrr�r�r��cleaned_datarDr�r�r��contract_start_dater�r�r�r�r7r:r.�sendr$r%�employee_user_idrr�rr��countr)r�r7�payslipsr�r��	instances�	employeesrerfr9rdr[r;�datar�rNrNrQ�generate_payslip�s�

��




��



��
�rZcCsl|j�d�}|j�d�}tjj|dd���}|r|t|j�kr#td�St	d�}t	d�}tt
d||j|d	��S)
z_
    Check if the employee's contract start date is after the provided payslip start date.
    r8rer�r:rzkWhen this payslip is run, the payslip start date will be updated to match the employee contract start date.zEmployee Contract Start Datea�
        <div id='messageDiv' style='background-color: hsl(48, 100%, 94%);
            border: 1px solid hsl(46, 97%, 88%);
            border-radius: 18px; padding:5px; font-weight: bold; display: flex;'>
            {text_content}: {contract_start_date}
            <img style='width: 20px; height: 20px; cursor: pointer;'
                src='/static/images/ui/info.png' class='ml-2' title='{title_message}'>
        </div>
        )�text_contentrR�
title_message)r�r�rDr�r�r�rrRrr�r)r�r8rer[r\r[rNrNrQ�check_contract_start_dates*���	��r]cCsZ|r||_t��}|jdk�r%|j�d�}t|j�d�t�r*t�|j�d�d��	�n|j�d�}|rR|rRt
jj|dd��
�}|rR||jkrR|j��}|j|d<||_t�|j�}|���r%|jd}|jd}|jd}tjj|||d��
�}|���r%|jd}|jd}|jd}t|||�}	||	d	<i}
||
d
<|	d|
d<|	d|
d<|j�d�dur�d
n|jd|
d<|	d|
d<|	d|
d<|	d|
d<|	d|
d<|	d|
d<t�|	d�|
d<t|
�|	d|
d<td'i|
��|	d<t��}t�|td��|	d}tj|jj|j dddddt!dd|j"id �d!d"�	t#d#|	dj$�d$��St%|d%d&|i�S)(a
    Create a payslip for an employee.

    This method is used to create a payslip for an employee based on the provided form data.

    Args:
        request: The HTTP request object.

    Returns:
        A rendered HTML template for the payslip creation form.
    r�r8rer�r�r:rf�r8rerfr;rdr=Nr<r\r]rrr�r�rzr�r>rsr�z
Payslip Savedr?r@rArBrCrDrErFrGrHz6<script>window.location.href = "/payroll/view-payslip/z/"</script>z#payroll/payslip/create_payslip.html�individual_formrN)&r�r��PayslipFormr�r��
isinstancerr�strptimerrDr�r�r�rRr�r�rQrGr�r�r�r�r7r:rr�r�r.rSr$r%rTr�pkrr�r)r��
new_post_datar�r8rer[rdrfr;r�rYrNrNrQ�create_payslip.s��
���





��



��
��
��rec
Csd}d}|j�d�}|j�d�}|j�d�}|r t�|d���}|r*t�|d���}d}d|d�}|D]$}tjj|d	d
��	�}	|durW||	j
krWd|	j�d�}||d
<d|d<q3|durn|durn||krnd}||d
<d|d<|dur�|t����kr�d}||d
<d|d<t
|�S)zW
    This method to validate the contract start date and the pay period start date
    Nrerfr8r�rT)�valid�messager�)�employee_id__idr�z<ul class='errorlist'><li>The zV's                 contract start date is smaller than pay period start date</li></ul>rgFrfzq<ul class='errorlist'><li>The end date must be greater than                 or equal to the start date.</li></ul>zI<ul class="errorlist"><li>The end date cannot be in the future.</li></ul>)r�r��getlistrrbrrDr�r�r�rRr8�todayr)
r��end_datetime�start_datetimererfr8�
error_message�responser�r[rNrNrQ�validate_start_date�sD
���ro�payroll.view_payslipcCst|||�}t|d|�S)�K
    This method is used to render the template for viewing a payslip.
    z'payroll/payslip/individual_payslip.html)r�r)r�r8rerfr�rNrNrQ�view_individual_payslip�s�rrcCs�|j�d�rtj��}ntjj|jd�}t��}t|j	|�}|j
}t��}|j	�d�}|tj
��vr;|jdd��|�}t||j	�d��}|j	��}t|�}tt|�t|d|||t|j	�||tjd��S)	rqrp)�employee_id__employee_user_id�group_byF��group_name__isnullr�z"payroll/payslip/view_payslips.html)rVr��
export_column�
export_filterr7r��	gp_fields)r$�has_permrGr��allr�r��PayslipExportColumnFormr2r�r�rPr��__dict__�keys�order_byr9r�rrrr3�fields)r�rVrw�filter_formr7r�r�r�rNrNrQ�view_payslip�s4

��r�cCsD|j��}|j�d�rt|j�j}n|j��}tjj	|jj
d���}|j
}t|�|d<t|�j}d}|j�
d�}|dkrGd}|j	dd	��d
�}t||d�}g}|j�
d�s^t|�}tt|�d
|vrs|d
}	t|	�dkrs|	dg|d
<|j�
d�}
|
dkr�|
dur�t||
|j�
d�d�}d}n	t||j�
d��}t|||||d��S)zX
    Filter and retrieve a list of payslips based on the provided query parameters.
    rp)rTr8z"payroll/payslip/payslip_table.htmlr��cardz#payroll/payslip/group_payslips.htmlFruz-group_namer!�	dashboardr=�rr�rNr�zpayroll/payslip/group_by.html)rVr�r�)r�r�r$rzr2r�r�r#r�r�r�r�rr�rr!rrrG�lenr)r9r)r�r�rV�emp_requestrdr8r�r�r��status_listr�rNrNrQ�filter_payslip�sF



��r�zpayroll.change_payslipcCsL|j�d�rt|dt��t|j�d��Std�td�td�td�d�}g}i}t|j�j}t	�
��d	�}d
|�d�}|j�d�}t��}|s^|j
dj}|j�d
�}	t�|	�}
tjj|
d�}tjD]}|d}|d}
||vrv|�||
f�qa|D]s\}}|�d�}g||<|D]c}|}|D]}t||d�}|dur�nq�|dur�t|�nd}|dkr�|�|d�}t|�t	kr�|jj��}t�t|�d	��	�}t� �D]
\}}||kr�|�|�}q�n
|dur�t|�nd}||�|�q�qyt!j"|d�}t#dd�}d|�d�|d<t!j$|dd�}|j%�&dd��j'|dd d!�|j(d }|�)d"d#�|�*�|S)$z|
    This view exports payslip data based on selected fields and filters,
    and generates an Excel file for download.
    r5�*payroll/payslip/payslip_export_filter.html)rwrx�Draft�Review Ongoing�	Confirmed�Paid�r<�review_ongoing�	confirmed�paidr��Payslip_excel_�.xlsx�selected_fields�ids��id__inrr��__Nr�Status)rY�Aapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet��content_typezattachment; filename="�"�Content-Disposition�
xlsxwriter)�enginecSsdS)Nztext-align: centerrN��xrNrNrQ�<lambda>esz payslip_export.<locals>.<lambda>F�Sheet1)�index�
sheet_namezA:Z�)+rr�rr�r|r2r�r�r�rrjr�rir��initialr�r�rGr�r��
excel_columnsr�r�getattrrr�r$r%�get_date_formatrrbr*�itemsr��	DataFramer�ExcelWriter�style�applymap�to_excel�sheets�
set_columnrG)r��choices_mapping�selected_columns�
payslips_datarV�
today_date�	file_namer�r�r��id_listr�r��key�column_value�column_name�nested_attributesr;�attrrY�date_formatre�format_name�
format_string�
data_framern�writer�	worksheetrNrNrQ�payslip_exports���
�

�
�
�����
r�cCst��}t|dd|i�S)z;
    This method is used to render htmx allowance form
    zpayroll/htmx/form.htmlr�)r�r�rr�rNrNrQ�hx_create_allowancensr�c	Cs�t�}|j�d�}|j�d�}tjj|d�}t|dd�r!t|j	�s1t
�|d�|r-td�St
t�Stdd	��}|D]}|j}|||d
<||d�|�||dd
7<q9t|||d�}|��t
�|d�|rotd�St
t�S)z
    Send payslip method
    r�r�r��$dynamic_from_email_with_display_nameNzEmail server is not configured�)<script>window.location.reload()</script>cSsdgdd�S)Nr)r8rWrUrNrNrNrNrQr��szsend_slip.<locals>.<lambda>r8rWrUr�)�result_dictr�zMail processing)rr�r�rirGr�r�r�r�r�rrrrr�rr8r�rK�start�info)	r��
email_backendr��payslip_idsrVr�r;r8�mail_threadrNrNrQ�	send_slipxs6���r�c	Csj|jd}|j�d�}|dkr#|r#tjj|d�}tj||jd�d�}ntjd|id�}|jdkr�tj|j	d|id�}t
jj|dd	���}t
jj|d���}|��r�|��t�|td
��|dkr�|r�|r�|j|jkr�tdd�}|�|j|j|jd
��|��t||�tjj|j|j|jd
���}td|j�d��St�|td��|��td�St|d|||d��S)Nr8rE�Noner�)r8r�r�r�r�r:zBonus AddedT��mutabler^�4<script>window.location.href='/payroll/view-payslip/�
'</script>z;No active contract found for  {} during this payslip periodr�zpayroll/bonus/form.html�r�r8rE)r�r�rGr�r��PayslipAllowanceFormre�	BonusFormr�r�rDr�r�r#r�r�rr�r�rRrr r8rfrrerr��warning�formatr)	r�r8rEr�r�r[rdrdr;rNrNrQ�	add_bonus�sj

�
��
��
�����
�r�cCs|jd}|j�d�}tjj|d�}|jdkrttj|j||jd�d�}|�	�rs|j
dd�}d	|_|�
�|j�
|g�d|_|�
�td	d
�}|�|j|j|jd��|��t||�tjj|j|j|jd���}td|j�d
��Sn
tj||jd�d�}t|d|||d��S)Nr8rEr�r�)r8�
one_time_dater�F��commitTr�r^r�r�z%payroll/deduction/payslip_deduct.htmlr�)r�r�rGr�r�r��PayslipDeductionFormr�rer�r�r�r��setr�rr r8rfrrer�r�rr�r)r�r8rEr�r��deduction_instancerdr;rNrNrQ�
add_deduction�sV


�
��
���� 
�
�r�zpayroll.view_loanaccountc	Cs�tj��}|jdd�}|jdd�}|jdd�}t�t|jddd���}t�t|jddd���}t�t|jddd���}t||d�}t||d�}t||d�}t	�}t
|d	t||j�
d
��t||j�
d��t||j�
d��|||t||j�
d
��|d��S)zO
    This method is used to render template to disply all the loan records
    �loan�r��advanced_salary�finer�T��flatr!zpayroll/loan/view_loan.htmlr��lpage�apage�fpage)�recordsr��
adv_salary�fine_ids�loan_ids�adv_salary_idsr�r�)rFr�r{r�r�r�r�r�r!r1rr9r�r�)	r�r�r�r�r�r�r�r��filter_instancerNrNrQ�
view_loanss0
��r�cCs�tt|j�d���}tjj|d���}tj	|d�}|j
dkr8tj	|j|d�}|��r8|�
�t�|d�td�St|d||d��S)	zD
    This method is used to create and update the loan instance
    �instance_idr�r�r�zLoan created/updatedr�zpayroll/loan/form.html)r�r�)rrr�r�rFr�r�r�r��LoanAccountFormr�r�r�r�rr�rr�r�r�r�r�rNrNrQ�create_loan.s
�r�c	Csf|jd}tjj|d�}|j��}|j�d�}|r't�|�}t|t	|��\}}t
|d|||||d��S)z
    View install ments
    �loan_idr�r��payroll/loan/installments.html)rsr�r�r�r�)r�rFr�r�r�r{r�r�rr!r)r�r�r�rs�requests_ids_json�requests_idsr�r�rNrNrQ�view_installmentsBs"


��r�zpayroll.delete_loanaccountcCsv|j�d�}tjj|d�}|D]'}|js0tjjt|jj	ddd��d��
�s0|��t�
|d�qt�|d�qtt�S)	z
    Delete loan
    r�r�r�Tr�)�installment_ids__inzLoan account deletedzLoan account cannot be deleted)r�rirFr�r��settledrGr�r�r�r	rrr�rrr�)r�r��loansr�rNrNrQ�delete_loan]s ����rcCs�|j�d�}|j�d�}|j�d�rt|j�d��nd}tjj|d���}|j�	��
d�}|j|d���}|j|jd�}|j|jd�}|�t
d��d	pMd}	t|�dkrXt|�nd
}
|j|	||
}t|d�}|	||jkrv|j|	}d}|��s�||_|��|j|jd�D]	}||_|��q�t|�dkr�|dkr�t|j�}
t|||
�}|j�|�t�|d�nt�|d
�t|d|j�	�|d��S)Nr��ded_idrjrr�r�)�one_time_date__lt)�one_time_date__gt�amount__sumr�r�z'Installment amount updated successfullyz Cannot change paid installments r�)rsr�)r�r�r�r�rFr�r�r�r�r{rr��	aggregaterr��loan_amount�round�installment_paysliprjr�r rJ�addrr�rr)r�r�rr�r�rcr��deductions_before�deductions_after�	total_sum�balance_instalment�new_installmentrur�installmentrNrNrQ�edit_installment_amountwsD 



��rc
Cs.t|j�j}|jdd�}|jdd�}|jdd�}t�t|jddd���}t�t|jddd���}t�t|jddd���}t||d�}t||d�}t||d�}t	|j�
��}tt|�|j�
d	�}	d
}
|	dkrhd}
t||
t||j�
d
��t||j�
d��t||j�
d��t||j�
d��|||||j�
�d�	�S)z
    Search loan method
    r�r�r�r�r�Tr�r!r�zpayroll/loan/records_card.htmlr�zpayroll/loan/records_list.htmlr�r�r�r�)	r�r�r�r�r�r�r�r�r�)r1r�r�r�r�r�r�r�r!rr�rrFr�rr9)r�r�r�r�r�r�r�r�r�r�r�rNrNrQ�search_loan�s<
��rzpayroll.add_loanaccountcCs�t�d�rtddd�}|jd}|jd}|jj|d�}tjj|d�}t��}|j	dkrZt�|j
�}|��rZ|jdd�}||_
d	|_t��|_||_|��t�|td
��td�St|d|||d
��S)z
    Add asset fine method
    �asset��	app_label�model�asset_idr8r�r�Fr�r�zAsset fine addedzg<script>$('#assetFineModal').removeClass('oh-modal--show');$('#reloadMessagesButton').click();</script>zpayroll/asset_fine/form.html)r�rr8)r�is_installedr,r�r�r�r#r��
AssetFineFormr�r�r�r�r8r�rrj�
provided_daterrr�r�rr)r��Assetrr8rrdr�r�rNrNrQ�
asset_fine�s2




�
�r�payroll.view_reimbursementc

Cs�d}tj��r	d}|jrt|j�}ntddi�}t||jd�}|jdd�}|jdd�}|jd	d�}ddgi}|j�d
�}d}	t	||	t
||j�d��t
||j�d
��t
||j�d��t
||j�d��||j��|||d�	�S)zG
    This method is used to render template to view reimbursements
    FTr=�	requestedr�
reimbursementr��leave_encashment�bonus_encashmentr�z-payroll/reimbursement/view_reimbursement.htmlr��rpager��bpage)	�requests�reimbursements�leave_encashments�bonus_encashmentsr�r�r�r��reimbursement_exists)rHr�r	r�r4rr�r�r�rr9r�)
r�r'�
filter_objectr#r$r%r r�r�r�rNrNrQ�view_reimbursement�sB
�
����r)cCs�tt|j�d���}d}|rtjj|d���}tj	|d�}|j
dkr>tj	|j|j|d�}|�
�r>|��t�|d�td�St|dd	|i�S)
z5
    This method is used to create reimbursement
    r�Nr�r�r�zReimbursent saved successfullyr�zpayroll/reimbursement/form.htmlr�)rrr�r�rHr�r�r�r��ReimbursementFormr�r��FILESr�r�rr�rrr�rNrNrQ�create_reimbursements
r,c
Cs:t|j�j}t||d�}t|j���}|jdd�}|jdd�}|jdd�}t�t	|j
ddd���}t�t	|j
ddd���}t�t	|j
ddd���}t||d	�}t||d	�}t||d	�}|j�d
�}	d}
|	dkrid
}
t
t|�t||
t||j�d��t||j�d��t||j�d��t||j�d��||j��|||d�	�S)z<
    This method is used to search/filter reimbursement
    rrr�rr r�Tr�r!r�z(payroll/reimbursement/request_cards.htmlr�z-payroll/reimbursement/reimbursement_list.htmlr�r!r�r")	r#r$r%r&r�r��reimbursements_ids�leave_encashments_ids�bonus_encashment_ids)r4r�r�rrr�r�r�r�r�r�r!r�rrHrr9)r�r#r�r$r%r r-r.r/r�r�rNrNrQ�search_reimbursement3sN��
����r0cCsNt�d�rtddd�}|jj|jdddd��dd	d
d���}tt	|�dd
�S)zS
    This method is used to return assigned leaves of the employee
    in Json
    �leave�availableleaver�
employeeIdr�T)rh�total_leave_days__gte�leave_type_id__is_encashable�leave_type_id__name�available_days�carryforward_days�leave_type_id__idF)�safe)
rrr,r�r�r��values�distinctrr�)r��AvailableLeave�assigned_leavesrNrNrQ�get_assigned_leavesds$
����r?zpayroll.change_reimbursementcCsf|j�d�}|jd}|dkrd}|j�d�rt|j�d��nd}td|�}tjj|d�}|r�t|�r�|D]?}|j	dkr@||_
n|j	d	krH||_
|j}||_|�
�|jd
krft�|�jset�|td��q5t�|td|���d
���q5|dkr�tj|jj|jdddddtd�d|j��dd�	tt�Stj|jj|jdddddtd�d|j��dd�	tt�S)zL
    This method is used to approve or reject the reimbursement request
    r�r=�canceled�rejectedrjrr�rr rz#Please check the data you provided.zRequest z
 successfullyz-Your reimbursement request has been rejected.uAتم رفض طلب استرداد النفقات الخاص بك.z&Ihr Erstattungsantrag wurde abgelehnt.z,Su solicitud de reembolso ha sido rechazada.u0Votre demande de remboursement a été rejetée.zview-reimbursementz?id=�	checkmarkrHz-Your reimbursement request has been approved.uBتمت الموافقة على طلب استرداد نفقاتك.u+Ihr Rückerstattungsantrag wurde genehmigt.z)Se ha aprobado tu solicitud de reembolso.u2Votre demande de remboursement a été approuvée.)r�rir�r�maxrHr�r�r�r�rjr8r=r�r�get_messages�_queued_messagesr�r�r��get_status_displayr.rSr$r%rTrr�rr))r�r�r=rjr$r�emprNrNrQ�approve_reimbursements�sd
�



�����rHzpayroll.delete_reimbursementcCsh|j�d�}tjj|d�}|D]}|jj}q|��t�	|d�t
j|jj
|dddddd	d
d�	tt�S)z:
    This method is used to delete the reimbursements
    r�r�zReimbursements deletedz,Your reimbursement request has been deleted.u/تم حذف طلب استرداد نفقاتك.u+Ihr Rückerstattungsantrag wurde gelöscht.z,Tu solicitud de reembolso ha sido eliminada.u2Votre demande de remboursement a été supprimée.r��trashrH)r�rirHr�r�r8rTrrr�r.rSr$r%rr))r�r�r$rr$rNrNrQ�delete_reimbursements�s$
�rJTcCsPtjj|d�}|j�d�}|rt�|�}t||�\}}||||d�}t|d|�S)zS
    This method is used to render the individual view of reimbursement object
    r�r�)rr�r�r�z3payroll/reimbursement/reimbursenent_individual.html)rHr�r�r�r�r�rr)r�r�rr�r�r�r�r�rNrNrQ�reimbursement_individual_view�s
��rKcCstjj|d�}t|dd|i�S)z[
    This method is used to render all the attachements under the reimbursement object
    r�z&payroll/reimbursement/attachments.htmlr)rHr�r�r)r�r�rrNrNrQ�reimbursement_attachments�s�rLcCs2|j�d�}tjj|d���t�|d�tt	�S)z8
    This mehtod is used to delete the attachements
    r�r�zAttachment deleted)
r�rirIr�r�rrr�rr))r��_reimbursement_idr�rNrNrQ�delete_attachments�srNc	Cs�|j�d�}g}|r�tjj|d�jddd�}g}|D]c}|dD]}|�d�r,|�|�q |dD]}|�d�r=|�|�q1|d	D]}|�d�rN|�|�qB|d
D]}|�d�r_|�|�qS|dD]}|�d�rp|�|�qd|dD]}|�|�quq|jd
d�d�dd�t|dd�d�D�}|�	�D]/\}}	|	dd}
t
dd�|	D��}t
dd�|	D��}||}
|dkr�|�||
|||
d��q�t|dd|i�S)z<
    This method is used to get the contribution report
    r8)rh�
pay_head_dataTr�r�r&rrwrxryr�cS�|dS�Nr&rNr�rNrNrQr�)�z)get_contribution_report.<locals>.<lambda>)r�cSsi|]	\}}|t|��qSrN)r�)rlr��grouprNrNrQ�
<dictcomp>*s��z+get_contribution_report.<locals>.<dictcomp>cSrPrQrNr�rNrNrQr�,rRr�titlecs��|]	}|�dd�VqdS)rjrN�r�rtrNrNrQrn1s�z*get_contribution_report.<locals>.<genexpr>csrV)�employer_contribution_amountrNrWrtrNrNrQrn2s�
�)r&rU�employee_contribution�employer_contribution�total_contributionz#payroll/dashboard/contribution.html�contribution_deductions)r�r�rGr�r�r�r��sortrr�r�r)r�r8r\�	pay_headsrc�headr��grouped_deductionsr&rSrUrYrZr[rNrNrQ�get_contribution_report	sr�

�

�

�

�

�������	�racCsFg}gd�}|D]}||��vr ||D]}d|vr|�|�qq|S)N)rr�rwrxryr�r&)r~r�)�pay_head�extracted_items�potential_lists�	list_namerurNrNrQ�all_deductionsGs	
�rfc%
std�td�td�td�d�}g}g}i}t|j�j}|j�d�}t��}tj�	�}t
j�	�}	|s7|jdj}tj
D]}
|
\}}||vrK|�||f�q:|dd�|jd	d	d
�D�7}|ddg7}|d
d�|	jd	d	d	d�D�7}|gd�7}dd�|jd	d	d
�D�}
dd�|	jd	d	d	d�D�}dddddddd�}|�|
�|�|�|�|�|D�]�}i}d}d}d}d}d}|jd}||7}|jd}t|j�}|r�|D]!�t�fdd�|D��s�|�ddur�dnd7}|�d7}q�|�r|D]#�t�fdd�|D���s|�ddu�r�dnd7}|�d7}q�|D]�\}}|�d�}|}|D]}t||d�}|du�r8n�q)|du�rCt|�nd}|dk�rP|�|d�}t|t��r{|jj��}t�t|�d���} t� �D]\}!}"|!|k�rx| �!|"�}�qjn|du�r�t|�nd}|�r�|D]�t�d �t|�k�r��d du�r�t"�d�nd}�q�|�r�|D]�t�d �t|�k�rʈd du�r�t"�d�nd}�q�|||<||v�r�z
||t"|�7<W�qt#�y�Y�qw�q||d!<||d"<||d#<||d$<||d%<|d!|7<|d"|7<|d#|7<|d$|7<|d%|7<|�|�q�i}#|D]\}$}||v�rG|||#|<�q6d&|#|<�q6|d!|#d!<|d"|#d"<|d#|#d#<|d$|#d$<d'|#d(<|�|#�||t$|jd	d	d
�j%d d	d)��t$|	jd	d	d	d�j%d d	d)��d*�S)+zd
    This view create the data for exporting payslip data based on selected fields and filters,
    r�r�r�r�r�r�cS�g|]}|j|jf�qSrN�rU�rlr�rNrNrQr�{�
��z0payslip_detailed_export_data.<locals>.<listcomp>T)�one_time_date__isnullr�)�other_allowances�Other Allowances)�total_allowances�Total AllowancescSrgrNrhrirNrNrQr��rj)rkr��update_compensation__isnull))r��Federal Tax)�other_deductions�Other Deductions)r��Total DeductionscS�i|]}|jd�qS�rrh�rlr�rNrNrQrT����z0payslip_detailed_export_data.<locals>.<dictcomp>cSrurvrhrwrNrNrQrT�rxr)rmrsrortzNet Payz	Gross Payrqr�rpc3�(�|]\}}t�d�t|�kVqdS�rUN�r�rlrur�)rmrNrQrn��
��
�z/payslip_detailed_export_data.<locals>.<genexpr>rjNc3ryrzr{r|)r�rNrQrn�r}r�rr�r�rUrmrsrortrq�-�Totalr#r�)r�r�rprc)&r�r2r�r�rir�r|rCr�r{rEr�r�r�r�r�r rOrf�anyrr�rr�rarr$r%r�rrbr*r�r�r�r"r�r�)%r�r�r�r��totalsrVr�r�rprcr�r�r��allowance_totals�deduction_totals�other_totalsr;r��other_allowances_sum�other_deductions_sumrq�total_deduction�total_federal_taxr��allos�deductsr�r�r�r�rYr�rer�r��
totals_rowrurN)rmr�rQ�payslip_detailed_export_data]s`�


����������
�







��
��

�


��������
��

�������r�c$	Cs~|j�d�rt|dt��t|j�dd��St|�}|d}|d}|d}|d}t�	��
d	�}d
|�d�}ttdd
�tdd
�tdd
�tdd
�d�}ttdd
�d�}	t
�}
|
j}d|_dd�|D�}|ddg}
|gd�}t|�t|
�t|�}t|
�}t|�}d|ddf|d||ddf||d|||ddfg}d|||||g}|D]7\}}}}|jd|d|d�|jd||d�}t|dd�|_td d!�|_||_|t|�kr�||	|jd|d"�_q�t|�d#}d$|jd_d%|jd#_d$|j|_|d&|�tddd'�f|
tddd'�f|tddd'�fg}d}|D]%\}}|D]}|jd#|t|�d�}||_td d!�|_||_|d7}�q!�qt|d(�D]=\}} t|d�D]2\}}|j||| �|d)�d�}||k�rrtdd*d'�|_td+d!�|_n||v�r}tdd,�|_||_�qO�qFt|d�D]\}}!td-d.�|t|�D��}"|"d#|jt|�_�q�|d/|_ t!d0d1�}#d2|�d�|#d3<|
�"|#�|#S)4a
    Generate an Excel file for download containing detailed payslip data based on
    filters.

    Args:
        request (HttpRequest): The incoming HTTP request object.

    Returns:
        HttpResponse: A response object with the Excel file as an attachment.
    r5r�T)rwrx�reportr�r�rprcr�r�r��thin)r�)�left�right�top�bottom)r��PayslipscSsg|]\}}|�qSrNrN)rlr��col_namerNrNrQr�bsz+payslip_detailed_export.<locals>.<listcomp>rmro)rqrsrtr�zEmployee Details�0000FF�
Allowances�008000�
Deductions�FF0000)�	start_row�start_column�end_row�
end_column)�row�columnr�)�color�bold�center)�
horizontal)r�r�r��r�N)r�r��r�800080r�)r�css(�|]}|jdurtt|j��VqdSrM)r�r�r)rl�cellrNrNrQrn�s�
��z*payslip_detailed_export.<locals>.<genexpr>�B3r�r�zattachment; filename=r�)#rr�rr�r|r2r�r�rrjr�rrrr�rUr��merge_cellsr�r�fontr�	alignment�border�row_dimensions�heightr�	enumeraterCr�column_dimensions�width�freeze_panesrr�)$r��export_datar�r�rprcr�r��thin_border�right_border�wb�ws�
header_row�allowances_header�deductions_header�
basic_cols�allowance_cols�deduction_cols�merged_sections�	bold_cols�	start_col�end_colrUr�r��last_row�
subheaders�col_num�	subheaderr��header�row_numr�r��
max_lengthrnrNrNrQ�payslip_detailed_export5s���
�


��
������


�
�
�
r�rM)��__doc__r�r��collectionsrrr�	itertoolsr�urllib.parser�pandasr��django.appsr�django.contribr�django.core.exceptionsr	r
�django.db.modelsr�django.httprr
rr�django.shortcutsrr�django.urlsr�django.utils.htmlr�django.utils.translationrr��openpyxlr�openpyxl.stylesrrrr�openpyxl.utilsr�payroll.models.models�payroll�
base.backendsr�base.methodsrrrrr r!�base.modelsr"�employee.modelsr#r$�horilla.decoratorsr%r&r'r(�horilla.group_byr)�horilla.horilla_settingsr*�horilla.methodsr+r,r-�notifications.signalsr.�payroll.filtersr/r0r1r2r3r4�
payroll.formsr5r��payroll.methods.deductionsr6�payroll.methods.methodsr7r8r9r:�payroll.methods.payslip_calcr;r<r=r>r?r@rA�payroll.methods.tax_calcrBrCrDrErFrGrHrIrJ�payroll.threadings.mailrK�payroll.views.viewsrLrR�eq�nerUrVrWrX�containsr�r�r�r�r�r�r�r�rrrr)r*r+r4rZr]rerorrr�r�r�r�r�r�r�r�r�r�rrrrr)r,r0r?rHrJrKrLrNrarfr�r�rNrNrNrQ�<module>sv  $	(
�
v#3&L%],
",Q"75 /'*/
:



<
X

Youez - 2016 - github.com/yon3zu
LinuXploit