
    vg5                        d Z ddlZddlZddl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 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 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) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d ed      fd ed      fgZ=dZ> G d de	j"                        Z G d d e	j"                        Z? G d! d"e?      Z@ G d# d$e?      ZAd% ZB G d& d'e      ZC G d( d)e      ZD G d* d+e      ZE G d, d-e)      ZF G d. d/e      ZG G d0 d1e      ZH G d2 d3e      ZIg d4ZJ G d5 d6e	j                        ZL G d7 d8e	j                        ZM G d9 d:e      ZN G d; d<e      ZO G d= d>e	j                        ZP G d? d@e	j                        ZQ G dA dBe(      ZR G dC dDe      ZS G dE dFe      ZT G dG dHe      ZU G dI dJe      ZV G dK dLe      ZW ej                  dM      r4dNdOlYmZZZm[Z[  G dP dQe      Z\ G dR dSe	j                        Z] G dT dUe      Z^yy)Vz
This module provides Horilla ModelForms for creating and managing leave-related data,
including leave type, leave request, leave allocation request, holidays and company leaves.
    N)datedatetime)Any)forms)apps)ValidationError)Q)	ModelForm)	TextInputrender_to_string)gettext_lazy)filtersubordinatesemployeemodelreload_queryset)CompanyLeavesHolidays)EmployeeFilter)MultipleFileField)Employee)horilla_middlewares)HorillaFormHorillaModelForm)HorillaMultiSelectField)HorillaMultiSelectWidget)calculate_requested_dayscompany_leave_dates_listget_leave_day_attendanceholiday_dates_listleave_requested_dates)AvailableLeaveLeaveAllocationRequestLeaveallocationrequestCommentLeaveRequestLeaverequestCommentLeaverequestFile	LeaveTypeRestrictLeaveyesYesnoNog     j@c                   "     e Zd ZdZ fdZ xZS )r
   z
    Customized ModelForm class with additional functionality for field customization
    based on the type of widget and setting initial values based on the current request.
    c                    t        |   |i | t        t        j                  dd      }t        | j                         | j                  j                         D ]  \  }}|j                  }t        |t        j                        rA|j                  j                  j                  ddi       t        j                         |_        nt        |t        j"                  t        j$                  t        j&                  f      r3|j                  j                  j                  d|j(                  d       t        |t        j*                  f      r)|j                  j                  j                  ddi       t        |t        j,                        r6|j                  j                  j                  d|j(                  dd	d
       nt        |t        j.                  t        j0                  f      s|j                  j                  j                  ddi        	 |j2                  j4                  | j                  d   _        	 |j2                  j4                  j6                  | j                  d   _        y#  Y 8xY w#  Y yxY w)a>  
        Initializes the ModelForm instance.

        This method customizes field attributes such as CSS classes and placeholders
        based on the type of widget. It also sets initial values for specific fields
        based on the current request, particularly for 'employee_id' and 'company_id' fields.
        requestNclassz oh-input oh-calendar-input w-100oh-input w-100r/   placeholderz/oh-select oh-select-2 select2-hidden-accessible   (   r/   r2   rowscolsoh-switch__checkboxemployee_id
company_id)super__init__getattrr   _thread_localsr   fieldsitemswidget
isinstancer   	DateInputattrsupdater   todayinitialNumberInput
EmailInputr   labelSelectTextareaCheckboxInputCheckboxSelectMultipleuseremployee_getget_companyselfargskwargsr.   
field_namefieldrA   	__class__s          /var/www/horilla/leave/forms.pyr<   zModelForm.__init__:   s    	$)&)-<<iN$!%!2!2!4  	LJ\\F&5??4""))74V*WX $

**E,<,<eooN "")).u{{K FU\\O4""))OP FU^^5""))!1',{{ ! "	 ''00 ""))74I*JKA 	LB	181J1JDKK&.	070I0I0U0UDKK%-			s    (I< 	2J <J J)__name__
__module____qualname____doc__r<   __classcell__rX   s   @rY   r
   r
   4   s    
4 4    r
   c                        e Zd Z fdZ xZS )ConditionFormc                    t        |   |i | t        t        j                  dd       }t        | j                         | j                  j                         D ]  \  }}|j                  }t        |t        j                  f      rd|j                  j                  d<   Ht        |t        j                        rA|j                  j                  j                  ddi       t        j                          |_        t        |t        j$                  t        j&                  t        j(                  f      r4|j                  j                  j                  d|j*                  d       t        |t        j,                        r6|j                  j                  j                  d|j*                  ddd	       `t        |t        j.                  t        j0                  f      s|j                  j                  j                  dd
i        	 |j2                  j4                  | j                  d   _        	 |j2                  j4                  j6                  | j                  d   _        y #  Y 8xY w#  Y y xY w)Nr.   zjwidth:100%; height:50px;border: 1px solid hsl(213deg,22%,84%);border-radius: 0rem;padding: 0.8rem 1.25rem;styler/   r0   r1   r3   r4   r5   r8   r9   r:   )r;   r<   r=   r   r>   r   r?   r@   rA   rB   r   rK   rD   rC   rE   r   rF   rG   rH   rI   r   rJ   rL   rM   rN   rO   rP   rQ   rR   s          rY   r<   zConditionForm.__init__r   s   $)&)-<<iN$!%!2!2!4  	LJ\\F&5<</2 A ""7+ FU__6""))74D*EF $

**E,<,<eooN "")).u{{K FU^^5""))!1',{{ ! "	 ''00 ""))74I*JKA 	LB	181J1JDKK&.	070I0I0U0UDKK%-			s   (I. ;2I5 .I25I9)rZ   r[   r\   r<   r^   r_   s   @rY   rb   rb   q   s    - -r`   rb   c            
            e Zd Z eej
                  j                          ededdd      d      Z	 G d d	      Z
 fd
Z fdZ fdZ xZS )LeaveTypeFormemployee-widget-filterfemployee_filters.htmlFfilter_route_namefilter_classfilter_instance_contex_namefilter_template_pathrequiredr   querysetrA   rJ   c                       e Zd ZeZdZdgZd ed      iZ e	ddd       e
j                          e
j                          e
j                  d	d
i      dZy)LeaveTypeForm.Meta__all__	is_activenameNamecolorheight:40px;typerd   rD   r{   r   )rx   	period_in
total_dayscarryforward_expire_dateN)rZ   r[   r\   r&   modelr?   exclude_labelsr   r   HiddenInputrC   widgets r`   rY   Metars      sh    -AfI
 g%OP***,+%++-(7vv>N(O	
r`   r   c                    t         |          }d| j                  v r| j                  d= d| j                  v r| j                  d= t        |d   dz        dz  |d<   |d   st        |d<   d|d<   d|d	<   d
|d<   d
|d<   |S )Nr9   exceed_daysr~   r3   limit_leaveTresetyearlyreset_based1reset_month	reset_dayr;   cleanerrorsroundLEAVE_MAX_LIMITrS   cleaned_datarX   s     rY   r   zLeaveTypeForm.clean   s    w}DKK'M*DKK'M*%*<+E+I%JQ%N\"M*)8L&$(L!*2L'*-L'(+L%r`   c                     t        |   |i |}| j                  j                  d      x}rN|D ]H  }t        j
                  j                  |      }t        |||j                        j                          J y y )Nr9   id)leave_type_idr9   available_days)	r;   savedatagetlistr   objectsgetr    r~   )rS   rT   rU   
leave_type	employeesr9   employeerX   s          rY   r   zLeaveTypeForm.save   s}    W\4262
		))-8898( #++//;/?", (#-#8#8 $& 9r`   c                 $    t        |   |i | y N)r;   r<   rS   rT   rU   rX   s      rY   r<   zLeaveTypeForm.__init__   s    $)&)r`   )rZ   r[   r\   r   r   r   allr   r   r9   r   r   r   r<   r^   r_   s   @rY   rf   rf      sY    )!!%%''6'(+!8
 
K
 
 	* *r`   rf   c                   F     e Zd Z fdZ G d d      Z fdZ fdZ xZS )UpdateLeaveTypeFormc                    t        t        | 
  |i | g }| j                  j                  j                         D ]W  \  }}||dk(  s|j                  d      r(t        j                  dd|      }|j                  |       |j                  |       Y t        | j                               D ]t  \  }}t        | j                  j                               |   |v s/d|j                  j                   j"                  d<   d|j                  j                   j"                  d<   v | j                  j$                  x}	r|	| j                  d<   y y )	N _idz_id$zwdisplay:none;width:100%; height:50px;border: 1px solid hsl(213deg,22%,84%);border-radius: 0rem;padding: 0.8rem 1.25rem;rd   Tzdata-hiddenr   )r;   r   r<   instance__dict__r@   endswithresubappend	enumeratevisible_fieldslistr?   keysrW   rA   rD   r   )rS   rT   rU   empty_fieldsrV   field_valueforeign_key_field_nameindexvisibleexpire_daterX   s             rY   r<   zUpdateLeaveTypeForm.__init__   s<   !414B6B'+}}'='='C'C'E 	0#J"kR&7&&u--/VVFB
-K* ''(>?##J/	0 ((;(;(=> 	ANE7DKK$$&'.,> N $$**73 =A$$**=9	A --@@@;@6ADKK23 Ar`   c                   `    e Zd ZeZdZg dZ eddd       ej                  ddi      d	Z
y
)UpdateLeaveTypeForm.Metart   )r}   r~   ru   rx   ry   rz   r|   r{   r   )rx   r   N)rZ   r[   r\   r&   r   r?   r   r   r   rC   r   r   r`   rY   r   r      s8    :g%OP(7vv>N(O
r`   r   c                     t         |          }d| j                  v r| j                  d= t        |d   dz        dz  |d<   |d   st        |d<   d|d<   d|d	<   d
|d<   d
|d<   |S )Nr   countr3   r   r~   Tr   r   r   r   r   r   r   r   s     rY   r   zUpdateLeaveTypeForm.clean   s    w}DKK'M* %l7&;a&? @1 DWM*)8L&$(L!*2L'*-L'(+L%r`   c                 $    t        |   |i |}y r   )r;   r   )rS   rT   rU   r   rX   s       rY   r   zUpdateLeaveTypeForm.save  s    W\4262
r`   )rZ   r[   r\   r<   r   r   r   r^   r_   s   @rY   r   r      s"    B*
 
3 3r`   r   c                    t        | |      }t        j                  j                         }t	        |      }t
        j                  j                         }t        ||       |j                  dk(  rA|j                  dk(  r2t        t        |z               t        fd|D              }||z
  }|S d}	|j                  dk(  r|D ]  }
|
|v s|	dz  }	 ||	z
  }|j                  dk(  rt        fd|D              }||z
  }|S )Nr(   c              3   &   K   | ]  }|v  
 y wr   r   ).0requested_datetotal_leavess     rY   	<genexpr>z/cal_effective_requested_days.<locals>.<genexpr>  s       
/=Nl* 
   r      c              3   &   K   | ]  }|v  
 y wr   r   )r   r   company_leave_datess     rY   r   z/cal_effective_requested_days.<locals>.<genexpr>&  s      &" "55&r   )r   r   r   r   r   r   r   exclude_company_leaveexclude_holidayr   setsum)
start_dateend_dater   requested_daysrequested_datesholidaysholiday_datescompany_leavestotal_leave_countholiday_countr   company_leave_countr   r   s               @@rY   cal_effective_requested_daysr     s'   +JAO##%H&x0M"**..0N2>:N++u4))U2C0C CDE  
AP 
 
 (*;;  ((E1"1 '!]2!Q&M' ,m;N..%7"% &&5& # ,.AANr`   c                        e Zd Z ej                   ej
                  ddi            Z ej                   ej
                  ddi            Z fdZ fdZ	d Z
 G d d	      Z xZS )
LeaveRequestCreationFormr{   r   r|   rA   c                 0   t         |          }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        j                  j                  |||      j                  | j                  j                  	      }	|j                  d
k(  r"| t        j                  dt        d      i      ||k  st        j                  t        d            ||k(  r#||k7  rt        j                  t        d            t        j                  j                  ||      j                         st        j                  t        d            |	j                  ddg      j                         rt        j                  t        d            t        j                  j                  ||      }
|
j                  |
j                   z   }t#        ||||      }t%        ||||      }t'        ||      }|D cg c]#  }|j(                   d |j*                  d       % }}t-        j.                         }t1        t3        |            }|j4                   d|j(                   |v r/|j7                  |j+                  d       d|j(                          |
j9                  |      }|
j:                  j<                  dv r/|
j:                  j>                  |k  r|
j:                  j>                  n|}|
j:                  j<                  dk(  r|
j                   r||
j                   z
  }||z  }||k  st        j                  t        d            |S c c}w )Nr   r   r9   r   start_date_breakdownend_date_breakdown
attachmentr9   start_date__lteend_date__gter   r(   0An attachment is required for this leave request,End date should not be less than start date.DThere is a mismatch in the breakdown of the start date and end date.r9   r   Employee has no leave type..	cancelledrejected
status__in:Employee has already a leave request for this date range..r   r   r   r   -%mcarryforwardzcarryforward expireno carryforward)Employee doesn't have enough leave days..) r;   r   r   r#   r   filterr   r   r   require_attachmentr   r   r   r    existsr   carryforward_daysr   r   r   yearstrftimer   rF   r   r   monthremoveforcasted_leavesr   carryforward_typecarryforward_max)rS   r   r   r   r9   r   r   r   r   overlapping_requestsavailable_leavetotal_leave_daysr   effective_requested_daysleave_datesr   
month_yearrF   unique_datesforcated_daysrX   s                       rY   r   zLeaveRequestCreationForm.clean2  s   w}!%%l3
##J/"&&}5$((9+//0FG)--.BC!%%l3
+33::#XZ  ;  

'}}  
 	
 ++u4!++$aN'  X%''@A  !#'99++^ 
 %%,,#= - 

&( ''*H(IJJ''K3L'MTTV''NO  )0044#= 5 
 **_-N-NN 	 2"68J
 $@!')	$
  ,JAGRSt1]T]]4%8$9:S
S C
O,kk]!EJJ<(L85>>$#7"8%** FG'88D ,,>>67-->>AQQ )):: " 	 ));;?PP11//2S2SSM)'+;;''*U(VWW1 Ts   =(Nc                    t        |   |i | d| j                  d   j                  j                  d<   | j                  d   j                  j                  j                  ddddd	d
       | j                  d   j                  j                  j                  dddd       | j                  d   j                  j                  j                  ddddd	d
       y )N.jpg, .jpeg, .png, .pdfr   acceptr   z#leaveRequestCreateForm#availableLeaveCount	outerHTMLchange%/leave/employee-available-leave-count
hx-include	hx-targetzhx-swap
hx-triggerhx-getr9   #id_leave_type_id_parent_divz=/leave/get-employee-leave-types?form=LeaveRequestCreationFormr  r  r  r   r;   r<   r?   rA   rD   rE   r   s      rY   r<   z!LeaveRequestCreationForm.__init__  s    $)&);TL!((..x8O$++118873&&A	
 	M"))//66;&Y	
 	L!((..5573&&A	
r`   c                 &    d| i}t        d|      }|S S
        Render the form fields as HTML table rows with Bootstrap styling.
        formhorilla_form.htmlr   rS   rT   rU   context
table_htmls        rY   as_pzLeaveRequestCreationForm.as_p        4.%&97C
r`   c                       e Zd ZeZg dZy)LeaveRequestCreationForm.Metar9   r   r   r   r   r   r   descriptionNrZ   r[   r\   r#   r   r?   r   r`   rY   r   r$        	
r`   r   rZ   r[   r\   r   	DateFieldrC   r   r   r   r<   r!  r   r^   r_   s   @rY   r   r   .  s^     vv>N(OPJuoeooVV<L&MNHTl
<
 
r`   r   c                        e Zd Z ej                   ej
                  ddi            Z ej                   ej
                  ddi            Z fdZ fdZ	d Z
 G d d	      Z xZS )
LeaveRequestUpdationFormr{   r   r|   r   c                    t         |          }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        j                  j                  |||      j                  | j                  j                        }||k  st        j                  t        d	            ||k(  r#||k7  rt        j                  t        d
            t        j                  j                  ||      j                         st        j                  t        d            |j                  ddg      j                         rt        j                  t        d            t        j                  j                  ||      }	|	j                  |	j                  z   }
t!        ||||      }t#        ||||      }t%        ||      }|D cg c]#  }|j&                   d |j(                  d       % }}t+        j,                         }t/        t1        |            }|j2                   d|j&                   |v r/|j5                  |j)                  d       d|j&                          |	j7                  |      }|	j8                  j:                  dv r/|	j8                  j<                  |
k  r|	j8                  j<                  n|
}
|	j8                  j:                  dk(  r|	j                  r|
|	j                  z
  }
|
|z  }
||
k  st        j                  t        d            |S c c}w )Nr   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r;   r   r   r#   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   rF   r   r   r   r   r   r   r   r  )rS   r   r   r   r9   r   r   r   r  r  r  r   r  r  r   r  rF   r  r	  rX   s                      rY   r   zLeaveRequestUpdationForm.clean  sK   w}!%%l3
##J/"&&}5$((9+//0FG)--.BC+33::#XZ  ;  

'T]]%%'
& 	 X%''@A  !#'99++^ 
 %%,,#= - 

&( ''*H(IJJ''K3L'MTTV''NO  )0044#= 5 
 **_-N-NN 	 2"68J
 $@!')	$
  ,JAGRSt1]T]]4%8$9:S
S C
O,kk]!EJJ<(L85>>$#7"8%** FG'88D ,,>>67-->>AQQ )):: " 	 ));;?PP11//2S2SSM)'+;;''*U(VWW1 Ts   ;(Mc                    t        |   |i | | j                  d   j                  j                  j                  dddddd       | j                  d   j                  j                  j                  d	dd
d       d| j                  d   j                  j                  d<   | j                  d   j                  j                  j                  dddddd       y )Nr   z#leaveRequestUpdateFormz#assinedLeaveAvailableCountr  r  r  r  r9   r  z=/leave/get-employee-leave-types?form=LeaveRequestUpdationFormr  r  r   r  r   r  r   s      rY   r<   z!LeaveRequestUpdationForm.__init__  s    $)&)O$++11887:&&A	
 	M"))//66;&Y	
 <UL!((..x8L!((..557:&&A	
r`   c                 &    d| i}t        d|      }|S r  r   r  s        rY   r!  zLeaveRequestUpdationForm.as_p'  r"  r`   c                       e Zd ZeZg dZy)LeaveRequestUpdationForm.Meta)r   r9   r   r   r   r   r   r&  Nr'  r   r`   rY   r   r1  /  r(  r`   r   r)  r_   s   @rY   r,  r,    s^     vv>N(OPJuoeooVV<L&MNHFP
>
 
r`   r,  c                       e Zd ZdZ ej
                  ej                  j                         ej                  d      Z
 ej
                  ej                  j                         ej                  d      Z G d d      Zy)AvailableLeaveForma  
    Form for managing available leave data.

    This form allows users to manage available leave data by specifying details such as
    the leave type and employee.

    Attributes:
        - leave_type_id: A ModelChoiceField representing the leave type associated with the available leave.
        - employee_id: A ModelChoiceField representing the employee associated with the available leave.
    N)rq   rA   empty_labelc                       e Zd ZeZg dZy)AvailableLeaveForm.Meta)r   r9   ru   N)rZ   r[   r\   r    r   r?   r   r`   rY   r   r6  T  s    >r`   r   )rZ   r[   r\   r]   r   ModelChoiceFieldr&   r   r   SelectMultipler   r   r9   r   r   r`   rY   r3  r3  =  su    	 +E**""&&(##M
 )%((!!%%'##K? ?r`   r3  c            
            e Zd ZdZ eej                  j                          ede	ddd      d      Z
 G d	 d
      Z fdZ xZS )LeaveOneAssignForma=  
    Form for assigning available leave to employees.

    This form allows administrators to assign available leave to a single employee
    by specifying the employee and setting the is_active flag.

    Attributes:
        - employee_id: A HorillaMultiSelectField representing the employee to assign leave to.
    rg   rh   ri   Trj   r   rp   c                       e Zd ZdZeZddgZy)LeaveOneAssignForm.Meta3
        Meta class for additional options
        r9   ru   NrZ   r[   r\   r]   r    r   r?   r   r`   rY   r   r<  p  s    	 -r`   r   c                 N    t        |   |i | t        | j                         y r   )r;   r<   r   r?   r   s      rY   r<   zLeaveOneAssignForm.__init__x  s!    $)&)$r`   )rZ   r[   r\   r]   r   r   r   r   r   r   r9   r   r<   r^   r_   s   @rY   r:  r:  Y  sV     *!!%%''6'(+!8
 
K. .% %r`   r:  c                   $    e Zd ZdZ G d d      Zy)AvailableLeaveUpdateForma  
    Form for updating available leave data.

    This form allows users to update available leave data by modifying fields such as
    available_days, carryforward_days, and is_active.

    Attributes:
        - Meta: Inner class defining metadata options.
            - model: The model associated with the form (AvailableLeave).
            - fields: A list of fields to include in the form.
    c                       e Zd ZdZeZg dZy)AvailableLeaveUpdateForm.Metar=  )r   r   ru   Nr>  r   r`   rY   r   rC    s    	 Er`   r   NrZ   r[   r\   r]   r   r   r`   rY   rA  rA  }  s    
F Fr`   rA  c                   $    e Zd ZdZ G d d      Zy)CompanyLeaveForma  
    Form for managing company leave data.

    This form allows users to manage company leave data by including all fields from
    the CompanyLeaves model except for is_active.

    Attributes:
        - Meta: Inner class defining metadata options.
            - model: The model associated with the form (CompanyLeaves).
            - fields: A special value indicating all fields should be included in the form.
            - exclude: A list of fields to exclude from the form (is_active).
    c                       e Zd ZdZeZdZdgZy)CompanyLeaveForm.Metar=  rt   ru   N)rZ   r[   r\   r]   r   r   r?   r   r   r`   rY   r   rH    s    	 -r`   r   NrD  r   r`   rY   rF  rF    s       r`   rF  c                   "    e Zd Z ej                   ej
                  ddi            Z ej                   ej
                  ddi            Z ej                   e	d      ej                        Z fdZ fdZd	 Z G d
 d      Z xZS )UserLeaveRequestFormr{   r   r|   r   DescriptionrJ   rA   c                    t         |          }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        j                  j                  |||      j                  | j                  j                        }t        j                  j                  |	      }	|	st        j                  t        d
            |j                  dk(  r3|j                  d      }
|
 t        j                  dt        d      i      ||k(  r#||k7  rt        j                  t        d            ||k  st        j                  t        d            |j                  ddg      j                         rt        j                  t        d            t        ||||      }t        j                  j                  ||      }|j                   |j"                  z   }t%        ||||      }||k  st        j                  t        d            |S )Nr   r   r9   r   r   r   r   r   )r9   z6You dont have enough leave days to update the request.r(   r   r   r   r   r   r   r   z=Employee has already a leave request for this date range.....r   r   r   )r;   r   r   r#   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   )rS   r   r   r   r9   r   r   r   r  assigned_leave_typesr   r   r  r  r  rX   s                  rY   r   zUserLeaveRequestForm.clean  s]   w}!%%l3
##J/"&&}5+//0FG)--.BC$((9+33::#XZ  ;  

'T]]%%'
& 	  .55<<<U#''JK  ++u4%)),7J!++$aN'  !#'99++^ 
 X%''@A   ''K3L'MTTV''QR  2"68J
 )0044#= 5 
 **_-N-NN 	 $@!')	$
  (+;;''*U(VWWr`   c                 p   |j                  dd       }|j                  dd       }t        t        |   |i | d| j                  d   j
                  j                  d<   |r_|j                  j                         }t        j                  j                  |j                  dd      	      }|| j                  d   _        |rut        j                  j                  |d   j                  
      | j                  d   _        |d   j                  | j                  d   _        d | j                  d   _        y y )NrG   r   r  r   r  r   Tflatid__inr   )popr;   rJ  r<   r?   rA   rD   r  r   r&   r   r   values_listrq   r   rG   r4  )rS   rT   rU   r   r   available_leavesrN  rX   s          rY   r<   zUserLeaveRequestForm.__init__  s   ZZ	40
::j$/"D2DCFC;TL!((..x8'77;;=#,#4#4#;#;'33O$3O $< $  5IDKK(14=4E4E4L4Lo.11 5M 5DKK(1 4>o3N3Q3QDKK(07;DKK(4 r`   c                 &    d| i}t        d|      }|S r  r   r  s        rY   r!  zUserLeaveRequestForm.as_p  r"  r`   c                   B    e Zd ZdZeZg dZd ej                         iZ	y)UserLeaveRequestForm.Metar=  r%  r9   N
rZ   r[   r\   r]   r#   r   r?   r   r   r   r   r`   rY   r   rY    s/    	 	
 ,5,,.
r`   r   )rZ   r[   r\   r   r*  rC   r   r   	CharFieldr   rL   r&  r   r<   r!  r   r^   r_   s   @rY   rJ  rJ    sx     vv>N(OPJuoeooVV<L&MNH!%//-(8PK:x<$
 
r`   rJ  )r   approved_available_daysapproved_carryforward_days
created_atr   c            
       *   e Zd ZdZej
                  j                         ZeD  cg c]8  }t        |d      r*|j                  t        vr|j                  |j                  f: c}}}} Z ej                  eej                  g d      Zyc c}}}} w )AvailableLeaveColumnExportForma  
    Form for selecting columns to export in available leave data.

    This form allows users to select specific columns from the AvailableLeave model
    for export. The available columns are dynamically generated based on the
    model's meta information, excluding specified excluded_fields.

    Attributes:
        - model_fields: A list of fields in the AvailableLeave model.
        - field_choices: A list of field choices for the form, consisting of field names
          and their verbose names, excluding specified excluded_fields.
        - selected_fields: A MultipleChoiceField representing the selected columns
          to be exported.
    verbose_name)r9   r   r   r   r  choicesrA   rG   N)rZ   r[   r\   r]   r    _meta
get_fieldsmodel_fieldshasattrrv   excluded_fieldsra  field_choicesr   MultipleChoiceFieldrN   selected_fieldsr   rW   rg  rh  s   0000rY   r`  r`  $  s     "''224L " 5.)ejj.O 
U''(M
 0e//++

O   =B
r`  c                       e Zd ZdZ ej
                   ed       ej                  ddd            Z G d d	      Z	y
)
RejectFormz
    Form for rejecting a leave request.

    This form allows administrators to provide a rejection reason when rejecting
    a leave request.

    Attributes:
        - reason: A CharField representing the reason for rejecting the leave request.
    Rejection Reason   p-4 oh-input w-100r6   r/   r|   rL  c                       e Zd ZdZeZdgZy)RejectForm.Metar=  reject_reasonN)rZ   r[   r\   r]   r#   r   r?   r   r`   rY   r   ru  W  s    	 !"r`   r   N
rZ   r[   r\   r]   r   r[  r   rL   reasonr   r   r`   rY   ro  ro  G  s@     U__"#u~~Q9M$NOF
# #r`   ro  c                        e Zd Z ej                   ej
                  ddi            Z ej                   ej
                  ddi            Zd Z fdZ	 fdZ
 G d d	      Z xZS )
UserLeaveRequestCreationFormr{   r   r|   r   c                 &    d| i}t        d|      }|S r  r   r  s        rY   r!  z!UserLeaveRequestCreationForm.as_pd  r"  r`   c                    |j                  dd       }t        |   |i | d| j                  d   j                  j
                  d<   |r_|j                  j                         }t        j                  j                  |j                  dd            }|| j                  d   _        | j                  d   j                  j
                  j                  d	d
dddd       || j                  d   _        y )Nr   r  r   r  r   TrP  rR  z#userLeaveFormr  r  r  r  r  r9   )rT  r;   r<   r?   rA   rD   r  r   r&   r   r   rU  rq   rE   rG   )rS   rT   rU   r   rV  rN  rX   s         rY   r<   z%UserLeaveRequestCreationForm.__init__l  s    ::j$/$)&);TL!((..x8'77;;=#,#4#4#;#;'33O$3O $< $  5IDKK(1O$++1188.3&&A	
 .6M"*r`   c                    t         |          }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        j                  j                  |||      j                  | j                  j                        }||k  st        j                  t        d	            |j                  d
k(  r3|j                  d      }	|	 t        j                  dt        d      i      ||k(  r#||k7  rt        j                  t        d            t        j                  j                  ||      j                         st        j                  t        d            |j                  ddg      j                         rt        j                  t        d            t        j                  j                  ||      }
|
j                  |
j                   z   }||z
  j"                  dz   }||d<   ||k  st        j                  t        d            |S )Nr   r   r9   r   r   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   r   r   )r;   r   r   r#   r   r   r   r   r   r   r   r   r   r    r   r   r   days)rS   r   r   r   r9   r   r   r   r  r   r  r  r   rX   s                rY   r   z"UserLeaveRequestCreationForm.clean  sT   w}!%%l3
##J/"&&}5$((9+//0FG)--.BC+33::#XZ  ;  

'T]]%%'
& 	 X%''@A  ++u4%)),7J!++$aN'  !#'99++^ 
 %%,,#= - 

&( ''*H(IJJ''K3L'MTTV''NO  )0044#= 5 
 **_-N-NN 	 #Z/559)7%&!11''*U(VWWr`   c                   b    e Zd ZdZeZg dZ ej                          ej                         dZ	y)!UserLeaveRequestCreationForm.Metar=  )	r   r9   r   r   r   r   r   r&  r   )r9   r   NrZ  r   r`   rY   r   r    s:    	 

 -5,,./e//1
r`   r   )rZ   r[   r\   r   r*  rC   r   r   r!  r<   r   r   r^   r_   s   @rY   rz  rz  `  s]     vv>N(OPJuoeooVV<L&MNH6*6p
 
r`   rz  c                   *    e Zd ZdZd Z G d d      Zy)LeaveAllocationRequestForma=  
    Form for creating a leave allocation request.

    This form allows users to create a leave allocation request by specifying
    details such as leave type, employee, requested days, description, and attachment.

    Methods:
        - as_p: Render the form fields as HTML table rows with Bootstrap styling.
    c                 &    d| i}t        d|      }|S r  r   r  s        rY   r!  zLeaveAllocationRequestForm.as_p  r"  r`   c                       e Zd ZdZeZg dZy)LeaveAllocationRequestForm.Metar=  )r   r9   r   r&  r   N)rZ   r[   r\   r]   r!   r   r?   r   r`   rY   r   r    s    	 '
r`   r   N)rZ   r[   r\   r]   r!  r   r   r`   rY   r  r    s    
 
r`   r  c                       e Zd ZdZ ej
                   ed       ej                  ddd            Z G d d	      Z	y
) LeaveAllocationRequestRejectForma  
    Form for rejecting a leave allocation request.

    This form allows administrators to provide a rejection reason when rejecting
    a leave allocation request.

    Attributes:
        - reason: A CharField representing the reason for rejecting the leave allocation request.
    rp  rq  rr  rs  r|   rL  c                       e Zd ZeZdgZy)%LeaveAllocationRequestRejectForm.Metarv  N)rZ   r[   r\   r!   r   r?   r   r`   rY   r   r    s    &!"r`   r   Nrw  r   r`   rY   r  r    s@     U__"#u~~Q9M$NOF
# #r`   r  c            
       *   e Zd ZdZej
                  j                         ZeD  cg c]8  }t        |d      r*|j                  t        vr|j                  |j                  f: c}}}} Z ej                  eej                  g d      Zyc c}}}} w )LeaveRequestExportForma  
    Form for selecting fields to export in a leave request export.

    This form allows users to select specific fields from the LeaveRequest model
    for export. The available fields are dynamically generated based on the
    model's meta information, excluding certain fields specified in 'excluded_fields'.

    Attributes:
        - model_fields: A list of fields in the LeaveRequest model.
        - field_choices: A list of field choices for the form, consisting of field names
          and their verbose names, excluding specified excluded_fields.
        - selected_fields: A MultipleChoiceField representing the selected fields
          to be exported.
    ra  )	r9   leave_type_Assignidr   r   r   r   r   r&  statusrb  N)rZ   r[   r\   r]   r#   rd  re  rf  rg  rv   rh  ra  ri  r   rj  rN   rk  rl  s   0000rY   r  r    s      %%002L " 5.)ejj.O 
U''(M 0e//++

Orm  r  c            
           e Zd ZdZ ej
                  ej                  j                          ej                  ddd      ddd	      Z
 eej                  j                          ed
eddd      d      Z fdZ fdZ xZS )AssignLeaveFormz
    Form for Payslip
    zoh-select oh-select-2 mb-2T)r/   ro   r|   N
Leave TypeF)rq   rA   r4  rJ   ro   rg   rh   ri   rj   r   rp   c                     t         |          }|j                  d      }|j                  d      }|st        j                  ddi      |st        j                  ddi      |S )Nr9   r   zThis field is required)r;   r   r   r   r   )rS   r   r9   r   rX   s       rY   r   zAssignLeaveForm.cleanI  sh    w}"&&}5$((9''8P(QRR'':R(STTr`   c                    t        |   |i | t        | j                         | j                  d   j                  j
                  j                  dt        j                         d      f d| j                  d   _	        y )Nr9   T)ro   r   r  r   )
r;   r<   r   r?   rA   rD   rE   uuiduuid4rJ   r   s      rY   r<   zAssignLeaveForm.__init__T  sj    $)&)$M"))//66TZZ\2	
 	 .:O$*r`   )rZ   r[   r\   r]   r   r7  r&   r   r   r8  r   r   r   r   r   r9   r   r<   r^   r_   s   @rY   r  r  /  s     +E**""&&(#u##8dK
 M *!!%%''6'(+!8
 
K	: :r`   r  c                   $    e Zd ZdZ G d d      Zy)LeaverequestcommentFormz"
    LeaverequestComment form
    c                       e Zd ZdZeZdZy)LeaverequestcommentForm.Metar=  commentNrZ   r[   r\   r]   r$   r   r?   r   r`   rY   r   r  b  s    	 $r`   r   NrD  r   r`   rY   r  r  ]       r`   r  c                   V     e Zd ZdZdZ G d d      Z fdZd Zd
dede	f fd	Z
 xZS )LeaveCommentForm*
    Leave request comment model form
    Add Commentc                       e Zd ZdZeZdZy)LeaveCommentForm.Metar=  rt   Nr  r   r`   rY   r   r  r  s    	 $r`   r   c                     t        |   |i | t        d      | j                  d<   d| j                  d   j                  j
                  d<   d| j                  d   _        y )NfilesrJ   r  r  F)r;   r<   r   r?   rA   rD   ro   r   s      rY   r<   zLeaveCommentForm.__init__z  sX    $)&)0w?G6OG##))(3(-G%r`   c                 &    d| i}t        d|      }|S r  r  zcommon_form.htmlr   rS   r  r   s      rY   r!  zLeaveCommentForm.as_p        4.%&8'B
r`   commitreturnc                    g }d }| j                   j                  d      rt| j                   j                  d      }|d   | j                  _        g }|D ]>  }t	               }||_        |j                          |j                  |j                         @ t        |   |      }|r |j                   j                  |  ||fS Nr  r   r  r   r   
attachemntr%   filer   r   pkr;   addrS   r  multiple_files_idsr  r  file_instancer   rX   s          rY   r   zLeaveCommentForm.save      ::g&JJ&&w/E',QxDMM$!## <
 0 2%/"""$"))-*:*:;	<
 7<'HNN 23r`   .rZ   r[   r\   r]   ra  r   r<   r!  boolr   r   r^   r_   s   @rY   r  r  k  s;     !L .4 #  r`   r  c                   $    e Zd ZdZ G d d      Zy)!LeaveallocationrequestcommentFormz.
    Leave Allocation Requestcomment form
    c                       e Zd ZdZeZdZy)&LeaveallocationrequestcommentForm.Metar=  r  NrZ   r[   r\   r]   r"   r   r?   r   r`   rY   r   r    s    	 .r`   r   NrD  r   r`   rY   r  r    r  r`   r  c                   V     e Zd ZdZdZ G d d      Z fdZd Zd
dede	f fd	Z
 xZS )LeaveAllocationCommentFormr  r  c                       e Zd ZdZeZdZy)LeaveAllocationCommentForm.Metar=  rt   Nr  r   r`   rY   r   r    s    	 .r`   r   c                 ~    t        |   |i | t        d      | j                  d<   d| j                  d   _        y )Nr  r  F)r;   r<   r   r?   ro   r   s      rY   r<   z#LeaveAllocationCommentForm.__init__  s9    $)&)0w?G(-G%r`   c                 &    d| i}t        d|      }|S r  r   r  s      rY   r!  zLeaveAllocationCommentForm.as_p  r  r`   r  r  c                    g }d }| j                   j                  d      rt| j                   j                  d      }|d   | j                  _        g }|D ]>  }t	               }||_        |j                          |j                  |j                         @ t        |   |      }|r |j                   j                  |  ||fS r  r  r  s          rY   r   zLeaveAllocationCommentForm.save  r  r`   r  r  r_   s   @rY   r  r    s;     !L .
4 #  r`   r  c                        e Zd Z ej                   ej
                  ddi            Z ej                   ej
                  ddi            Zd Z G d d      Z	 fdZ
 xZS )	RestrictLeaveFormr{   r   r|   r   c                     | j                   j                  d      }| j                   j                  d      }|r|r||k  rt        t        d            |S )Nr   r   z3End date should not be earlier than the start date.)r   r   r   r   )rS   r   r   s      rY   clean_end_datez RestrictLeaveForm.clean_end_date  sW    &&**<8
$$((4(x*'<!GH  r`   c                   .    e Zd ZeZdZdgZd ed      iZy)RestrictLeaveForm.Metart   ru   titleTitleN)	rZ   r[   r\   r'   r   r?   r   r   r   r   r`   rY   r   r    s#    -QwZ
r`   r   c                     t        t        | 
  |i | d| j                  d   j                  j
                  d<   | j                  d   j                  j
                  j                  ddddd       y )	Nr  autocomplete
departmentz#leaveRestrictFormz#restrictLeaveJobPositionr  z!/leave/get-restrict-job-positions)r  r  r  r  )r;   r  r<   r?   rA   rD   rE   r   s      rY   r<   zRestrictLeaveForm.__init__  sk    /@@<CG##)).9L!((..5528&=		
r`   )rZ   r[   r\   r   r*  rC   r   r   r  r   r<   r^   r_   s   @rY   r  r    sd     uff%56J uuff%56H	
 


 

r`   r  
attendancer   )CompensatoryLeaveRequestCompensatoryLeaverequestCommentc                   F     e Zd ZdZ G d d      Z fdZd Z fdZ xZS )CompensatoryLeaveFormaU  
        Form for creating a leave allocation request.

        This form allows users to create a leave allocation request by specifying
        details such as leave type, employee, requested days, description, and attachment.

        Methods:
            - as_p: Render the form fields as HTML table rows with Bootstrap styling.
        c                   B    e Zd ZdZ ej
                  d      ZeZg dZ	y)CompensatoryLeaveForm.Meta;
            Meta class for additional options
            T)ro   )r9   attendance_idr&  N)
rZ   r[   r\   r]   r   rj  r  r  r   r?   r   r`   rY   r   r    s'     6E55tDM,EFr`   r   c                    t        t        | 
  |i | t        t        j
                  dd       }d }| j                  r| j                  j                  }|rtt        |d      rht        |j                  d      rR|j                  j                  }t        ||      }t        |j                  dd            }|| j                  d   _        t!        |t"        j$                  j'                  d	            t"        j$                  j'                  |j                  
      z  j)                         }|| j                  d   _        | j                  d   j,                  j.                  j1                  dddd       y )Nr.   rO   rP   )comp_idr   attendance_dater  T)ru   )employee_user_idr9   z#id_attendance_id_parent_divr  z!/leave/get-leave-attendance-datesr  )r;   r  r<   r=   r   r>   r   r   rg  rO   rP   r   r   rU  r?   rc  r   r   r   r   distinctrq   rA   rD   rE   )
rS   rT   rU   r.   instance_idr   holiday_attendanceattendance_datesrq   rX   s
            rY   r<   zCompensatoryLeaveForm.__init__  sJ   '7HH1@@)TRGK}}"mm..GV,GLL.9"<<44%=k&" $(&2249JK$  8HO,4/X--44t4D ""))7<<)HI hj  3;DKK&/KK&--33::!?"*Ar`   c                 &    d| i}t        d|      }|S )z[
            Render the form fields as HTML table rows with Bootstrap styling.
            r  r  r   r  s        rY   r!  zCompensatoryLeaveForm.as_pA  s      tnG)*=wGJr`   c                    t         |          }|j                  d      }|t        |      dk  r t	        j
                  dt        d      i      |j                  d      }d}d }| j                  r| j                  j                  }|D ][  }t        j                  j                  ||      j                  t        |      t        d	      z        j                         sYd
} n |rt	        j
                  ddi      |S )Nr  r   zThis field is required.r9   F)r9   r  r   r   )r  Tz;This attendance is already converted to complimentory leave)r;   r   r   lenr   r   r   r   r   r  r   r   r   r	   r   )rS   r   r  r   attendance_repeatr  r  rX   s          rY   r   zCompensatoryLeaveForm.cleanI  s    7=?L(,,_=M$M(:Q(>++$a(A&BC  $''6H %K}}"mm..+ 	
,44;;$,J <  WQ+.*1EEFVX	 )-%	 !++')f 
  r`   )	rZ   r[   r\   r]   r   r<   r!  r   r^   r_   s   @rY   r  r    s'    		 	#	J		  	 r`   r  c                       e Zd ZdZ ej
                   ed       ej                  ddd            Z G d d	      Z	y
)"CompensatoryLeaveRequestRejectForma<  
        Form for rejecting a compensatory leave request.

        This form allows administrators to provide a rejection reason when rejecting
        a compensatory leave request.

        Attributes:
            - reason: A CharField representing the reason for rejecting the  compensatory leave request.
        rp  rq  rr  rs  r|   rL  c                       e Zd ZeZdgZy)'CompensatoryLeaveRequestRejectForm.Metarv  N)rZ   r[   r\   r  r   r?   r   r`   rY   r   r  w  s    ,E%&Fr`   r   Nrw  r   r`   rY   r  r  g  s@    	 !&'!5>>=Q(RS

	' 	'r`   r  c                   $    e Zd ZdZ G d d      Zy)#CompensatoryLeaveRequestcommentFormz*
        LeaverequestComment form
        c                       e Zd ZdZeZdZy)(CompensatoryLeaveRequestcommentForm.Metar  r  N)rZ   r[   r\   r]   r  r   r?   r   r`   rY   r   r    s     4E!Fr`   r   NrD  r   r`   rY   r  r  {  s    		" 	"r`   r  )_r]   mathr   r  r   r   typingr   djangor   django.appsr   django.core.exceptionsr   django.db.modelsr	   django.formsr
   django.forms.widgetsr   django.template.loaderr   django.utils.translationr   r   base.methodsr   r   base.modelsr   r   employee.filtersr   employee.formsr   employee.modelsr   horillar   horilla_widgets.formsr   r   2horilla_widgets.widgets.horilla_multi_select_fieldr   &horilla_widgets.widgets.select_widgetsr   leave.methodsr   r   r   r   r   leave.modelsr    r!   r"   r#   r$   r%   r&   r'   CHOICESr   rb   rf   r   r   r   r,  r3  r:  rA  rF  rJ  rh  Formr`  ro  rz  r  r  r  r  r  r  r  r  r  is_installedmodelsr  r  r  r  r  r   r`   rY   <module>r     s"  
  	  #    2  " * 3 6 I / + , $ ' ? V K 	 	 	 1U8tQtWo
.: :z.EOO .b8*M 8*v/3- /3d>K
y K
\~
y ~
B? ?8!%) !%HFy F, y  0m
9 m
` UZZ  F# #2m
9 m
`
 
D#uzz #*%UZZ %P+:k +:\i -y -`	 + +\%
	 %
P 4\"Qc 	 c J'UZZ '("i "y #r`   