
    siɵ                        d Z ddlZddlZddl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 ddlmZ dd	lmZ dd
lmZ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%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z0 ddl.m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC  ej                  eE      ZF G d de0      ZG G d de0      ZH G d de0      ZI G d d ej                        ZK G d! d"e0      ZL G d# d$e0      ZM G d% d&ej^                        ZN G d' d(e0      ZO G d) d*eO      ZPg d+ZQ G d, d-ej                        ZS G d. d/ej                        ZT G d0 d1ej                        ZU G d2 d3ej                        ZV G d4 d5e0      ZW G d6 d7ej                        ZX G d8 d9e0      ZYd: ZZ G d; d<e0      Z[ G d= d>e0      Z\ G d? d@e0      Z]y)Aa  
forms.py

This module contains the form classes used in the application.

Each form represents a specific functionality or data input in the
application. They are responsible for validating
and processing user input data.

Classes:
- YourForm: Represents a form for handling specific data input.

Usage:
from django import forms

class YourForm(forms.Form):
    field_name = forms.CharField()

    def clean_field_name(self):
        # Custom validation logic goes here
        pass
    N)
month_name)OrderedDict)AnyDict)forms)apps)ValidationError)QuerySet)	DateInputDateTimeInput	TimeInputrender_to_string)format_html)gettext_lazy)AttendanceFilters)
AttendanceAttendanceActivityAttendanceLateComeEarlyOutAttendanceOverTimeAttendanceRequestCommentAttendanceValidationConditionBatchAttendance	GraceTimeWorkRecordsattendance_date_validatestrtime_secondsvalidate_time_format)	ModelForm)MultipleFileField)filtersubordinatesemployeemodelget_working_daysis_reportingmanagerreload_queryset)CompanyEmployeeShift)EmployeeFilter)Employee)horilla_middlewares)HorillaMultiSelectField)HorillaMultiSelectWidgetc                   F     e Zd ZdZdZ G d d      Zd Z fdZd Z xZ	S )AttendanceUpdateFormz
    This model form is used to direct save the validated query dict to attendance model
    from AttendanceUpdateForm. This form can be used to update existing attendance.
    attendanceUpdateFormFieldsc                       e Zd ZdZdZg dZeZ eddi       eddi       eddi       eddi       eddi      dZ	y	)
AttendanceUpdateForm.Meta7
        Meta class to add the additional info
        __all__)overtime_secondat_work_secondattendance_dayrequest_descriptionapproved_overtime_secondrequest_typerequested_datais_validate_requestis_validate_request_approvedattendance_overtime	is_active
is_holidaytypetimeattrsdateattendance_clock_inattendance_clock_outattendance_clock_out_dateattendance_dateattendance_clock_in_dateN)
__name__
__module____qualname____doc__fieldsexcluder   modelr   widgets     4/var/www/python-projects/worksol/attendance/forms.pyMetar0   S   sd    	 
 #07G#H$18H$I)6ff=M)N,FF3CD(5VV<L(M
rS   rU   c           
          | j                   |   j                  j                  j                  t	        t        j                               ddddddd       y)4Update the widget attributes for worked hour fields.#attendanceUpdateForm%#id_attendance_worked_hour_parent_div	outerHTML$/attendance/update-worked-hour-fieldchange delay:300msid
hx-include	hx-targethx-swapz	hx-selecthx-get
hx-triggerNrN   widgetrB   updatestruuiduuid4self
field_names     rT   update_worked_hour_hx_fieldsz1AttendanceUpdateForm.update_worked_hour_hx_fieldsp   J    J&&,,33$**,'5D&D@2
	
rS   c           	         |j                  d      x}rt        j                  j                         }|r!|j                  rt        |j                        nd}|j                  j                  d      |j                  j                  d      |j                  j                  d      d}|j                  <|j                  j                  d      |d<   |j                  j                  d      |d<   ||d<   t        | 4  |i | | j                  d	   j                  j                   j#                  d
t%        t'        j(                               i       | j                  d   j                  j                   j#                  t%        t'        j(                               dddddd       dD ]  }| j+                  |        | j                  d   j                  j                   j#                  ddi       | j                  d   j                  j                   j#                  d
t%        t'        j(                               i       |=|j,                  s1t        |j.                        k  s|j0                  s| j                  d= t3        | j                  d   j4                        dgz   | j                  d   _        | j                  d   j                  j                   j#                  ddi       y )Ninstancer   %Y-%m-%d%H:%MrH   rE   rI   rF   rG   initialemployee_idr^   shift_idrX   z%#attendanceUpdateFormFields,#personalchangerZ   %/attendance/update-fields-based-shiftr^   r_   r`   rc   ra   rb   rI   rE   rG   rF   rH   onchangeattendanceDateChange($(this))work_type_idattendance_overtime_approvebatch_attendance_iddynamic_createzDynamic createdynamicBatchAttendance($(this)))getr   objectsfirstminimum_overtime_to_approver   rH   strftimerE   rI   rG   rF   super__init__rN   re   rB   rf   rg   rh   ri   rm   r~   r<   attendance_validatedlistchoices)rk   argskwargsrp   	conditionrt   field	__class__s          rT   r   zAttendanceUpdateForm.__init__~   s   zz*--8- 6==CCEI !F!F  	 E EF  $,#;#;#D#DZ#P'/'C'C'L'LW'U,4,M,M,V,V-G 11=11::7C ./ 66??
K 34 !(F9$)&)M"))//66c$**,>O7PQJ&&,,33$**,'5D&&A		

 	5E --e4	5 	%&--33::;	

 	N#**0077s4::<?P8QR  88 < <=	I44 9:59KK-.666
1263)*2 	)*1177>>=	
rS   c                 .    ||f}d| i}t        d|      }|S S
        Render the form fields as HTML table rows with Bootstrap styling.
        formzattendance_form.htmlr   rk   r   r   _context
table_htmls         rT   as_pzAttendanceUpdateForm.as_p   )     &L4.%&<gF
rS   )
rJ   rK   rL   rM   container_idrU   rm   r   r   __classcell__r   s   @rT   r-   r-   K   s*    
 0L
 
:
C
JrS   r-   c            	            e Zd ZdZdZ eej                  j                  d       e	de
dd       ed	      
      Z G d d      Zd Z fdZd fd	Zd Zdeeef   f fdZd Z xZS )AttendanceFormz)
    Model form for Attendance model
    attendanceFormFieldsF)employee_work_info__isnullzemployee-widget-filterfzemployee_filters.html)filter_route_namefilter_classfilter_instance_contex_namefilter_template_path	Employeesquerysetre   labelc                       e Zd ZdZeZdZg dZ eddi       eddi       eddi       eddi       eddi      dZ	y	)
AttendanceForm.Metar1   r2   )r~   attendance_overtime_calculationr4   r3   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rC   rD   N)
rJ   rK   rL   rM   r   rP   rN   rO   r   rQ   rR   rS   rT   rU   r      sd    	 
" $17G#H$18H$I)6ff=M)N,FF3CD(5VV<L(M
rS   rU   c           
          | j                   |   j                  j                  j                  t	        t        j                               ddddddd       y)rW   #attendanceCreateFormrY   rZ   r[   r\   r]   Nrd   rj   s     rT   rm   z+AttendanceForm.update_worked_hour_hx_fields   rn   rS   c           	      V   |j                  di       }t        j                  j                         j                         j	                  d      t        j                  j                         j                         j	                  d      d}|j                  d      x}r|j                  |j                  j	                  d      |j                  j	                  d      |j                  j	                  d      d       |j                  <|j                  j	                  d      |d<   |j                  j	                  d      |d<   |j                  |       ||d<   t        | 8  |i | t        | j                          | j                   d	   j"                  j$                  j                  d
t'        t)        j*                               i       | j                   d   j"                  j$                  j                  t'        t)        j*                               dddddd       dD ]  }| j-                  |        | j                   d   j"                  j$                  j                  ddi       | j                   d   j"                  j$                  j                  d
t'        t)        j*                               i       t/        | j                   d   j0                        dgz   | j                   d   _        | j                   d   j"                  j$                  j                  ddi       y )Nrt   rq   rr   )rG   rF   rp   rs   rF   rG   ru   r^   rv   r   z#attendanceFormFields,#personalrw   rZ   rx   ry   rz   rH   r{   r|   r}   r   r   r   )popdatetimetodayrC   r   r@   r   rf   rH   rE   rI   rG   rF   r   r   r$   rN   re   rB   rg   rh   ri   rm   r   r   )rk   r   r   view_initialrt   rp   r   r   s          rT   r   zAttendanceForm.__init__  s   zz)R0 *2):):)@)@)BTVXj!$,$5$5$;$;$=$B$B$D$M$Mg$V	
 zz*--8-NN'/'?'?'H'H'T+3+G+G+P+P, 190Q0Q0Z0Z"1
 11=11::7C ./ 66??
K 34
 	|$#y$)&)$M"))//66c$**,>O7PQJ&&,,33$**,'5>&&A		

 	5E --e4	5 	%&--33::;	

 	N#**0077s4::<?P8QR59KK-.666
1263)*2 	)*1177>>=	
rS   c                    t         |   d      }| j                  j                  d      D ]  }t	        |      t	        |j
                  j                        k7  s/| j                  j                         }|j                  dt        |      i       t        |      j                  d      }|j                           |r|j                          |S )NFcommitru   )r   savedatagetlistintru   r^   copyrf   rg   r-   )rk   r   rp   emp_id	data_copy
attendancer   s         rT   r   zAttendanceForm.saveV  s    7<u<-ii''6 	"F6{c("6"6"9"9:: IINN,	  -V!=>1)<AAAO
!	" MMOrS   c                 .    ||f}d| i}t        d|      }|S r   r   r   s         rT   r   zAttendanceForm.as_pb  r   rS   returnc                    t         |           t        j                  j	                  | j
                  j                  d            j                         | j                  _	        | j                  j                  dd        | j                  j                  t        dt        d      i      t         |           | j
                  j                  d      }t        j                  j	                  | j
                  d         j	                  |      }|j!                         rG|D cg c]  }|j                  j#                          }}t        dddj%                  |       d	i      y c c}w )
Nru   )r^   zThis field is requiredrH   )rH   )employee_id__id__inzAlready attendance exists for z, z
 employees)r   cleanr(   r   filterr   r   r   rp   ru   errorsr   r	   r   r   r   exists__str__join)rk   employee_idsexisting_attendancer   employee_namesr   s        rT   r   zAttendanceForm.cleank  sE   $,$4$4$;$;yy}}]+ %< %

%' 	! 	t,==$$,!=!4L2M"NOOyy((7(0077 II&78 8 

&\&
2 	 %%'CV5?
&&..0N  "!%CDIInD]C^^h#i 	 (s   (!E,c                 Z   | j                   d   }|D ]d  }t        j                  j                  || j                  d         j                         }|Bt        t        dj                  |                   |j                         t        t        d            |j                         S )z4
        Used to validate employee_id field
        ru   rH   ru   rH   z0Attendance for the date already exists for {emp})empzEmployee not chosen)	cleaned_datar   r   r   r   r   r	   r   format)rk   employeer   r   s       rT   clean_employee_idz AttendanceForm.clean_employee_id  s     $$]3 	C#++22;L1M 3 eg  %%KSS # T  	 >>#!!$9":;;~~rS   T)rJ   rK   rL   rM   r   r*   r(   r   r   r+   r'   r   ru   rU   rm   r   r   r   r   rg   r   r   r   r   r   s   @rT   r   r      s     *L)!!((E(J'6'(+!8	
 n	K
 
>
I
V
tCH~ 0 rS   r   c                   6     e Zd ZdZ G d d      Z fdZ xZS )AttendanceActivityFormz1
    Model form for AttendanceActivity model
    c                   n    e Zd ZdZeZdZ eddi       eddi       eddi       eddi      dZy)	AttendanceActivityForm.Metar1   r2   r?   r@   rA   rC   )clock_in	clock_outclock_in_dateclock_out_dateN)	rJ   rK   rL   rM   r   rP   rN   r   rQ   rR   rS   rT   rU   r     sN    	 #%VV,<=&ff-=>*&&1AB+662BC	
rS   rU   c                    |j                  d      x}r|j                  j                  d      |j                  j                  d      |j                  j                  d      d}|j
                  <|j
                  j                  d      |d<   |j                  j                  d      |d<   ||d<   t        |    |i | y )Nrp   rq   rr   )rH   r   r   r   r   rt   )	r   rH   r   r   r   r   r   r   r   )rk   r   r   rp   rt   r   s        rT   r   zAttendanceActivityForm.__init__  s    zz*--8-
 $,#;#;#D#DZ#P!)!7!7!@!@!L$--66w?G
 !!-'/'9'9'B'B7'K$,4,C,C,L,LZ,X() 'F9$)&)rS   )rJ   rK   rL   rM   rU   r   r   r   s   @rT   r   r     s    
 
* *rS   r   c                   "     e Zd ZdZ fdZ xZS )MonthSelectFieldz 
    Generate month choices
    c                     t        dd      D cg c]9  }t        |   j                         t        t        |   j	                               f; }}t        |   |d|i| y c c}w )N      r   )ranger   lowerr   
capitalizer   r   )rk   r   r   ir   r   s        rT   r   zMonthSelectField.__init__  sg    LQRSUWL
GHZ]  "Ajm&>&>&@$AB
 
 	4::6:
s   >A")rJ   rK   rL   rM   r   r   r   s   @rT   r   r     s    ; ;rS   r   c                   Z     e Zd ZdZ e ed            Z G d d      Z fdZd Z	 xZ
S )AttendanceOverTimeFormz1
    Model form for AttendanceOverTime model
    Month)r   c                   l    e Zd ZdZeZdZg dZ ed       ed       ed       ed       ed      d	Z	y
)AttendanceOverTimeForm.Metar1   r2   )hour_account_secondr3   month_sequencehour_pending_secondr=   r(   YearzWorked HourszPending HoursOvertime)ru   yearworked_hourspending_hoursovertimeN)
rJ   rK   rL   rM   r   rP   rN   rO   r   labelsrR   rS   rT   rU   r     sH    	 #
 Z=fIn-/*
rS   rU   c                     t        |   |i | | j                  d   j                  j                  j                  dt        t        j                               i       y )Nru   r^   )	r   r   rN   re   rB   rf   rg   rh   ri   rk   r   r   r   s      rT   r   zAttendanceOverTimeForm.__init__  sH    $)&)M"))//66c$**,>O7PQrS   c                 .    ||f}d| i}t        d|      }|S r   r   r   s         rT   r   zAttendanceOverTimeForm.as_p  r   rS   )rJ   rK   rL   rM   r   r   monthrU   r   r   r   r   s   @rT   r   r     s-     1W:.E
 
,RrS   r   c                   $    e Zd ZdZ G d d      Zy)AttendanceLateComeEarlyOutFormz>
    Model form for attendance AttendanceLateComeEarlyOut
    c                       e Zd ZdZeZdZy)#AttendanceLateComeEarlyOutForm.Metar1   r2   N)rJ   rK   rL   rM   r   rP   rN   rR   rS   rT   rU   r     s    	 +rS   rU   NrJ   rK   rL   rM   rU   rR   rS   rT   r   r     s     rS   r   c                      e Zd ZdZdddddZ ej                  dd ej                  ddd	       e e	d
       e	d                  Z
 ej                  dd ej                  ddd	       e	d            Z ej                  dd ej                  ddd	       e	d            Z ej                   e	d      ej                  j!                         d ej"                  ddi	            Z G d d      Zy)!AttendanceValidationConditionFormz6
    Model form for AttendanceValidationCondition
       )validation_at_workr   overtime_cutoff
company_idTz00:00zoh-input w-100z09:00)classplaceholderrA   zn<span title='Do not Auto Validate Attendance if an Employee Works More Than this Amount of Duration'>{}</span>z'Worked Hours(At Work) Auto Approve Till)requiredrt   re   r   z00:30z Minimum Hour to Approve Overtimez02:00z Maximum Allowed Overtime Per Dayr%   Fr  oh-select oh-select-2 w-100)r   r   r  re   c                       e Zd ZdZeZdZdgZy)&AttendanceValidationConditionForm.Meta3
        Meta class for additional options
        r2   r=   N)rJ   rK   rL   rM   r   rP   rN   rO   rR   rS   rT   rU   r  3  s    	 .-rS   rU   N)rJ   rK   rL   rM   colsr   	CharField	TextInputr   r   r  r   r  ModelMultipleChoiceFieldr%   r   allSelectMultipler  rU   rR   rS   rT   r  r    s/   
 !')	D )u,WE
  A 78	
 #2%//u,WE
 23# &eoou,WE
 23O 0//	l$$&#u##74Q*RS	J   rS   r  c                   `     e Zd ZdZddiZd Z fdZ G d d      Zd Zdd	e	d
e
f fdZ xZS )AttendanceRequestFormz
    AttendanceRequestForm
    r6   r  c           
          | j                   |   j                  j                  j                  t	        t        j                               ddddddd       yrW   #attendanceRequestFormrY   rZ   r[   r\   r]   Nrd   rj   s     rT   rm   z2AttendanceRequestForm.update_worked_hour_hx_fieldsD  J    J&&,,33$**,'6D&D@2
	
rS   c                    |j                  d      x}r|j                  j                  d      |j                  j                  d      |j                  j                  d      d}|j
                  <|j                  j                  d      |d<   |j
                  j                  d      |d<   ||d<   t        |    |i | d| j                  d   _
        d| j                  d   _
        | j                  j                  sS| j                  d	   j                  j                  j                  t!        t#        j$                               d
dddd       dD ]  }| j'                  |        | j                  d   j                  j                  j                  ddi       | j                  d   j                  j                  j                  dt!        t#        j$                               i       t)        | j                  d   j*                        dgz   | j                  d   _        | j                  d   j                  j                  j                  ddi       y )Nrp   rq   rr   rs   rF   rG   rt   Frv   r  z#attendanceRequestDiv	innerHTMLrx   )r^   r_   r`   ra   rb   rz   rH   r{   r|   r}   r^   r   r   r   )r   rH   r   rE   rI   rG   rF   r   r   rN   r  rp   pkre   rB   rf   rg   rh   ri   rm   r   r   )rk   r   r   rp   rt   r   r   s         rT   r   zAttendanceRequestForm.__init__R  s+   zz*--8- $,#;#;#D#DZ#P'/'C'C'L'LW'U,4,M,M,V,V-G 11=11::7C ./ 66??
K 34 !(F9$)&)<A/097<*+4}}KK
#**0077djjl+":!8*E
 	5E --e4	5 	%&--33::;	

 	N#**0077s4::<?P8QR59KK-.666
1263)*2 	)*1177>>=	
rS   c                       e Zd ZdZeZg dZ eddi       eddi       eddi       eddi       eddi      dZ e	ddi       e	ddi       e
ddi       e
ddi       e
ddi      dZy	)
AttendanceRequestForm.Metar  )rH   rv   r}   rI   rE   rG   rF   attendance_worked_hourminimum_hourr6   r   r?   rC   rA   r@   )rH   rE   rI   rF   rG   rD   N)rJ   rK   rL   rM   r   rP   rN   r   rQ   r   r   rR   rS   rT   rU   r    s    	 
  -FF3CD#07G#H(5VV<L(M$18H$I)6ff=M)N
 $-FF3C#D$-VV4D$E)2&&9I)J(/?@(18H(I
rS   rU   c                 .    ||f}d| i}t        d|      }|S r   r   r   s         rT   r   zAttendanceRequestForm.as_p  r   rS   r   r   c                 "    t         |   d      S )NF)r   r   )rk   r   r   s     rT   r   zAttendanceRequestForm.save  s    w|E""rS   ).)rJ   rK   rL   rM   r  rm   r   rU   r   boolr   r   r   r   s   @rT   r  r  =  sG     "2&D
4
l!
 !
F#4 ## # #rS   r  c                   B     e Zd ZdZ fdZd Zdeeef   f fdZ	 xZ
S )NewRequestFormz
    NewRequestForm class
    c                    t        |   |i | |j                  di       }| j                  }t	        j
                  t        j                  j                  d      t        d      t	        j                  ddddd	
      |j                  d            t	        j                  dt        d      t	        j                  ddddd	
            d}|j                  |       || _        ||d<   y )Nrt   Tr=   r(   r	  z(#id_shift_id_parent_div,#id_shift_id_divr  z)/attendance/get-employee-shift?bulk=Falser  r`   ra   rb   rA   ru   )r   r   re   rt   FCreate Bulkoh-checkbox#genericModalBodyz-/attendance/request-bulk-attendance?bulk=Truer  r   re   )ru   create_bulk)r   r   r   rN   r   ModelChoiceFieldr(   r   r   r   Selectr   BooleanFieldCheckboxInputrf   )rk   r   r   r   old_dictnew_dictr   s         rT   r   zNewRequestForm.__init__  s    $)&)zz)R0;; 11!))0040@
m||!>%O#."M	 %((7 !--&**!.%8#."Q	
6 	!(yrS   c                 .    ||f}d| i}t        d|      }|S )r   r   z)requests/attendance/request_new_form.htmlr   rk   r   r   r   r   	form_htmls         rT   r   zNewRequestForm.as_p  s-     &L4.$7
	 rS   r   c                    t         	|           | j                  d   }| j                  d   }t        j                  j                  ||      }|r t        |d      st        t        d            ||| j                  d   | j                  d   | j                  d   | j                  d	   | j                  d
   | j                  d   | j                  d   | j                  d   d
}|j                         r"|j                  |d<   t        |      |d<   | j                  d   |d<   | j                  d   |d<   |d   dk(  rd n|d   |d<   |d	   dk(  rd n|d	   |d	<   | j                  d   |d<   | j                  d
   |d
<   |j                         }|j                         D ]  \  }}t        |      ||<    t        j                   |      |_        d|_        |j&                  dk7  rd|_        | j                  d   |_        |j+                          d | _        y t        di |}d|_        d|_        | j                  d   |_        d|_        || _        y )Nru   rH   r   employee_work_infoEmployee work info not foundrI   rE   rF   rG   rv   r}   r  r  )
ru   rH   rI   rE   rF   rG   rv   r}   r  r  NoneTcreate_requestupdate_requestr6   FrR   )r   r   r   r   r   r   hasattrr	   r   r   r   r^   rg   r   itemsjsondumpsr9   r:   r8   r6   r   new_instancer   )
rk   r   rH   attendancesr   r   keyvaluer@  r   s
            rT   r   zNewRequestForm.clean  s   $$]3++,=> ((// / 0 
 GH.BC!!$B"CDD#.(,(9(9:T(U#'#4#45J#K$($5$56L$M)-):):;V)W))*5 --n=&*&7&78P&Q IIn5
 "*++D&)/&:D"#/3yy9S/TD+,*.))4I*JD&' ./69 01 '( 34> 56 ,-
 $(99^#<D #yy4D$**,J"jjl '
UJS	'(,

4(8J%-1J*&&*::*:
'-1YY7L-MJ*OO $D!)D)+/(,1)+/995J+K($4!(rS   )rJ   rK   rL   rM   r   r   r   rg   r   r   r   r   s   @rT   r$  r$    s,    #)J	9tCH~ 9 9rS   r$  )r^   attendance_id__employee_idin_datetimeout_datetimer9   r4   r7   r:   r;   r6   r8   r   r   horilla_historyc            
       *   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 )AttendanceExportFormz
    This form allows users to choose which fields of the `Attendance` model
    they want to include in the export excel file as column. The fields are
    presented as a list of checkboxes, and the user can select multiple fields.
    verbose_name)
ru   rv   r}   rH   rE   rI   rF   rG   r  r   r   re   rt   N)rJ   rK   rL   rM   r   _meta
get_fieldsmodel_fieldsr<  nameexcluded_fieldsrJ  field_choicesr   MultipleChoiceFieldCheckboxSelectMultipleselected_fields.0r   r<  rP  s   0000rT   rI  rI  <  s     ##..0L " 5.)ejj.O 
U''(M 0e//++
O   =B
rI  c            
       B   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
                  j                         ZeD  cg c];  }t        |d      r-|j                  t        vrd|j                  z   |j                  f= c}}}} Zeez   Z e ej&                  e            Z ej*                  eej,                  g d      Zyc c}}}} w c c}}}} w )LateComeEarlyOutExportForma   
    This form allows users to choose fields from both the `AttendanceLateComeEarlyOut`
    model and the related `Attendance` model to include in the export excel file.
    The fields are presented as checkboxes, and users can select multiple fields.
    rJ  attendance_id__)ru   r?   attendance_id__attendance_date'attendance_id__attendance_clock_in_date"attendance_id__attendance_clock_in(attendance_id__attendance_clock_out_date#attendance_id__attendance_clock_outrK  N)rJ   rK   rL   rM   r   rL  rM  rN  r<  rO  rP  rJ  field_choices_1r   model_fields_2field_choices_2rQ  r   r   fromkeysr   rR  rS  rT  rU  s   0000rT   rY  rY  \  s    .33>>@L " 5.)ejj.O 
U''(O
  %%002N $ 5.)ejj.O 
UZZ	'););<O
 $o5M---m<=M/e//++
Os   =D
A D
rY  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 )AttendanceActivityExportFormz
    This form allows users to choose specific fields from the `AttendanceActivity`
    model to include in the export excel file. The fields are presented as checkboxes,
    enabling users to select multiple fields.
    rJ  )ru   rH   r   r   r   r   rK  N)rJ   rK   rL   rM   r   rL  rM  rN  r<  rO  rP  rJ  rQ  r   rR  rS  rT  rU  s   0000rT   re  re         &++668L " 5.)ejj.O 
U''(M
 0e//++
OrW  re  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 )AttendanceOverTimeExportFormz
    This form allows users to choose specific fields from the `AttendanceOverTime`
    model to include in the export. The fields are presented as checkboxes,
    enabling users to select multiple fields.
    rJ  )ru   r   r   r   r   r   rK  N)rJ   rK   rL   rM   r   rL  rM  rN  r<  rO  rP  rJ  rQ  r   rR  rS  rT  rU  s   0000rT   rh  rh    rf  rW  rh  c                       e Zd ZdZddddZ ej                  ej                  j                         d e
d            Z G d d      Zy	)
GraceTimeForm.
    Form for create or update Grace time
    r  )allowed_timer  shiftsFz1Allcocate this grace time for Check-In Attendance)r   r  	help_textc                   n    e Zd ZdZeZdZ ej                          ej                  ddi      dZ
g dZy)	GraceTimeForm.Metar  r2   r  z00:00:00 HoursrA   )
is_defaultrl  )r   allowed_time_in_secsr=   N)rJ   rK   rL   rM   r   rP   rN   r   HiddenInputr  rQ   rO   rR   rS   rT   rU   rp    sD    	 +%++-+EOO=BR2ST

 CrS   rU   N)rJ   rK   rL   rM   r  r   r  r&   r   r  r   rm  rU   rR   rS   rT   rj  rj    sQ     bB?D+U++&&**,GHFC CrS   rj  c                        e Zd ZdZ ej
                  ej                  j                               Z	 e
d      Zd Z fdZ xZS )GraceTimeAssignFormrk  )r   zAssign Shiftsc                 .    ||f}d| i}t        d|      }|S r   r   zcommon_form.htmlr   r4  s         rT   r   zGraceTimeAssignForm.as_p  )     &L4.$%7A	rS   c                 p    t        |   |i | d| j                  d   j                  j                  d<   y )Nzoh-select w-100 oh-select-2rm  r  )r   r   rN   re   rB   r   s      rT   r   zGraceTimeAssignForm.__init__  s4    $)&)6SH$$**73rS   )rJ   rK   rL   rM   r   r  r&   r   r  rm  r   rJ  r   r   r   r   s   @rT   ru  ru    sK     ,U++&&**,F _%LT TrS   ru  c                   $    e Zd ZdZ G d d      Zy)AttendanceRequestCommentFormz'
    AttendanceRequestComment form
    c                       e Zd ZdZeZdZy)!AttendanceRequestCommentForm.Metar  )commentN)rJ   rK   rL   rM   r   rP   rN   rR   rS   rT   rU   r}    s    	 )rS   rU   Nr   rR   rS   rT   r{  r{    s     rS   r{  c                 T   t        ||      }|d   }|j                          g }t        |      dkD  rt        j                  d      r6ddlm}  ||d   |d   dd      }|j                  j                  | 	      }nt               j                         }g }t        |      dkD  r|D ]  }	||	j                         z  } t        |d   |d   d
      }
|
j                  j                  | 	      }t        |j                  dd            }|D cg c]  }||vr|vr| }}|S c c}w )zA
    This method will return a list of company working dates
    working_days_onr   leave)LeaveRequestFilterapproved)	from_dateto_datestatusr   )ru   )attendance_date__gteattendance_date__lterH   T)flat)r"   sortlenr   is_installedleave.filtersr  qsr   r
   nonerequested_datesr   r   values_list)ru   r  r  working_datesworking_date_listattendance_datesr  approved_leave_dates_filteredapproved_leave_datesr  attendance_filtersr   rC   	date_lists                 rT   get_date_listr    su    %Y8M%&78
!W%8,>!21!504(-) -J,L,L,S,S' -T -) -5JOO,=)!,-16 @$(=(=(??$@.(9!(<(9"(=
 133::{:S++,=D+I
 &''D8L,L 	I 
 s   D%c                       e Zd ZdZ ej
                  ej                  j                  d       ej                  dddd       e
d	      
      Z ej                  dd e
d       ej                  ddddd            Z ej                  d e
d       ej                   ddd            Z ej                  d e
d       ej                   ddd            Z ej
                  ej                  j)                         dd ej                  ddi            Z G d d      Zd Z fdZd  Zd" fd!	Z xZS )#BulkAttendanceRequestFormz-
    Bulk attendance request create form
    Tr&  z#id_shift_id_parent_divr  z(/attendance/get-employee-shift?bulk=True)r`   ra   rb   rA   r(   r   Fr(  r)  r*  z-/attendance/request-new-attendance?bulk=Falser'  )r  rt   r   re   z	From DaterC   zform-control oh-input w-100)r?   r  r+  zTo DateBatchr{   r   )r   r  r   re   c                   F    e Zd ZdZeZdZ eddi       eddi      dZy)BulkAttendanceRequestForm.Metar  )ru   r,  r  r  rv   r}   rE   rF   r  r  r6   r?   r@   rA   rE   rF   N)	rJ   rK   rL   rM   r   rP   rN   r   rQ   rR   rS   rT   rU   r  \  s8    	 
 $-FF3C#D$-VV4D$E
rS   rU   c           
          | j                   |   j                  j                  j                  t	        t        j                               ddddddd       yr  rd   rj   s     rT   rm   z6BulkAttendanceRequestForm.update_worked_hour_hx_fieldsu  r  rS   c                 .   t        t        j                  dd       }|j                  j                  }t        |   |i | |r6t        |d      r*|j                  j                  }|| j                  d   _        dD ]  }| j                  |        |j                  j                  d      st        |      rjt        |t         j"                  j%                         d      }|t         j"                  j'                  |j                        z  | j                  d	   _        n<t         j"                  j'                  |j                        | j                  d	   _        t+        | j                  d
   j,                        dgz   | j                  d
   _        y )Nrequestr7  rv   r  zattendance.add_attendancezpms.add_feedback)perm)employee_user_idru   r   r   )getattrr)   _thread_localsuseremployee_getr   r   r<  r7  rv   rN   rt   rm   has_permr#   r!   r(   r   r  r   r   r   r   )	rk   r   r   r  r   shiftr   	employeesr   s	           rT   r   z"BulkAttendanceRequestForm.__init__  sm   -<<iN<<,,$)&)*>?//88E.3DKK
#+
 	5E --e4		5
 <<  !<=ATB
 8))--/6HI 3<h>N>N>U>U!( ?V ? 3DKK&/ 3;2B2B2I2I!( 3J 3DKK&/ 6:KK-.666
1263)*2rS   c                    | j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }t        j                  j                         j	                         }t        j                  j                         j                         }	t        |       t        |       t        |       t        |       t        |||      }
|r|r||kD  rt        dt        d      i      ||	k(  r||kD  rt        dd| d	i      |r t        |d
      st        t        d            t        |
      dk  rt        t        d            |S )Nr  r  r  r  rF   ru   z!To date should be after from datez-Check out time is in the future for the date .r7  r8  r   zKThere is no valid date to create attendance request between this date range)r   r   r   nowr@   r   rC   r   r   r  r	   r   r<  r  )rk   r   r  r  r  r  rF   ru   r  r   r  s              rT   r   zBulkAttendanceRequestForm.clean  sz   (( $$[1	""9-!-!1!12J!K#''7+//0FG"&&}5##%**,!!'')..034\* + )!+y'B	Y%8!9a0S.T"UVVe 4s :!*GyPQR  w{4HI!!$B"CDDy>Q!a 
 rS   c           
      f   | j                   }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	|j                  d      }
|j                  d	      }|j                  j                  }t	        |||      }|j                  d
      r|j                  d
      nd }||||||
d||	d	}|D ]  }|j                  |||d       t        |      }|j                         rH|j                  d      }d|_	        ||_
        d|_        d|_        |r||_        |j                          |t        |j                          t         | !  d      }|r|j                          |S )Nru   r  r  rv   rE   rF   r6   r  r  r   T)	ru   rv   r}   rE   rF   r  r:   r  r6   )rH   rI   rG   r  Fr   r:  )r   r   r7  r}   r  rf   r$  is_validr   r:   ru   r8   is_bulk_requestr   loggerr   r   )rk   r   r   ru   r  r  rv   rE   rF   r6   r  r  r}   r  batchinitial_datarC   r   rp   r   s                      rT   r   zBulkAttendanceRequestForm.save  s   (("&&}5 $$[1	""9-##J/*../DE+//0FG*../DE!-!1!12J!K#''7"55BB!+y'B	  56 23 	 ' (#6$8&<#'(#6

  	$D'+0415 "|4D}}99E92/3,'2$(8%+/(38H0t{{#'	$( 7<u<-MMOrS   r   )rJ   rK   rL   rM   r   r-  r(   r   r   r.  r   ru   r/  r0  r,  	DateFieldr   r  r  r   r  r   rU   rm   r   r   r   r   r   s   @rT   r  r  *  sf    )%((!!((4(8u||6&D
 
m
K %%$$"u""&0&I	
	K  nu!,IJ
I eoo	lu!,IJ
G 1%00 ((,,.u||:/P"QR	
 
2
3:!F7 7rS   r  c                   $    e Zd ZdZ G d d      Zy)WorkRecordsFormz
    WorkRecordForm
    c                       e Zd ZdZdZeZy)WorkRecordsForm.Metar  r2   N)rJ   rK   rL   rM   rN   r   rP   rR   rS   rT   rU   r    s    	 rS   rU   Nr   rR   rS   rT   r  r    s     rS   r  c                   L     e Zd ZdZ ed      Z G d d      Zd Z fdZ xZ	S )BatchAttendanceFormz
    BatchAttendanceForm
    zCreate attendance batchc                       e Zd ZdZdZeZdgZy)BatchAttendanceForm.Metar  r2   r=   N)rJ   rK   rL   rM   rN   r   rP   rO   rR   rS   rT   rU   r    s    	 -rS   rU   c                 .    ||f}d| i}t        d|      }|S rw  r   r4  s         rT   r   zBatchAttendanceForm.as_p  rx  rS   c                 r    t        |   |i | | j                  j                  rt	        d      | _        y y )NzUpdate attendance batch)r   r   rp   r  r   rJ  r   s      rT   r   zBatchAttendanceForm.__init__$  s5    $)&)== !"; <D rS   )
rJ   rK   rL   rM   r   rJ  rU   r   r   r   r   s   @rT   r  r    s.     ./L   = =rS   r  )^rM   r   r>  loggingrh   calendarr   collectionsr   typingr   r   djangor   django.appsr   django.core.exceptionsr	   django.db.models.queryr
   django.formsr   r   r   django.template.loaderr   django.utils.htmlr   django.utils.translationr   r   attendance.filtersr   attendance.modelsr   r   r   r   r   r   r   r   r   r   r   r   
base.formsr   BaseModelFormr    base.methodsr!   r"   r#   r$   base.modelsr%   r&   employee.filtersr'   employee.modelsr(   horillar)   2horilla_widgets.widgets.horilla_multi_select_fieldr*   &horilla_widgets.widgets.select_widgetsr+   	getLoggerrJ   r  r-   r   r   ChoiceFieldr   r   r   r  r  r$  rP  FormrI  rY  re  rh  rj  ru  r{  r  r  r  r  rR   rS   rT   <module>r     s  .      #    2 + < < 3 ) 6 0    2 (  / + $ ' V K			8	$= DJ ] J Z!*] !*H	;u(( 	;(] (V] 7  7 ty#M y#xn* nb$5:: @! !H5:: 65:: 6CM C8T%** T0= /dP Pfm =- =rS   