
    siŞ                    <   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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/m0Z0 ddl1m2Z3 ddl4m5Z5 ddl6m7Z8 ddl6m9Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZR ddlSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^ ddl_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZj ddlkmlZl ddlmmnZn ddlompZpmqZq ddlrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZz ddl{m|Z|m}Z}m~Z~mZ dd lmZ 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        ZedB        Ze edC       e<d2dDg      dE                      Ze edC       e<d2g      dF                      ZdG ZdH Ze edI      dJ               Ze edI      dK               Ze edL      dM               ZedN        ZeedO               ZeedP               Ze edQ      e e<d2g      dR                             Ze edQ       e<d2g      dS                      Ze edT      dU               Ze edV       e<d2g      dW                      Ze e<d2g       ed/      dX                      Ze ed/      dY               ZedZ        Ze ed/      d[               Ze ed/      d\               Zed]        Zeed^               Zeed_               Zeed`               Zeda        Zedb        Zeedc               Zedd        Zede        Zedf        Zedg        Zedh        Zedi        Zee edj      dk                      Zee edl      dm                      Zee edn      do                      Ze edp      dq               Ze edr      ds               Ze edr      dt               Zedu        Zedv        Zedw        Zedx        Zedy        Zeedz               Ze ed{      d|               Zee ed}      d~                      Ze ed      d               Ze ed      d               Zed        Zee ed      d                      Ze ed      d               Ze ed      d               Ze ed      d               Zee edV      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urlparse)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TrackLateComeEarlyOutForm)choosesubordinatesclosest_numberseval_validateexport_datafiltersubordinatesfiltersubordinatesemployeemodel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/python-projects/worksol/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   pkr   r{   rr   r   r   r   r   r|   s
             rm   attendance_tabr      s     !!((  ) H jjh!G((++!GHO%--44" 5   $zz%9::  "))00R0@H::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*   rN   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                    | j                   dk(  r| j                  d   }|j                  j                  d      d   j	                         }|dk(  rt        j                  |      n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.csvrecordsNr?   created_counterror_countmodel	path_infoimport_popup.html)r   FILESnamesplitlowerr   read_csv
read_excelto_dictrK   rJ   lenr   r   r   )
ry   filefile_extensionr   attendance_dictsr   r   created_attendance_countr|   htmls
             rm   r   r     s     ~~}}01-b1779!/5!8BKKbmmD>Q 	 &--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)   rQ   )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"   rR   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   rN   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strftimer   r   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: r   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_deleter3    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_attendancer5  ;  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
    r6  r   zAttendance account added.z'attendance/attendance_account/form.htmlr   r   r   )r,   rN   r   r   r   r   r   r   r   r   r   r   r   )ry   r   r   s      rm   attendance_overtime_creater8  `  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_attendanceovertimer6  r   )r   r   r   r   r   filter_dict)r   r   r#   r   rf   r   rs   rR   distinctr,   rN   r   rT   rC   r   r9   r   r$   r   )ry   r   r   r   self_accountr   r   	data_dicts           rm   attendance_overtime_viewrA  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   rQ   rC   ry   r|   s     rm   attendance_account_exportrI    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   rJ  r   z(Attendance account updated successfully.z.attendance/attendance_account/update_form.htmlr   r   r   )rC   rd   r   r,   rN   r   r   r   r   r   r   r   r   r   r   r   )ry   r   r
  r   r   s        rm   attendance_overtime_updaterL    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                  j                  }|j                          |dk(  rt        j                  | t        d             |r|dk(  rt        j
                  j#                         }|j%                         rd| j                  j                  dd      }t'        |      }|j(                  j+                  d	      }	|	d
k(  rt-        d|       S t-        d 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 accountHTTP_HX_CURRENT_URLr  z$attendance/attendance-overtime-view/z'/attendance/attendance-overtime-search?z//attendance/attendance-overtime-individual-tab/z/?deleted=true)<script>window.location.reload()</script>)r   r   r  r   rC   rd   r   rx   r  r   r   r   r  r  
ValueErrorr  r   re   rf   r   pathlstripr   r   )
ry   r   r   	hx_targetri   r   hour_accountrR  
parsed_urlparsed_paths
             rm   attendance_overtime_deleterX    s    KK))+M  !148IN'//33v3>
 ,,//
"Wa(?&@A Y*,)11557 <<##$94@D!$J$//005KDD=m_M   Ek]R`a    KLL	~ 
- ++]JG A
"NN7A&>$?@ N
"NN7A&K$LMNs   A*E AG,GG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})rU  messageSuccess)r   rv   loadsrC   rd   r   r  r   r   r   r  r   r  r  r   r   )ry   r"  rx   rU  s       rm   attendance_account_bulk_deleter]    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           
         | j                   d   }| j                   j                  d      }t        j                         }t	        |j
                  |j                  |j                        }|)|dk7  r$t        j                  |d      j	                         }|j                  d      j                         }t        j                  j                  ||      j                         }d}d}d}	|}
|8|j                  }|j                   }|j"                  }	||k  r|t%        d	
      z   }
|	}|t	        |j
                  |j                  |j                        k(  r%t        j                         j                  d      }d}t'        t)        |      |	      }	t+        |||j                  d      |	||
j                  d      d      S )zE
    This method is used to update the shift details to the form
    shift_idr  )dayr  year %Y-%m-%d%Ashift_id__idday__dayr   r)  days%H:%M)shift_start_timeshift_end_timecheckin_dateminimum_hourworked_hourcheckout_date)r   r   r	   r   r   r`  r  ra  strptimer  r   rW   rd   r   r   
start_timeend_timeminimum_working_hourr
   r5   strr   )ry   r_  attendance_date_strtodayr  r`  schedule_todayrk  rl  rn  attendance_clock_out_datero  s               rm   form_shift_dynamic_datarz    s   
 ||J'H!,,**+<=LLNEuyy%**MO&+>"+D"++,?LQQS

"
"4
(
.
.
0C*2299 : eg  NL /!)44'00%::,,(7):K(K%K$599EKKejjQQ!009*3+?NL 0,+44Z@(&6??
K	
	 	ro   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
    r:  r<  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   rR   r>  order_byrv   rw   r9   rx   rf   r   r   r!   r   )ry   r   r   attendance_activitiesself_attendance_activitiesr{   r|  r   s           rm   attendance_activity_viewr  B  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   r\  rO   r?   r  r   )ry   r   request_copyr   r  r  instance_idsr  r  r|   ri   s              rm   activity_single_viewr  g  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
| 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 attendance activity
    args:attendance-activity-delete
        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//?&instance_ids=&deleted=true)r   r  r  r   r@   rd   r   r  r   r   r   r  r  r   r   rv   r\  removerO   ry   r   r  r   r  instances_listr  r  s           rm   attendance_activity_deleter    se    ;;##%L_d+ **,MF""&&&&188:!$A"BC ;;???+A-QRR8M2^#!!&)+:JJ}%v,
(= :=/M?Zhiwhx  yF  G
 	
 ** Lw"I JK Fw"C DEF   AD0 02F$'FFc           	      j   	 | j                   j                  dd      }	 t        j                  |      }	 |D cg c]  }t        |       }}|s*t        j                  | t        d             t        d      S t        j                         5  t        j                   j#                  |      }|j%                         }|j'                          ddd       dkD  r0t        j(                  | t        d	      j+                  |
             nt        j,                  | t        d             t        d      S # t        j                  $ r- t        j                  | t        d             t        d      cY S w xY wc c}w # t        t        f$ r- t        j                  | t        d             t        d      cY S w xY w# 1 sw Y   xY w# t.        $ ra}t0        j3                  d       t        j                  | t        d      j+                  t5        |                   Y d}~t        d      S d}~ww xY w)zN
    Deletes a bulk of AttendanceActivity records based on a list of IDs.
    r"  r#  zInvalid list of IDs provided.z.<script>$('.filterButton')[0].click()</script>z/No attendance activities selected for deletion.r$  Nr   z3{count} attendance activities deleted successfully.)countz7No matching attendance activities were found to delete.z1Error during bulk delete of attendance activitiesz/Failed to delete attendance activities: {error})r  )r   r   rv   r\  JSONDecodeErrorr   r  r   r   intrQ  	TypeErrorwarningr   r,  r@   rd   r   r  r  r   r  infor   loggerr   ru  )ry   ids_jsonr"  i
activitiesr  r  s          rm   attendance_activity_bulk_deleter    s   -
<<##E40	R**X&C
	R#&'a3q6'C'
 LM   PQQ ! 	 +33::#:FJ$$&E	 
 19GHOO P  MMKL HIIU ## 	RNN7A&E$FG PQQ	R
 (I& 	RNN7A&E$FG PQQ	R	  	 $  
LM?@GGcRSfGU	
 	

 HII
s   G D5 E= E8E= +G 9G AF<AG 5=E52G 4E55G 8E= =9F96G 8F99G <GG 	H2AH--H2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_outr  r  r=   r   r.  r[   rd   r   r   r:   r   isnar<   anykeysr4   employee_user_iddjango_timezone
make_awarer	   combiner   )activity_dictsr  r  sorted_activity_dictserror_dictsr  r  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_sheet5  s    9%ro   r   )rR  urlpatternszactivity-error-sheet-)r   zattendance/)r   r   r   r   attendance.urlsrR  r  uuiduuid4r.  r   )
error_datar   r  rR  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  E  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 Dater  r  r  r  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 )	NrC  rD  r   )r   r   z1attendance/attendance_activity/export_filter.htmlr   Attendance_activityr   )	r  r   r(   r    r@   rd   re   r   rQ   )ry   r   r|   s      rm   attendance_activity_exportr  s  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    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.htmlr:  *attendance.view_attendancelatecomeearlyoutNr   )r   r   r   r=  late_in_early_out_ids)r&   r   r   rf   r   rs   rR   r>  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    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  r  r   rB   rd   r   r   rv   r\  rO   r   )
ry   r   r  r   r  r  r  r  r  r|   s
             rm   late_in_early_out_single_viewr    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
| 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/r  r  r  )r   r  r  r   rB   rd   r   r  r   r   r   r  r  r   r   rv   r\  r  rO   r  s           rm   late_come_early_out_deleter    se    ;;##%L_d+ **,MO"**..&.9@@B!$?"@A
 ;;???+A-QRR8M2^#!!&)+:JJ}%v,
(= 8r-Xfgufv  wD  E
 	
 &22 Jw"G HI Ow"L MNOr  c           
         | j                   d   }t        j                  |      }g }|D ]D  }	 t        j                  j                  |      }|j                          |j                  |       F t        j                  | t        dj                  t!        |                         t#        ddi      S # t        j                  t        t        f$ r" t        j                  | t        d             Y w xY w)z;
    This method is used to delete bulk of attendances
    r"  r   zAttendance not found.z{} Late-in early-out deleted.rZ  r[  )r   rv   r\  rB   rd   r   r  r.  r  r  rQ  r   r  r   r   r  r   r   )ry   r"  del_idsattendance_idr  s        rm   late_come_early_out_bulk_deleter    s     ,,u
C
**S/CG @	@2::>>->PINN9%	@ Wa ? F Fs7| TUVI.// +77
S 	@NN7A&=$>?	@s   AB44=C43C4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.
    rC  rD  r   r   z1attendance/late_come_early_out/export_filter.htmlr   
Late_come_r   )	r  r   r&   rB   rd   re   r2   r   rQ   rH  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  1  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                 @   t        j                  | j                  d         }d}g }g }g }|D ]k  }	 t        j                  j                  |      }|j                  j                  | j                  j                  j                  k7  r|j                  |       m | j                  j                  r|}|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t1        d      d|j                   z   d	       1 |dkD  r.t3        j4                  | t        d      j#                  |             ||z   D ]3  }d|v rt3        j6                  | |       t3        j8                  | |       5 t;        ddi      S # t        j                  $ r |j                  t        d             Y et        t        f$ r |j                  t        d             Y w xY w# t        j                  $ r |j                  t        d             Y <t        t        f$ r |j                  t        d             Y gw xY w)z@
    This method is used to validate a bulk of attendances.
    r"  r   r   Attendance not foundzInvalid attendance IDz<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   iconz{} Attendances validated.PendingrZ  r   )rv   r\  r   r?   rd   r   r   rx   rs   rt   r.  r  r   r  rQ  is_superuserr   r  r  r   r   rb   sendr  r   r   r   r  r  r   )	ry   r"  validate_req_countsuccess_messagesr0  filtered_idsr   ri   msgs	            rm   validate_bulk_attendancer	  D  s    **W\\%(
)CNL >	>#++//6/:J%%((GLL,E,E,H,HH##F+	> ||    >	>#++//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.//k && 	=!!!$:";<z* 	>!!!$;"<=	>F && 	=!!!$:";<z* 	>!!!$;"<=	>s8   A(I":A*K%B?K"-J>(J>=J>-L1(LLc                 z   	 t         j                  j                  |      }| j                  j                  s{|j
                  j                  | j                  j                  j                  k(  rDt        j                  | t        d             t        | j                  j                  dd            S 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        | j                  j                  dd            S # t         j0                  t2        f$ r" t        j                  | t        d             Y _w xY w)zZ
    This method is used to validate attendance
    args:
        id  : attendance id
    r   z(You cannot validate your own attendance.r  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?   rd   r   rs   r  r   rx   rt   r   r  r   r   r  r   r   r   r   r   r  r  rb   r  r  r   r  rQ  ry   r   ri   r   r   s        rm   validate_this_attendancer    s   ;''++v+6
||((%%((GLL,E,E,H,HHw*T(UV+GLL,<,<^S,QRR*.
'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   B0G? 3D'G? ?8H:9H: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    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                  |      }| j                  j                  s{|j
                  j                  | j                  j                  j                  k(  rDt        j                  | t        d             t        | j                  j                  dd            S d|_        |j                          | j                  j!                         }d| }t        j"                  | |j
                   d|j$                  j'                  d       d	       t)        j*                  t,              5  t/        j0                  | j                  j                  |j
                  j2                  d
|j$                   dd|j$                   dd|j$                   dd|j$                   dd|j$                   dt5        d      d|j                   z   d	       ddd       t        | j                  j                  dd            S # 1 sw Y   .xY w# t         j6                  t8        f$ r" t        j                  | t        d             Y kw xY w)zf
    This method is used to approve attendance overtime
    args:
        obj_id  : attendance id
    r   z%You cannot approve your own overtime.r  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?   rd   r   rs   r  r   rx   rt   r   r  r   r   r  r	  r   r   r   r   r  r  r  r  r   rb   r  r  r   r  r  r  s        rm   approve_overtimer    s    ;''++v+6
||((%%((GLL,E,E,H,HHw*Q(RS+GLL,<,<^S,QRR15
.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:;s2   B0H  3BH  B&H'H  HH   8IIc                    | j                   d   }t        j                  |      }g }g }|D ]W  }	 t        j                  j                  |      }|j                  j                  | j                  k7  r|j                  |       Y | j                  j                   r|}|D ]  }	 t        j                  j                  |      }d|_        |j%                          |j                  |       t'        j(                  | j                  j*                  |j                  j                  d|j,                   dd|j,                   d|j,                   d	d
|j,                   d|j,                   t/        d      d|j0                   z   d	        |r7t        j2                  | t        dj5                  t7        |                         t9        ddi      S # t        j                  t        t        f$ r# t        j                  | t        d             Y w xY w# t        j                  t        t        f$ r# t        j                  | t        d             Y w xY w)z;
    This method is used to approve bulk of attendance
    r"  r   r  Tz+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  z {} Overtime approvedrZ  r[  )r   rv   r\  r?   rd   r   r   r  rs   r.  r  r  rQ  r   r  r   r  r	  r   rb   r  rt   r  r   rx   r   r  r   r   )ry   r"  otapprove_idsr  r  ri   s         rm   approve_bulk_overtimer  
  s3    ,,u
C
**S/CML ?	?#++//=/AJ%%66',,F##M2	? ||  % ?	?#++//=/AJ59J2OO  ,KK))$00AA!112-A//02//0
<//02//02 !;<jmm_?UU ?0 !$;$B$B3}CU$V"WXI.//? ''
C 	?NN7A&<$=>	?4 ''
C 	?NN7A&<$=>	?s%   AG#C*H=HH=II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.rP  NzAttendances added to r   z/attendance/attendance/attendance_add_batch.html)batchesr"  )rG   rd   re   r   r   r   stripr   r  r   r   r   r   r?   r!  r   r   r  r  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                 6   | j                   j                  d      }| j                  j                  d      }|dk(  s|dk(  s
|dk(  s|dk(  r| j                   j                  d      n| j                   j                  d      }|rY|dk(  s|dk(  s|dk(  s
|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(  s|dk(  rt3        |      n|dk(  rt5        |      nt7        |      }t9        | d| |d      S )Nr_  rN  attendanceUpdateattendanceRequestattendanceUpdateFormFieldsattendanceFormFieldsr   attendanceRequestDivr   r$  r  rc  rd  )r_  rg  rb  r   r)  rh  work_type_idrj  )
r0  r_  r   rn  r  attendance_clock_inattendance_clock_outrh   attendance_clock_in_datery  r   )attendance/attendance/update_hx_form.htmlry   r   )r   r   r  r   r[   rd   r   r	   rq  r   rw  r  r   rW   r   rr  rs  rt  r
   r   r  r5   ru  rY   findr.   r3   r*   r   )ry   r_  rT  r1  employee_querysetrv  r  r`  rx  rk  rl  rn  ry  ro  initial_datar   s                   rm   update_fields_based_shiftr9  _  s    {{z*H  !12I **++44..	 	&
 [[  / $  ..22//88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$ **i;W.W 	\2 // <05 	 3T* ro   c                    t        | j                  j                  d      | j                  j                  d            }t        | 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 )  
    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.
    r3  r1  ry  r2  r   r     02:<   rh   r   r4  r5  )	r;   r   r   total_secondsdivmodmaxr  r*   r   ry   r  r  r@  hoursminutesworked_hours_strr   s           rm   update_worked_hour_fieldrG    s     23-.H 34./I 3;yX	,,.b  Cq148NE7e*R#gm*<R)@A#;=M"NOD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 )r;  create_bulkrc  r3  r1  ry  r2  r   r   r<  r=  r>  r?  rh   r   r4  r5  )r;   r   r   r   r  r@  rA  rB  r  r*   r   rC  s           rm   rG  rG    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   rc  r_  rd  re  rn  )r   r	   rq  r   r  r   rW   rd   r   r   rt  ru  r5   r   )ry   rv  rn  r  r_  r`  rx  s          rm   form_date_checkingrK    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  rM  )r?   rd   r   at_work_secondr  overtime_secondr   rv   r\  rO   r   )ry   rx   rN  at_work_secondshours_at_workminutes_at_workrl   over_time_secondshours_over_timeminutes_over_timerO  r  r  r  r  s                  rm   user_request_one_viewrX  '  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?   r6  r   r  s      rm   get_attendance_activitiesrZ  O  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   r<  r:  Gemployee_id__employee_work_info__reporting_manager_id__employee_user_idr1  total_countFsafe)r   r   rs   r  rC   rd   re   r   ru  rx   r  r   )ry   page_numberr|   	employeesempr1  r`  s          rm   hour_attendance_selectrf  Z  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   r<  r   r:  r]  r_  )r   r   rv   r\  rs   r  r#   rC   rd   re   r   r   ru  rx   r  r   
ry   rc  filteredfiltersemployee_filterfiltered_employeesre  r1  r`  r|   s
             rm   hour_attendance_select_filterrn  q  !   ++//&)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   ru  rx   r  r   ry   rc  rd  re  r1  r`  r|   s          rm   activity_attendance_selectrs    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 rh  )r   r   rv   r\  rs   r  r    r@   rd   re   r   r   ru  rx   r  r   ri  s
             rm   !activity_attendance_select_filterrv    ro  rp  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  r:  r]  r_  Fra  )r   r   rs   r  rB   rd   re   r   ru  rx   r  r   rr  s          rm   latecome_attendance_selectrx    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,, 6rt  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   r:  r]  r_  )r   r   rv   r\  rs   r  r&   rB   rd   re   r   r   ru  rx   r  r   ri  s
             rm   !latecome_attendance_select_filterrz    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rp  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.rP  *attendance/grace_time/grace_time_form.html)r   r}  )rP   r   r   r1   r   r   r   cleaned_datar   grace_time_idr   r   r   r   r   )ry   r}  r   r  	gracetimer~  shifts          rm   create_grace_timer     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   r~  z(Grace time added to shifts successfully.rP  z'attendance/grace_time/assign_shift.html)r   
grace_time)rH   rd   r   r   r0   r   r   r   r  r   r  r   r   r   r   r   r   )ry   grace_idr  r   r  r~  r  s          rm   assign_shiftr  !  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.rP  )r   r  r  r   )rH   rd   r   r1   r   r   r   r   r   r   r   r   r   )ry   r  r  r   r{   r|   s         rm   update_grace_timer  :  s     ""&&(&3J*-D~~W\\J?==?yyy.HMMOWa(J&KL KLLG =w ro   zattendance.delete_gracetimec                    	 d}t         j                  j                  d      j                         }t         j                  j                  d      j                         }t         j                  j	                  |      }|j
                  }|j                          t        j                  | t        d             |rrt        d      S t        d	      S d
k(  rrt        d      S d
k(  rst        d	      S t        d      S # t         j                  $ r$ d}t        j                  | t        d             Y t        $ r$ d}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
    FTr}  r   z Grace time deleted successfully.zGrace Time Does not exists..zRelated datas exists.z7<script>$('#default-containerReload').click();</script>z9<script>$('#gracetime-containerReload').click();</script>r)  zU<script>$('#default-containerReload').click();$('.defaultGraceNav').click();</script>z4<script>$('#reloadMessagesButton').click();</script>)rH   rd   r   r  r   r}  r  r   r   r   r  r  r   r   )ry   r  delete_errordefault_grace_time_countgrace_time_countr  grace_time_types          rm   delete_grace_timer  Y  sN   <#,#4#4#;#;t#;#L#R#R#T $,,33u3EKKM&&**h*7
$//!$F"GH I  WXX	!Q	&?c
 	
 
Q	WXXNOO% !! Cw"@ AB <w"9 :;<s   B9D 4E&:)E&%E&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   rD  Tr   z!Gracetime activated successfully.r  rZ  Fz#Gracetime deactivated successfully.)r   r   rH   rd   	is_activer   r   r   )ry   r  r  r  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
    r  r  updater   r  rD  Tr   z.Gracetime applicable on clock-In successfully.r  Fz1Gracetime unapplicable on clock-In  successfully.r  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   r  r  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   r}  rf   r   r   r   r\   r  r  r  rb   r  r   r   )
ry   r  r   ri   re  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   r}  rf   r   r   r   r   rE   r   r   r.  r  r  r   )
ry   r  r  r  r  r  commentattachmentsr   file_instances
             rm   view_attendancerequest_commentr  F	  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
    rb  r   zComment deleted successfully!)rD   rd   r   r  r   r   r   r   )ry   r  scriptr  s       rm    delete_attendancerequest_commentr  e	  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
    rb  r"  r$  zFile deleted successfully)
r   r   rE   rd   r   r  r   r   r   r   )ry   r  r"  s      rm   delete_comment_filer  q	  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   rw  r   r   r   )ry   rw  r   r|   s       rm   work_recordsr  }	  sC    JJLEKK))+MG ? ro   c                 n   | j                   j                         }t        | j                   xs d       }t        | |j                  d      }| j                   j                  dt        j                         j                   dt        j                         j                         }	 t        t        |j                  d            \  }}| j                  j                  gt!        |      z   }t#        j$                  ||      D cg c]&  }|D ]  }|rt'        |||      j                         ! ( }	}}t(        j*                  j-                  |	|      j/                  ddd      }
|
D ci c]%  }|j0                  j2                  |j                  f|' }}|D ci c].  }||	D cg c]  }|j                  |j2                  |f      ! c}0 }}}t5        t!        |j7                               t9                     }|j;                  | j                   j                  d            }|	t=        ||      ||t        j                         |d	}t?        | d
|      S # t        $ r@ t        j                         j                  t        j                         j                  }}Y w xY wc c}}w c c}w c c}w c c}}w )Nr   r  -)date__inr(  r   r_  r  r   )current_month_dates_listleave_datesr   r   r  r   z,attendance/work_record/work_record_list.html) r   r   rZ   rS   r   r   r   rw  ra  r  mapr  r   rQ  rs   rt   listcalendarmonthcalendarr	   rI   rd   r   select_relatedr   rx   r   itemsrU   get_pager8   r   )ry   r   employee_filter_formrd  	month_strra  r  weekr`  month_datesr  wrwork_records_dictrz   r  r   	paginatorr   r|   s                      rm   work_records_change_monthr  	  s|    KK))+M)'++*=>/%((*FI DJJL,=,=+>a

@R@R?S)TUI<#ys34e **+d9o=I **47  	uc"''))K  &&--i . n]J@  EQQb"..++RWW5r9QQ "
 	 	 +
 !!8;;"=>
 	
D  $tzz|,n.>?Igkkoof56D %0)%6

!G 'I7SSM  <jjl''););e<
 R
s7   %"I +J!'*J'
J1"$J,J1AJJ,J1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 ]~  }d|i}t7        ||      D ]X  \  }}||vr/|t        j                         k  r|	j                  ||fd      ||<   9|	j                  ||fd	      }|dk7  r|nd	||<   Z |j9                  |        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  ra  z Invalid month or year parameter.)date__month
date__yearr)  c                       y)NABS r  ro   rm   <lambda>z$work_record_export.<locals>.<lambda>	  s    ro   r[   DFTrb  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   rw  r  ra  rQ  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   rB  astyperu  r  r   
set_columnseekr   read)"ry   r  ra  rd  r   num_daysr`  all_date_objectsr  record_lookuprecord
record_keydate_formatformat_stringformatted_dates	data_rowsrz   row_dataformatted_dayr   r   dfoutputwriterworkbook	worksheetformatsrow_idxrowcol_idx
cell_valuecolmax_lenr   s"                                     rm   work_record_exportr  	  s9   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 	HC+%#

*<*7*;*;XsOU*S'$(((C"=26%-$R'	H 	"# l_,G	i	1BZZ\F	|	4 <
F%H=;;MM(+	 &&&i@ &&&i@ &&&i@ ''&i@ &&&i@
$ &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;	<9<@ KKNXH 'WH"#OS  J%&HIIJ R P"< <s2   BQ 4Q8Q!CQ&/BQ&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  
  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   rM   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 {} successfullyrP  )r   boolr   r   rX   rd   get_or_creater  r   r   r   r   r  r   )ry   enabler  createdrZ  s        rm   +enable_disable_tracking_late_come_early_outr  0
  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  B
  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   r  
setting_Idr   )enable_check_inz,Check In/Check Out has been successfully {}.r  r  z+attendance/components/in_out_component.htmlrb  )r   r   r   r  rA   rd   r   r  r   r  r   r   r   r   )ry   
is_checked
setting_idr  updatedrZ  s         rm   enable_disable_check_inr!  O
  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.
    Tr  z%attendance/grace_time/grace_time.html)r   default_grace_timegrace_times)rF   rd   r   rH   r   re   r  r   )ry   r   r$  r%  s       rm   grace_time_viewr&  j
  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#  Tr  z%attendance/break_point/condition.html)r   r$  )rF   rd   r   rH   r   r   )ry   r   r$  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   r5  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_enabledrP  FN)	rL   r   rV   rd   r   creater   r1  r   )ry   r   ip_restictions      rm   enable_ip_restrictionr4  
  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_addressr8  
  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)r1  additional_datarP  ,attendance/ip_restriction/restrict_form.htmlr   )r   rL   r   r   r  r   rV   rd   r   r  r;  intersectionr   r  r  r  unionr   r   r  r2  r   r   )ry   r   r:  r/  existing_ipsnew_ips
duplicatesnon_duplicatess           rm   create_allowed_ipsrC  
  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;  r  rP   r   r   r   r  r   )ry   r"  r/  ipsrx   s        rm   delete_allowed_ipsrG    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.rE  r/  rx   r   r:  r   r   zIP address already exists.zIP address updated successfullyrP  zInvalid ID provided.r<  )r   rx   )rV   rd   r   r   r  r   r;  r   r   r  r   
IndexErrorrL   r   r   r   r  r  discardr  r  r   r   r   rQ  r   )ry   r/  rF  rx   
initial_ipr   new_ipr?  s           rm   edit_allowed_ipsrM  5  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   r   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   base.methodsrN   rO   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   r3  r5  r8  rA  rI  rL  rX  r]  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r)  r9  rG  rK  rX  rZ  rf  rn  rs  rv  rx  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r!  r&  r(  r+  r-  r/  r4  r8  rC  rG  rM  r  ro   rm   <module>rr     s
     O .			8	$   	  # . . +  # + 9 ! + (  . 3  4 % 2 6 =	 	 	          O L I	 	 	  , =  )," H  H, +,!H - !HH ./N 0  N6 01' 2 ': 01! 2 !H  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%   = %P ;<0 = 04 ( (V ! !H  : ;<vx()
 * = 
@ ;<vh3J   = 3Jl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D0 3   D0N 12&G 3 &GR !D !DH 12'G 3 'GT 12,0 3 ,0^  D X  Xv   B '  'T  6 $ $N    - -, % %@ - -* % %@ - -* % %@ /0 1  < 01
 2  
, 23 4  8 23$P 4 $PN 23" 4 "4 23+" 4 +"\ t tn  <         	 	 1T  1Th 23M 4 M` >?	# @  	# 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   