
    vg/                       d Z ddlZddlZddlmZmZ ddlmZ  ej                  e	      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mZmZ ddlmZ ddlZddlmZ dd	lmZ dd
lmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z2 ddl3m4Z4 ddl5m6Z7 ddl5m8Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD ddlEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQ ddlRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] ddl^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZi ddljmkZk ddllmmZm ddlnmoZompZpmqZq ddlrmsZsmtZtmuZumvZvmwZwmxZxmyZy ddlzm{Z{m|Z|m}Z}m~Z~ dd lmZ dd!lmZmZ dd"lmZmZmZmZmZ dd#lmZ d$ Zeed%               Ze ed&      d'               Zee ed(      d)                      Ze ed(      d*               Ze ed(      d+               Zed,        Ze ed-      d.               Zee ed/      d0                      Ze ed1       e;d2g      d3                      Ze ed1       e;d2g      d4                      Zed5        Zee ed6      d7                      Zed8        Zd9 Ze ed:      ed;                      Ze ed<       e;d2g      d=                      Ze ed>      d?               Zed@        ZedA        Ze edB       e;d2dCg      dD                      Ze edB       e;d2g      dE                      ZdF ZdG Ze edH      dI               Ze edH      dJ               Ze edK      dL               ZedM        ZeedN               ZeedO               Ze edP      e e;d2g      dQ                             Ze edP       e;d2g      dR                      Ze edS      dT               Ze edU       e;d2g      dV                      Ze e;d2g       ed/      dW                      Ze ed/      dX               ZedY        Ze ed/      dZ               Ze ed/      d[               Zed\        Zeed]               Zeed^               Zed_        Zed`        Zeeda               Zedb        Zedc        Zedd        Zede        Zedf        Zedg        Zee edh      di                      Zee edj      dk                      Zee edl      dm                      Ze edn      do               Ze edp      dq               Ze edp      dr               Zeds        Zedt        Zedu        Zedv        Zedw        Zeedx               Ze edy      dz               Zee ed{      d|                      Ze ed}      d~               Ze ed      d               Zed        Zee ed      d                      Ze ed      d               Ze ed      d               Ze ed      d               Zee edU      d                      Ze ed(      d               Ze ed(      d               Zd Ze ed(      d               Ze ed1      d               Ze ed/      d               Zy)a  
views.py

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

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

This module is part of the recruitment project and is intended to
provide the main entry points for interacting with the application's functionality.
    N)DYNAMIC_URL_PATTERNSHORILLA_DATE_FORMATSremove_dynamic_urldefaultdict)datedatetime	timedelta)parse_qs)messages)	Paginator)validate_ipv46_address)transaction)ProtectedError)ValidationError)HttpResponseHttpResponseBadRequestHttpResponseRedirectJsonResponse)redirectrender)render_to_string)reverse)timezone)now)gettext)gettext_lazy)require_http_methods)AttendanceActivityFilterAttendanceActivityReGroupAttendanceFiltersAttendanceOverTimeFilterAttendanceOvertimeReGroupAttendanceReGroupLateComeEarlyOutFilterLateComeEarlyOutReGroup)AttendanceActivityExportFormAttendanceExportFormAttendanceFormAttendanceOverTimeExportFormAttendanceOverTimeFormAttendanceRequestCommentFormAttendanceUpdateForm!AttendanceValidationConditionFormGraceTimeAssignFormGraceTimeFormLateComeEarlyOutExportFormNewRequestForm)Requestattendance_day_checkingformat_timeis_reportingmangermonthly_leave_dayspaginator_qry
parse_dateparse_datetime
parse_timesort_activity_dictsstrtime_seconds)
AttendanceAttendanceActivityAttendanceGeneralSettingAttendanceLateComeEarlyOutAttendanceOverTimeAttendanceRequestCommentAttendanceRequestFileAttendanceValidationConditionBatchAttendance	GraceTimeWorkRecords)handle_attendance_errors)process_attendance_data)AttendanceAllowedIPFormAttendanceAllowedIPUpdateFormTrackLateComeEarlyOutForm)choosesubordinatesclosest_numberseval_validateexport_datafiltersubordinatesget_key_instancesget_pagination)AttendanceAllowedIPEmployeeShiftScheduleTrackLateComeEarlyOutWorkType)EmployeeFilter)EmployeeEmployeeWorkInformation)hx_request_requiredinstall_requiredlogin_requiredmanager_can_enterpermission_required)notifyc                     t         j                  j                         }t        d      }|j	                         rt        |d   j
                        }t        | j                        }||k\  S )z
    This method is is used to check condition for at work in AttendanceValidationCondition
    model instance it return true if at work is smaller than condition
    args:
        attendance : attendance object
    z09:00r   )rF   objectsallr>   existsvalidation_at_workattendance_worked_hour)
attendance
conditionscondition_for_at_workat_works       */var/www/horilla/attendance/views/views.pyattendance_validatern      s_     /66::<J+G4 /
10P0P Qj??@G G++    c                     | j                   }|j                  }|j                  j                         }t	        j
                  |D cg c]  }|j                   c}      }||d}t        | d|      S c c}w )z
    This function is used to view attendance tab of an employee in profile view.

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

    Returns: return asset-request-tab template

    )attendancesattendances_idsz tabs/profile-attendance-tab.html)useremployee_getemployee_attendancesre   jsondumpsidr   )requestrs   employeeru   instancerr   contexts          rm   profile_attendance_tabr}      sp     <<D  H#88<<>jj>R!S((++!STO+*G '=wGG "Ts   A2zemployee.view_employeec                     t         j                  j                  d|      }t        j                  |D cg c]  }|j
                   c}      }t         j                  j                  d|      }t        j                  |D cg c]  }|j
                   c}      }t        j                  j                  |      }t        j                  |D cg c]  }|j
                   c}      }||||||d}	t        | d|	      S c c}w c c}w c c}w )	z
    This function is used to view attendance tab of an employee in individual view.

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

    Returns: return attendance-tab template
    T)is_validate_requestemployee_idF)attendance_validatedr   r   )requestsrr   accountsaccounts_idsvalidate_attendancesvalidate_attendances_idsztabs/attendance-tab.htmlr|   )r?   rd   filterrv   rw   rx   rC   r   )
ry   emp_idr   r{   rr   r   r   r   r   r|   s
             rm   attendance_tabr      s     !!((  ) H jjh!G((++!GHO%--44" 5   $zz%9::  "))00V0DH::8Dxx{{DEL *$ 4$<G '5wGG% "H
 	; Es   DDDattendance.add_attendancec                 ^   | j                   j                  d      rG| j                   j                         }| j                   j                  d      }||d<   t	        |      }n
t	               }t        | |d      }| j                  dk(  rt	        | j                        }t        | |d      }|j                         re|j                          t        j                  | t        d             t        | dd|i      }t        |j                  j!                  d	      d
z         S t        | dd|i      S )zV
    This method is used to render attendance create form and save if it is valid
    previous_urlr   initialr   POSTzAttendance added.zattendance/attendance/form.htmlformutf-8#<script>location.reload();</script>)GETgetdictgetlistr*   rO   methodr   is_validsaver   success_r   r   contentdecode)ry   dataemployee_listr   responses        rm   attendance_creater      s    {{~&{{!++M:+]d+gt-HID~~gll+!'41LM==?IIKWa(;&<=:VTNH    ''03XX  '<vtnMMro   c                     	 g d}t        j                  |      }t        d      }d|d<   |j                  |d       |S # t        $ r}t        |      cY d	}~S d	}~ww xY w)
z
    Generate an empty Excel template for attendance data with predefined columns.

    Returns:
        HttpResponse: An HTTP response containing an empty Excel template with predefined columns.
    )
Badge IDShiftz	Work typezAttendance datezCheck-in datezCheck-inzCheck-out datez	Check-outzWorked hourzMinimum hourcolumnsapplication/ms-excelcontent_typez)attachment; filename="my_excel_file.xlsx"Content-DispositionFindexN)pd	DataFramer   to_excel	Exception)_requestr   
data_framer   	exceptions        rm   attendance_excelr      sg    '
 \\'2
-CD*U&'HE2 'I&&'s   ?A 	A 
AA A c                 L   | j                   dk(  rO| j                  d   }t        j                  |      }|j	                  d      }t        |      }d}|rt        |      }t              t              z
  }|t        |      t        d      d}t        d|      }t        |      S )a  
    Save the import of attendance data from an uploaded Excel file, validate the data,
    and return an Excel file with error details if validation fails for anyone
    of the attendance data.

    Parameters:
        request (HttpRequest): The HTTP request object containing the uploaded Excel file.

    Returns:
        HttpResponse or redirect: An HTTP response with an Excel file containing error details
        if validation fails, or a redirect to the attendance view if successful.
    r   attendance_importrecordsNr?   created_counterror_countmodel	path_infoimport_popup.html)r   FILESr   
read_excelto_dictrK   rJ   lenr   r   r   )	ry   filer   attendance_dictsr   r   created_attendance_countr|   htmls	            rm   r   r     s     ~~}}01]]4(
%--i834DE	01BCI"#34s;L7MM1,-<	G /9Dro   c                 
   | j                   }|rZ|j                  rN|j                  dk(  r?t        | dt        t        j
                  j                               t               d      S t        | t        t        t        d      S )Nzattendance-info-export-formz(attendance/attendance/export_filter.htmlquerysetexportexport_formr   Attendance_exportry   r   filter_class
form_class	file_name)	resolver_matchurl_namer   r"   r?   rd   re   r)   rR   )ry   r   s     rm   attendance_exportr   7  s{    ++N####'DD6+Z5G5G5K5K5MN35
 	
 &'% ro   attendance.view_attendancec                    | j                   j                         }t               }t        j                  j                         }t        d      }|!|j                  t        |j                        }t        j                  j                  dd      }t        j                  j                  dd      }t        j                  j                  ddd      }t        | j                   |      }t        | |j                  d      }t        | j                   |      j                  }t        | |d      }t        | j                   |      j                  }t        | |d      }t        j                  j                         }	|	j                         rd	}
nd
}
t        j                   t#        || j                   j%                  d            j&                  D cg c]  }|j(                   c}      }t        j                   t#        || j                   j%                  d            j&                  D cg c]  }|j(                   c}      }t        j                   t#        || j                   j%                  d            j&                  D cg c]  }|j(                   c}      }t+        | |
||||||t,        j.                  d      S c c}w c c}w c c}w )z2
    This method is used to view attendances.
    00:00FT)r   employee_id__is_activer   )overtime_second__gtr   r   r   r   z*attendance/attendance/attendance_view.htmlz+attendance/attendance/attendance_empty.htmlvpageopagepage)r   r   ot_attendances_idsrr   fr   	gp_fields)r   	urlencoder*   rF   rd   firstr>   minimum_overtime_to_approver?   r   r"   rS   qsre   rf   rv   rw   r9   r   object_listrx   r   r%   fields)ry   previous_datar   	conditionminotr   rq   ot_attendances
filter_objcheck_attendancetemplater{   r   r   rr   s                  rm   attendance_viewr   P  s    KK))+MD-55;;=IG$E!F!F!R	 E EF%--44"4 5  $$++!$ , K  ''..!# / N
 #7;;EJ$ <K -2b  .%'C 'w{{^LOON'!=N "))--/ ?@#zz *$gkkoog&>k		
 KK	
   * 8k		
 KK	
 jj *W[[__V4k		
 KK	
O  )A"4.*11	
 /	
	
	
s   K,KKattendance.change_attendancec                 ~   t         j                  j                  |      }| j                  j                  d      r%t	        | j                  j                               }nt	        |      }t        | |d      }| j                  dk(  rt	        | j                  |      }t        | |d      }|j                         rY|j                          t        j                  | t        d             | j                  j                         }d| }t        d	      S t!        | d
|| j                  j                         |d      S )z}
    This method render form to update attendance and save if the form is valid
    args:
        obj_id : attendance id
    rx   r   r   r{   r   r   zAttendance Updated./attendance/attendance-view/?z~
                    <script>
                        window.location.reload();
                    </script>
                z&attendance/attendance/update_form.html)r   r   obj_id)r?   rd   r   r   r.   r   rO   r   r   r   r   r   r   r   r   r   r   )ry   r   ri   r   r   modified_urls         rm   attendance_updater     s    ##''6'2J{{~&#GKK,<,<,>?#
 gt-KLD~~#GLL:F!'41OP==?IIKWa(=&>?--/I:9+FL  0GKK$9$9$;vN ro   zattendance.delete_attendancer   c           	      R   	 t         j                  j                  |      }|j                  }|j	                  d      j                         }|j                  j                  j                  |      j                         }||j                  rZt        |j                        }t        |j                        }||kD  r||z
  }n||z
  }t        |      |_        |j                          	 |j!                          t#        j$                  | t'        d             tC        | jD                  j                  d	d
            S # t(        $ r}t+               }|j,                  D ]>  }	|j/                  t1        |	j2                  j4                  j7                                      @ dj9                  |      }
t#        j:                  | t'        dj=                  |
                   Y d}~d}~ww xY w# t         j>                  t@        f$ r# t#        j:                  | t'        d             Y w xY w)z\
    This method is used to delete attendance.
    args:
        obj_id : attendance id
    r   %B)monthNzAttendance deleted., z*An attendance entry for {} already exists.zAttendance Does not exists..HTTP_REFERER/)#r?   rd   r   attendance_datestrftimelowerr   employee_overtimer   lastattendance_overtime_approver>   overtimeattendance_overtimer6   r   deleter   r   r   r   setprotected_objectsadd___metaverbose_name
capitalizejoinerrorformatDoesNotExistOverflowErrorr   META)ry   r   ri   r   r  total_overtimeattendance_overtime_secondsemodel_verbose_names_setobjmodel_names_strs              rm   attendance_deleter    s   #C''++v+6
**t$**,));;BBBOTTV55!01B1B!C.=22/+ "$??%36Q%QN%@>%QN$/$?!!!#  !,A*BC    0 0 EFF " *-%'.. YC+//3993I3I3T3T3V0WXY"&)),C"DEMM+  ##]3 Cw"@ ABCs7   C'G* */D> >	G'BG"G* "G''G* *8H&%H&c           
         d}g }| j                   j                  dd      }t        j                  j	                  |      }|j                  dd      }t        j                  j	                  |      j                         }t        j                         5  |D ]  }	 |j                  j                  d	      j                         }|j                  |j                  j                        }	|	rd|j                   rXt#        |	j$                        }
t#        |j&                        }t)        |
|z
        }
t+        |
      |	_        |	j-                          |j/                          |d
z  } 	 ddd       |rtA        jB                  | | d       |D ]  }tA        jD                  | |        tG        d      S # t0        $ r{}|j2                  D ch c]/  }t5        |j6                  j8                  j;                               1 nc c}w }}dj=                  |      }|j?                  d| d       Y d}~d}~ww xY w# 1 sw Y   xY w)z=
    This method is used to delete a bulk of attendances
    r   ids[]id__inr   Tflat)employee_id__inr      r   z%An attendance entry is protected by: .Nz" attendances deleted successfully.z/attendance/attendance-search)$r   r   r?   rd   r   values_listrC   in_bulkr   atomicr   r   r   r   r   rx   r  r>   r  r  absr6   r   r  r   r  r	  r
  r  r  r  appendr   r   r  r   )ry   success_counterror_messagesr  rq   employee_ids	overtimesri   r   r  r  r  r  r  r  r  r  s                    rm   attendance_bulk_deleter-    s#    MN
,,

ud
+C$$++3+7K**=t*DL"**11$ 2 gi  
			 % 	J"22;;DAGGI$==)?)?)B)BC
 F F%4X5F5F%GN2A"663/ &):U)U%VN(3N(CH%MMO!!#"!	< ]O3U"VW 'w&'344 "   !22+ syy--88:;+ +' + #')),C"D%%;O;LAN ' sC   H;C	F4&H;4	H8=H34H 
?.H3-H;3H88H;;Ic           
      &   | j                   }	 |j                  }|j                  }|j                  j	                         }t               }|j                         rd}nd}t        j                  t        || j                  j                  d            j                  D cg c]  }|j                   c}      }t        | |t        || j                  j                  d            ||t        j                   d      S #  t        d      cY S xY wc c}w )zB
    This method is used to view self attendances of employee
    z/employee/employee-profilez3attendance/own_attendance/view_own_attendances.htmlz(attendance/own_attendance/own_empty.htmlr   )rq   rr   r   r   )rs   rt   r   ru   re   r"   rf   rv   rw   r9   r   r   r   rx   r   r%   r   )ry   rs   rz   ru   r   r   r{   rr   s           rm   view_my_attendancer/  ;  s    
 <<D6$$   H#88<<> F""$H=jj *$gkkoof&=k		
 KK	
O ()=w{{v?VW.*11		
	 	#6455	
s   C< D<D!attendance.add_attendanceovertimec                    t               }t        | |d      }| j                  dk(  rt        | j                        }t        | |d      }|j	                         re|j                          t        j                  | t        d             t        | dd|i      }t        |j                  j                  d      dz         S t        | dd|i      S )z\
    This method is used to render overtime creating form and save if the form is valid
    r0  r   zAttendance account added.z'attendance/attendance_account/form.htmlr   r   r   )r,   rO   r   r   r   r   r   r   r   r   r   r   r   )ry   r   r   s      rm   attendance_overtime_creater2  `  s     "#Dgt-PQD~~%gll3!'41TU==?IIKWa(C&DEBVTNH    ''03XX  'DvtnUUro   c                 0   | j                   j                         }t        | j                         }|j                  j	                         rd}nd}|j                  j                  | j                        }t        | |j                  d      }||z  }|j                         }t               }t        | |d      }t        |      }t        t        |       t        | |t        || j                   j!                  d            |||t"        j$                  |d      S )zM
    This method is used to view attendance account or overtime account.
    z;attendance/attendance_account/attendance_overtime_view.htmlz1attendance/attendance_account/overtime_empty.htmlemployee_id__employee_user_id"attendance.view_attendanceovertimer0  r   )r   r   r   r   r   filter_dict)r   r   r#   r   rf   r   rs   rS   distinctr,   rO   r   rT   rC   r   r9   r   r$   r   )ry   r   r   r   self_accountr   r   	data_dicts           rm   attendance_overtime_viewr;  x  s    
 KK))+M)'++6J}}PF==''gll'SL! DH ,&H  "H!#Dgt-PQD'I()4%h0GH299$	
 ro   c                     | j                   j                  d      dk(  r#t               t               d}t	        | d|      S t        | t        t        t        d      S )NHTTP_HX_REQUESTtrue)
export_objexport_fieldszCattendance/attendance_account/attendance_account_export_filter.htmlr   Attendance_Accountr   )r  r   r#   r+   r   rR   rC   ry   r|   s     rm   attendance_account_exportrC    sc    ||)*f4249;

 Q
 	

  -/& ro   $attendance.change_attendanceovertimec                    t         j                  j                  |      }t        |      }t	        | |d      }| j
                  dk(  rt        | j                  |      }t	        | |d      }|j                         re|j                          t        j                  | t        d             t        | dd|i      }t        |j                  j                  d      d	z         S t        | dd|i      S )
z
    This method is used to update attendance overtime and save if the forms is valid
    args:
        obj_id : attendance overtime id
    r   r   rD  r   z(Attendance account updated successfully.z.attendance/attendance_account/update_form.htmlr   r   r   )rC   rd   r   r,   rO   r   r   r   r   r   r   r   r   r   r   r   )ry   r   r  r   r   s        rm   attendance_overtime_updaterF    s     "))---8H!84Dgt-STD~~%gllXF!'41WX==?IIKWa(R&ST@H
    ''03XX  AFD> ro   z$attendance.delete_attendanceoverTimec                    | j                   j                         }| j                  j                  dd      }	 t        j
                  j                  |      }|j                          |dk(  rt        j                  | t        d             |rL|dk(  rGt        j
                  j                         }|j!                         rt#        d|       S t%        d	      S |r
t%               S y# t        j                  t        t        f$ r' |dk(  rt        j                  | t        d             Y t        $ r' |dk(  rt        j                  | t        d             Y w xY w)
zm
    This method is used to delete attendance overtime
    args:
        obj_id : attendance overtime id
    HTTP_HX_TARGETNr   zot-tablezHour account deleted.zHour account not foundz#You cannot delete this hour accountz'/attendance/attendance-overtime-search?)<script>window.location.reload()</script>)r   r   r  r   rC   rd   r  r   r   r   r  r  
ValueErrorr  r   re   rf   r   r   )ry   r   r   	hx_targetri   hour_accounts         rm   attendance_overtime_deleterM    s4    KK))+M  !148I
N'//33v3>

"Wa(?&@A Y*,)11557 Em_UVV KLL	~ 
 ++]JG A
"NN7A&>$?@ N
"NN7A&K$LMNs   AC' 'AE+,EEz$attendance.delete_attendanceovertimec                 (   | j                   d   }t        j                  |      }|D ]l  }	 t        j                  j                  |      }|j                          t        j                  | t        d      j                  |j                               n t        dd	i      S # t        j                  $ r" t        j                  | t        d             Y t        $ r2 t        j                  | t        d      j                               Y w xY w)
z8
    This method is used to bulk delete for Payslip
    r  r   z {employee} hour account deleted.rz   zHour account not found.z You cannot delete {hour_account})rL  messageSuccess)r   rv   loadsrC   rd   r   r  r   r   r   r  r   r  r  r   r   )ry   r  rx   rL  s       rm   attendance_account_bulk_deleterS    s     ,,u
C
**S/C 	-5599R9@L!45<<)55 = 	" I.// ".. 	BNN7A&?$@A 	NN45<<,<W	s   A)B##2D7DDc           
      T   | j                   j                         }t        | j                         }|j                  }|j	                  | j
                        }t        | |j                  d      }||z  }|j                         }|j                  d      }t        j                  t        |d      D cg c]  }|j                   c}      }|j                         rd}nd}t        | |t        || j                   j                  d            ||t         j"                  |d      S c c}w )	zN
    This method will render a template to view all attendance activities
    r4  r6  z-pkNz<attendance/attendance_activity/attendance_activity_view.htmlz2attendance/attendance_activity/activity_empty.htmlr   )r   r   r   r   activity_ids)r   r   r    r   r   rs   rS   r8  order_byrv   rw   r9   rx   rf   r   r   r!   r   )ry   r   r   attendance_activitiesself_attendance_activitiesr{   rU  r   s           rm   attendance_activity_viewrY    s   
 KK))+M)'++6J&MM!6!=!=&-ll "> " / D 24NN1::<1::5A::%23H$%OPPL ##%QG!"79PQ299(	

 
 	Qs   2D%c                    | j                   j                         }|j                  dd        |j                         }t        j
                  j                  |      j                         }| j                   d   }|rt        j                  |      ng }t        ||      \  }}|||||d}	|r=t        j
                  j                  |j                        j                         }
|
|	d<   t        | d|	      S )Ninstances_idsr   )r   activityprevious_instancenext_instanceinstance_ids_json)r   ri   z>attendance/attendance_activity/single_attendance_activity.htmlr   )r   copypopr   r@   rd   r   r   rv   rR  rP   r?   r   r   )ry   r   request_copyr   r\  r_  instance_idsr]  r^  r|   ri   s              rm   activity_single_viewrd  0  s    ;;##%L_d+ **,M!))00F0;AACHO44E4::/02L'6|V'L$}.&.G ''..$44 / 

%' 	 !+H ro   z$attendance.delete_attendanceactivityDELETEc                 "   | j                   j                         }|j                  dd       |j                         }	 t        j
                  j                  |      j                          t        j                  | t        d             | j                   j                  d      st        d|       S | j                   j                  d      }t        j                   |      }||v r|j#                  |       t%        t        j                   |      |      \  }}t        d| d	| d
|       S # t        j                  $ r" t        j                  | t        d             Y t        $ r# t        j                  | t        d             Y w xY w)zm
    This method is used to delete attendance activity
    args:
        obj_id : attendance activity id
    r[  Nr   zAttendance activity deletedz%Attendance activity Does not exists..zYou cannot delete this activityz'/attendance/attendance-activity-search?z,/attendance/attendance-activity-single-view//?&instances_ids=)r   r`  ra  r   r@   rd   r   r  r   r   r   r  r  r   r   rv   rR  removerP   ry   r   rb  r   r[  instances_listr]  r^  s           rm   attendance_activity_deleterl  N  s[    ;;##%L_d+ **,MF""&&&&188:!$A"BC
 ;;???+A-QRR8M2^#!!&)+:JJ}%v,
(= :=/M?Zijxiyz
 	
 ** Lw"I JK Fw"C DEF   AD/ /2F#'FFc                    | j                   d   }t        j                  |      }|D ]l  }	 t        j                  j                  |      }|j                          t        j                  | t        d      j                  |j                               n t!        ddi      S # t        j                  t        t        f$ r" t        j                  | t        d             Y w xY w);
    This method is used to delete bulk of attendances
    r  r   z{employee} activity deleted.rO  Attendance not found.rP  rQ  )r   rv   rR  r@   rd   r   r  r   r   r   r  r   r  r  rJ  r  r   )ry   r  attendance_idr\  s       rm   attendance_activity_bulk_deleterr  p  s     ,,u
C
**S/C 
@		@)11555GHOO0188(BVBV8W	
@ I.// #//
K 	@NN7A&=$>?	@   A)B##=C#"C#c                 `   ddl m}m} | sg S t        |       }g }|D ]  }|j	                  d      }|sd|d<   |j                  |       .t        j                  j                  |      j                         }|sd|d<   |j                  |       ut        |d	   d
|      }t        |d   d|      }	t        j                  |d         st        |d         nd }
t        j                  |d         st        |d         nd }t        d |j                         D              r|j                  |       |
rG	  |t!        |j"                  ||
t%        j&                  t)        j*                  ||
                         |se|	si	  |t!        |j"                  |	|t%        j&                  t)        j*                  |	|                          |S # t,        $ r#}d| |d<   |j                  |       Y d }~{d }~ww xY w# t,        $ r$}d| |d<   |j                  |       Y d }~d }~ww xY w)Nr   )clock_in	clock_outr   z2Please add the Badge ID column in the Excel sheet.zError 1)badge_idzInvalid Badge IDzError 2In DatezError 4Out DatezError 5Check In	Check Outc              3   >   K   | ]  }|j                  d         yw)ErrorN)
startswith).0keys     rm   	<genexpr>z)process_activity_dicts.<locals>.<genexpr>  s     B3s~~g&Bs   )rs   r	   timer
   z Got an error in import clock in zError 6z!Got an error in import clock out zError 7)attendance.views.clock_in_outru  rv  r=   r   r(  r[   rd   r   r   r:   r   isnar<   anykeysr4   employee_user_iddjango_timezone
make_awarer
   combiner   )activity_dictsru  rv  sorted_activity_dictserror_dictsr\  rw  rz   check_in_datecheck_out_datecheck_in_timecheck_out_timer  s                rm   process_activity_dictsr    s8   A	/?K) <-<<
+"VHYx(##**H*=CCE"4HYx("8I#6	8L#HZ$8)XN 778J/0 x
+, 	 778K01 x,- 	 B(--/BBx(-%66**!0!;!;$,,]MJ"		 n-%66++!0!;!;$,,^^L"		a<-| )  -(H&L#""8,,-   -(I!&M#""8,,-s2   7AGAH 	G=G88G= 	H-	H((H-c                 .   t        j                  |       }t        d      dd<   |j                  d       fd}dd	lm}m} d
t        j                          |j                   ||             t        j                         d S )Nr   r   z'attachment; filename="ImportError.xlsx"r   Fr   c                     t               S Nr   )ry   r   r   s    rm   get_activity_error_sheetz>handle_activity_import_error.<locals>.get_activity_error_sheet  s    9%ro   r   )pathurlpatternszactivity-error-sheet-)namezattendance/)r   r   r   r   attendance.urlsr  r  uuiduuid4r(  r   )
error_datar   r  r  r  r   r   s        @@rm   handle_activity_import_errorr    s     j)J )?@H&OH"#. 2 (

~6ItI'?iPQ	* i[)Iro   z!attendance.add_attendanceactivityc                    | j                   dk(  r| j                  d   }t        j                  |      }|j	                  d      }|r|t        |      }t        |      }t        |      t        |      z
  }|t        |      t        d      |d}t        d|      }t        j                  | t        d             t        |      S t        | d      S )	Nr   activity_importr   zAttendance Activityr   r   z)Attendance activity imported successfullyz3attendance/attendance_activity/import_activity.html)r   r   r   r   r   r  r  r   r   r   r   r   r   r   )	ry   r   r   r  import_error_dictsr   created_activity_countr|   r   s	            rm   attendance_activity_importr    s     ~~}}./]]4(
#++I6!7!G45GHI%(%83?Q;R%R"!7"#5601&	G $$7ADWa(S&TU%%'PQQro   c                     | j                   dk(  r>t        j                  g d      }t        d      }d|d<   |j	                  |d	       |S y )
Nr   )r   r[   zAttendance Daterx  rz  r{  ry  r   Aapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetr   z*attachment; filename="activity_excel.xlsx"r   Fr   )r   r   r   r   r   )ry   r   r   s      rm    attendance_activity_import_excelr    s]     ~~\\


  \
 +W&'HE2# ro   z$attendance.change_attendanceactivityc                     | j                   j                  d      dk(  rCt               }|t        t        j
                  j                               d}t        | d|      S t        | t        t        t        d      S )	Nr=  r>  r   )r   r   z1attendance/attendance_activity/export_filter.htmlr   Attendance_activityr   )	r  r   r(   r    r@   rd   re   r   rR   )ry   r   r|   s      rm   attendance_activity_exportr    s~     ||)*f424&.+33779
 ?
 	

  -/' ro   c                 &   t        | j                        j                  }t        j                  j                  |D cg c]  }|j                   c}d      j                  dd      }|j                  |      }d|i}t        | d|	      S c c}w )
zK
    This method render template to view all on come early out entries
    	late_come)attendance_id__id__intypeattendance_id__idTr  r  rq   z-attendance/attendance/attendance_on_time.htmlr   )
r"   r   r   rB   rd   r   rx   r$  excluder   )ry   total_attendancesri   ids_to_excluder|   s        rm   on_time_viewr  2  s    
 *'++699/77>>?PQz}}Q ?  k%Dk1 
 *111H(G @'  Rs   Bc           
      x   t        | j                        }|j                  j                         rd}nd}|j                  j	                  | j
                        }t        | |j                  d      }||z  }|j                         }t        j                  t        |d      D cg c]  }|j                   c}      }| j                  j                         }t        |      }t        t        |       t!        | |t        || j                  j#                  d            |t$        j&                  ||d      S c c}w )zM
    This method render template to view all late come early out entries
    z+attendance/late_come_early_out/reports.htmlz1attendance/late_come_early_out/reports_empty.htmlr4  *attendance.view_attendancelatecomeearlyoutNr   )r   r   r   r7  late_in_early_out_ids)r&   r   r   rf   r   rs   rS   r8  rv   rw   r9   rx   r   r   rT   rB   r   r   r'   r   )	ry   r   r   self_reportsreportsr{   r  r   r:  s	            rm   late_come_early_out_viewr  F  s    (4J}}@F==''gll'SL  LG $G G JJ%27D%ABB KK))+M'I0)<!'7;;??6+BC077$%:	

 
 	Cs   $D7c                 p   | j                   j                         }|j                  dd        |j                         }t        j
                  j                  |      j                         }| j                   d   }|rt        j                  |      ng }t        ||      \  }}|||||d}	t        | d|	      S )Nr[  r   )late_in_early_outr]  r^  r_  r   z1attendance/late_come_early_out/single_report.htmlr   )r   r`  ra  r   rB   rd   r   r   rv   rR  rP   r   )
ry   r   rb  r   r  r_  rc  r]  r^  r|   s
             rm   late_in_early_out_single_viewr  k  s     ;;##%L_d+ **,M2::AAVALRRTO44E4::/02L'6|V'L$}..&.G Dg ro   z,attendance.delete_attendancelatecomeearlyoutc                 "   | j                   j                         }|j                  dd       |j                         }	 t        j
                  j                  |      j                          t        j                  | t        d             | j                   j                  d      st        d|       S | j                   j                  d      }t        j                   |      }||v r|j#                  |       t%        t        j                   |      |      \  }}t        d| d	| d
|       S # t        j                  $ r" t        j                  | t        d             Y t        $ r# t        j                  | t        d             Y w xY w)z
    This method is used to delete the late come early out instance
    args:
        obj_id : late come early out instance id
    r[  Nr   zLate-in early-out deletedz#Late-in early-out does not exists..z(You cannot delete this Late-in early-outz'/attendance/late-come-early-out-search?z*/attendance/late-in-early-out-single-view/rg  rh  )r   r`  ra  r   rB   rd   r   r  r   r   r   r  r  r   r   rv   rR  ri  rP   rj  s           rm   late_come_early_out_deleter    s[    ;;##%L_d+ **,MO"**..&.9@@B!$?"@A
 ;;???+A-QRR8M2^#!!&)+:JJ}%v,
(= 8r-Xghvgwx
 	
 &22 Jw"G HI Ow"L MNOrm  c                    | j                   d   }t        j                  |      }|D ]l  }	 t        j                  j                  |      }|j                          t        j                  | t        d      j                  |j                               n t!        ddi      S # t        j                  t        t        f$ r" t        j                  | t        d             Y w xY w)ro  r  r   z%{employee} Late-in early-out deleted.rO  rp  rP  rQ  )r   rv   rR  rB   rd   r   r  r   r   r   r  r   r  r  rJ  r  r   )ry   r  rq  r  s       rm   late_come_early_out_bulk_deleter    s     ,,u
C
**S/C @
	@2::>>->PI9:AA&22 B 	@ I.// +77
S 	@NN7A&=$>?	@rs  z,attendance.change_attendancelatecomeearlyoutc                     | j                   j                  d      dk(  rAt        t        j                  j                               t               d}t        | d|      S t        | t        t        t        d      S )	a  
    Export late come early out data to an Excel file.
    This view function takes a GET request and exports attendance late come early out data into an Excel file.
    The exported Excel file will include the selected fields from the AttendanceLateComeEarlyOut model.
    r=  r>  r   r   z1attendance/late_come_early_out/export_filter.htmlr   
Late_come_r   )	r  r   r&   rB   rd   re   r2   r   rR   rB  s     rm   late_come_early_out_exportr    s{     ||)*f4,3;;??A 67	
 ?
 	

 (+- ro   z/attendance.change_attendancevalidationconditionc                    	 t         j                  j                  |      j                          t	        j
                  | t        d             t        d      S # t         j                  $ r, t	        j                  | t        d             Y t        d      S t        $ r, t	        j                  | t        d             Y t        d      S w xY w)zx
    This method is used to delete created validation condition
    args:
        obj_id  : validation condition id
    r   zvalidation condition deleted.z&validation condition Does not exists..z,You cannot delete this validation condition.z%/attendance/validation-condition-view)rF   rd   r   r  r   r   r   r  r  r   r   )ry   r   s     rm   validation_condition_deleter    s    S%--11V1<CCE!$C"DE
 ;<<	 )55 Mw"J KL ;<<  Sw"P QR;<<Ss   AA 2C'CCc                 n   t        j                  | j                  d         }d}g }g }|D ].  }	 t        j                  j                  |      }|j                  r?|j                  t        d      j                  |j                  |j                               pd|_        |j                          |dz  }t        j                  | j                   j"                  |j                  j$                  d|j                   dd	|j                   d
|j                   dd|j                   d|j                   dt'        d      d|j(                   z   d	       1 |dkD  r.t1        j2                  | t        d      j                  |             ||z   D ]3  }d|v rt1        j4                  | |       t1        j6                  | |       5 t9        ddi      S # t        j*                  $ r |j                  t        d             Y t,        t.        f$ r |j                  t        d             Y w xY w)z@
    This method is used to validate a bulk of attendances.
    r  r   r   z<Pending attendance update request for {}'s attendance on {}!Tr"  Your attendance for the date  is validatedu2   تم التحقق من حضورك في تاريخ u    Ihre Anwesenheit für das Datum u    wurde bestätigtz+Se ha validado su asistencia para la fecha    Votre présence pour la date u    est validéeview-my-attendance?id=	checkmark	recipientverbverb_arverb_deverb_esverb_frr   iconAttendance not foundzInvalid attendance IDz{} Attendances validated.PendingrP  r   )rv   rR  r   r?   rd   r   r   r(  r   r  r   r   r   r   rb   sendrs   rt   r  r   rx   r  r  rJ  r   r   infor  r   )ry   r  validate_req_countsuccess_messagesr*  r   ri   msgs           rm   validate_bulk_attendancer    s!    **W\\%(
)CN  >	>#++//6/:J--%%VfZ33Z5O5OP
 .2J+OO!# KK))$00AA4Z5O5O4PP]^LZMgMgLhi::;U;U:VVghEjF`F`Eab7
8R8R7SS`a !564
9OO 
# >F AQ23::;MN	
  .0 )MM'3'NN7C(	) I.//! && 	=!!!$:";<z* 	>!!!$;"<=	>s   A*GB?G-H4(H43H4c                 X   	 t         j                  j                  |      }d|_        |j	                          | j
                  j                         }d| }t        j                  | |j                   d|j                  j                  d       dz   t        d      z          t        j                  | j                  j                   |j                  j"                  d|j                   dd	|j                   d
|j                   dd|j                   dd|j                   dt%        d      d|j&                   z   d	       t/        | j0                  j                  dd            S # t         j(                  t*        f$ r" t        j,                  | t        d             Y _w xY w)zZ
    This method is used to validate attendance
    args:
        id  : attendance id
    r   Tr    %d %b %YzAttendance validated.r  r  u+   تم تحقيق حضورك في تاريخ u!   Deine Anwesenheit für das Datum u    ist bestätigt.z&Se valida tu asistencia para la fecha r#  r  u    est validée.r  r  r  r  r  r   r   )r?   rd   r   r   r   r   r   r   r   r   r   r   r   rb   r  rs   rt   r  r   rx   r  rJ  r  r   r  ry   r   ri   r   r   s        rm   validate_this_attendancer  )  s   ;''++v+6
*.
'KK))+	6ykB))*!J,F,F,O,OPZ,[+]^+,-	
 	LL%% ,,==01K1K0LMZA*B\B\A]^7
8R8R7SScd<Z=W=W<XXYZ3J4N4N3O~^12tJMM?5KK
	
   0 0 EFF ##Z0 ;w"8 9:;s   EE. .8F)(F)c                 ^   t         j                  j                  |      }t        | |      s| j                  j                  d      rOd|_        |j                          t        j                  t              5  t        j                  | j                  j                  |j                  j                  j                   j"                  |j                   d|j$                   d|j                   d|j$                   |j                   d|j$                   |j                   d|j$                   |j                   d	|j$                   t'        d
      d|j(                   z   d	       ddd       t+        | j,                  j                  dd            S t/        d      S # 1 sw Y   9xY w)zc
    This method is used to not validate the attendance.
    args:
        id  : attendance id
    r   r   Fz0 requested revalidation for                     z attendanceuN    طلب إعادة                      التحقق من حضور تاريخ zC beantragte eine Neubewertung der                     Teilnahme am uP    solicitó la validación nuevamente                     para la asistencia del uF    a demandé une revalidation pour la                     présence du r  r  refreshr  Nr   r   z(You Cannot Request for others attendance)r?   rd   r   r7   rs   has_permr   r   
contextlibsuppressr   rb   r  rt   r   employee_work_inforeporting_manager_idr  r   r   rx   r   r  r   ry   r   ri   s      rm   revalidate_this_attendancer  P  s    ##''6'2J':.',,2G2G&3 +0
'  + 	KK))**==RRcc"../ 0//0=%112 3==G=W=W<XZ%112 3"","<"<!=?%112 3,,6,F,F+GI%112 3"","<"<!=? !564
9OO!	& $GLL$4$4^S$IJJBCC)	 	s   9C2F##F,c                    	 t         j                  j                  |      }d|_        |j	                          | j
                  j                         }d| }t        j                  | |j                   d|j                  j                  d       d       t        j                  t              5  t        j                   | j"                  j$                  |j                  j&                  d|j                   dd	|j                   d
d|j                   dd|j                   d
d|j                   dt)        d      d|j*                   z   d	       ddd       t5        | j6                  j                  dd            S # 1 sw Y   .xY w# t         j,                  t.        f$ r" t        j0                  | t3        d             Y kw xY w)zf
    This method is used to approve attendance overtime
    args:
        obj_id  : attendance id
    r   Tr   z's r  z overtime approvedzYour z4's attendance                     overtime approved.ur   تمت الموافقة على إضافة ساعات العمل الإضافية لتاريخ                     r#  u   Die Überstunden für den z'                      wurden genehmigt.zISe ha aprobado el tiempo extra de asistencia para el                     u>   Les heures supplémentaires pour la date                      u    ont été approuvées.attendance-overtime-viewr  r  r  Nr  r   r   )r?   rd   r   r  r   r   r   r   r   r   r   r   r  r  r   rb   r  rs   rt   r  r   rx   r  r  r  r   r   r  r  s        rm   approve_overtimer  u  s   ;''++v+6
15
.KK))+	6ykB%%&c**D*D*M*Mj*Y)ZZlm	
   + 	KK))$00AAZ778 9' (//034Z5O5O4P Q( )//03!1122IK !;<jmm_?UU 	&   0 0 EFF'	 	" ##]3 ;w"8 9:;s+   B.F 0B&FF FF 8G
	G
c                    | j                   d   }t        j                  |      }|D ]  }	 t        j                  j                  |      }d|_        |j                          t        j                  | t        d             t        j                  | j                  j                  |j                  j                   d|j"                   dd|j"                   d|j"                   d	d
|j"                   d|j"                   t%        d      d|j&                   z   d	        t1        ddi      S # t        j(                  t*        t,        f$ r# t        j.                  | t        d             Y Kw xY w)z;
    This method is used to approve bulk of attendance
    r  r   TzOvertime approvedz+Overtime approved for                      z's attendanceuc   تمت الموافقة على العمل الإضافي لحضور تاريخ                     u9   Überstunden für die Anwesenheit am                     z
 genehmigtzAHoras extra aprobadas para la asistencia del                     uM   Heures supplémentaires approuvées pour la présence du                     r  r  r  r  r  rP  rQ  )r   rv   rR  r?   rd   r   r  r   r   r   r   rb   r  rs   rt   r   r  r   r   rx   r  r  rJ  r  r   )ry   r  rq  ri   s       rm   approve_bulk_overtimer    so    ,,u
C
**S/C ?	?#++//=/AJ59J2OOWa(;&<=KK))$00AA!112-A//02//0
<//02//02 !;<jmm_?UU ?0 I.// ''
C 	?NN7A&<$=>	?s   C8D22=E32E3c                    t         j                  j                         }| j                  j	                  d      }| j
                  dk(  rR| j                  d   }|j                  d      j                  d      D cg c]*  }t        |j                         j                  d            , }}| j                  j                  d      }|rt         j                  j                  |      j                         }|D ]H  }	 t        j                  j                  |      j                         }||_        |j                          J t'        j,                  | t)        d| d             t+        d	      S t'        j$                  | t)        d             t+        d	      S t/        | d||d      S c c}w # t         $ rK}	t"        j%                  |	       t'        j$                  | t)        d             t+        d	      cY d
}	~	c S d
}	~	ww xY w)z:
    This method is used to add attendance to a batch
    r  r   r  ,'batch_attendance_idr   zSomething went wrong.rI  NzAttendances added to r#  z/attendance/attendance/attendance_add_batch.html)batchesr  )rG   rd   re   r   r   r   stripsplitintr   r   r   r   r?   r  r   r   loggerr  r   r   r   r   r   )
ry   r  r  xint_idsbatch_idbatchrx   attendance_reqr  s
             rm   attendance_add_to_batchr    s    %%))+G
++

e
$C~~kk% 69iio6K6KC6PQ3qwwys+,QQ<<##$9:#++22h2?EEGE UU%/%7%7%>%>"%>%E%K%K%MN9>N6"'')	U Wa*?wa(H&IJ KLLNN7A&=$>? KLL9C( % R ! ULLONN7A.E,FG'(STTUs%   ;/F,=AF11	H:>H 8H Hc                    | j                   j                  d      }| j                  j                  d      }|dk(  s|dk(  r| j                   j                  d      n| j                   j                  d      }|rJ|dk(  s|dk(  r t        j
                  j                  |      n!t        j
                  j                  |      nd }| j                   j                  d      }|r$t        j                  |d	      j                         n!t        j                         j                         }|j                  d
      j                         }|r/t        j
                  j                  ||      j                         nd }|r|j                  nd}	|r|j                   nd}
|r|j"                  nd}|r$|
|	k  r|t%        d      z   j                  d	      }n|j                  d	      }|t        j                         j                         k(  r%t        j&                         j)                         }
d}n|}t+        t-        |      |      }t/        j0                  | j                   j                  d            ||||j                  d	      |	r|	j                  d      nd|
r|
j                  d      nd||j                  d	      |d
}|dk(  rt3        |      n|dk(  rt5        |      nt7        |      }t9        | d| |d      S )Nshift_idrH  attendanceUpdateFormattendanceRequestDivr   r   r  r   %Y-%m-%d%A)r  day__day r   r"  )dayswork_type_idz%H:%M)
r  r  r   minimum_hourr   attendance_clock_inattendance_clock_outrh   attendance_clock_in_dateattendance_clock_out_dater   )attendance/attendance/update_hx_form.htmlry   r   )r   r   r  r   r[   rd   r   r
   strptimer	   todayr   r   rW   r   
start_timeend_timeminimum_working_hourr   r   r  r5   strrY   findr.   r3   r*   r   )ry   r  rK  r+  employee_querysetattendance_date_strr   dayschedule_todayshift_start_timeshift_end_timer  r  worked_hourinitial_datar   s                   rm   update_fields_based_shiftr    s    {{z*H  !12I ..)?U2U 	&[[  /  	 2222   L 1 !!(((=   "++//*;<  	-z:??A^^""$ 
 
"
"4
(
.
.
0C  	%%,,h,MSSU  5C~000>^,,BN:H>66gL.+;;%4ya7H%H$R$R%
! %4$<$<Z$H!(..*//11!,,."*3+?NL !gkkoon&EF($*33J?2B%%g. 1?N##G,B"-$3$<$<Z$H%>L$ .. 	\2 22 <05 	 3T* ro   c                    t        | j                  j                  d      rt               j	                  d      n| j                  j                  d      | j                  j                  d            }t        | j                  j                  d      rt               j	                  d      n| j                  j                  d      | j                  j                  d            }|r|r||z
  j                         nd}t        t        |d      d	      \  }}t        |      d
dt        |dz        d
}t        d|i      }t        | d| |d      S )a  
    Update the worked hour field based on clock-in and clock-out times.

    This view function calculates the total worked hours for an employee
    by parsing the clock-in and clock-out dates and times from the request
    parameters. It computes the duration between the two times and formats
    the result as a string in the "HH:MM" format. The computed worked hours
    are then initialized in an AttendanceForm, which is rendered in the
    specified HTML template.
    create_bulkr  r  r	  r  r
  r     02:<   rh   r   r  r  )r;   r   r   r   r   total_secondsdivmodmaxr  r*   r   )ry   ru  rv  r&  hoursminutesworked_hours_strr   s           rm   update_worked_hour_fieldr,  ;  s(     {{}- ENN:&!;<-.H  {{}- ENN:&!<=./I 3;yX	,,.b  Cq148NE7e*R#gm*<R)@A#;=M"NOD3T* ro   c                    | j                   d   }d}t        j                  |d      j                         }| j                   d   rk| j                   d   }|j	                  d      j                         }t        j                  j                  ||      j                         }||j                  }t        |      }t        ||      }t        d|i      S )Nr   r   r  r  r  )shift_id__idr  r  )r   r
   r  r	   r   r   rW   rd   r   r   r  r  r5   r   )ry   r  r  r   r  r  r  s          rm   form_date_checkingr/  g  s    !,,'89L''(;ZHMMOO||J<<
+&&t,224.66==!C > 

%' 	
 %)>>L/*O*?LILL	
 ro   c                    t         j                  j                  |      }|j                  }|dz  }|dz  dz  }dj	                  ||      }|j
                  }|dz  }|dz  dz  }	dj	                  ||	      }
| j                  d   }|rt        j                  |      ng }t        ||      \  }}t        | d|||
|||| j                  j                  d      d      S )	z
    function used to view one user attendance request.

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

    Returns:
    GET : return one user attendance request view template
    r   r"  r%  z{:02}:{:02}r[  z1attendance/attendance/attendance_request_one.html	dashboard)attendance_requestrl   	over_timer]  r^  r_  r1  )r?   rd   r   at_work_secondr  overtime_secondr   rv   rR  rP   r   )ry   rx   r2  at_work_secondshours_at_workminutes_at_workrl   over_time_secondshours_over_timeminutes_over_timer3  r_  rc  r]  r^  s                  rm   user_request_one_viewr<    s     $++//2/6(77O#t+M&-"4O""=/BG*::'4/O*T1b8$$_6GHIO44E4::/02L'6|R'H$};"4"!2*!2 5	
 ro   c                 L    t        j                  |      }t        | dd|i      S )Nz4attendance/attendance/attendance_activites_view.htmlri   r   )r?   r  r   r  s      rm   get_attendance_activitiesr>    s-     (J>z* ro   c                    | j                   j                  d      }i }|dk(  r| j                  j                  d      rt        j
                  j                         }nUt        j
                  j                  | j                        t        j
                  j                  | j                        z  }|D cg c]  }t        |j                         }}|j                         }||d}t        |d      S c c}w 	Nr   re   r6  r4  Gemployee_id__employee_work_info__reporting_manager_id__employee_user_idr+  total_countFsafe)r   r   rs   r  rC   rd   re   r   r  rx   countr   )ry   page_numberr|   	employeesempr+  rD  s          rm   hour_attendance_selectrK    s    ++//&)KGe<<  !EF*22668I*2299.5ll : "**11X_XdXd 2 I 099CFF99oo'#/Le,, :s   6C6c                    | j                   j                  d      }| j                   j                  d      }|rt        j                  |      ni }|dk(  r| j                  j                  d      r*t        |t        j                  j                               }n`t        |t        j                  j                  | j                        t        j                  j                  | j                        z        }|j                  }|D cg c]  }t        |j                         }}|j                         }||d}	t        |	      S y c c}w 	Nr   r   re   r6  r   r4  rA  rC  )r   r   rv   rR  rs   r  r#   rC   rd   re   r   r   r  rx   rG  r   
ry   rH  filteredfiltersemployee_filterfiltered_employeesrJ  r+  rD  r|   s
             rm   hour_attendance_select_filterrS    !   ++//&)K{{x(H&.djj"BGe<<  !EF6"4"<"<"@"@"BO 7+33::29,, ;  %,,33\c\h\h 4 O -///ABCFFBB(..0#/LG$$1 & C   
E	c                    | j                   j                  d      }|dk(  r| j                  j                  d      rt        j
                  j                         }nUt        j
                  j                  | j                        t        j
                  j                  | j                        z  }D cg c]  }t        |j                         }}|j                         }||d}t        |d      S c c}w r@  )r   r   rs   r  r@   rd   re   r   r  rx   rG  r   ry   rH  rI  rJ  r+  rD  r|   s          rm   activity_attendance_selectrX    s    ++//&)Ke<<  !EF*22668I*2299.5ll : "**11X_XdXd 2 I ,55CCK5L5//#K+KHGe,, 6   4C4c                    | j                   j                  d      }| j                   j                  d      }|rt        j                  |      ni }|dk(  r| j                  j                  d      r*t        |t        j                  j                               }n`t        |t        j                  j                  | j                        t        j                  j                  | j                        z        }|j                  }|D cg c]  }t        |j                         }}|j                         }||d}	t        |	      S y c c}w rM  )r   r   rv   rR  rs   r  r    r@   rd   re   r   r   r  rx   rG  r   rN  s
             rm   !activity_attendance_select_filterr[    rT  rU  c                    | j                   j                  d      }|dk(  r| j                  j                  d      rt        j
                  j                         }nUt        j
                  j                  | j                        t        j
                  j                  | j                        z  }D cg c]  }t        |j                         }}|j                         }||d}t        |d      S c c}w )	Nr   re   r  r4  rA  rC  FrE  )r   r   rs   r  rB   rd   re   r   r  rx   rG  r   rW  s          rm   latecome_attendance_selectr]  %  s    ++//&)Ke<<  !MN2::>>@I2::AA.5ll B *2299X_XdXd : I ,55CCK5L5//#K+KHGe,, 6rY  c                    | j                   j                  d      }| j                   j                  d      }|rt        j                  |      ni }|dk(  r| j                  j                  d      r*t        |t        j                  j                               }n`t        |t        j                  j                  | j                        t        j                  j                  | j                        z        }|j                  }|D cg c]  }t        |j                         }}|j                         }||d}	t        |	      S y c c}w )	Nr   r   re   r  r   r4  rA  rC  )r   r   rv   rR  rs   r  r&   rB   rd   re   r   r   r  rx   rG  r   rN  s
             rm   !latecome_attendance_select_filterr_  ;  s"   ++//&)K{{x(H&.djj"BGe<<  !MN4"<"D"D"H"H"JO 53;;BB29,, C  -44;;\c\h\h < O -///ABCFFBB(..0#/LG$$1 & CrU  zattendance.add_gracetimec                    t        | j                  j                  d            }t        d|i      }| j                  dk(  rt        | j
                        }|j                         ru|j                  }|j                         }|j                  d      }|D ]  }||_	        |j                           t        j                  | t        d             t        d      S t        | d||d	      S )
z
    function used to create grace time .

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

    Returns:
    GET : return grace time form template
    default
is_defaultr   r   shiftsz Grace time created successfully.rI  *attendance/grace_time/grace_time_form.html)r   rb  )rQ   r   r   r1   r   r   r   cleaned_datar   grace_time_idr   r   r   r   r   )ry   rb  r   re  	gracetimerc  shifts          rm   create_grace_timeri  \  s     w{{y9:J,
!;<D~~W\\*==?,,L		I!%%h/F &/#

 Wa(J&KL KLL4Z0 ro   zbase.change_employeeshiftc                    |r.t         j                  j                  |      j                         nd }|rt	               }| j
                  dk(  rt	        | j                        }|j                         re|j                  }|j                  d      }|D ]  }||_
        |j                           t        j                  | t        d             t        d      S t!        | d||d      S y )Nr   r   rc  z(Grace time added to shifts successfully.rI  z'attendance/grace_time/assign_shift.html)r   
grace_time)rH   rd   r   r   r0   r   r   r   re  r   rf  r   r   r   r   r   r   )ry   grace_idrg  r   re  rc  rh  s          rm   assign_shiftrm  }  s     BJ	!!((H(5;;=tI"$>>V#&w||4D}}#00%))(3# !E*3E'JJL!   !,V*WX#$OPP53
 	
 ro   zattendance.change_gracetimec                    t         j                  j                  |      }t        |      }| j                  dk(  rst        | j
                  |      }|j                         rL|j                  d      }|j                          t        j                  | t        d             t        d      S ||d}t        | d	|
      S )z
    function used to create grace time .

    Parameters:
    request (HttpRequest): The HTTP request object.
    grace_id: id of grace time object
    Returns:
    GET : return grace time form template
    r   r   r   F)commitz Grace time updated successfully.rI  )r   rl  rd  r   )rH   rd   r   r1   r   r   r   r   r   r   r   r   r   )ry   rl  rk  r   r{   r|   s         rm   update_grace_timerp    s     ""&&(&3J*-D~~W\\J?==?yyy.HMMOWa(J&KL KLLG =w ro   zattendance.delete_gracetimec                 n   	 t         j                  j                  |      j                          t	        j
                  | t        d             t        j                  j                         t         j                  j                  d      j                         t         j                  j                         j                  d      d}t        | d|      S # t         j                  $ r" t	        j                  | t        d             Y t        $ r" t	        j                  | t        d             Y w xY w)	z
    function used to delete grace time .

    Parameters:
    request (HttpRequest): The HTTP request object.
    grace_id: id of grace time object
    Returns:
    GET : return grace time form template
    r   z Grace time deleted successfully.zGrace Time Does not exists..zRelated datas exists.Trb  r   default_grace_timegrace_timesz+attendance/grace_time/grace_time_table.html)rH   rd   r   r  r   r   r   r  r  r   rF   r   r   re   r  r   )ry   rl  r|   s      rm   delete_grace_timerv    s    <*113!$F"GH 3::@@B'//66$6GMMO ((,,.66$6GG 'H'RR !! Cw"@ AB <w"9 :;<s   AC 2D4
'D43D4zattendance.update_gracetimec                 D   | j                   j                  d      }| j                   j                  d      }t        j                  j                  |      }|dk(  rd|_        dt        d      d}nd	|_        dt        d
      d}|j                          t        |      S )z
    ajax function to update is active field in GraceTime.
    Args:
    - isChecked: Boolean value representing the state of grace time,
    - gracetimeId: Id of GraceTime object
    	isCheckedgracetimeIdr   r>  Tr   z!Gracetime activated successfully.r  rP  Fz#Gracetime deactivated successfully.)r   r   rH   rd   	is_activer   r   r   )ry   rx  ry  rg  r   s        rm   update_isactive_gracetimer|    s       -I,,""=1K!!%%%5IF"	<=

 $	>?
 NN!!ro   c                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j                  |      }|dk(  r1|dk(  rd|_        dt        d	      d
}nZd|_        dt        d      d
}nD|dk(  r1|dk(  rd|_        dt        d      d
}n$d|_        dt        d      d
}ndt        d      d
}|j                          t        |      S )z
    ajax function to update is active field in grace time.
    Args:
    - isChecked: Boolean value representing the state of grace time,
    - gracetimeId: Id of PayslipAutoGenerate object
    rx  ry  updater   ru  r>  Tr   z.Gracetime applicable on clock-In successfully.rz  Fz1Gracetime unapplicable on clock-In  successfully.rv  z/Gracetime applicable on clock-out successfully.z1Gracetime unapplicable on clock-out successfully.r  zSomething went wrong .)	r   r   rH   rd   allowed_clock_inr   allowed_clock_outr   r   )ry   rx  ry  r~  	garcetimer   s         rm   #update_gracetime_clock_in_clock_outr    s      -I,,""=1K\\h'F!!%%%5I)-I&!MNH
 */I&!PQH 
;	*.I'!NOH
 +0I'!PQH 12
 NN!!ro   c                 V	   | j                   j                         }t        j                  j	                  |      j                         }| j                  j                  }t        |j                  |d      }| j                  dk(  rt        | j                        }|j                         r||j                  _        ||j                  _        |j!                          t"        j                  j	                  |      j%                  d      }d}|j'                         sd}t        |j                  |d      }t)        j*                  | t-        d	             t.        j                  j	                  |j                  
      }|j'                         r|j                  j0                  j2                  	 p| j                  j                  j                  |j                  j                  k(  r|j                  j0                  j2                  j4                  }	t7        j8                  | j                  j                  |	|j                   dd|j                   d|j                   dd|j                   dd|j                   dt;        d      d|j                   z   d	       n| j                  j                  j                  |j                  j0                  j2                  j                  k(  rc|j                  j4                  }	t7        j8                  | j                  j                  |	dddddt;        d      d|j                   z   d	       n1|j                  j4                  |j                  j0                  j2                  j4                  g}	t7        j8                  | j                  j                  |	|j                   dd|j                   d|j                   dd|j                   dd|j                   dt;        d      d|j                   z   d	       na|j                  j4                  }	t7        j8                  | j                  j                  |	dddddt;        d      d|j                   z   d	       t=        | d|||d      S t=        | d|||d      S )zU
    This method renders form and template to create Attendance request comments
    r   )r   
request_idr   r   r  -created_atFTzComment added successfully!r   z-'s attendance request has received a comment.u   تلقت طلب الحضور u    تعليقًا.z7s Anfrage zur Anwesenheit hat einen Kommentar erhalten.zLa solicitud de asistencia de z ha recibido un comentario.u   La demande de présence de u    a reçu un commentaire.zrequest-attendance-viewr  zchatbox-ellipsesr  z/Your attendance request has received a comment.u<   تلقى طلب الحضور الخاص بك تعليقًا.z8Ihr Antrag auf Anwesenheit hat einen Kommentar erhalten.z5Tu solicitud de asistencia ha recibido un comentario.u2   Votre demande de présence a reçu un commentaire.+requests/attendance/attendance_comment.htmlcommentsno_commentsr  )r   r  r   )r   r   r?   rd   r   r   rs   rt   r-   rx   r   r   r   r{   r   r  r   rD   rV  rf   r   r   r   r\   r  r  r  rb   r  r   r   )
ry   rq  r   ri   rJ  r   r  r  	work_inforecs
             rm    create_attendancerequest_commentr    sJ   
 KK))+M##**m*<BBDJ
,,
#
#C' #mDD ~~+GLL9==?(+DMM%'1DMM$IIK/77>>( ? h}%   K??$"/(+mLD Wa(E&FG/77>>&22 ? I !**==RR  ||0033z7M7M7P7PP&22EEZZkk  #LL55&)$.$:$:#;;h!i&CJDZDZC[[k$l'1'='=&>>u$v&DZE[E[D\\w$x&A*BXBXAYYq$r%,-F%G $Z]]O4&5!3  1144%11DDYY\\] )44EE#LL55&)!R$b$^$[$X%,-F%G $Z]]O4&5!3 '22CC&22EEZZkk #LL55&)$.$:$:#;;h!i&CJDZDZC[[k$l'1'='=&>>u$v&DZE[E[D\\w$x&A*BXBXAYYq$r%,-F%G $Z]]O4&5!3 %00AACKK11"%N ^ Z W T!()B!C 0"1/ = (#."/  5'	
 ro   c                    t         j                  j                  |      j                  d      }d}|j	                         sd}| j
                  r| j
                  j                  d      }| j                  d   }t         j                  j                  |      }g }|D ]4  }t               }	||	_
        |	j                          |j                  |	       6  |j                  j                  |  t        | d|||d	      S )
zA
    This method is used to show Attendance request comments
    r  r  FTfiles
comment_idr   r  r  )rD   rd   r   rV  rf   r   r   r   r   rE   r   r   r(  r  r  r   )
ry   rq  r  r  r  r  commentattachmentsr   file_instances
             rm   view_attendancerequest_commentr    s    
 (//66  7 h}  K??}}%%g.[[.
*2266*6E 	.D13M!%M }-		.
 	;'5kW ro   c                     d}t         j                  j                  |      }|j                          t	        j
                  | t        d             t        |      S )zC
    This method is used to delete Attendance request comments
    r  r   zComment deleted successfully!)rD   rd   r   r  r   r   r   r   )ry   r  scriptr  s       rm    delete_attendancerequest_commentr    sM    
 F&..22j2AGNNWa ?@Aro   c                     d}| j                   j                  d      }t        j                  j	                  |      j                          t        j                  | t        d             t        |      S )z#
    Used to delete attachment
    r  r  r  zFile deleted successfully)
r   r   rE   rd   r   r  r   r   r   r   )ry   r  r  s      rm   delete_comment_filer    s^    
 F
++

e
$C!!(((4;;=Wa ;<=ro   c                     t        j                         }| j                  j                         }||d}t	        | d|      S )N)current_dater   z,attendance/work_record/work_record_view.htmlr   )r	   r  r   r   r   )ry   r  r   r|   s       rm   work_recordsr    sE    JJLEKK))+M G ? ro   c                 @   | j                   j                         }t               }| j                   j                  d      rV| j                   j                  d      }t	        |j                  d      d         }t	        |j                  d      d         }n<t        j                         j                  }t        j                         j                  }t        t        j                  j                               }t        t        j                  j                  d            }t        | j                         }t        |j                         }g }t#        j$                  ||      }	|	D 
cg c]  }
|
D ]
  }|dk7  s	|  }}
}|D cg c]  }t'        |||      j                          }}t(        j                  j                  |      j+                  d      }t-        d	       }|D ](  }|||j.                  j0                     |j                  <   * t-        t2              }|D ]6  }|||j4                     |j6                  j6                  j9                         <   8 |D ]  }t;        t;        |d
d       dd       }g }|D ]  }|j=                  d      j9                         }|j                  |i       j                  |d       }||j0                     j                  |d       }|s|r|j>                  dk(  rd nd}|jA                  |        |jA                  ||d        tC        ||      }| j                   j                  d      }tE        |tG                     }|jI                  |      }||||t        j                         |d}tK        | d|      S c c}}
w c c}w )Nr   -r"  r   T)r{  )date__inr   c                      t        d       S )Nc                       y r   r  ro   rm   <lambda>z=work_records_change_month.<locals>.<lambda>.<locals>.<lambda>      ro   r   r  ro   rm   r  z+work_records_change_month.<locals>.<lambda>  s    K,E ro   r  r  r  r   EW)rz   work_recordr   )current_month_dates_listleave_datesr   r   r  r   z,attendance/work_record/work_record_list.htmlr   )&r   r   rZ   r   r  r  r	   r  r   yearlistrW   rd   re   r[   r   r   calendarmonthcalendarr
   rI   select_relatedr   r   rx   r   r  r  r   getattrr   r  r(  r8   r   rU   get_pager   )ry   r   employee_filter_formdate_objr   r  	schedulesrI  r   month_matrixweekr  r  current_month_date_listall_work_recordswork_records_dictrecordschedules_dictschedulerz   rh  work_record_listr  r  r  rH  	paginatorr|   s                               rm   work_records_change_monthr    sZ    KK))+M)+{{w;;??7+HNN3'*+8>>#&q)*

""zz|  *226689IX%%,,t,<=I)'++6),,-ID))$6L(EDDESC1HCECEDELPQSxeS9>>@QQ"**11( 2 n]#  $$EF" G@F&,,//0=G !&N OFNx(()(,,*:*:*@*@*BCO  
*>EzSWX3 	1L''-335C%))%488dCH+HKK8<<\4PK $x'D'D'O  
 ##K0	1 	$/	
#
0 %UD1K++//&)K$ 01Ik*D %<"

!G ? i FQs   )N9N"Nzattendance.view_workrecordsc           
         	 t        | j                  j                  d      xs t        j                         j
                        }t        | j                  j                  d      xs t        j                         j                        }t        | j                        j                  }t        j                  j                  ||      }t        j                  ||      d   }t!        d|dz         D cg c]  }t        |||       }}t#        t%        ||            }t'        d       }	|D ]K  }
|
j                  t        j                         k  s%|
j(                  |
j                  f}|
j*                  |	|<   M | j,                  j.                  j1                         }t3        j                  |      }|D cg c]  }|j5                  |       }}g }|D ]u  }d|i}t7        ||      D ]O  \  }}||vr/|t        j                         k  r|	j                  ||fd      ||<   9|	j                  ||fd	      ||<   Q |j9                  |       w dg|z   }t;        j<                  ||
      }t?        j@                         }t;        jB                  |d      5 }|jE                  |dd       |jF                  }|jH                  d   }|jK                  ddd      |jK                  ddd      |jK                  ddd      |jK                  ddd      |jK                  ddd      d}tM        |jO                  d      d      D ]9  \  }}tM        |dd  d      D ]!  \  }}||v s|jQ                  |||||          # ; tM        |jR                        D ]d  \  }}tU        ||   jW                  tX              j[                  t\              jU                         t]        |            }|j_                  |||       f 	 d d d        |ja                  d       tc        |je                         d      } d| d<   | S # t        $ r t        d      cY S w xY wc c}w c c}w # 1 sw Y   `xY w) Nr   r  z Invalid month or year parameter.)date__month
date__yearr"  c                       y)NABSr  r  ro   rm   r  z$work_record_export.<locals>.<lambda>5	  r  ro   r[   r  r  r   
xlsxwriter)engineFSheet1)r   
sheet_namez#808080z#ffffff)bg_color
font_colorz#38c338z#dfdf52z#000000z#ed4c4cz#a8b1ff)r  FDPHDPCONFr  r   )startr   r  r   z.attachment; filename="work_record_export.xlsx"r   )3r  r   r   r	   r  r   r  rJ  r   rZ   r   rI   rd   r   r  
monthrangeranger  r8   r   r   work_record_typers   rt   get_date_formatr   r   zipr(  r   r   ioBytesIOExcelWriterr   booksheets
add_format	enumerate
itertupleswriter   r(  astyper  mapr   
set_columnseekr   read)!ry   r   r  rI  r   num_daysr  all_date_objectsr  record_lookupr  
record_keydate_formatformat_stringformatted_dates	data_rowsrz   row_dataformatted_dayr   dfoutputwriterworkbook	worksheetformatsrow_idxrowcol_idx
cell_valuecolmax_lenr   s!                                    rm   work_record_exportr  &	  s(   JGKKOOG,B

0B0BC7;;??6*?djjl.?.?@ w{{+..I!!((Ut(LG""4/2H:?8a<:PQ3T5#.QQ(56K.M @;;$**,& ,,fkk:J(.(?(?M*%@
 ,,++;;=K(,,[9M>NOss||M2OOOI #)"%&6"H 	QC+%#

*<*7*;*;XsOT*R'*7*;*;XsOR*P'		Q
 	"# l_,G	i	1BZZ\F	|	4 <
F%H=;;MM(+	 &&&i@ &&&i@ &&&i@ ''&i@ %%9I&VW
  &bmm%m&@J 	WLGS'0QR'B W#(OOGWj'*BUVW	W
 &bjj1 	<LGS"S'..-11#6::<c#hGG  '7;	<5<< KKNXH 'WH"#OM  J%&HIIJ R P < <s2   BP9 4Q8QCQ&BQ9QQQ&z'attendance.add_attendancegeneralsettingc                     t         j                  j                         }|r|n	t               }d| j                  j	                         v |_        |j                          t        d      S )zF
    This method is used to enable/disable the timerunner feature
    time_runnerr   )rA   rd   r   r   r  r  r   r   )ry   r  s     rm   enable_timerunnerr  u	  sV     +2288:K!,+2J2LK+w{{/?/?/AAK	""ro   zbase.view_tracklatecomeearlyoutc                     t         j                  j                         }t        |rd|j                  ini       }t        | dd|i      S )zU
    Renders the form to track late arrivals and early departures in attendance.
    	is_enabler   z,attendance/late_come_early_out/tracking.htmlr   )rX   rd   r   rN   r  r   )ry   trackingr   s      rm   track_late_come_early_outr  	  sO     %,,224H$5=h0012D ?&$ ro   z!base.change_tracklatecomeearlyoutc                 z   | j                   dk(  rt        | j                  j                  d            }t        j
                  j                         \  }}||_        |j                          |rt        d      n
t        d      }t        j                  | t        d      j                  |             t        d      S )z_
    Enables or disables the tracking of late arrivals and early departures in attendance.
    r   r  enableddisabledz,Tracking late come early out {} successfullyrI  )r   boolr   r   rX   rd   get_or_creater  r   r   r   r   r  r   )ry   enabler  createdrP  s        rm   +enable_disable_tracking_late_come_early_outr  	  s     ~~gll&&{34199GGI'#"(!I,a
mQEFMMgV	
 CDDro   c                 \    t         j                  j                         }t        | dd|i      S )z$
    Check in check out setting
    z7attendance/settings/check_in_check_out_enable_form.htmlattendance_settings)rA   rd   re   r   )ry   r  s     rm   check_in_check_out_settingr  	  s5    
 3::>>@A	 34 ro   z*attendance.change_attendancegeneralsettingc                    | j                   dk(  r| j                  j                  d      }| j                  j                  d      }t        |      }t        j
                  j                  |      j                  |      }|rTt        d      j                  |rt        d      n
t        d            }t        j                  | |       |rt        | d	      S t        d
      S )z1
    Enables or disables check-in check-out.
    r   rx  
setting_Idr   )enable_check_inz,Check In/Check Out has been successfully {}.r  r   z+attendance/components/in_out_component.htmlr  )r   r   r   r  rA   rd   r   r~  r   r  r   r   r   r   )ry   
is_checked
setting_idr  updatedrP  s         rm   enable_disable_check_inr  	  s     ~~\\%%k2
\\%%l3
j!*2299Z9HOO" P 
 FGNN &)AjMG Wg.g'TUUro   z-attendance.view_attendancevalidationconditionc                    t         j                  j                         }t        j                  j	                  d      j                         }t        j                  j                         j                  d      }t        | d|||d      S )<
    This method view attendance validation conditions.
    Trr  z%attendance/grace_time/grace_time.htmlrs  )rF   rd   r   rH   r   re   r  r   )ry   r   rt  ru  s       rm   grace_time_viewr  	  s~     .55;;=I"**11T1BHHJ##'')11T1BK/""4&	
 ro   c                     t         j                  j                         }t        j                  j	                  d      j                         }t        | d||d      S )r  Trr  z%attendance/break_point/condition.html)r   rt  )rF   rd   r   rH   r   r   )ry   r   rt  s      rm   validation_condition_viewr  	  sW     .55;;=I"**11T1BHHJ/7IJ ro   z,attendance.add_attendancevalidationconditionc                    t               }| j                  dk(  r^t        | j                        }|j                         r9|j	                          t        j                  | t        d             t               }t        | dd|i      S )zt
    This method render a form to create attendance validation conditions,
    and create if the form is valid.
    r   z(Attendance Break-point settings created.*attendance/break_point/condition_form.htmlr   )	r/   r   r   r   r   r   r   r   r   r  s     rm   validation_condition_creater  	  sp     -.D~~0>==?IIKWa(R&ST46D4	 ro   c                 D   t         j                  j                  |      }t        |      }| j                  dk(  rVt        | j
                  |      }|j                         r/|j                          t        j                  | t        d             t        | d||d      S )zx
    This method is used to update validation condition
    Args:
        obj_id : validation condition instance id
    r   r   r   z(Attendance Break-point settings updated.r  )r   r   )rF   rd   r   r/   r   r   r   r   r   r   r   r   )ry   r   r   r   s       rm   validation_condition_updater  
  s     .5599V9DI,i@D~~0	R==?IIKWa(R&ST4I. ro   c                 \    t         j                  j                         }t        | dd|i      S )z7
    This function is used to view the allowed ips
    z-attendance/ip_restriction/ip_restriction.htmlallowed_ips)rV   rd   r   r   )ry   r  s     rm   r  r  
  s3     &--335K7	$ ro   c                 P   t               }| j                  dk(  rt        j                  j	                         }|s+t        j                  j                  d      }t        d      S |j                  sd|_        n|j                  rd|_        |j                          t        d      S y)z9
    This function is used to enable the allowed ips
    r   T)
is_enabledrI  FN)	rL   r   rV   rd   r   creater   r  r   )ry   r   ip_restictions      rm   enable_ip_restrictionr   +
  s     #$D~~+3399;/77>>$>OM KLL'''+M$%%',M$GHH  ro   c                 N    	 t        |       |S # t        $ r t        d      w xY w)z
    This function is used to check if the provided IP is in the ipv4 or ipv6 format.

    Args:
        value: The IP address to validate
    z#Enter a valid IPv4 or IPv6 address.)r   r   )selfvalues     rm   validate_ip_addressr$  B
  s6    Eu% L  ECDDEs    $c                 t   | j                   dk(  rt        | j                        }|j                         rt|j                  j                  d      }t        j                  j                         }|rt        |j                  j                  dg             }t        |      }|j                  |      }|r(t        j                  | ddj                  |              ||z
  }|rXt        |j!                  |            |j                  d<   |j#                          t        j$                  | d       t+        d
      S t        j&                  | d       t+        d
      S t        j                  j)                  dd|i	       t        j$                  | d       t+        d
      S t               }t-        | dd|i      S )z:
    This function is used to create the allowed IPs.
    r   ip_addressesr  zIP addresses already exist: r   zIP addresses saved successfullyz:All provided IP addresses are already in the allowed list.T)r  additional_datarI  ,attendance/ip_restriction/restrict_form.htmlr   )r   rL   r   r   re  r   rV   rd   r   r  r'  intersectionr   r  r  r  unionr   r   r  r  r   r   )ry   r   r&  r  existing_ipsnew_ips
duplicatesnon_duplicatess           rm   create_allowed_ipsr/  P
  s    ~~&w||4==?,,00@L-55;;=K";#>#>#B#B=RT#UVl+$11,?
NN#?		*@U?V!W "):!5!AE$**>:BK//>  $$&$$W.OP   KLL MMT   KLL $++22#m\5R 3    *KL KLL&(?&$ ro   c                    	 | j                   j                  d      }t        j                  j	                         }|j
                  d   }|D ]  }|j                  t        |              ||j
                  d<   |j                          t        j                  | d       t        d      S #  t        j                  | d       Y t        d      S xY w)z9
    This function is used to delete the allowed ips
    rx   r  zIP address removed successfullyz
Invalid idallowed-ips)r   r   rV   rd   r   r'  ra  rQ   r   r   r   r  r   )ry   r  r  ipsrx   s        rm   delete_allowed_ipsr3  
  s    .kk!!$')11779))-8 	'BGGM"%&	' 69##M2"CD M"".w-M""s   BB+ +Cc                    t         j                  j                         }|s!t        j                  | d       t        d      S |j                  j                  dg       }| j                  j                  d      }	 t        |      }|dk  s|t        |      k\  rt        ||   }t        d|i      }| j                  dk(  rt        | j                        }|j                         r|j                   d   d   }t#        |j                  j                  dg             }||v rt        j                  | d	       n`|j%                  |       |j'                  |       t)        |      |j                  d<   |j+                          t        j,                  | d
       t/        d      S t3        | d|d      S # t0        t        f$ r t        j                  | d       Y 7w xY w)z8
    This function is used to edit the allowed IPs.
    zNo allowed IPs found.r1  r  rx   r   r&  r   r   zIP address already exists.zIP address updated successfullyrI  zInvalid ID provided.r(  )r   rx   )rV   rd   r   r   r  r   r'  r   r   r  r   
IndexErrorrL   r   r   r   re  r  discardr  r  r   r   r   rJ  r   )ry   r  r2  rx   
initial_ipr   new_ipr+  s           rm   edit_allowed_ipsr9  
  s    &--335Kw 78&&

%
%
)
)-
<C		B8W6R3s8^W
&
/KL>>V#*7<<8D}}**>:1=";#>#>#B#B=RT#UV\)NN7,HI ((4 $$V,AElASK//>$$&$$W.OP#$OPP
 6R   
# 8w 678s   :D'F2 2%GG)__doc__loggingr  horilla.horilla_settingsr   r   horilla.methodsr   	getLogger__name__r  r  r  r  rv   collectionsr   r
   r	   r   urllib.parser   pandasr   django.contribr   django.core.paginatorr   django.core.validatorsr   	django.dbr   django.db.modelsr   django.formsr   django.httpr   r   r   r   django.shortcutsr   r   django.template.loaderr   django.urlsr   django.utilsr   r  django.utils.timezoner   django.utils.translationr   r	  r   r   django.views.decorators.httpr   attendance.filtersr    r!   r"   r#   r$   r%   r&   r'   attendance.formsr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   attendance.methods.utilsr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   attendance.modelsr?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   )attendance.views.handle_attendance_errorsrJ   (attendance.views.process_attendance_datarK   
base.formsrL   rM   rN   base.methodsrO   rP   rQ   rR   rS   rT   rU   base.modelsrV   rW   rX   rY   employee.filtersrZ   employee.modelsr[   r\   horilla.decoratorsr]   r^   r_   r`   ra   notifications.signalsrb   rn   r}   r   r   r   r   r   r   r   r  r-  r/  r2  r;  rC  rF  rM  rS  rY  rd  rl  rr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r,  r/  r<  r>  rK  rS  rX  r[  r]  r_  ri  rm  rp  rv  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r$  r/  r3  r9  r  ro   rm   <module>r^     s
     O .			8	$   	  # . . !  # + 9 ! + (  . 3  4 % 2 6 =	 	 	          O L 
    , =  )," H  H, +,!H - !HH ./N 0  N6 01' 2 ': 01 2 B  0 /0Z 1 Zz 12! 3  !H 34vh*G   5 *GZ 34vh/5   5 /5d ! !H 67V 8  V*  D* 9:  ; 8 ;<vh   = : ;<0 = 04 ! !H  : ;<vx()
 * = 
> ;<vh0   = 0(GT4 89R : R* 89 : * ;< = .  &     F   ( CDvh
    E 
> CDvh0   E 0* CD E 8 FGvh=   H =  vh1260 3   60r 12"G 3 "GJ !D !DH 12#G 3 #GL 120 3 0B  D R  Rj '  'T  6 $ $N    - -, % %@ - -* % %@ - -* % %@ /0 1  < 01
 2  
* 23 4  8 23S 4 S4 23" 4 "4 23+" 4 +"\ t tn  <         
 
 H  HV 23J 4 JZ >?	# @  	# 67
 8 
 89E : E  	 	 AB C  0 DE F $ DE F  CD E & FG H  ( 01	 2 	 01I 2 I* 01- 2 -` 34# 5 #( 34- 5 -ro   