
    vg?>             	       D	   d Z ddlZddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlZddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZ ddlmZmZ ddlmZ ddl m!Z! ddl"m#Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.Z/ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZImJZJ ddlKmLZL ddlMmNZNmOZOmPZPmQZQmRZRmSZS ddlTmUZV ddlWmXZX ddlYmZZZm[Z[m\Z\m]Z]m^Z^ dd l_m`Z`maZambZbmcZcmdZdmeZemfZf dd!lgmhZh dd"l.miZimjZjmkZkmlZlmmZmmnZnmoZompZp dd#lqmrZr dd$lsmtZt d% Zuej                  ej                  ej                  ej                  ej                  ej                  ej                  eud&Z}d' Z~e@e?d(               Ze@ eBd)      d*               Ze@ eBd+      d,               Ze@e?d-               Ze@e? eBd+      d.                      Ze@ eBd/      d0               Ze@e? eBd1      d2                      Ze@ eBd3      d4               Ze@ eBd+      d5               Ze@e?d6               Ze@e? eBd+      d7                      Ze@ eBd8      d9               Ze@e? eBd:      dhd;                     Zdd<lmZmZ d= Ze@ eBd>      d?               Ze@e?d@               Ze@ eBd>      dhdA              Ze@ eBd>      dB               Ze@ eBdC      dD               Ze@dE        Ze@e?dF               Ze@ eBdG      dH               Ze@ eBd)      dI               Ze@ eBd>      dJ               Ze@ eBd)      dK               Ze@ eBd3      dL               Ze@ eBdM      dN               Ze@e?dO               Ze@ eBdM      dP               Ze@ eBdQ      dR               Ze@ eBdM      dS               Ze@e? eBdM      dT                      Ze@ eBdU      dV               Ze@dW        Ze@e?dX               Ze@e?dY               Ze@dZ        Ze@ eBd[      d\               Ze@ eBd]      d^               Ze@ eAd_end`      da               Ze@ eAd_end`      db               Ze@ eAd]end`      dc               Ze@ eBdC      dd               Zde Ze@df        Ze@ eBdG      dg               Zy)izf
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_net_pay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_payslipc                      y N )abs     1/var/www/horilla/payroll/views/component_views.pyreturn_nonerU   ]   s        )equalnotequalltgtlege	icontainsrangec           	      &   t        | ||      }|d   }|d   }|d   }|d   }|d   }|d   }	|d   }
t        | |d||      }|d   }|d	   }|j                  st        |      nd
}||z
  }| ||||
d}t	        d*i |}t        d |d   D              }||d<   ||d<   t        d*i |}|d   }|d	   }||d<   t        d*i |}t        d*i |}|d   |d   z  }t        d*i |}t        d*i |}t        d*i |}t        d |d   D              }t        d |d   D              }t        d |d   D              }t        d |d   D              }||z   |z   |z   |z   }||z
  }t        |||||||      }t        | |d||      }|d   }|d	   } t        ||d   fi |}!|!d   }"| D ]  }#|"j                  |#        ||!d   z
  }i 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$|j                  d#       d%}$|$j!                         }%| j"                  |%d<   |j                  d&      |%d'<   |j                  d&      |%d(<   t%        j&                  |%      }&|&|$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_daterb   day_dictc              3   &   K   | ]	  }|d      ywamountNrQ   ).0	allowances     rT   	<genexpr>z&payroll_calculation.<locals>.<genexpr>   s     X))H-X   
allowancestotal_allowance	gross_payinstallmentsc              3   &   K   | ]	  }|d      ywrn   rQ   rp   items     rT   rr   z&payroll_calculation.<locals>.<genexpr>   s     NT$x.Nrs   c              3   &   K   | ]	  }|d      ywrn   rQ   ry   s     rT   rr   z&payroll_calculation.<locals>.<genexpr>   s      !X!rs   pretax_deductionsc              3   &   K   | ]	  }|d      ywrn   rQ   ry   s     rT   rr   z&payroll_calculation.<locals>.<genexpr>   s      #X#rs   post_tax_deductionsc              3   &   K   | ]	  }|d      ywrn   rQ   ry   s     rT   rr   z&payroll_calculation.<locals>.<genexpr>   s      Xrs   tax_deductions)net_payrv   total_pretax_deductiontotal_post_tax_deductiontotal_tax_deductionsfederal_taxloss_of_pay_amountr   net_pay_deductionnet_pay_deductionsnet_deductionri   taxable_gross_paybasic_pay_deductionsgross_pay_deductionsnet_deductionstotal_deductionsz%b %d %Yz - )r   rj   rk   r^   %Y-%m-%drj   rk   	json_datarQ   )r:   r7   deduct_leave_from_basic_payfloatr=   sumr>   rA   r@   rC   rB   rD   r9   r?   appendstrftimecopyidjsondumps)'ri   rj   rk   basic_pay_detailsr`   ra   rb   rc   rd   re   working_days_detailsupdated_basic_pay_datar   r   kwargsrt   ru   updated_gross_pay_datarv   r   r|   r~   rw   r   r   r   r   r   r   r   r   updated_net_pay_dataupdate_net_pay_deductionsr   net_pay_deduction_list	deductionpayslip_datadata_to_jsonr   s'                                          rT   payroll_calculationr   m   s    1:xP ,H%o6M!+.I#M2K!+.I#M2K,\::)[*h ''<=I1,? #+"F"FkA  ..I  (F %.v.J Xz,?WXXO%F< /F0:6:&{3I1,?#F;3=f=6@@ 	.),?,OO  4=f=,6v6N*4V4KNZ5MNNO  !#45H#I!   # ##67L#M#    #12B#C 
 	
"	#
	 	 		  **G5!91-G 9'9j( ##89G 4\ B4/0 
 00DE. 1	%%i01*?;;GH 	Y 	Y	
 	./BC 	7 	j. 	Y 	{ 	 4 	 4 	./BC 	23HI 	.)9: 	0  	,!" 	{#$ # ''
34C8I8I*8U7VW+L.  $$&L'{{L!+!4!4Z!@L'00<L

<(I )L#/L rV   c           	      $   g }g }t         j                  j                  |      }|j                  j	                  d      j                         }|r|j                  nd}|rt        j                  j	                  |      t        j                  j	                  d      j                  |      z  t        j                  j	                  d	      j                  |      z  }|D ]  }d}	|j                  rt        |j                  j                  d
dd            }
|
j                  |j                  |j                  |j                   j#                         j%                  dd      f       |
D ]B  \  }}}t'        ||      }|+t)        j                  |      | t+        |      |            r@d}	 n |	s|j                  |        |D cg c]D  }t)        j                  |j,                        |j.                  dk(  r|nd|j0                        r|F }}t2        j                  j	                  |      t2        j                  j	                  d      j                  |      z  t2        j                  j	                  d	      j                  |      z  }|D ]  }d}	|j                  rt        |j                  j                  d
dd            }
|
j                  |j                  |j                  |j                   j#                         j%                  dd      f       |
D ]B  \  }}}t'        ||      }|+t)        j                  |      | t+        |      |            r@d}	 n |	s|j                  |        |r-t5        j6                  |D cg c]  }|j8                   c}      nd}|r-t5        j6                  |D cg c]  }|j8                   c}      nd}|||r|nd||r|nd||d}t;        | d|      S c c}w c c}w c c}w )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 _Frb   r   )active_contractsrb   rt   allowance_idsrh   deduction_idsri   z!tabs/allowance_deduction-tab.html)context)r$   objectsgetcontract_setfilterfirstwagerE   excluder   listother_conditionsvalues_listr   r   r   r   lowerreplacer,   operator_mappingtypeif_condition	if_choice	if_amountrG   r   r   r   r   )requestemp_idemployee_deductionsemployee_allowancesri   r   rb   rt   rq   
applicable
conditionsr   operatorr   valrh   r   instancer   r   r   s                        rT   allowances_deductions_tabr      s3    ##v#.H,,33H3MSSU)9 %%tI$$$A&&$&?GG"* H  &&&EMM"* N 	 	 $ 	6IJ++!..::g

 !!!!++!--/77SA /9 *E8U&x7C{*:*>*>x*HYT#Yu-+ &+
 #**95/	64 1
##I$:$:;&00K?	Q## 
 
 $$#+ %  &&#' ' gg12 &&)- ' gg12 	 $ 	6IJ++!..::g

 !!!!++!--/77SA /9 *E8U&x7C{*:*>*>x*HYT#Yu-+ &+
 #**95/	66  	

0CDHHKKDE   	

0CDHHKKDE  --@)d&-@)d&G '>PPC
^ E
 Es   A	PPPzpayroll.add_allowancec                 h   t        j                         }| j                  dk(  rt        j                  | j                        }|j	                         rR|j                          t        j                         }t        j                  | t        d             t        t              S t        | dd|i      S )zD
    This method is used to create allowance condition template
    POSTzAllowance created.payroll/common/form.htmlform)formsAllowanceFormmethodr   is_validsaver	   successr   r   view_allowancer   r   r   s     rT   create_allowancer   w  s      D~~""7<<0==?IIK&&(DWa(<&=>N++'5~FFrV   zpayroll.view_allowancec                 P   t         j                  j                  d      }t        | j                        }t        || j                  j                  d            }t        j                  |j                  D cg c]  }|j                   c}      }t        | d|||d      S c c}w )zQ
    This method is used render template to view all the allowance instances
    Tonly_show_under_employeepagez%payroll/allowance/view_allowance.html)rt   fr   )rE   r   r   r0   GETr;   r   r   r   object_listr   r   )r   rt   allowance_filterr   r   s        rT   r   r     s     ""**D*IJ&w{{3z7;;??6+BCJJJJ<R<RSSTM/$!*	
   T   8B#c                    t        |       }t        j                  |      }| j                  j	                  d      }d|i}|r3t        j                  |      }t        ||      \  }}||d<   ||d<   ||d<   ||d<   t        | d|      S )zV
    This method is used render template to view the selected allowance instances
    instances_idsrq   nextpreviousr   pdz,payroll/allowance/view_single_allowance.html)	r.   rE   findr   r   r   loadsr   r   )	r   allowance_idprevious_datarq   allowance_ids_jsonr   r   previous_idnext_ids	            rT   view_single_allowancer     s     "'*M|,I 9YG 

#56.}lKW!)
#0 !GDM6 rV   c           	         | j                   j                         }t        | j                         j                  j	                  d      }d}d}|}| j                   j                  d      dk(  r|}t        | |d      }t        || j                   j                  d            }t        j                  |j                  D cg c]  }|j                   c}      }t        |      }t        t        |       t        | |||||d	      S c c}w )
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   )rt   r   filter_dictr   )r   	urlencoder0   qsr   r   r"   r;   r   r   r   r   r   r    rE   r   )	r   query_stringrt   	list_view	card_viewtemplater   r   	data_dicts	            rT   filter_allowancer          ;;((*L -0088RV8WJ7I7IH{{v&(X6Jz7;;??6+BCJJJJ<R<RSSTM&Ii+$$*		
	 	  T   ?Dzpayroll.change_allowancec                    t         j                  j                  |      }t        j                  |      }| j
                  dk(  rot        j                  | j                  |      }|j                         r>|j                          t        j                  | t        d             t        t              S t        | dd|i      S )zb
    This method is used to update the allowance
    Args:
        id : allowance instance id
    r   r   r   zAllowance updated.r   r   )rE   r   r   r   r   r   r   r   r   r	   r   r   r   r   r   )r   r   r   r   r   s        rT   update_allowancer    s       $$$5H1D~~""7<<(C==?IIKWa(<&=>N++'5~FFrV   zpayroll.delete_allowancec                 T   t        |       }	 t        j                  j                  |      j	                         }|r0|j                          t        j                  | t        d             nt        j                  | t        d             | j                  j                  d      d   dk(  st        j                  j                         st        d	      S | j                   j#                  d
      }|rTt%        j&                  |      }t)        ||      \  }}||v r,|j+                  |       d| }	d| d| }
t-        |	|
z         S t-        d|       S # t        $ rI}t        j                  | t        d             t        j                  | t        |             Y d}~d}~ww xY w)z>
    This method is used to delete the allowance instance
    r   zAllowance deleted successfullyzAllowance not foundz.An error occurred while deleting the allowanceN/   zdelete-employee-allowance*<script>window.location.reload();</script>r   z/payroll/single-allowance-view/?&instances_ids=z/payroll/filter-allowance?)r.   rE   r   r   r   deleter	   r   r   error	Exceptionstrpathsplitexistsr   r   r   r   r   r   remover   )r   r   r   rq   er   instances_listprevious_instancenext_instanceurlparamss              rT   delete_allowancer    sr    "'*M	(%%,,,=CCE	Wa(H&IJNN7A&;$<= 	3"&AA  '')HIIKKOOO4MM2+:><+X(=>)!!,/3M?CC~6FGFC&L))0@AA)  (w"R STwA''(s   A?E 	F'>F""F'zpayroll.add_deductionc                 @   t        j                         }| j                  dk(  rmt        j                  | j                        }|j	                         r>|j                          t        j                  | t        d             t        t              S t        | dd|i      S )z1
    This method is used to create deduction
    r   zDeduction created.r   r   )r   DeductionFormr   r   r   r   r	   r   r   r   view_deductionr   r   s     rT   create_deductionr    sy      D~~""7<<0==?IIKWa(<&=>N++'5~FFrV   c                 P   t         j                  j                  d      }t        | j                        }t        || j                  j                  d            }t        j                  |j                  D cg c]  }|j                   c}      }t        | d|||d      S c c}w )zQ
    This method is used render template to view all the deduction instances
    Tr   r   z%payroll/deduction/view_deduction.html)rh   r   r   )rG   r   r   r1   r   r;   r   r   r   r   r   r   )r   rh   deduction_filterr   r   s        rT   r  r    s     ""**D*IJ&w{{3z7;;??6+BCJJJJ<R<RSSTM/$!*	
   Tr   c                 *   t        |       }t        j                  j                  |      j	                         }||d}| j
                  j                  d      }|r/t        j                  |      }t        ||      \  |d<   |d<   ||d<   | j                  j                  dd      }|j                  d	      j                  d	      }d
|v r|j                  d| dd       n|dd dt        |d         gk(  r%	 |j                  dt        |d          dd       n[|j#                  d      r6|j                  d| j$                  j&                  j(                   dd       n|j                  ddd       t+        | d|      S # t         $ r Y w xY w)zN
    Render template to view a single deduction instance with navigation.
    r   )r   r   r   r   r   r   HTTP_REFERER r  zview-deduction/payroll/filter-deduction?z#payroll-deduction-container)load_hx_urlload_hx_targetNzemployee-view#/payroll/allowances-deductions-tab/z#allowance_deductionzemployee-profile/z,payroll/deduction/view_single_deduction.html)r.   rG   r   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_partss	            rT   view_single_deductionr4  5  s    "'*M!!((L(9??AI%];G !9

#56/><0
,
WV_ $1  <<##NB7L '',2237M=(!;M?K"@	
 
rs	]25F1GH	H	NN%H][]M^I_H`#a&< 
		2	3!DW\\E^E^EaEaDbc"8	
 	ttDE'I7SS  		s   :#F 	FFc           	         | j                   j                         }t        | j                         j                  j	                  d      }d}d}|}| j                   j                  d      dk(  r|}t        | |d      }t        || j                   j                  d            }t        j                  |j                  D cg c]  }|j                   c}      }t        |      }t        t        |       t        | |||||d	      S c c}w )
z2
    This method is used search the deduction
    Tr   z%payroll/deduction/list_deduction.htmlz%payroll/deduction/card_deduction.htmlr   r   r"   r   )rh   r   r   r   )r   r   r1   r   r   r   r"   r;   r   r   r   r   r   r    rG   r   )	r   r   rh   r   r   r   r   r   r   s	            rT   filter_deductionr6  j  r   r  zpayroll.change_deductionc                    t         j                  j                  |      }t        j                  |      }| j
                  dk(  rot        j                  | j                  |      }|j                         r>|j                          t        j                  | t        d             t        t              S t        | dd|i      S )z>
    This method is used to update the deduction instance
    r   r  r   zDeduction updated.r   r   )rG   r   r   r   r  r   r   r   r   r	   r   r   r   r  r   )r   r1  r   r   r   s        rT   update_deductionr8    s       $$$5H1D~~""7<<(C==?IIKWa(<&=>N++'5~FFrV   zpayroll.delete_deductionc                 \   | j                   j                  d      }d }d }d}|r@t        |       }t        j                  |      }t        ||      \  }}|j                  |       t        j                  j                  |      j                         }|r0|j                          t        j                  | t        d             nt        j                  | t        d             d| j                   j!                          d| d| d	| d
| d}	| j"                  j                  d      }
|	j                  |
      }|
r;|
dk(  r)t        j                  j                         st%        d      S |rt'        |      S |
r| j(                  n| j"                  j                  dd      }t+        |      S )Nr   r"  r   zDeduction deleted successfullyzDeduction not foundr#  r(  z/payroll/single-deduction-view/r	  r
  )payroll-deduction-containerallowance_deductionobjectDetailsModalTargetHTTP_HX_TARGETr:  r  r!  r  )r   r   r.   r   r   r   r  rG   r   r   r   r  r	   r   r   r  r   r)  r   r   r  r   )r   r1  r   r   r  r  r   r  r   pathshttp_hx_targetredirected_pathdefault_redirects                rT   delete_deductionrB    s    KKOOO4MMNM%g.M2+:><+X(=l+!!((L(9??AI!$D"EFw"7 89 *DGKKDYDYD[C\']!DVHM&Em_TUVcUdds  uC  tD  %EE
 \\%%&67Nii/O;;%%,,. LMMO,,&GLL,<,<^S,Q    011rV   )r   	timedeltac                     g }t        dd      D ]T  }t        | |d      }|dk(  rt        | dd      }nt        | |dz   d      }|t        d      z
  }|j                  ||f       V |S )N            )days)r^   r   rC  r   )yearstart_end_datesmonthrj   rk   
next_months         rT   get_month_start_endrN    s}    Oq" 7$q)
 B;D"b)HdEAIq1J!I1$55H
H567 rV   zpayroll.add_payslipc                 N   | j                   j                  d      rA| j                   j                  d      dk(  r#t        j                         }t	        | dd|i      S g }g }t        j                         }| j
                  dk(  rt        j                  | j                        }|j                         rg }|j                  d   }|j                  d   }|j                  d	   }|j                  d
   }	|D ]j  }
t        j                  j                  |
d      j                         }||j                  k  r|j                  }t        |
||      }|j                  |       |j                  |d          ||d<   i }|
|d<   |	|d
<   |d   |d<   |d	   |d	<   d|d<   |d   |d<   |d   |d<   |d   |d<   |d   |d<   |d   |d<   t!        j"                  |d         |d<   t%        |       |d   |d<   t'        d(i |}|j                  |       t)        j*                  | j,                  j.                  |
j0                  dddddt3        dd |j4                  i!      d"#	       m t7        j8                  | |j;                          d$       t=        d%|	       S t	        | 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_idrj   rk   
group_namer   rS  r   r   payslipri   draftstatusra   rb   rv   r   r   r   pay_datarw   #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   rQ   )r)  r   r   GeneratePayslipFormr   r   r   r   cleaned_datarF   r   r   r   contract_start_dater   r   r   r   r8   r<   r/   sendr/  r0  employee_user_idr   r   r	   r   countr   )r   rR  payslipsr   r   	instances	employeesrj   rk   rT  ri   r`   rV  datar   s                  rT   generate_payslipru    s    	*+LL-.2KK--/	6)$
 	

 HI$$&D~~((6==?I))-8I**<8J((4H**<8J% '#++22 (( 3 %'   < <<!)!=!=J-h
HM(  !56%,	"#+Z %/\"%,\%:\"#*:#6Z !(X(/(@_%$+K$8[!$+K$8[!$+,>$?[!"))"4Y#'::gk.B#CZ /5'.~'>^$'/$/  *LL--&77>CHCL$.hkk7R !7'P W):(;;R&STI*V  '5~FFrV   c                    | j                   j                  d      }| j                   j                  d      }t        j                  j	                  |d      j                         }|r|t        |j                        k\  rt        d      S t        d      }t        d      }t        t        d||j                  |	            S )
z_
    Check if the employee's contract start date is after the provided payslip start date.
    rS  rj   r   rU  r"  zkWhen this payslip is run, the payslip start date will be updated to match the employee contract start date.zEmployee 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_contentrm  title_message)r   r   rF   r   r   r   r  rm  r   r   r   )r   rS  rj   r`   rx  rw  s         rT   check_contract_start_datery  &  s     ++//-0K.J&& ' eg  zS)E)E%FFBuM 34L & ( < <'	
 rV   c                    |r|| _         t        j                         }| j                  dk(  r>| j                   j	                  d      }t        | j                   j	                  d      t              r=t        j                  | j                   j	                  d      d      j                         n| j                   j	                  d      }|rr|rpt        j                  j                  |d      j                         }|r?||j                  k  r0| j                   j                         }|j                  |d<   || _         t        j                  | j                         }|j!                         r|j"                  d   }|j"                  d   }|j"                  d   }t$        j                  j                  |||      j                         }|j!                         r|j"                  d   }|j"                  d   }|j"                  d   }t'        |||      }	||	d	<   i }
||
d
<   |	d   |
d<   |	d   |
d<   | j(                  j	                  d      dn| j(                  d   |
d<   |	d   |
d<   |	d   |
d<   |	d   |
d<   |	d   |
d<   |	d   |
d<   t+        j,                  |	d         |
d<   t/        |
       |	d   |
d<   t1        d&i |
|	d<   t        j                         }t3        j4                  | t7        d             |	d   }t9        j:                  | j<                  j>                  |j@                  dddddtC        dd|jD                  i      d !	       tG        d"|	d   jH                   d#      S tK        | 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   rS  rj   r   r   rU  rk   rS  rj   rk   rV  ri   rX  rW  ra   rb   rv   r   r   r   r   rY  rw   r   zPayslip SavedrZ  r[  r\  r]  r^  r_  r`  ra  rb  rc  z6<script>window.location.href = "/payroll/view-payslip/z/"</script>z#payroll/payslip/create_payslip.htmlindividual_formrQ   )&r   r   PayslipFormr   r   
isinstancer  r   strptimer   rF   r   r   r   rm  r   r   rl  rI   r   r   r   r   r8   r<   r	   r   r   r/   rn  r/  r0  ro  r   pkr   r   r   )r   new_post_datar   rS  rj   r`   ri   rk   rV  r   rt  s              rT   create_payslipr  M  sw    $D~~ll&&}5 ',,**<8#> gll..|<jINNP!!,/ 	 :''..' / eg  J)E)EE ' 1 1 3.6.J.Jl+,  .==?((7H**<8J((4Hoo,,$h - eg  }},,];!..|<
,,Z828ZR*1Y'#+Z %1,%?\"#/
#;Z  {{x08  X. X
 )5_(E_%$0$=[!$0$=[!$01C$D[!".y"9Y#'::l;.G#HZ /5'3N'C^$+7+?$+?Z(((*  !O*<=&z2LL--&77>CHCL$.gjj7Q ! $L\ZdMeMhMhLiitu  -	D! rV   c                    d}d}| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|r$t        j                  |d      j                         }|r$t        j                  |d      j                         }d}d|d}|D ]^  }t        j                  j                  |d	
      j                         }	|5||	j                  k  sEd|	j                   d}||d<   d|d<   ` ||||kD  rd}||d<   d|d<   |1|t        j                         j                         kD  rd}||d<   d|d<   t        |      S )zW
    This method to validate the contract start date and the pay period start date
    Nrj   rk   rS  r   r"  T)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>r  Fr  zq<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   getlistr   r  r   rF   r   r   r   rm  rS  todayr   )
r   end_datetimestart_datetimerj   rk   rS  error_messageresponser   r`   s
             rT   validate_start_dater    s    LN.J{{z*H++%%m4K!**:zBGGI((:>CCEM-8H 	&##**"H + 

%' 	 %.8;W;W*W<X=Q=Q<R ST UM"/HY %HW	& 	"$\)7+!(..*//11gM"/HY %HW!!rV   payroll.view_payslipc                 6    t        |||      }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   rS  rj   rk   r   s        rT   view_individual_payslipr    s(     '{JIL1 rV   c                 6   | j                   j                  d      rt        j                  j	                         }n*t        j                  j                  | j                         }t        j                         }t        | j                  |      }|j                  }t        j                         }| j                  j                  d      }|t        j                  j                         v r!|j                  d      j                  |      }t!        || j                  j                  d            }| j                  j#                         }t%        |      }t'        t        |       t)        | d|||t        | j                        ||t*        j,                  d      S )	r  r  )employee_id__employee_user_idgroup_byFgroup_name__isnullr   z"payroll/payslip/view_payslips.html)rq  r   export_columnexport_filterrR  r   	gp_fields)r/  has_permrI   r   allr   r   PayslipExportColumnFormr3   r   r   rk  r   __dict__keysorder_byr;   r   r   r    r   r4   fields)r   rq  r  filter_formrR  r   r   r   s           rT   view_payslipr    s8   
 ||34??&&(??)))U113MX6K~~H))+IKKOOJ'E  %%''??e?<EEeLXw{{v'>?HKK))+M'Igy), **7;;7"$'..	
 rV   c                    | j                   j                         }| j                  j                  d      r t	        | j                         j
                  }n| j                   j                         }t        j                  j                  | j                  j                        j                         }|j                  }t        |      |d<   t	        |      j
                  }d}| j                   j                  d      }|dk(  r#d}|j                  d	      j                  d
      }t        | |d      }g }| j                   j                  d      st!        |      }t#        t$        |       d|v r|d   }	t'        |	      dkD  r	|	d   g|d<   | j                   j                  d      }
|
dk7  r,|
*t)        ||
| j                   j                  d      d      }d}n%t+        || j                   j                  d            }t-        | ||||d      S )zX
    Filter and retrieve a list of payslips based on the provided query parameters.
    r  )ro  rS  z"payroll/payslip/payslip_table.htmlr   cardz#payroll/payslip/group_payslips.htmlFr  z-group_namer"   	dashboardrX  rE  r'  r   r"  r   zpayroll/payslip/group_by.html)rq  r   r   )r   r   r/  r  r3   r   r   r$   r   r   r   r   r  r   r  r"   r   r    rI   lenr*   r;   r   )r   r   rq  emp_requestri   rS  r   r   r   status_listr   s              rT   filter_payslipr    s    ;;((*L||34 -00kk&&(##**GLLOO*LRRTkk%(%5M" -003H;;??6"Dv~8??e?<EEmTgx2HI;;??;'\*	'9-9){a#.r?"3IhKKOOG$E{u($Xugkkoof6MvV2 7;;??6+BC $	
 rV   zpayroll.change_payslipc                    | j                   j                  d      r5t        | dt        j                         t        | j                        d      S t        d      t        d      t        d      t        d      d}g }i }t        | j                        j                  }t        j                         j                  d	      }d
| d}| j                  j                  d      }t        j                         }|si|j                  d   j                  }| j                  j                  d      }	t        j                   |	      }
t"        j$                  j'                  |
      }t        j(                  D ]$  }|d   }|d   }||v s|j+                  ||f       & |D ]  \  }}|j-                  d      }g ||<   |D ]  }|}|D ]  }t/        ||d      }| n |t1        |      nd}|dk(  r|j                  |d      }t3        |      t        k(  r| j4                  j6                  j9                         }t;        j<                  t1        |      d	      j                         }t?        j@                         D ]  \  }}||k(  s|j                  |      } n|t1        |      nd}||   j+                  |          tC        jD                  |      }tG        d      }d| d|d<   tC        jH                  |d      }|jJ                  jM                  d       jO                  |dd        |jP                  d   }|jS                  d!d"       |jU                          |S )#z|
    This view exports payslip data based on selected fields and filters,
    and generates an Excel file for download.
    rP  *payroll/payslip/payslip_export_filter.html)r  r  DraftReview Ongoing	ConfirmedPaidrW  review_ongoing	confirmedpaidr   Payslip_excel_.xlsxselected_fieldsidsid__inr   rE  __Nr"  Status)rt  Aapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetcontent_typezattachment; filename=""Content-Disposition
xlsxwriter)enginec                      y)Nztext-align: centerrQ   xs    rT   <lambda>z payslip_export.<locals>.<lambda>  s    rV   FSheet1)index
sheet_namezA:Z   )+r)  r   r   r   r  r3   r   r   r   r   r  r   r  r  initialr   r   rI   r   r   excel_columnsr   r  getattrr  r   r/  r0  get_date_formatr   r  r+   itemsr   	DataFramer   ExcelWriterstyleapplymapto_excelsheets
set_columnrb  )r   choices_mappingselected_columnspayslips_datarq  
today_date	file_namer  r   r  id_listr   r   keycolumn_valuecolumn_namenested_attributesrV  attrrt  date_formatrj   format_nameformat_string
data_framer  writer	worksheets                               rT   payslip_exportr  :  s8    ||)*8!&!>!>!@!.w{{!;
 	
 7,-{^&		O MW[[),,H&&z2J E2Ikk))*;<O((*D++&78@@kkooe$**S/??)))9$$ 2aAhO###UCL1	2 &6 4!k(..t4%'k" 	4GE) tT2= "'!23u:Dh&&**5"5E{d"%ll77GGI%..s5z:FKKM
2F2L2L2N B.K"k1)22=AB &+%6s5zB+&--d3'	440 =1JXH )?yk&KH"#^^H\:F<=FFe G  h'I#
LLNOrV   c                 H    t        j                         }t        | dd|i      S )z;
    This method is used to render htmx allowance form
    zpayroll/htmx/form.htmlr   )r   r   r   r   s     rT   hx_create_allowancer    s&      D'3fd^DDrV   c                    t               }| j                  j                  d      }| j                  j                  d      }t        j
                  j                  |      }t        |dd      rt        |j                        s2t        j                  | d       |rt        d      S t        t              S t        d       }|D ]=  }|j                   }|||   d	<   ||   d
   j#                  |       ||   dxx   dz  cc<   ? t%        | ||      }|j'                          t        j(                  | d       |rt        d      S t        t              S )z
    Send payslip method
    r   r   r  $dynamic_from_email_with_display_nameNzEmail server is not configured)<script>window.location.reload()</script>c                      d g ddS )Nr   )rS  rr  rp  rQ   rQ   rV   rT   r  zsend_slip.<locals>.<lambda>  s    2B rV   rS  rr  rp  rE  )result_dictr  zMail processing)r   r   r   r  rI   r   r   r  r  r  r	   r  r   r   r  r   rS  r   rM   startinfo)	r   email_backendr   payslip_idsrq  r  rV  rS  mail_threads	            rT   	send_slipr    s:    +,M;;??6"D++%%d+K%%[%9H=tCCDw @A KLLN++BK  /))2=K /K -44W=K )Q.)	/
 !k{SKMM',-GHH''rV   c                    | j                   d   }| j                   j                  d      }|dk7  rF|rDt        j                  j                  |      }t	        j
                  ||j                  d      }nt	        j                  d|i      }| j                  dk(  rt	        j                  | j                  d|i      }t        j                  j                  |d	      j                         }t        j                  j                  |      j                         }|j                         rN|j                          t!        j"                  | t%        d
             |dk7  r|r|r|j&                  j                  k  rt)        d      }|j+                  |j,                  |j                  |j.                  d       |j1                          t3        | |       t        j                  j                  |j,                  |j                  |j.                        j                         }t5        d|j6                   d      S t!        j8                  | t%        d      j;                  |             t5        d      S t=        | d|||d      S )NrS  r`  Noner   )rS  r   r  r   r   rU  zBonus AddedTmutabler{  4<script>window.location.href='/payroll/view-payslip/
'</script>z;No active contract found for  {} during this payslip periodr  zpayroll/bonus/form.htmlr   rS  r`  )r   r   rI   r   r   PayslipAllowanceFormrj   	BonusFormr   r   rF   r   r   r$   r   r   r	   r   r   rm  r   r+  rS  rk   r  r  r   r   warningformatr   )	r   rS  r`  r   r   r`   ri   r  rV  s	            rT   	add_bonusr    s3    ++m,K.JV
??&&*&5))$/9L9LM
 {'CD~~w||m[5QR##**#X + 

%' 	 ##**k*:@@B==?IIKWa&67V#
 < <@S@S S$-d$;M!((+3+?+?*2*=*=(0(9(9 OO%"7M:%oo44$,$8$8#+#6#6!)!2!2 5  eg	 
 (NwzzlZde  $$Y &*	   KLL!kL rV   c                    | j                   d   }| j                   j                  d      }t        j                  j                  |      }| j                  dk(  r]t        j                  | j                  ||j                  d      }|j                         rA|j                  d      }d	|_        |j                          |j                  j                  |g       d|_        |j                          t        d	
      }|j!                  |j"                  |j                  |j$                  d       |j'                          t)        | |       t        j                  j+                  |j"                  |j                  |j$                        j-                         }t/        d|j0                   d      S t        j                  ||j                  d      }t3        | d|||d      S )NrS  r`  r   r   )rS  one_time_dater  FcommitTr  r{  r  r  z%payroll/deduction/payslip_deduct.htmlr  )r   r   rI   r   r   r   PayslipDeductionFormr   rj   r   r   r   r   setr   r   r+  rS  rk   r  r  r   r   r   r   r   )r   rS  r`  r   r   deduction_instancer  rV  s           rT   add_deductionr
    s    ++m,K.J""j"1H~~))LL$/(BUBUV
 ==?!%%!8:>7##% 1155{mD:?7##% &d3M  #+#7#7"*"5"5 ( 1 1 OO7M2oo,,$00#..!** -  eg	 
  FwzzlR\] 
 ))$/(BUBUV
 /kL rV   zpayroll.view_loanaccountc                 j   t         j                  j                         }|j                  d      }|j                  d      }|j                  d      }t	        j
                  t        |j                  dd                  }t	        j
                  t        |j                  dd                  }t	        j
                  t        |j                  dd                  }t        | |d      }t        | |d      }t        | |d      }t               }t        | d	t        || j                  j                  d
            t        || j                  j                  d            t        || j                  j                  d            |||t        || j                  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"   zpayroll/loan/view_loan.htmlr   lpageapagefpage)recordsr  
adv_salaryfine_idsloan_idsadv_salary_idsr  r   )rH   r   r  r   r   r   r   r   r"   r2   r   r;   r   r   )	r   r  r  r  r  r  r  r  filter_instances	            rT   
view_loansr  +  sd    !!%%'G>>v>&D%67J>>v>&Dzz$t//4/@ABHzz$t//4/@ABHZZZ%;%;Dt%;%L MNN'4*DX6J'4*D')O%$Wgkkoof.EF!$(@A'
GKKOOG4LM  ,!$(@A 		
 rV   c                    t        t        | j                  j                  d                  }t        j
                  j                  |      j                         }t        j                  |      }| j                  dk(  rbt        j                  | j                  |      }|j                         r1|j                          t        j                  | d       t!        d      S t#        | d||d      S )	zD
    This method is used to create and update the loan instance
    instance_idr   r  r   zLoan created/updatedr  zpayroll/loan/form.html)r   r  )r   r  r   r   rH   r   r   r   r   LoanAccountFormr   r   r   r   r	   r   r   r   r   r  r   r   s       rT   create_loanr   M  s      GKKOOM$B CDK""))[)9??AH  (3D~~$$W\\HE==?IIKW&<= KLL)D+U rV   c           
      N   | j                   d   }t        j                  j                  |      }|j                  j                         }| j                   j                  d      }|r-t        j                  |      }t        |t        |            \  }}t        | d|||d      S )z
    View install ments
    loan_idr   r   payroll/loan/installments.html)rw   r  r   r   r   )r   rH   r   r   r   r  r   r   r   r,  r   )r   r"  r  rw   requests_ids_jsonrequests_idsr   r   s           rT   view_installmentsr&  a  s     kk)$G""g".D%%))+L8zz"34.|S\JW((.#	

 
rV   zpayroll.delete_loanaccountc           	         | j                   j                  d      }t        j                  j	                  |      }|D ]  }|j
                  syt        j                  j	                  t        |j                  j                  dd                  j                         s'|j                          t        j                  | d       t        j                  | d        t        t               S )	z
    Delete loan
    r  r  r   Tr  )installment_ids__inzLoan account deletedzLoan account cannot be deleted)r   r  rH   r   r   settledrI   r   r   r   r  r  r	   r   r  r   r  )r   r  loansr  s       rT   delete_loanr+  |  s     ++

e
$C&&c&2E  FOO**$(&&224d2C% +  fh	 KKMW&<=NN7$DEF JrV   c                 V   | j                   j                  d      }| j                   j                  d      }| j                  j                  d      r$t        | j                  j                  d            nd}t        j
                  j                  |      j                         }|j                  j                         j                  d      }|j                  |      j                         }|j                  |j                        }|j                  |j                        }|j                  t        d            d	   xs d}	t        |      dk7  rt        |      nd
}
|j                  |	z
  |z
  |
z  }t!        |d      }|	|z   |j                  kD  r|j                  |	z
  }d}|j#                         s||_        |j'                          |j                  |j                        D ]  }||_        |j'                           t        |      dk(  rB|dk7  r=t)        |j                        }t+        |||      }|j                  j-                  |       t/        j0                  | d       nt/        j2                  | d       t5        | d|j                  j                         |d      S )Nr"  ded_idro   r   r   r  )one_time_date__lt)one_time_date__gtamount__sumrE  r  z'Installment amount updated successfullyz Cannot change paid installments r#  )rw   r  )r   r   r   r   rH   r   r   r   r   r  r  r  	aggregater   r  loan_amountroundinstallment_payslipro   r   r!   rL   addr	   r   r  r   )r   r"  r-  r   r  rh   r   deductions_beforedeductions_after	total_sumbalance_instalmentnew_installmentrz   r   installments                  rT   edit_installment_amountr<    sV    kkooi(G[[__X&F181A1A(1KE',,""8,-QRE%%%1779D##'')22?CJ!!V!,224I"))I<S<S)T!((9;R;R(S!++CM:=INQI256F2G12L-.RS'')3e;?QQOOQ/O54+++  9,((* 	%%	8O8O%P 	D)DKIIK	  A%/Q*>+I,C,CDD,T?DIK"";/"KLw BC( ..224	
 rV   c                 8   t        | j                        j                  }|j                  d      }|j                  d      }|j                  d      }t	        j
                  t        |j                  dd                  }t	        j
                  t        |j                  dd                  }t	        j
                  t        |j                  dd                  }t        | |d      }t        | |d      }t        | |d      }t        | j                  j                               }t        t        |       | j                  j                  d	      }	d
}
|	dk(  rd}
t        | |
t        || j                  j                  d            t        || j                  j                  d            t        || j                  j                  d            t        || j                  j                  d            ||||| j                  j                         d	      S )z
    Search loan method
    r  r  r  r  r   Tr  r"   r   zpayroll/loan/records_card.htmlr   zpayroll/loan/records_list.htmlr   r  r  r  )	r  r  r  r  r  r  r  r   r   )r2   r   r   r   r   r   r   r   r"   r   r   r    rH   r   r   r;   )r   r  r  r  r  r  r  r  r   r   r   s              rT   search_loanr>    s     ,//G>>v>&D%67J>>v>&Dzz$t//4/@ABHzz$t//4/@ABHZZZ%;%;Dt%;%L MNN'4*DX6J'4*D..01Ik9-;;??6"D/Hv~3$Wgkkoof.EF!$(@A'
GKKOOG4LM!$(@A  ,$++'')
	
 rV   zpayroll.add_loanaccountc                    t        j                  d      rt        dd      }| j                  d   }| j                  d   }j                  j                  |      }t        j                  j                  |      }t        j                         }| j                  dk(  rt        j                  | j                        }|j                         rz|j                  d      }||_        d	|_        t        j                          |_        ||_        |j                          t'        j(                  | t+        d
             t-        d      S t/        | d|||d      S )z
    Add asset fine method
    asset	app_labelmodelasset_idrS  r   r   Fr  r  zAsset fine addedzg<script>$('#assetFineModal').removeClass('oh-modal--show');$('#reloadMessagesButton').click();</script>zpayroll/asset_fine/form.html)r   rD  rS  )r   is_installedr-   r   r   r   r$   r   AssetFineFormr   r   r   r   rS  r   r   r  provided_daterD  r	   r   r   r   r   )r   AssetrD  rS  r@  ri   r   r   s           rT   
asset_finerI    s$    !''I{{:&H++m,KMM*E##{#3H D~~""7<<0==?yyy.H#+H "HM%)ZZ\H" %HMMOWa(:&;<y  &8KH rV   c                    d}t         j                  j                         rd}| j                  rt	        | j                        }nt	        ddi      }t        | |j                  d      }|j                  d      }|j                  d      }|j                  d	      }ddgi}| j                  j                  d
      }d}	t        | |	t        || j                  j                  d            t        || j                  j                  d            t        || j                  j                  d            t        || j                  j                  d            || j                  j                         |||d	      S )zG
    This method is used to render template to view reimbursements
    FTrX  	requestedpayroll.view_reimbursementreimbursementr  leave_encashmentbonus_encashmentr   z-payroll/reimbursement/view_reimbursement.htmlr   rpager  bpage)	requestsreimbursementsleave_encashmentsbonus_encashmentsr   r   r   r   reimbursement_exists)rJ   r   r  r   r5   r   r   r   r   r   r;   r   )
r   rV  filter_objectrR  rS  rT  rO  r   r   r   s
             rT   view_reimbursementrX    sQ   
 !##%#{{+GKK8+X{,CD!!!#?H __/_:N -?@,>?K=)I;;??6"D>H%h0GH+NGKKOOG<TU!.!7;;??7#;" "/ '++//'":" ++'')$$8	
 rV   c                    t        t        | j                  j                  d                  }d}|r.t        j
                  j                  |      j                         }t        j                  |      }| j                  dk(  rmt        j                  | j                  | j                  |      }|j                         r1|j                          t        j                   | d       t#        d      S t%        | dd	|i      S )
z5
    This method is used to create reimbursement
    r  Nr   r  r   zReimbursent saved successfullyr  zpayroll/reimbursement/form.htmlr   )r   r  r   r   rJ   r   r   r   r   ReimbursementFormr   r   FILESr   r   r	   r   r   r   r  s       rT   create_reimbursementr\  =  s      GKKOOM$B CDKH ((//;/?EEG""H5D~~&&w||W]]XV==?IIKW&FG KLL'<vtnMMrV   c                 R   t        | j                        j                  }t        | |d      }t	        | j                  j                               }|j                  d      }|j                  d      }|j                  d      }t        j                  t        |j                  dd                  }t        j                  t        |j                  dd                  }t        j                  t        |j                  dd                  }t        | |d	      }t        | |d	      }t        | |d	      }| j                  j                  d
      }	d}
|	dk(  rd}
t        t        |       t        | |
t!        || j                  j                  d            t!        || j                  j                  d            t!        || j                  j                  d            t!        || j                  j                  d            || j                  j                         |||d	      S )z<
    This method is used to search/filter reimbursement
    rL  rM  r  rN  rO  r   Tr  r"   r   z(payroll/reimbursement/request_cards.htmlr   z-payroll/reimbursement/reimbursement_list.htmlr   rP  r  rQ  )	rR  rS  rT  rU  r   r   reimbursements_idsleave_encashments_idsbonus_encashment_ids)r5   r   r   r   r   r   r   r   r   r   r   r"   r   r    rJ   r   r;   )r   rR  r   rS  rT  rO  r^  r_  r`  r   r   s              rT   search_reimbursementra  Q  s    #7;;/22H!'85QRH..01I__/_:N -?@,>?D)C)CDt)C)T$UV JJ**4d*;<  ::))$T):; G^X>Nw(98Dg'7B;;??6"D9Hv~BmY/%h0GH+NGKKOOG<TU!.!7;;??7#;" "/ '++//'":" %++'')"4%:$8	
 rV   c                    t        j                  d      rt        dd      }j                  j	                  | j
                  d   dd      j                  dd	d
d      j                         }t        t        |      d      S )zS
    This method is used to return assigned leaves of the employee
    in Json
    leaveavailableleaverA  
employeeIdrE  T)r  total_leave_days__gteleave_type_id__is_encashableleave_type_id__nameavailable_dayscarryforward_daysleave_type_id__idF)safe)
r   rE  r-   r   r   r   valuesdistinctr   r   )r   AvailableLeaveassigned_leavess      rT   get_assigned_leavesrq    s     !0%5

 	%%#KK5"#)- 	& 	

 
!	

 
  _-E::rV   zpayroll.change_reimbursementc                    | j                   j                  d      }| j                   d   }|dk(  rd}| j                   j                  d      r$t        | j                   j                  d            nd}t	        d|      }t
        j                  j                  |      }|rt        |      r|D ]  }|j                  dk(  r||_
        n|j                  d	k(  r||_
        |j                  }||_        |j                          |j                  d
k(  r@t        j                  |       j                   rt        j"                  | t%        d             t        j&                  | t%        d|j)                          d              |dk(  rdt+        j,                  | j.                  j0                  j2                  dddddt5        d      dj6                   z   d	       t9        t:              S t+        j,                  | j.                  j0                  j2                  dddddt5        d      dj6                   z   d	       t9        t:              S )zL
    This method is used to approve or reject the reimbursement request
    r  rX  canceledrejectedro   r   r  rN  rO  rK  z#Please check the data you provided.zRequest z successfullyz-Your reimbursement request has been rejected.uA   تم رفض طلب استرداد النفقات الخاص بك.z&Ihr Erstattungsantrag wurde abgelehnt.z,Su solicitud de reembolso ha sido rechazada.u0   Votre demande de remboursement a été rejetée.zview-reimbursementz?id=	checkmarkrc  z-Your reimbursement request has been approved.uB   تمت الموافقة على طلب استرداد نفقاتك.u+   Ihr Rückerstattungsantrag wurde genehmigt.z)Se ha aprobado tu solicitud de reembolso.u2   Votre demande de remboursement a été approuvée.)r   r  r   r   maxrJ   r   r   r  r   ro   rS  rX  r   r	   get_messages_queued_messagesr  r   r   get_status_displayr/   rn  r/  r0  ro  r   r   r   rX  )r   r  rX  ro   rS  rM  emps          rT   approve_reimbursementsr{    s    ++

e
$C[["F4;KKOOH4Mgkkooh/0ST  F^F"**111=N#f++ 	M!!%77'-$##'99'-$++C#)M  ##{2 --g6GGMM'1-R+ST  !A!A!C DMRS	" ZKK))..D[@FJ !564@P@P?Q9RR 
. &'' KK))..D\ECL !564@P@P?Q9RR 
 &''rV   zpayroll.delete_reimbursementc                    | j                   j                  d      }t        j                  j	                  |      }|D ]  }|j
                  j                  } |j                          t        j                  | d       t        j                  | j                  j                  dddddd	d
	       t        t              S )z:
    This method is used to delete the reimbursements
    r  r  zReimbursements deletedz,Your reimbursement request has been deleted.u/   تم حذف طلب استرداد نفقاتك.u+   Ihr Rückerstattungsantrag wurde gelöscht.z,Tu solicitud de reembolso ha sido eliminada.u2   Votre demande de remboursement a été supprimée.r  trashrc  )r   r  rJ   r   r   rS  ro  r  r	   r   r/   rn  r/  r0  r   rX  )r   r  rS  rM  r/  s        rT   delete_reimbursementsr~    s     ++

e
$C"**111=N' :((99:W67
KK!!;A=>D
 &''rV   rL  Tc                     t         j                  j                  |      }| j                  j                  d      }|r$t	        j
                  |      }t        ||      \  }}||d}t        | d|      S )zS
    This method is used to render the individual view of reimbursement object
    r   r   )rM  r   r   r   z3payroll/reimbursement/reimbursenent_individual.html)rJ   r   r   r   r   r   r   r   )r   r  rM  r$  r%  r   r   r   s           rT   reimbursement_individual_viewr    s~     "))---=M8zz"34.|[IW&*	G = rV   c                 `    t         j                  j                  |      }t        | dd|i      S )z[
    This method is used to render all the attachements under the reimbursement object
    r   z&payroll/reimbursement/attachments.htmlrM  )rJ   r   r   r   )r   r  rM  s      rT   reimbursement_attachmentsr    s8     "))---=M0	-( rV   c                     | j                   j                  d      }t        j                  j	                  |      j                          t        j                  | d       t        t              S )z8
    This mehtod is used to delete the attachements
    r  r  zAttachment deleted)
r   r  rK   r   r   r  r	   r   r   rX  )r   _reimbursement_idr  s      rT   delete_attachmentsr    sU     ++

e
$C#++22#2>EEGW23&''rV   c           	          | j                   j                  d      }g }|rt        j                  j	                  |      j                  dd      }g }|D ]  }|d   D ]%  }|j                  d      s|j                  |       ' |d   D ]%  }|j                  d      s|j                  |       ' |d	   D ]%  }|j                  d      s|j                  |       ' |d
   D ]%  }|j                  d      s|j                  |       ' |d   D ]%  }|j                  d      s|j                  |       ' |d   D ]  }|j                  |         |j                  d        t        |d       D ci c]  \  }}|t        |       }	}}|	j                         D ]S  \  }
}|d   d   }t        d |D              }t        d |D              }||z   }|dkD  s=|j                  |
||||d       U t        | dd|i      S c c}}w )z<
    This method is used to get the contribution report
    rS  )r  pay_head_dataTr  r   r1  r   r|   r~   r   r   c                     | d   S Nr1  rQ   r  s    rT   r  z)get_contribution_report.<locals>.<lambda>G  s    a&7 rV   )r  c                     | d   S r  rQ   r  s    rT   r  z)get_contribution_report.<locals>.<lambda>J  s    .@Q rV   r   titlec              3   @   K   | ]  }|j                  d d        yw)ro   r   Nr   ry   s     rT   rr   z*get_contribution_report.<locals>.<genexpr>O  s     'P$1(='P   c              3   @   K   | ]  }|j                  d d        yw)employer_contribution_amountr   Nr  ry   s     rT   rr   z*get_contribution_report.<locals>.<genexpr>P  s!      (@D7;(r  )r1  r  employee_contributionemployer_contributiontotal_contributionz#payroll/dashboard/contribution.htmlcontribution_deductions)r   r   rI   r   r   r   r   sortr   r   r  r   r   )r   rS  r  	pay_headsrh   headr   r  groupgrouped_deductionsr1  r  r  r  r  s                  rT   get_contribution_reportr  '  sq    ++//-0K OO**;*GSS$ T 
	 
 	-D!"89 1	==0%%i01 ""89 1	==0%%i01 ""56 1	==0%%i01 ""78 1	==0%%i01 ""23 1	==0%%i01 ""23 -	!!),-!	-& 	78 &j6QR
U e
 

 $6#;#;#= 	L%!HW%E$''P%'P$P!$' (HM( %! "79N!N$q('..(4!&1F1F.@	" -	"$;< -
s   <H
c                     g }g d}|D ]5  }|| j                         v s| |   D ]  }d|v s|j                  |        7 |S )N)r   r   r|   r~   r   r   r1  )r  r   )pay_headextracted_itemspotential_lists	list_namerz   s        rT   all_deductionsr  e  s_    OO % 1	' + 1!T)#**4011 rV   c           
      Z  %& t        d      t        d      t        d      t        d      d}g }g }i }t        | j                        j                  }| j                  j	                  d      }t        j                         }t        j                  j                         }t        j                  j                         }	|s|j                  d   j                  }t
        j                  D ]  }
|
\  }}||v s|j                  ||f       ! ||j                  dd      D cg c]  }|j                   |j                   f c}z  }|d	d
gz  }||	j                  ddd      D cg c]  }|j                   |j                   f c}z  }|g dz  }|j                  dd      D ci c]  }|j                   d }}|	j                  ddd      D ci c]  }|j                   d }}dddddddd}|j#                  |       |j#                  |       |j#                  |       |D ]  }i }d}d}d}d}d}|j$                  d   }||z  }|j$                  d   }t'        |j$                        }|r2|D ]-  %t)        %fd|D              s|%d   %d   ndz  }|%d   z  }/ |r2|D ]-  &t)        &fd|D              s|&d   &d   ndz  }|&d   z  }/ |D ]  \  }}|j+                  d      }|}|D ]  }t-        ||d      }| n |t/        |      nd}|dk(  r|j1                  |d      }t3        |t4              r| j6                  j8                  j;                         }t=        j>                  t/        |      d      j5                         } tA        jB                         D ]  \  }!}"|!|k(  s| jE                  |"      } n|t/        |      nd}|r7|D ]2  %t/        %d         t/        |      k(  s%d   tG        %d         nd}4 |r7|D ]2  &t/        &d         t/        |      k(  s&d   tG        &d         nd}4 |||<   ||v sy	 ||xx   tG        |      z  cc<    ||d<   ||d<   ||d<   ||d<   ||d<   |dxx   |z  cc<   |dxx   |z  cc<   |dxx   |z  cc<   |dxx   |z  cc<   |dxx   |z  cc<   |j                  |        i }#|D ]  \  }$}||v r	||   |#|<   d|#|<    |d   |#d<   |d   |#d<   |d   |#d<   |d   |#d<   d |#d!<   |j                  |#       ||tK        |j                  dd      jM                  dd"            tK        |	j                  ddd      jM                  dd"            d#S c c}w c c}w c c}w c c}w # tH        $ r Y w xY w)$zd
    This view create the data for exporting payslip data based on selected fields and filters,
    r  r  r  r  r  r  T)one_time_date__isnullr   )other_allowancesOther Allowances)total_allowancesTotal Allowances)r  r   update_compensation__isnull))r   Federal Tax)other_deductionsOther Deductions)r   Total Deductionsr   )r  r  r  r  zNet Payz	Gross Payr  r   rt   c              3   X   K   | ]!  \  }}t        d          t        |      k(   # ywr  Nr  )rp   rz   r  rq   s      rT   rr   z/payslip_detailed_export_data.<locals>.<genexpr>  1      )k 	'*+s;/??   '*ro   Nc              3   X   K   | ]!  \  }}t        d          t        |      k(   # ywr  r  )rp   rz   r  r   s      rT   rr   z/payslip_detailed_export_data.<locals>.<genexpr>  r  r  r  r"  r  r   r  r  r  r  r  r  -Totalr$   r  )r  r  rt   rh   )'r   r3   r   r   r  r   r  rE   r   r  rG   r  r  r  r   r   r  r+  r  r  anyr  r  r  r   r~  r   r/  r0  r  r   r  r+   r  r   r   r-  r   r   )'r   r  r  r  totalsrq  r  r   rt   rh   r   r   r  r  allowance_totalsdeduction_totalsother_totalsrV  r   other_allowances_sumother_deductions_sumru   total_deductiontotal_federal_taxr   allosdeductsr  r  r  rt  r  rj   r  r  
totals_rowrz   rq   r   s'                                        @@rT   payslip_detailed_export_datar  {  s?    7,-{^&		O MFW[[),,Hkk))*;<O((*D""&&(J""&&(J++&78@@$$ 2
sO###UCL1	2 &&"& ' 
 
ekk"  00 
 &&"&%)(, ' 
 
ekk"     &,,"&%) - 
 	1  &,,"&%)(, - 
 	1  L MM"#
MM"#
MM, ^+  ++M:[(%%l3 !6!67" 7	 -=  )/8/B/N	(+TU(  9X#667 $ 7	 -=  )/8/B/N	(+TU(  9X#667 *: ,	%L+ , 2 24 8E) tT2= "'!23u:Dh&&**5"5%&%ll77GGI%..s5z:FKKM
2F2L2L2N B.K"k1)22=AB &+%6s5zB!& I9W-.#k2BB  )1= ")H"56!"  !( I9W-.#k2BB  )1= ")H"56!"  )-L%f$;'5;6'U,	\ ,@'(+?'(+:'(+:'(&1]#!"&::"!"&::"!"o5"!"o5"},\*}^+@ J- *k& &,[&9J{#&)J{#	* &,,>%?J!"%+,>%?J!"%+,>%?J!"%+,>%?J!"$Jz$ ',&*)-   k'k-	
 &*)-,0   k'k-
 OJ " s*   !V	#V#VVV	V*)V*c           
         | j                   j                  d      r6t        | dt        j                         t        | j                        dd      S t        |       }|d   }|d   }|d   }|d   }t        j                         j                  d	      }d
| d}t        t        d      t        d      t        d      t        d            }t        t        d            }	t               }
|
j                  }d|_        |D cg c]  \  }}|	 }}}|ddgz   }|g dz   }t!        |      t!        |      z
  t!        |      z
  }t!        |      }t!        |      }d|ddf|dz   ||z   ddf||z   dz   ||z   |z   ddfg}d||z   ||z   |z   g}|D ]  \  }}}}|j#                  d|d|       |j%                  d||      }t'        |d      |_        t+        d      |_        ||_        |t!        |      k  sj||	z   |j%                  d|       _         t!        |      d!z   }d"|j0                  d   _        d#|j0                  d!   _        d"|j0                  |   _        |d$| t'        dd%      f|t'        dd%      f|t'        dd%      fg}d}|D ]M  \  }}|D ]C  } |j%                  d!|t5        |             }||_        t+        d      |_        ||_        |dz  }E O t7        |d&      D ]  \  }!}"t7        |d      D ]n  \  }} |j%                  |!||"j                  | d'            }|!|k(  r$t'        dd(%      |_        t+        d)      |_        n||v rt'        d*      |_        ||_        p  t7        |d      D ]C  \  }}t9        d+ |t;        |         D              }#|#d!z   |j<                  t;        |         _        E |d,   |_         tC        d-.      }$d/| d|$d0<   |
jE                  |$       |$S c c}}w )1a  
    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.
    rP  r  T)r  r  reportr  r  rt   rh   r   r  r  thin)r  )leftrighttopbottom)r  Payslipsr  r  )r  r  r  rE  zEmployee 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  c              3   p   K   | ].  }|j                    t        t        |j                                0 y wrP   )r   r  r  )rp   cells     rT   rr   z*payslip_detailed_export.<locals>.<genexpr>  s/      
zz% DJJ 
s   46B3r  r  zattachment; filename=r  )#r)  r   r   r   r  r3   r   r  r   r  r   r   r   r   r   r  r  merge_cellsr  r   fontr   	alignmentborderrow_dimensionsheightr  	enumeraterv  r   column_dimensionswidthfreeze_panesr   r   )%r   export_datar  r  rt   rh   r  r  thin_borderright_borderwbwsr   col_name
header_rowallowances_headerdeductions_header
basic_colsallowance_colsdeduction_colsmerged_sections	bold_cols	start_colend_colr  r  r  last_row
subheaderscol_num	subheaderr  headerrow_numr   
max_lengthr  s%                                        rT   payslip_detailed_exportr  S  s    ||)*8!&!>!>!@!.w{{!;
 	
 /w7K0M"#56\*J\*J&&z2J E2I v&!	K 6 23L	B	BBH.>?{q((?J?"&8:L%MM" &  Z3'8#99C@Q<RRJ*+N*+N 
J*H5	an4lHM'!+'.8		
	O 	
^#^#n4I -< 
O(	7E5
iw 	 	
 ww1Yew<u40	"h7!c*o%4?,4NBGG'G*1
O =!A%H"$Ba"$Ba)+Bh& 
KZ	 $D"AB	Dd(;<	Dd(;<J G% 	4 	F77qF7DDDI&(;DN%DKqLG	 "+=!!< 
&(Q7 		&OGV77G<3C3CFB3O  D (" d(;	!*g!>I% dO	%DK		&
&  
A. P
 
,W56
 


 BLa.w78>P hBOXH )>i[&NH"#GGHOq @s   	O;rP   )__doc__r   r   collectionsr   r   r   	itertoolsr   urllib.parser   pandasr   django.appsr   django.contribr	   django.core.exceptionsr
   r   django.db.modelsr   django.httpr   r   r   r   django.shortcutsr   r   django.urlsr   django.utils.htmlr   django.utils.translationr   r   openpyxlr   openpyxl.stylesr   r   r   r   openpyxl.utilsr   payroll.models.modelspayrollbase.backendsr   base.methodsr   r   r   r    r!   r"   base.modelsr#   employee.modelsr$   r%   horilla.decoratorsr&   r'   r(   r)   horilla.group_byr*   horilla.horilla_settingsr+   horilla.methodsr,   r-   r.   notifications.signalsr/   payroll.filtersr0   r1   r2   r3   r4   r5   payroll.formsr6   r   payroll.methods.deductionsr7   payroll.methods.methodsr8   r9   r:   r;   r<   payroll.methods.payslip_calcr=   r>   r?   r@   rA   rB   rC   payroll.methods.tax_calcrD   rE   rF   rG   rH   rI   rJ   rK   rL   payroll.threadings.mailrM   payroll.views.viewsrN   rU   eqnerY   rZ   r[   r\   containsr   r   r   r   r   r   r   r  r  r  r  r4  r6  r8  rB  rC  rN  ru  ry  r  r  r  r  r  r  r  r  r  r
  r  r   r&  r+  r<  r>  rI  rX  r\  ra  rq  r{  r~  r  r  r  r  r  r  r  rQ   rV   rT   <module>r'     s     # #  !   # F   S S -  ) 6  9 9 ,  0    =  / 9 P P )  3 D    >	 	 	 3 4
 [[
++
++
++
++""	 Od sQ  sQl ,-G . G -. / &   0 -. /  8 /0G 1 G" /0 B 1   BF ,-G . G -. / ( 0T  0Tf -. /  8 /0G 1 G /0#2 1  #2L %" *+JG , JGZ "  "J *+Z , Zz *+)" , )"X +,
 - 
  D )  )X -.N / Nb ,-E . E *+( , (D ,-4 . 4n ,-2 . 2j /0 1 @   $ /0 1 2 12  3  0 /0, 1 ,^ /0$ 1  $N ./ 0 > ' 'T N  N$ ,  ,^ ; ;6 347( 5 7(t 34( 5 (2 -}dC D , -}dC	 D 	 /E( F ( +,9 - 9x, T Tn -.C / CrV   