
    six                       d Z ddlZddlZddlmZmZ ddl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mZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl m"Z# ddl$m%Z% ddl&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZM dd lNmOZOmPZPmQZQ  ej                  eS      ZTd! ZUd" ZVdpd#ZWd$ ZX G d% d&e      ZY G d' d(e      ZZe4e3 e5d)      d*                      Z[e4e3 e5d+      d,                      Z\e4e3e2 e5d+      d-                             Z]e4e3e2 e5d.      d/                             Z^e4e3e2 e5d+      d0                             Z_e4e3e2 e5d+      d1                             Z`e4e3e2 e5d2      d3                             Zad4 Zbd5 Zcd6 Zdd7 Zed8 Zfd9 Zge4e3e2 e5d)      d:                             Zhe4e3e2 e5d)      d;                             Zie4e3e2 e5d)      dqd<                            Zjd= Zkd> Zld? Zmd@ Zne4e3 e5dA      dB                      Zoe4e3e2 e5dA      dC                             Zpe4e3 e5dD      dE                      Zqe4e3 e5dF      dG                      Zre4e3e2 e5dF      dH                             Zse4e3 e5dD      dI                      Zte4e3 e5dD      dJ                      Zue4e3 e5dD      dK                      Zve4e3e2 e5dL      dM                             Zwe4e3e2dN                      Zxe4e3e2dO                      Zye4e2e3dP                      Zze4e2e3dqdQ                     Z{e4e3e2 e5dD      dqdR                            Z|e4e3e2 e5d+      dS                             Z}dT Z~dU ZdV ZdW ZdX ZdY ZdZ Zd[ Zd\ Zd] Z	 eOj                  j                         j                  d^_      ZeOj                  j                  d`a      D ]  Zes eUej                        dkD  sej                  dbk(  r@ e       Zej#                  dc dd eUej                        e       ej%                          nej                  dfk(  rO e       Zej#                  dg dd eUej                        dhej&                   i       ej%                          ej                  djk(  rA e       Zej#                  dk dd eUej                        e       ej%                          ej                  dlk(  rA e       Zej#                  dm dd eUej                        e       ej%                          oej                  dnk(  rA e       Zej#                  do dd eUej                        e       ej%                           y#  Y yxY w)ra  
Module for managing biometric devices and employee attendance.

Includes classes and functions for adding, editing, and deleting biometric devices,
as well as scheduling attendance capture. Also provides views for managing employees,
registered on biometric devices.
    N)datetime	timedelta)EventThread)parse_qsunquote)BackgroundScheduler)messages)HttpResponseJsonResponse)redirectrenderrender_to_string)timezone)gettext)gettext_lazy)ZK)	exception)Request)AttendanceActivity)clock_in	clock_out)get_key_instancesget_pagination)EmployeeEmployeeWorkInformation)hx_request_requiredinstall_requiredlogin_requiredpermission_required)HorillaPaginator)BIO_DEVICE_THREADS)	TIME_ZONE   CrossChexCloudAPI)COSECBiometric)DahuaAPI)ETimeOfficeAPI)BiometricDeviceFilter)BiometricDeviceFormBiometricDeviceSchedulerFormCosecUserAddFormCOSECUserFormDahuaUserFormEmployeeBiometricAddFormMapBioUsers)BiometricDevicesBiometricEmployeesCOSECAttendanceArgumentsc                 |    g d}t        d t        |t        t        | j	                  d                  D              S )z
    this method is used reconvert time in H:M formate string back to seconds and return it
    args:
        time : time in H:M format
    )i  <   r%   c              3   ,   K   | ]  \  }}||z    y wN ).0abs      3/var/www/python-projects/worksol/biometric/views.py	<genexpr>z#str_time_seconds.<locals>.<genexpr>E   s     EAq1uEs   :)sumzipmapintsplit)timeftrs     r>   str_time_secondsrH   =   s0     CES#c4::c?*C!DEEE    c                 P    t        | t                     }|j                  |      } | S )z3
    This method is used to paginate query set
    )r"   r   get_page)qrysetpage_number	paginators      r>   paginator_qryrO   H   s(     !)9:I,FMrI   c                     |dz
  |z  }||z  }i }| || |d<   t        |       }||z   dz
  |z  }|dkD  }||k  }	||r|dz
  nd|	r|dz   nd|||	d|d<   |S )zC
    This function is used to paginate a list of dictionaries.
    r%   usersN)numberprevious_page_numbernext_page_number	num_pageshas_previoushas_nextrN   )len)
	data_listrM   per_pagestart_index	end_indexpaginated_datatotal_itemstotal_pagesrV   rW   s
             r>   biometric_paginator_qryr`   Q   s     ?h.Kh&IN'I>N7i.K)A-(:K?L[(H 3?aT/7K!OT $#N; rI   c                 N    t        j                         }| j                  |       y)z
    Sets the time on the biometric device using the provided connection.

    Parameters:
    - conn: The connection to the biometric device.

    Returns:
    None
    N)r   todayset_time)connnew_times     r>   biometric_set_timerf   j   s     ~~HMM(rI   c                   .     e Zd ZdZ fdZd Zd Z xZS )ZKBioAttendancea8  
    Represents a thread for capturing live attendance data from a ZKTeco biometric device.

    Attributes:
    - machine_ip: The IP address of the ZKTeco biometric device.
    - port_no: The port number for communication with the ZKTeco biometric device.
    - conn: The connection object to the ZKTeco biometric device.
    - _stop_event: Event flag to signal thread termination.

    Methods:
    - run(): Overrides the run method of the Thread class to capture live attendance data.
    - stop(): Sets the _stop_event to signal the thread to stop gracefully.
    c                     t         |           || _        || _        t	        |      | _        t               | _        d | _        y r9   )	super__init__
machine_ipport_norD   passwordr   _stop_eventrd   )selfrl   rm   rn   	__class__s       r>   rk   zZKBioAttendance.__init__   s8    $H 7	rI   c           	      N   	 t        | j                  | j                  d| j                  dd      }|j	                         }|| _        |rt        j                  j                  | j                  | j                        j                         }|rk|j                  r]| j                  j                         sA|j                         }|D ]  }|r|j                  }|j                  }t!        j"                  |j$                        }|j'                         }	|j)                         }
|	|_        |
|_        |j/                          t0        j                  j                  ||      j                         }|s|dv r.	 t3        t5        |j6                  j8                  |	|
|             	 tA        t5        |j6                  j8                  |	|
|              | j                  j                         s@y y y y y # t:        $ r!}t<        j?                  d|       Y d }~Xd }~ww xY w# t:        $ r!}t<        j?                  d	|       Y d }~d }~ww xY w# tB        $ rC}tE        | j                  | j                  | j                        jG                          Y d }~y d }~ww xY w)
N   Fporttimeoutrn   	force_udp
ommit_ping)rl   ru   user_id	device_id   r         userdaterF   r   zGot an error in clock_in %szGot an error in clock_out)$r   rl   rm   rn   connectrd   r3   objectsfilterfirstis_livero   is_setlive_capturerz   punchdjango_timezone
make_aware	timestampr   rF   last_fetch_datelast_fetch_timesaver4   r   r   employee_idemployee_user_id	Exceptionloggererrorr   ConnectionResetErrorrh   start)rp   	zk_devicerd   deviceattendances
attendancerz   
punch_code	date_timer   rF   bio_idr   s                r>   runzZKBioAttendance.run   s^   E	R\\ I $$&DDI)1188#T\\ 9 %'  fnn"..557&*&7&7&9*5 1)J)*4*<*<-7-=-=
,;,F,F$.$8$8-"	 (1~~'7'0~~'79= 69= 6 &);)C)C)J)J,3v *K *""'%' !' $*'1Y'>)5,4079?9K9K9\9\9=9==F	12-.)5,5079?9K9K9\9\9=9==F	12-. !)c1) #..5577 -6	 D 09 )5,2LL0Mu-. -5)5  09 )5,2LL0KU-. -5	)5 $ 	RDOOT\\4==IOOQQ	Rsr   E6I 9I >,G>*I ,,H+I >	H(H#I #H((I +	I4I
I II 	J$!9JJ$c                 &    d| j                   _        y)z To stop the ZK live capture modeTN)rd   end_live_capturerp   s    r>   stopzZKBioAttendance.stop   s    %)		"rI   __name__
__module____qualname____doc__rk   r   r   __classcell__rq   s   @r>   rh   rh   x   s    FRP*rI   rh   c                   .     e Zd ZdZ fdZd Zd Z xZS )COSECBioAttendanceThreadaG  
    A thread class that handles the real-time retrieval and processing of
    biometric attendance data from a COSEC biometric device.

    Attributes:
        device_id (int): The ID of the biometric device to interact with.
        _stop_event (threading.Event): An event to signal when to stop the thread.

    Methods:
        run():
            Continuously fetches attendance data from the COSEC device, processes
            it, and updates the last fetched sequence and rollover count.

        stop():
            Signals the thread to stop by setting the _stop_event.
    c                 N    t         |           || _        t               | _        y r9   )rj   rk   r{   r   ro   )rp   r{   rq   s     r>   rk   z!COSECBioAttendanceThread.__init__   s    " 7rI   c           	      |   	 t         j                  j                  | j                        }|j                  sy t
        j                  j                  |      j                         }|rt        |j                        nd}|rt        |j                        nd}t        |j                  |j                  |j                  |j                  d      }| j                   j#                         s|j%                  |t        |      dz         }t'        |t(              s| j                   j+                  d       e|D ]  }|d   }t,        j                  j                  |	      j                         }	|	s9|d
   }
|d   }t/        j0                  |
d      j3                         }t/        j0                  |d      j5                         }t/        j6                  ||      }|d   }t9        |	j:                  j<                  ||t?        j@                  |            }	 |dv rtC        |       n|dv rtE        |        |r3|d   }|d   }|d   }t
        j                  jM                  |||d       | j                   j+                  d       | j                   j#                         sy y # tF        $ r!}tH        jK                  d|       Y d }~d }~ww xY w# tF        $ ra}t         j                  j                  | j                        }d|_        |jO                          tH        jK                  d|       Y d }~y d }~ww xY w)Nidr{   r   r%   
   rv   rs   detail-1ref_user_idr   rF   %d/%m/%Y%H:%M:%Sdetail-2r   135790246810Error processing attendance: seq-Noroll-over-countlast_fetch_roll_ovr_countlast_fetch_seq_numberr{   defaults   Fz#Error in COSECBioAttendanceThread: )(r3   r   getr{   r   r5   r   r   rD   r   r   r(   rl   ru   bio_usernamebio_passwordro   r   get_attendance_events
isinstancelistwaitr4   r   strptimer   rF   combiner   r   r   r   r   r   r   r   r   r   update_or_creater   )rp   r   device_argsr   r   cosecr   r   r   employeedate_strtime_strattendance_dateattendance_timeattendance_datetimer   request_datar   last_attendances                      r>   r   zCOSECBioAttendanceThread.run   s   O	G%--11T^^1DF>>2::AA  B eg  ?JK99:q & ;FK5561 " #!!####E &&--/#99-s3H/IA/M "+t4$$))!,"- MJ",Z"8K199@@$/  A  eg  $ )&1H)&1H&.&7&7*&M&R&R&TO&.&7&7*&M&R&R&TO*2*:*:'+' ",J!7J#*%11BB,,!0!;!;<O!P	$LM%)GG$\2'+EE%l37M> &1"oO,;H,E)0?@Q0R-,44EE"(9R5J" F    %%a(g &&--//H % M%DeLLM"  	G%--11T^^1DF"FNKKMLL>FF		GsI   6K GK J$4A,K $	K-K	K 	KK 	L;AL66L;c                 8    | j                   j                          y)z;Set the stop event to signal the thread to stop gracefully.N)ro   setr   s    r>   r   zCOSECBioAttendanceThread.stopE  s    rI   r   r   s   @r>   r   r      s    "#
PGdrI   r   zbiometric.view_biometricdevicesc           
         | j                   j                         }| j                   j                  d      }t        | j                         }|j                  j                  d      }|r|dk(  r|j                  d      }t        || j                   j                  d            }t        |      }t        t        |       t        | dt               ||||d      S )	a+  
    Renders and filters the list of biometric devices based on query parameters.

    Handles both initial page load and HTMX-based filter/search requests.

    Template:
    - "biometric/view_biometric_devices.html"

    Context:
    - biometric_form (BiometricDeviceForm): Form for adding new biometric devices.
    - devices (QuerySet): Filtered and paginated queryset of biometric devices.
    - f (BiometricDeviceFilter): Filter form.
    - pd (str): URL-encoded query params for HTMX push.
    - filter_dict (dict): Parsed filter query params.
    	is_activez-created_atunknownTr   pagez%biometric/view_biometric_devices.html)biometric_formdevicesfpdfilter_dict)GET	urlencoder   r+   qsorder_byr   rO   r   r   r3   r   r,   )requestprevious_datar   filter_formbiometric_devices	data_dicts         r>   biometric_devices_viewr   J  s    & KK))+M,I (4K#//> 	Y.-44t4D &&79PQ 'I&	2 /13($	

 
rI   z!biometric.change_biometricdevicesc                 	   t         j                  j                  |      dj                  i}t	        |      }||d}| j
                  dk(  r?t	        | j                        }|j                         rj                  dk(  r	 j                  }j                  }j                  }d}t        ||dt        |      d	d	
      }	|	j                         }|j                  d       | j                  j                  d      }
t         j                  j                  |      |
_        d_        d	_        j%                          t'               }|j)                  fddt+        j                               |j-                          t/        d      S j                  dk(  r| j                  j                  d      }
d_        |
_        j%                          t'               }|j)                  fddt+        j                               |j-                          t/        d      S j                  dk(  r| j                  j                  d      }
d_        d	_        |
_        j%                          t'               }|j)                  fddt+        j                               |j-                          t/        d      S j                  dk(  r| j                  j                  d      }
d_        d	_        |
_        j%                          t'               }t7        j                  j8                        }|r!|j;                          t6        j8                  = |j)                  fddt+        j                               |j-                          t/        d      S j                  dk(  r| j                  j                  d      }
d_        d	_        |
_        j%                          t'               }|j)                  fddt+        j                               |j-                          t/        d      S t/        d      S ||d<   t=        | d|      }t/        |j>                  jA                  d      dz         S t=        | d|      S # t0        $ r-}t2        j5                  d|       d}t/        |      cY d}~S d}~ww xY w) aX  
    Handles scheduling of attendance capture from a biometric device.

    Parameters:
    - request (HttpRequest): The HTTP request object.
    - device_id (uuid): The ID of the biometric device for which scheduling is being done.

    Returns:
    - HttpResponse: HTML response indicating success or failure of the scheduling operation.
    r   scheduler_durationinitial)scheduler_formr{   POSTzkNrs   Frt   r   indexTc                  .    t         j                        S r9   )!zk_biometric_attendance_schedulerr   r   s   r>   <lambda>z+biometric_device_schedule.<locals>.<lambda>  s     A&)) L rI   intervalseconds)<script>window.location.reload()</script>z,An error comes in biometric_device_schedule a  
                    <script>
                        Swal.fire({
                          title : "Schedule Attendance unsuccessful",
                          text: "Please double-check the accuracy of the provided IP Address and Port Number for correctness",
                          icon: "warning",
                          showConfirmButton: false,
                          timer: 3500,
                          timerProgressBar: true,
                          didClose: () => {
                            location.reload();
                            },
                        });
                    </script>
                    anvizc                  .    t         j                        S r9   )$anviz_biometric_attendance_schedulerr   r  s   r>   r  z+biometric_device_schedule.<locals>.<lambda>      @K rI   dahuac                  .    t         j                        S r9   )$dahua_biometric_attendance_schedulerr   r  s   r>   r  z+biometric_device_schedule.<locals>.<lambda>  r  rI   r   c                  .    t         j                        S r9   )$cosec_biometric_attendance_schedulerr   r  s   r>   r  z+biometric_device_schedule.<locals>.<lambda>  r  rI   etimeofficec                  .    t         j                        S r9   )*etimeoffice_biometric_attendance_schedulerr   r  s   r>   r  z+biometric_device_schedule.<locals>.<lambda>  s    FvyyQ rI   r   z$biometric/scheduler_device_form.htmlzutf-8z<script>$('#BiometricDeviceTestModal').removeClass('oh-modal--show');            $('#BiometricDeviceModal').toggleClass('oh-modal--show');</script>)!r3   r   r   r   r-   methodr   is_validmachine_typeru   rl   zk_passwordr   rD   r   
test_voiceis_schedulerr   r   r	   add_jobrH   r   r   r   r   r   r#   r   r   r   contentdecode)r   r{   initial_datar   contextrm   rl   rn   rd   r   duration	schedulerr   scriptexisting_threadresponser   s                   @r>   biometric_device_scheduler'  }  se    %%))Y)7F(&*C*CDL1,GN(G ~~5gllC""$""d*.0$kkG!'!2!2J%11HD ""$ !!$X"'#(!I %,,.DOO!O,&||//0DEH-5599Y9GF08F-*.F'%*FNKKM 3 5I%%L" 01J1J K & 
 OO%'(STT& $$/"<<++,@A&*#,4)/1	!!K,V-F-FG " 
 !#$OPP$$/"<<++,@A&*#!&,4)/1	!!K,V-F-FG " 
 !#$OPP$$/"<<++,@A&*#!&,4)/1	"4"8"8"C"#((**6995!!K,V-F-FG " 
 !#$OPP$$5"<<++,@A&*#!&,4)/1	!!Q,V-F-FG " 
 !#$OPP#$OPP$2 !'#I7S##G,PP
 	

 'A7KKo ! 0LL!OQVWF (//#0s   DR 	S&"SSSc                     | j                   j                         }t        j                  j	                  |      }d|_        |j                          t        j                  | t        d             t        d|       S )aT  
    Handles unschedule of attendance capture for a biometric device.

    Parameters:
    - request (HttpRequest): The HTTP request object.
    - device_id (uuid): The ID of the biometric device for which unscheduling is being done.

    Returns:
    - HttpResponseRedirect: Redirects to the biometric devices view after unscheduling.
    r   Fz)Biometric device unscheduled successfully#/biometric/view-biometric-devices/?)r   r   r3   r   r   r  r   r
   success_r   )r   r{   r   r   s       r>   biometric_device_unscheduler,    si     KK))+M%%))Y)7FF
KKMWa KLM9-IJJrI   zbiometric.add_biometricdevicesc                    t        | j                  j                               }|j                  d      r|dd n|}t	               }| j
                  dk(  r^t	        | j                        }|j                         r9|j                          t        j                  | t        d             t	               }||d}t        | d|      S )a  
    Handles the addition of a new biometric device.

    Parameters:
    - request (HttpRequest): The HTTP request object containing data about the request.

    Returns:
    - HttpResponse: Renders the 'add_biometric_device.html' template with the biometric device form.
    zpd=r}   Nr   z$Biometric device added successfully.)r   r   $biometric/biometric_device_form.html)r   r   r   
startswithr,   r  r   r  r   r
   r*  r+  r   )r   r   r   r!  s       r>   biometric_device_addr0  $  s     GKK1134M*55e<ab-  )*N~~,W\\:""$!Wa(N&OP02N!/}EG'A7KKrI   c                    t        j                  |      }|s+t        j                  | t	        d             t        | d      S t        |      }| j                  dk(  rVt        | j                  |      }|j                         r/|j                          t        j                  | t	        d             ||d}t        | d|      S )a  
    Handles the editing of an existing biometric device.

    Parameters:
    - request (HttpRequest): The HTTP request object containing data about the request.
    - device_id (uuid): The ID of the biometric device to be edited.

    Returns:
    - HttpResponse: Renders the 'edit_biometric_device.html' template with the biometric
                    device form pre-filled with existing data.
    Biometric device not found.r.  )instancer   z&Biometric device updated successfully.)r   r{   )r3   findr
   r   r+  r   r,   r  r   r  r   r*  )r   r{   r   r   r!  s        r>   biometric_device_editr5  A  s      ""9-Fw"? @AgEFF(&9N~~,W\\FK""$!Wa(P&QR(G 'A7KKrI   c                    | j                   j                         }t        j                  |      }|s-t	        j
                  | t        d             t        d|       S |j                   |_        |j                          |j                  st        d      n
t        d      }t	        j                  | t        d      d|iz         t        d|       S )z>
    This method is used to archive or un-archive devices
    r2  r)  archivedzun-archivedzDevice is %(message)smessage)r   r   r3   r4  r
   r   r+  r   r   r   r*  )r   r{   r   
device_objr8  s        r>   biometric_device_archiver:  b  s     KK))+M!&&y1Jw"? @A=m_MNN)333JOO#-#7#7a
mQ}=MGWa 78Iw;OOP9-IJJrI   z!biometric.delete_biometricdevicesc                 8   | j                   j                         }t        j                  |      }|s-t	        j
                  | t        d             t        d|       S |j                          t	        j                  | t        d             t        d|       S )a  
    Handles the deletion of a biometric device.

    Parameters:
    - request (HttpRequest): The HTTP request object containing data about the request.
    - device_id (uuid): The ID of the biometric device to be deleted.

    Returns:
    - HttpResponseRedirect: Redirects to the 'view-biometric-devices' page after deleting the
                            biometric device.

    r2  r)  z&Biometric device deleted successfully.)
r   r   r3   r4  r
   r   r+  r   deleter*  )r   r{   r   r9  s       r>   biometric_device_deleter=  v  s    " KK))+M!&&y1Jw"? @A=m_MNNWa HIJ9-IJJrI   c                 &    | ||d}t        d|      S )z\
    Helper function to render the connection
    response from device test connection.
    )titletexticonz%biometric/test_connection_script.htmlr   )r?  r@  rA  r!  s       r>   render_connection_responserB    s$     G
 CWMMrI   c                    d}| j                   }| j                  }| j                  }t        ||dt	        |      dd      }	 |j                         }|j                  d       t        | j                         t        t        d      t        d      d	      ||j                          S S # t        j                  $ r5 t        t        d
      t        d      d      cY ||j                          S S t        $ rL t        j!                  dd       t        t        d      t        d      d      cY ||j                          S S w xY w# ||j                          w w xY w)z"Test connection for ZKTeco device.Nr7   Frt   r   r  Connection Successfulz"ZKTeco test connection successful.r*  Authentication Error1Double-check the provided IP, Port, and Password.warningzZKTeco connection errorTexc_infoConnection unsuccessfulz(Please check the IP, Port, and Password.)ru   rl   r  r   rD   r   r  find_employees_in_zkr   rB  r+  
disconnectzk_exceptionZKErrorResponser   r   r   )r   rd   rm   rl   rn   r   s         r>   test_zkteco_connectionrO    sc   DkkG""J!!HXI  "a VYY')%&23
& OO  '' 
)$%AB
 	
 OO   
.>)'(89
 	
 OO 
 OO s1   AB+ +2EE	 2>E0E	 EE	 	Ec                 f   	 ddl m}  || j                  | j                  | j                  | j
                        }|j                         }|j                  d      rk|j                  d      | _        |j                  d      | _	        | j                          |j                         }t        t        d      t        d      d      S t        t        d	      t        d
      d      S # t        $ r@}t        j!                  dd       t        t        d	      t        d      d      cY d}~S d}~ww xY w)z!Test connection for Anviz device.r%   r&   api_urlapi_key
api_secretanviz_request_idtokenexpiresrD  z!Anviz test connection successful.r*  rJ  #API credentials might be incorrect.rG  zAnviz connection errorTrH  zAPI request failed.N)r  r'   rR  rS  rT  rU  test_connectionr   	api_tokenapi_expiresr   get_attendance_recordsrB  r+  r   r   r   )r   r'   r  test_responserecordsr   s         r>   test_anviz_connectionr_    s    
,!NNNN((#44	
 --/W%,009F!.!2!29!=FKKM224G-)*56  .+,78 
  
-=)'(!,A*BI
 	

s$   CC' C' '	D005D+%D0+D0c                 Z   t        | j                  | j                  | j                  | j                  d      }|j                         }|j                  d      r4t        | j                         t        t        d      t        d      d      S t        t        d      t        d      d	      S )
z!Test connection for COSEC device.r   r   apprD  z"Matrix test connection successful.r*  rJ  =Double-check the provided Machine IP, Username, and Password.rG  )r(   rl   ru   r   r   basic_configr   find_employees_in_cosecr   rB  r+  )r   r   r&  s      r>   test_cosec_connectionre    s    E !!#H||E		*)%&23
 	
 *'(MN
 	
rI   c                    t        | j                  | j                  | j                        }|j	                         }|j                  d      dk(  rt        t        d      t        d      d      S t        t        d      t        d      d	      S )
z!Test connection for Dahua device.ipusernamern   status_code   rD  z!Dahua test connection successful.r*  rJ  rb  rG  )r)   rl   r   r   get_system_infor   rB  r+  )r   r  r&  s      r>   test_dahua_connectionrm    s    $$$$E
 $$&H||M"c))%&12
 	
 *'(MN
 	
rI   c           
         t        j                         }t        | j                  | j                        }|j
                  dd|j                  dd|j                   d}|j
                  dd|j                  dd|j                   d|j                  dd|j                  d	}	 |j                  ||      }|j                  d      d	k(  rt        t        d
      t        d      d      S |j                  d      }t        t        d      t        d      j                  |      d      S # t        $ r5}t        t        d      t        dt!        |             d      cY d}~S d}~ww xY w)z(Test connection for e-TimeOffice device.ri  rn   02d/_00:00r+  r@   	from_dateto_dateMsgSuccessrD  z)e-Time Office test connection successful.r*  MessagerJ  z=Double-check the provided API Url, Username, and Password: {}rG  zConnection errorz#API request failed with exception: dangerN)r   nowr*   r   r   daymonthyearhourminutedownload_punch_datar   rB  r+  formatr   str)r   rz  r  rt  ru  r&  	error_msges           r>   test_etimeoffice_connectionr    sd   
,,.C $$$$K
 773-q3q
&AI773-q3q
!CHHS>3::cBRS 
22YPW2X<<)+-)*=>  LL+	)'(MNUU 
 	
  
) !3CF8<=
 	

s%   (AD- .>D- -	E+6*E& E+&E+c                 H   t         j                  j                  |      j                         }|st	        dd      S d}	 |j
                  dk(  rt        |      }ny|j
                  dk(  rt        |      }n^|j
                  dk(  rt        |      }nC|j
                  d	k(  rt        |      }n(|j
                  d
k(  rt        |      }nt        ddd      }t	        |      S # t        $ r8}t        j                  dd       t        ddd      }Y d}~t	        |      S d}~ww xY w)zB
    Test the connection with the specified biometric device.
    r   Device not found.  status r   r  r   r  r  rJ  'Please select a valid biometric device.rG  zError in biometric_device_testTrH  zAn unexpected error occurred.N)r3   r   r   r   r   r  rO  r_  re  rm  r  rB  r   r   r   )r   r{   r   r$  r   s        r>   biometric_device_testr  C  s    %%,,	,:@@BF/<<F
$&+F3F  G+*62F  G+*62F  G+*62F  M108F/)9F   
5E+%'F	
 
s   BC   	D!)$DD!c                 `   d}| j                   j                  d      }t        j                  j	                  |d      }|sIt        j                  | t        d             t        t        d      t        d      d      }t        |      S t        |      \  }}t        |t              r-t        t        d      t        d	|       d
      }t        |      S d|v r*t        t        d      t        d      d      }t        |      S t        t        d      t        d|       d      }t        |      S )Nr  selected_device_idsr   )id__inr  zBiometric device not supported.zBulk log fetching is currently available only for ZKTeco / eSSL devices. Support for other biometric systems will be added soon.rG  Logs Fetched Successfully?Biometric attendance logs fetched successfully. Total records: r*  AuthenticationrE  rF  Connection Unsuccessful0Please check the IP, Port, and Password. Error: )r   getlistr3   r   r   r
   r   r+  rB  r   zk_biometric_attendance_logsr   rD   )r   r$  zk_ids
zk_devicesattendance_counterror_messages         r>    biometric_device_bulk_fetch_logsr  l  s@   
 F[[  !67F!))00T0RJw"&+/0 S 
 F##&B:&N#m"C(+)*QRbQcd 
&  
]	*+$%AB
  ,'(@PQ

 rI   c                    t        j                  |      }|st        dd      S d}|j                  dk(  rt	        |      \  }}t        |t              r-t        t        d      t        d|       d      }t        |      S d	|v r*t        t        d
      t        d      d      }t        |      S t        t        d      t        d|       d      }t        |      S |j                  dk(  rrt        |      }t        |t              r-t        t        d      t        d|       d      }t        |      S t        t        d      t        d      d      }t        |      S |j                  dk(  rrt        |      }t        |t              r-t        t        d      t        d|       d      }t        |      S t        t        d      t        d      d      }t        |      S |j                  dk(  rrt        |      }t        |t              r-t        t        d      t        d|       d      }t        |      S t        t        d      t        d      d      }t        |      S |j                  dk(  rrt        |      }t        |t              r-t        t        d      t        d|       d      }t        |      S t        t        d      t        d      d      }t        |      S t        ddd      }t        |      S )z
    Fetches biometric attendance logs from a specified device.

    This view function connects to a biometric device based on the device type (zk, anviz, cosec, dahua, or etimeoffice)
    and retrieves the attendance logs.
    r  r  r  r  r   r  r  r*  r  rE  rF  rG  r  r  r  rJ  rX  r   rb  r  r  z9Double-check the provided API Url, Username, and Passwordr  )r3   r4  r   r  r  r   rD   rB  r+  anviz_biometric_attendance_logscosec_biometric_attendance_logsdahua_biometric_attendance_logs%etimeoffice_biometric_attendance_logs)r   r{   r   r$  r  r  s         r>   biometric_device_fetch_logsr    s\    ""9-F/<<Fd"*Fv*N'-&,/-.UVfUgh Fv i ./()EFFf [ 0+,D]OTUFZ Q 
			':6B&,/-.UVfUgh FJ { 0+,78Fz o 
			':6B&,/-.UVfUgh Fh Y 0+,QRFX O 
			':6B&,/-.UVfUgh FH 9 0+,QRF8 / 
			-@H&,/-.UVfUgh F(  0+,MNF  ,%5
 rI   c                    t        | j                  | j                  dt        | j                        dd      }|j                         }|j                          |j                         }|j                         }t        j                  j                  |       }|D ci c]  }|j                  | }}g }|D ]  }	|	j                  }
|	j                  }|j                  |
      }|s0|j                  }t         j                  j                  |      j#                         }|r||j$                  r|j$                  nd}|j&                  r|j&                  nd}|j(                  r|j(                  nd}||	j*                  d<   ||	j*                  d<   ||	j*                  d	<   n-d|	j*                  d<   d|	j*                  d<   d|	j*                  d	<   ||	j*                  d
<   |j,                  |	j*                  d<   g }|D ]-  }|j                  |k(  s|j/                  |j0                         / |sg }||	j*                  d<   |j/                  |	        |S c c}w )a  
    Fetch employee data from the specified ZK biometric device.

    Parameters:
    - device: Biometric device object containing machine IP, port, etc.

    Returns:
    - list: A list of dictionaries, where each dictionary represents an employee
            with associated data including user ID, employee ID, work email,
            phone number, job position, badge ID, and fingerprint data.
    r%   Frt   r   )r   N
work_emailphonejob_positionr   badge_idfinger)r   rl   ru   rD   r  r   enable_device	get_usersget_templatesr4   r   r   rz   uidr   r   r   r   emailmobilejob_position_id__dict__r  appendfid)r   r   rd   rQ   fingersbio_employeesbio
bio_lookup	employeesr   rz   r  r   r   employee_work_infor  r  r  finger_printr  s                       r>   zk_employees_fetchr    sA    [[V''(I DNNE  "G&..555GM.;<s#++s"<J<I $#,,hh())H!8!@!@!G!G$ "H "eg  "0B0H0H&,,d  6H5N5N*11TX *99 '66 
 /9l+).g&0<n-.2l+)-g&04n-(0DMM*%(0(9(9DMM*%L! 4::$ ''

34  !&2DMM(#T"I$#J Q =s   Ic                 &   g }t         j                  j                  |       }t        j                  j	                  |      }t        |j                  |j                  |j                  |j                        }|D ]  }|j                  |j                        }|j                  d      r|j                  |d<   |j                  |j                        }|j                  d      |d<   |j                  d      |d<   |j                  d      |d<   i }|j                         D ]  \  }	}
|	j                  d	d
      }|
||<    |j!                  |       t        j                  j                  |j"                        j%                           |S )ab  
    Fetch employee data from the COSEC biometric device associated with the specified device ID.

    Parameters:
    - device_id: ID of the biometric device.

    Returns:
    - list: A list of dictionaries, where each dictionary represents an employee with associated
            data including user ID, employee ID, finger count, and card count.
    r   r   rz   user-idr   zfinger-countz
face-countz
card-count-r+  )r3   r   r   r4   r   r(   rl   ru   r   r   get_cosec_userrz   r   get_user_credential_countitemsreplacer  r   r<  )r{   rQ   r   r  r   r   r   user_credentialnew_dictkeyvaluenew_keys               r>   cosec_employee_fetchr  M  su    E%%))Y)7F"**11F1CI6;;(;(;V=P=PE  D##H,<,<#=88I"*"6"6D#==hFVFV=WO#2#6#6~#FD !0!4!4\!BD!0!4!4\!BDH"jjl *
U++c3/$)!* LL"&&**hkk*:AACD LrI   c           	         t         j                  j                  |       }t        j                  j	                  d      j                  dd      }t        |j                  |j                  |j                  |j                        }t        j                  j	                  |      j                  dd      }g }|D ]  \  }}|s	|j                         st        |      d	k  s)|j                  |
      }|j                  d      sM|j                  d      |vsa|j                  t        |j                  d      |j                  d      ||               t        j                  j!                  |       y)~  
    Synchronize active employees with a COSEC biometric device.

    This function retrieves a list of active employees from the database,
    checks their presence on a specified COSEC biometric device, and updates
    the database with employees who are registered on the COSEC device.

    Args:
        device_id (uuid): The ID of the biometric device to synchronize with.
    r   Tr   r   r  r   rz   flat   r  r  ref-user-id)r   rz   employee_id_iddevice_id_idN)r3   r   r   r   r   values_listr(   rl   ru   r   r   r4   isalnumrX   r  r  bulk_create)	r{   r   r  r   existing_user_idsbiometric_employees_to_creater   r  r   s	            r>   rd  rd  p  sN    %%))Y)7F  ''$'7CCD*UI6;;(;(;V=P=PE +2299F9KWW X  %'!!* X((*s8}/B'''9Dxx	"txx	':BS'S-44&$(HH]$; $ 3'2%.		 **+HIrI   c           
         t         j                  j                  |       }t        j                  j	                  d      j                  dd      }t        t        j                  j	                  |       j                  dd            }t        |j                  |j                  t        |j                        d	
      }|j                         }|j                         D ci c]  }|j                  |j                    }}|D 	cg c]!  \  }}	|	r|	|v r|	|vrt        ||	   |	||       # }
}}	t        j                  j#                  |
       |j%                          yc c}w c c}	}w )r  r   Tr   r   r  r   rz   r  rs   )ru   rn   rv   )r  rz   r  r  N)r3   r   r   r   r   r  r   r4   r   rl   ru   rD   r  r   r  rz   r  r  rL  )r{   r   r  r  r   rd   r   zk_usersr   r  r  s              r>   rK  rK    sW    %%))Y)7F  ''$'7CCD*UI""))I)>JJD 	K 	

 c&:L:L6MWXI D37>>3CD4dhh&DHD &/	% "KH,AR1R 	"&"		
	%! 	% **+HIOO E	%s   #E!&E&z!biometric.view_biometricemployeesc           	      ^   | j                   j                         }t        j                  |      }|ru	 |j                  dk(  rNt               }t        |      }t        || j                   j                  d            }||||d}t        | d|      S |j                  dk(  rbt               }t        |      }t        |t        | j                   j                  dd                  }||j                  ||d}t        | d|      S |j                  dk(  r<t        j                   j#                  |	      }|j                  |d
}t        | d|      S |j                  dk(  r<t        j                   j#                  |	      }|j                  |d
}t        | d|      S 	 t1        t2              S t+        j(                  | t/        d             t1        t2              S # t$        $ rM}t&        j)                  d|       t+        j,                  | t/        d             Y d}~t1        t2              S d}~ww xY w)aA  
    View function to display employees associated with a biometric device.

    Depending on the machine type of the biometric device (either "zk" or "cosec"),
    this function fetches the relevant employees and renders the appropriate template.

    Args:
        request (HttpRequest): The HTTP request object.
        device_id (uuid): The ID of the biometric device.
        **kwargs: Additional keyword arguments.

    Returns:
        HttpResponse: The rendered template response or a redirect to `biometric_devices_view`
                      in case of an error.
    r   r   )r  r{   formr   z'biometric/view_employees_biometric.htmlr   r%   z#biometric/view_cosec_employees.htmlr  r   r{   r  z/biometric_users/dahua/view_dahua_employees.htmlr  z;biometric_users/etimeoffice/view_etimeoffice_employees.htmlAn error occurred: zFailed to establish a connection. Please verify the accuracy of the IP                    Address , Port No. and Password of the device.NBiometric device not found)r   r   r3   r4  r  r1   r  rO   r   r   r.   r  r`   rD   r   r4   r   r   r   r   r   r
   infor+  r   r   )	r   r{   kwargsr   r   employee_add_formr  r!  r   s	            r>   biometric_device_employeesr    s+   & KK))+M""9-F7	""d*$<$>!.v6	))W[[__V5LM	!*!*-'	 F  ""g-$4$6!0;	3s7;;??61#=>	 "+!'-'	 g'LgVV""g-.66==	=R	!'!* NPW  ""m3.66==	=R	!'!* Q  4, *++ 	w"> ?@*++  	LL.6MMD  *++	s-   AG A0G A
G A
G 	H,5H''H,c           	         | j                   j                         }| j                   j                  d      }t        j                  j                  |      }| j                   j                  d      }|j
                  dk(  rt        |      }|rQt        j                  j                  |      }|j                  dd      }|D cg c]  }|j                  |v s| }}t        || j                   j                  d	            }d
}	|||d}
n9|j
                  dk(  s|j
                  dk(  rft        j                  j                  |      }|r!t        j                  j                  ||      }|j
                  dk(  rdnd}	|j                  |d}
nt        |      }|r"t        j                  j                  ||      }n t        j                  j                  |      }|D cg c]  }|j                   }}|D cg c]  }|d   |v r| }}t        |t!        | j                   j                  d	d                  }d}	|||d}
t#        | |	|
      S c c}w c c}w c c}w )a  
    View function to search for employees associated with a specific biometric device.

    This function handles searching employees based on their first name and the type of
    biometric device (either "zk" or "cosec"). It then renders the appropriate template
    with the filtered employee list.

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

    Returns:
        HttpResponse: The rendered template response with the context.
    r   r   searchr   )+employee_id__employee_first_name__icontainsr  Tr  r   z'biometric/list_employees_biometric.html)r  r{   r   r  r  r   )r  r{   z/biometric_users/dahua/list_dahua_employees.htmlz;biometric_users/etimeoffice/list_etimeoffice_employees.htmlr  rz   r%   z-biometric/list_employees_cosec_biometric.html)r   r   r   r3   r   r  r  r4   r   r  r  rO   r   r  rz   r`   rD   r   )r   r   r{   r   r  r  search_employeessearch_uidsr   templater!  queryset_user_idsfiltered_employeess                r>   search_employee_devicer    s|   $ KK))+M)I%%))Y)7F[[__X&Fd"&v.	199@@<B  A   +66u46HK)2%hllk6QI  ")W[[__V-DE	<""

 
			'6+>+>-+O-55<<v<N199@@<Bf  A  
 ""g- >N 	  )
 )3	199@@<Bf  A    299@@6@R>NO(X--OO &
	"&77 
 

 5GKKOOFA$> ?
 C+"

 '8W--cD P
s   II$I=Iz#biometric.delete_biometricemployeesc                    t         j                  j                  |      }t        |j                  |j
                  dt        |j                        dd      }|j                         }|j                  |       t        j                  j                  |      j                         }|j                          t        j                  | t!        d      j#                  |j$                               d| d}t'        |      S )	a-  
    This function connects to the specified biometric device, deletes the user
    identified by the given UID, and removes the corresponding entry from the
    BiometricEmployees table in the local database

    Args:
        request (HttpRequest): The HTTP request object.
        uid (str): The UID of the user to be deleted from the biometric device.
        device_id (uuid): The ID of the biometric device.

    Returns:
        HttpResponse: A redirect response to the list of employees for the specified
                      biometric device.
    r   rs   Frt   )r  2{} successfully removed from the biometric device.&/biometric/biometric-device-employees/rq  )r3   r   r   r   rl   ru   rD   r  r   delete_userr4   r   r   r<  r
   r*  r+  r  r   r   )r   r  r{   r   r   rd   employee_bioredirect_urls           r>   delete_biometric_userr  \  s    $ %%))Y)7F[[V''(I D%--444=CCEL	
>?FF$$	
 <I;aHLL!!rI   z#biometric.change_biometricemployeesc                    t        j                  |      }|rt        |j                  |j                  |j
                  |j                        }|j                  |d      }|j                  d      }|dk(  r t        j                  | t        d             n?t        j                  | t        d             nt        j                  | t        d             t        d| d	      S )
a  
    View function to enable face recognition for a user on a COSEC biometric device

    Args:
        request (HttpRequest): The HTTP request object.
        user_id (str): The ID of the user for whom face recognition is to be enabled.
        device_id (uuid): The ID of the COSEC biometric device.

    Returns:
        HttpResponse: A redirect response to the list of employees for the specified
                      biometric device.
    T)rz   	enable_frResponse-Coder   z%Face recognition enabled successfullyz'Something went wrong when enabling facezDevice not foundr  rq  )r3   r4  r(   rl   ru   r   r   enable_user_face_recognitionr   r
   r*  r+  r   r   )r   rz   r{   r   r   r  response_codes          r>   enable_cosec_face_recognitionr    s      ""9-FKK	
 66wRV6W	!o6CWa(O&PQNN7A&O$PQw"4 56<YKqIJJrI   c                    t         j                  j                  |      }t        |j                  |j
                  |j                  |j                        }|j                  |      }|j                  d      rt        |d         }t        |d         }t        |d         }t        |||      }	|	j                  d      }
|d   t        t        |d               t        t        |d               t        t        |d	               |
d
}d|v rt        t        |d               |d<   t        |      }| j                  dk(  rt        | j                        }|j!                         r|j"                  d   }|j"                  d   }|j"                  d   }|j"                  d   }t%        |j"                  d         }t        j&                  |d      j)                         }|j*                  }|j,                  }|j.                  }|j"                  d   }|j1                  |d   |d   |t        |      t        |      t        |      t        |      |||
      }|j                  d      r>|j                  d      dk(  r*t3        j4                  | t7        d             t9        d      S |j                  d      r0|j                  d      }d|v r|j;                  dt7        d             t=        | d|||d      S y)a  
    View function to edit the details of a COSEC biometric user.

    Args:
        request (HttpRequest): The HTTP request object.
        user_id (str): The ID of the user to be edited.
        device_id (uuid): The ID of the COSEC biometric device.

    Returns:
        HttpResponse: The rendered form template for GET requests, and a response with
                      a success message for valid POST requests. Reloads the page after
                      successful update.

    r   namezvalidity-date-yyyyzvalidity-date-mmzvalidity-date-ddz%Y-%m-%dzuser-activevipzvalidity-enable)r  user_activer  validity_enablevalidity_end_datezby-pass-fingerby_pass_fingerr   r   r  r  r  r  r  )
rz   r   r  r  r  r  r  validity_date_ddvalidity_date_mmvalidity_date_yyyyr  r   z(Biometric user data updated successfullyr
  r   NziThis date cannot be used as the Validity End Date for                                the COSEC Biometric.zbiometric/edit_cosec_user.html)r  rz   r{   r!  )r3   r   r   r(   rl   ru   r   r   r  rD   r   strftimeboolr/   r  r   r  cleaned_datar  r   r   r}  r|  r{  set_cosec_userr
   r*  r+  r   	add_errorr   )r   rz   r{   r   r   r   r}  r|  r{  date_objectformatted_dater   r  r  r  r  r  validity_end_date_strr  validity_yearvalidity_monthvalidity_dayr  update_userr   s                            r>   edit_cosec_userr    s   & %%))Y)7F	E (Dxx4,-.D+,-$)*+tUC0$--j9LD$7 89DK()#C->(?$@A!/
 t#-1#d;K6L2M-NL)*\2>>V# .D}}((0"//>''."&"3"34E"F(+D,=,=>Q,R(S%$,$5$5):%$& " !2 6 6!2!8!8044!%!2!23C!D#22 O $] 3 #K 0C#&~#6$'$8%1%3'4 3   OOO4#8C?$$#M!N ((STT??7+'OOG4E+u4 !6 ,!gIN
 	
A rI   c                    t        j                  |      }|rt        j                  j	                  ||      j                         }t        |j                  |j                  |j                  |j                        }|j                  |      }|j                  d      r]|j                  d      dk(  rI|j                          t        j                  | t!        d      j#                  |j$                               n?t        j&                  | t!        d             nt        j&                  | t!        d             |rd| dnd	}t)        |      S )
a  
    View function to delete a user from a COSEC biometric device and database.

    Args:
        request (HttpRequest): The HTTP request object.
        user_id (str): The ID of the user to be deleted from the COSEC biometric device.
        device_id (uuid): The ID of the COSEC biometric device.

    Returns:
        HttpResponse: A redirect response to the list of employees for the specified
                      biometric device.
    ry   r  r   r  zBiometric user not foundr  r  rq  z"/biometric/view-biometric-devices/)r3   r4  r4   r   r   r   r(   rl   ru   r   r   delete_cosec_userr   r<  r
   r*  r+  r  r   r   r   )r   rz   r{   r   r  r   r&  r  s           r>   delete_horilla_cosec_userr  
  s)     ""9-F)1188v 9 

%' 	 KK	
 **73<<(X\\/-Jc-Q!FGNN ,, NN7A&@$ABw"> ?@  11=1 
 L!!rI   c                    d}| j                   d   }| j                   d   }t        j                  |      }t        j                  j                  |      }	 t        |j                  |j                  dt        |j                        dd      }|j                         }|D ]  }t        |      }|j                  |       t        j                  j                  |      j                         }|j!                          |j#                          t%        j&                  | t)        d	      j+                  |j,                                	 t5        ddi      S # t.        $ r }	t0        j3                  d
|	       Y d}	~	1d}	~	ww xY w)a  
    View function to delete multiple users from a ZK biometric device and the local database.

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

    Returns:
        JsonResponse: A JSON response indicating the success of the bulk delete operation.

    NidsdeviceIdr   rs   Frt   r  r  r  r
   rw  )r   jsonloadsr3   r   r   r   rl   ru   rD   r  r   r  r4   r   r   r<  refresh_datar
   r*  r+  r  r   r   r   r   r   )
r   rd   json_idsr{   r  r   r   rz   r  r   s
             r>   bio_users_bulk_deleter  :  sY    D||E"HZ(I
**X
C%%))Y)7F3++,
	   " 	G'lGW--55<<W<MSSUL!FGNN ,,	 Y/00  3*E223s   C2E 	F  E;;F c                    | j                   d   }| j                   d   }t        j                  |      }t        j                  j                  |      }	 t        |j                  |j                  |j                  |j                        }|D ]  }|j                  |       t        j                  j                  ||      j                         }|r|j                          t!        j"                  | |j$                   dt'        d      z           	 t/        d
di      S # t(        $ r }t*        j-                  d|       Y d	}~1d	}~ww xY w)a  
    View function to delete multiple users from a COSEC biometric device and database.

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

    Returns:
        JsonResponse: A JSON response indicating the success of the bulk delete operation.
    r  r  r   r  ry    z/successfully removed from the biometric device.r  Nr
   rw  )r   r  r  r3   r   r   r(   rl   ru   r   r   r  r4   r   r   r<  r
   r*  r   r+  r   r   r   r   )	r   r  r{   r  r   r   rz   r  r   s	            r>   cosec_users_bulk_deleter  h  s<    ||E"HZ(I
**X
C%%))Y)7F3KK	
  	G##G#4-55<<6 = eg  ##%++,A.EFG	 Y/00  3*E223s   B?D" "	E+EEz biometric.add_biometricemployeesc                 ,	   t         j                  j                  |      }|j                  dk(  r
t	               n	t               }| j                  dk(  rt         j                  j                  |      }	 |j                  dk(  r$t        |j                  |j                  dt        |j                        dd      }|j                         }|j                          |j                         D cg c]  }|j                   }}|j                         D cg c]  }|j                    }}d}	d}
| j"                  j%                  d	      }|D ]W  }t&        j                  j                  |      }t(        j                  j+                  ||
      j-                         }||	|v s|
|v rt        |
      }
|	dz  }	|
dz  }
|	|v r|
|v r|j/                  |	       |j/                  |
       |j1                         }|j3                  |	|ddt5        |
      d       t(        j                  j7                  |	t5        |
      ||       t9        j:                  | t=        d      j?                  |             *t9        j@                  | t=        d      j?                  |             Z nitC        |j                  |j                  |jD                  |jF                        }|jI                         }|j                  d      r| j"                  j%                  d	      }t(        j                  j+                  |      }tK        |jM                  dd            }|D ]  }t&        j                  j                  |      }|j1                         }|jN                  }
d}||v r
|dz  }||v r
|j/                  |       |jQ                  |
||ddddd      }|j                  d      }|s|dk(  st(        j                  j7                  ||
||        t[        d      S t]        | d||d !      S c c}w c c}w # tR        $ rR}|j                  dk(  r/jU                          tV        jY                  dt5        |             Y d}~t[        d      S d}~ww xY w)"a  
    View function to add a new user to a biometric device.

    This function adds a new user to the specified biometric device and stores their
    information in the database.

    Args:
        request (HttpRequest): The HTTP request object.
        device_id (uuid): The ID of the biometric device.

    Returns:
        HttpResponse: A JavaScript script to reload the current page after adding the user.

    r   r   r   rs   Frt   r%   i  employee_ids)r   r{   Nr  r   )r  r  rn   group_idrz   card)r  rz   r   r{   ){} added to biometric device successfullyz${} already added to biometric devicera  r   r   Tr  d   i  )rz   r   r  r  r  r  r  r   r  r   )r   rz   r   r{   r  r
  z!biometric/add_biometric_user.htmlr  r{   r  )/r3   r   r   r  r1   r.   r  r   rl   ru   rD   r  r   r  r  r  rz   r   r  r   r4   r   r   r  get_full_nameset_userr  creater
   r*  r+  r  r  r(   r   r   rc  r   r  r  r  r   disable_devicer   r   r   r   )r   r{   r   r  r   rd   r   existing_uidsr  r  rz   r  obj_idr   existing_biometric_employeeemployee_namer   basiccosec_usersexisting_ref_user_idsr   r&  r   s                          r>   add_biometric_userr/    s   & %%))Y)7F $& 	!" 
 ~~!))---;d	@""d*%% !3!34#$	 !((*""$6:nn6F Gd G G>Bnn>N$OdT\\$O!$O&||33NC* 'F'//33v3>H2D2L2L2S2S$, 3T 3eg 0 3:!]2gAR6R&)'lG1HC#qLG "]2gAR6R &,,S1)009(0(>(>(@ #!.%'%'$'L!" &  +2299 #$'L(0&,	 :  !((#IJQQ ( !#DELLXVI'R '%%KK''''	 **,99U##*<<#7#7#GL"4"<"<"C"Ci"C"XK,0#//D/I-) #/ #+#3#3#7#76#7#B(0(>(>(@"*"3"3&))-BB'1,K *-BB-44[A$33$+(3!.(,,0-.-./3  4 	  $(88O#<#C.66==,7(/,4*0	 > )< GHH+*C u !H$Oh  	@""d*##%2CJ?GHH		@sW   5A:P8 /P.P8 P3)BP8 P8 G P8 )=P8 'P8 -%P8 .
P8 8	R>RRc                 N   t        j                  |      }t        | j                  xs d      }d}|j                  dk(  rd}t        d      |j                  d   _        | j                  dk(  r|j                         r|j                  d   }|j                  d   }|ry|rwt        j                  j                  |||	       t        j                  | t        d
             t               }|j                  dk(  rt        d      |j                  d   _        t!        | |||d      S )zW
    Maps an horilla employee to a biometric user on a specified biometric device.
    Nz*biometric_users/dahua/map_dahua_users.htmlr  z6biometric_users/etimeoffice/map_etimeoffice_users.htmlzEmp Coderz   r   r   )rz   r   r{   z;Selected employee successfully mapped to the biometric userr#  )r3   r4  r2   r   r  r+  fieldslabelr  r  r  r4   r   r&  r
   r*  r   )r   r{   r   r  r  rz   r   s          r>   map_biometric_usersr3    s    ""9-Fw||+t,D;Hm+K'(}I$~~DMMO##I.$$]3h&&--X .  OP =D""m3/0}I&,I. rI   c           	         t        j                  |      }t               }| j                  dk(  rt        | j                        }|j                         r| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	| j                  j                  d      }
	 |r t        j                  j                  |	      nd
}t        |j                  |j                   |j"                        }|j%                  |r|j'                         nd|||||	|
      }|j                  d      dk(  rnt(        j                  j+                  ||||       t        j,                  | t        d      j/                  |r|j'                         nd             t               }nt        j                  | t        d             ||d}t        | d|      S # t        j                  $ r/ t        j                  | t        d             t        | d      cY S w xY w)z
    Adds a new employee to a Dahua biometric device.

    This view handles the process of adding an employee as a user to a Dahua biometric device.
    r   r   card_norz   card_status	card_typern   valid_date_endr   NzEmployee not found.z)biometric_users/dahua/add_dahua_user.htmlrg  r  )	card_namer5  rz   r6  r7  rn   r8  rj  rk  )dahua_card_norz   r   r{   r!  z'Failed to add user to biometric device.r#  )r3   r4  r0   r  r   r  r   r   r   DoesNotExistr
   r   r+  r   r)   rl   r   r   enroll_new_userr$  r4   r&  r*  r  )r   r{   r   r  r   r5  rz   r6  r7  rn   r8  r   r!  r  r&  s                  r>   add_dahua_biometric_userr=  A  s5    ""9-F?D~~W\\*==?!,,**:6Kll&&y1Gll&&y1G!,,**=9K((5I||''
3H$\\--.>?NCN8++//;/?TX $$,,,,E ,,6>(002B'#!- - H ||M*c1"**11")# ($	 2    ABII4<..0" %w*S(TU)4G'FPPO (( w*?(@AH' s   $I ?JJc                     | j                   j                  d      }|r*t        j                  j                  |      j                  nd}d| d}t        |      S )zK
    Retrieves the badge ID of an employee based on their employee ID.
    r   r   r  z
    <input type="text" name="user_id" maxlength="50" class="oh-input w-100"
           placeholder="User ID" required="" id="id_user_id" value="z">
    )r   r   r   r   r  r   )r   r   rz   input_fields       r>   find_employee_badge_idr@    s_     ++//*-K?Jh""k"2;;PRGEELI NK $$rI   c           	         d}	 | j                   dk(  rl|rjt        j                  j                  |      }|j	                          t        j                  | t        d      j                  |j                               d}| j                   dk(  r| j                  j                  d      }| j                  j                  d      }|rd	| d
}|rt        j                  j                  |      }|rP|j                         }|j	                          t        j                  | t        d      j                  |            f nt        j                  | t        d             t%        |      S # t        $ rK}t        j                   | t        d      j                  t#        |                   Y d}~t%        |      S d}~ww xY w)zI
    Deletes a Dahua biometric user or multiple users from a device.
    z*<script>window.location.reload();</script>r   r   {} successfully deleted!!<script>reloadMessage();</script>DELETEr  r{   Q
                            <span hx-get="/biometric/biometric-device-employees/a  /"
                                hx-target="#dahuUsersList" hx-select="#dahuUsersList" hx-trigger="load delay:200ms"
                                hx-swap="outerHTML" hx-on-htmx-before-request="reloadMessage();">
                            </span>
                        user_id__in{} users successfully deleted!4No rows are selected for deleting users from device.zAn error occurred: {}N)r  r4   r   r   r<  r
   r*  r+  r  r   r   r  r   countrG  r   r   r  r   )	r   r)  r$  r   user_idsr{   rQ   rJ  r  s	            r>   delete_dahua_userrL    s    :F K>>V#%--11V1<DKKM56==d>N>NO 9F>>X%{{**51H4IQQZP[ \ *2299h9O!KKMELLN$$#C!D!K!KE!R  $$PQ   Kw"9 : A A#a& IJJKs   EE* *	F>37F99F>c                    d}| j                   dk(  rt        j                  j                  |      }|j                  j
                  }|j                          t        j                  | t        d      j                  |j                               d}| j                   dk(  r| j                  j                  d      }| j                  j                  d      }|rd	| d
}|rt        j                  j                  |      }|rZ|j                         }|j                          t        j                  | t        d      j                  |            f t#        |      S t        j                   | t        d             t#        |      S )zQ
    Deletes a user or multiple users from the eTimeOffice biometric system.
    z,<script>window.location.href = '/';</script>r   r   rB  rC  rD  r  r{   rE  a#  /"
                                hx-target="#eTimeOfficeUsersList" hx-select="#eTimeOfficeUsersList" hx-trigger="load delay:200ms"
                                hx-swap="outerHTML" hx-on-htmx-before-request="reloadMessage();">
                            </span>
                        rF  rH  rI  )r  r4   r   r   r{   r   r<  r
   r*  r+  r  r   r   r  r   rJ  rG  r   )r   r)  r$  r   r{   rK  rQ   rJ  s           r>   delete_etimeoffice_userrN    sY    <F~~!))---8NN%%	Q1299$:J:JK	
 5~~!;;&&u-KKOOK0	QQZP[ \F &..55(5KE  Q?@GGN     LM
 rI   c                    | j                   j                  d      }| j                   j                  d      }t        j                  j                  |      }|dk(  }|r|j                  }|j
                  }t        |j                        }d}	 |j                  dk(  rzt        ||dt        |      dd	      }|j                         }t        |||      }	|j                  d
       |rd|_        d|_        |j                          |	j!                          n|j                  dk(  rt#        |j
                  |j                  |j$                  |j&                  d      }
|
j)                         }|j                  d      rWd|_        d|_        |j                          t+        |j,                        }|j!                          |t.        |j,                  <   nt0        	 d}|r|j7                          t;        |      S d|_        |j                          |j                  dk(  rBt/        j                  |j,                        }|r!|j9                          t.        |j,                  = d}t;        |      S # t0        $ r9}d|_        |j                          t2        j5                  d|       d}Y d}~d}~ww xY w# |r|j7                          w w xY w)z
    Activate or deactivate live capture mode for a biometric device based on the request parameters.

    :param request: The Django request object.
    :return: A JsonResponse containing a script to be executed on the client side.
    r   r  r   onNr   rs   Frt      r  Tr   r   r   ra  a  <script>
                    Swal.fire({
                      text: "The live capture mode has been activated successfully.",
                      icon: "success",
                      showConfirmButton: false,
                      timer: 1500,
                      timerProgressBar: true, // Show a progress bar as the timer counts down
                      didClose: () => {
                        },
                    });
                    </script>
                z'An error comes in biometric_device_livea  
           <script>
                Swal.fire({
                  title : "Connection unsuccessful",
                  text: "Please double-check the accuracy of the provided IP Address and Port Number for correctness",
                  icon: "warning",
                  showConfirmButton: false,
                  timer: 3000,
                  timerProgressBar: true,
                  didClose: () => {
                    location.reload();
                    },
                });
            </script>
            a  
           <script>
                Swal.fire({
                  text: "The live capture mode has been deactivated successfully.",
                  icon: "warning",
                  showConfirmButton: false,
                  timer: 3000,
                  timerProgressBar: true,
                  didClose: () => {
                    location.reload();
                    },
                });
            </script>
            )r   r   r3   r   ru   rl   rD   r  r  r   r   rh   r  r   r  r   r   r(   r   r   rc  r   r   r#   TimeoutErrorr   r   rL  r   r   )r   r   r{   r   rm   rl   rn   rd   r   r3  r   r&  threadr$  r   r%  s                   r>   biometric_device_liverT    sp    kkooi(G
+I%%))Y)7FoG++&&
v))*H	"""d*  ]#$	 !((**:wIb)%)FN*/F'KKMNN$$$/&%%KK'''' !--/<<&%)FN*/F'KKM5fii@FLLN4:&vyy1&&F@ !0 - ')044VYY?O$$&&vyy1 [  	"FNKKMLLBEJF		( ! s+   EI2 2	J4;/J/*J7 /J44J7 7Kc           
         t        | d      rt        | t              st        |       }n| g}g }g }ddd}t        j
                  j                  |      D ci c]  }|j                  |j                  f| }}|D ]  }|j                  }|j                  }	d}
t        |	|dt        |j                        dd	      }	 |j                         }
|
j                          |
j!                         }|s	 |
sy|
j#                          |d
   j$                  }|j&                  r|j(                  r|D cg c]y  }|j$                  j+                         |j&                  kD  sN|j$                  j+                         |j&                  k(  r)|j$                  j-                         |j(                  kD  r|{ }}n|}|j+                         |_        |j-                         |_        |j/                          |D ]H  }||_        |j2                  |v r||j2                     n|j4                  |_        |j7                  |       J 	 |
s|
j#                           |jG                  d        |D ]  }|j                  }|j4                  }tI        jJ                  |j$                        }|j+                         }|j-                         }|j0                  jL                  }|jO                  ||f      }|stQ        |jR                  jT                  |||      }	 |dv rtW        |       n|dv rtY        |        t[        |      |rdj]                  |      fS dfS c c}w c c}w # t8        j:                  $ r5}|j7                  d|j<                   dt?        |              Y d}~ed}~wt@        $ rZ}tB        jE                  d|j<                   dd       |j7                  d|j<                   dt?        |              Y d}~d}~ww xY w# |
r|
j#                          w w xY w# t@        $ r3 tB        jE                  d|j0                  j<                   dd       Y w xY w)ah  
    Retrieve and process attendance logs from one or more ZKTeco biometric devices.

    Handles scenarios where the same user_id may exist across multiple devices for the same employee.

    :param device_or_devices: A single BiometricDevice instance or a queryset/list of them.
    :return: Tuple (number_of_attendance_processed, error_message or None)
    __iter__r   r%   )inout)device_id__inNrs   Frt   r   [z] ZKError: z] General ErrorTrH  z	] Error: c                     | j                   S r9   )r   )r<   s    r>   r  z.zk_biometric_attendance_logs.<locals>.<lambda>  s
    AKK rI   )r  r   r|   >   r%   r   rs   z	[Device: z] Punch processing errorz; )/hasattrr   dictr   r4   r   r   r  rz   ru   rl   r   rD   r  r   r  get_attendancerL  r   r   r   r   rF   r   r   device_directionr   r  rM  rN  r  r  r   r   r   sortr   r   r   r   r   r   r   r   r   rX   join)device_or_devicesr   errorscombined_attendancespatch_directionr  
bio_id_mapr   rm   rl   rd   r   r   last_attendance_datetimeattfilteredr   r  rz   r   r   r   rF   r{   r   r   s                             r>   r  r  c  s2     *-j47 ()$%Fq)O &--4474K 
		3;;',J 
  7"++&&
++,
	*	"$$&D --/KH !G (32'@'@$%%&*@*@  +**,v/E/EE**,0F0FFMM..063I3II   ' &>%B%B%DF"%=%B%B%DF"KKM& 8
$*
! ../A $F$;$;<#))  
 %++J78 !o7"t "78* 
$$%%
#..z/C/CD	~~~~%%((	G 45"''88"	L	*\*9,l+'4 #$6dii&7KKtKKw82 ++ 	@MMAfkk]+c!fX>?? 	>LL1V[[M9DLIMMAfkk])CF8<==	> ! 4  
 1 1 6 677OP!  su    M>2M+M0A>M.BM:P	P!MP0*N P	 P,AP;P	PP		P!8QQc                 d    t        j                  |       }|r|j                  rt        |       yyy)z5
    Scheduler function used for attendance logs
    N)r3   r4  r  r  r{   r   s     r>   r  r    s.     ""9-F&%%$V, &vrI   c                 r   t        j                         }t        | j                  | j                  | j
                  | j                        }| j                  r6| j                  r*t        j                  | j                  | j                        n|j                  dddd      }|j                  || j                        }|j                         |j                         c| _        | _        | j                          |d   D ]  }|d   d   }|d   }t        j                   |d	   d
      }|j#                  t%        j&                               }	t(        j*                  j-                  |      j/                         }
|
s}t1        |
j2                  |	j                         |	j                         |	      }|dv r	 t5        |       	 t=        |        t?        |d         S # t6        $ r!}t8        j;                  d|       Y d}~d}~ww xY w# t6        $ r!}t8        j;                  d|       Y d}~1d}~ww xY w)z
    Retrieves attendance records from an Anviz biometric device and processes them.

    :param device_id: The Object Id of the Anviz biometric device.
    rQ  r   )r~  r  secondmicrosecond)
begin_timerV  r   r   workno	checktype	checktimez%Y-%m-%dT%H:%M:%S%z)r  r   >   r      zError in clock in NzError in clock out ) r   utcnowr'   rR  rS  rT  rU  r   r   r   r  r\  rZ  r   rF   r   r   
astimezoner   get_current_timezoner   r   r   r   r   r   r   r   r   r   r   rX   )r   current_utc_timeanviz_devicero  attendance_recordsr   r  r   date_time_utcdate_time_objr   r   r   s                r>   r  r    s     ($$$00	L !!f&<&< 	//1G1GH%%1Qqa%P 
 &<<V%5%5 =  	 3FF2 KKM(0 ?
j)(3,
 )){#%:
 &001U1U1WX##**H*=CCE".."'')"'')&	L X%>\*?l+/?4 !&)** ! >LL!5u==> ! ?LL!6>>?s0   7GH	H	(HH		H6H11H6c                 d    t        j                  |       }|r|j                  rt        |       yyy)zO
    Schedules the attendance log retrieval for an Anviz biometric device.
    N)r3   r4  r  r  rk  s     r>   r  r  	  .     ""9-F&%%'/ &vrI   c           	      f   t         j                  j                  |       j                         }|rt	        |j
                        nd}|rt	        |j                        nd}t        | j                  | j                  | j                  | j                  d      }|j                  |t	        |      dz         }t        |t              sy|D ]  }|d   }t        j                  j                  |      j                         }|s9|d	   }	|d
   }
t!        j"                  |	d      j%                         }t!        j"                  |
d      j'                         }t!        j(                  ||      }|d   }t+        |j,                  j.                  ||t1        j2                  |            }	 |dv rt5        |       n|dv rt7        |       n	  |r/|d   }t         j                  j?                  | |d   |d   d       tA        |      S # t8        $ r!}t:        j=                  d|       Y d}~ad}~ww xY w)zP
    Retrieves and processes attendance logs from a COSEC biometric device.
    r   r   r%   r   r   Nr   r   r   rF   r   r   r   r   r   r   r   r   r   r   r   r   )!r5   r   r   r   rD   r   r   r(   rl   ru   r   r   r   r   r   r4   r   r   r   rF   r   r   r   r   r   r   r   r   r   r   r   r   rX   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                     r>   r  r  	  s.    +2299F9KQQSK6AK112q  GRC A ABWXE --!3'<#=#AK k4(! A
 ,%--444MSSUf%f%"++HjAFFH"++HjAFFH&..P
+
%%66  $//0CD	
	A;;&99,'5A< %b/ ((99-<=N-O)8)B 	: 	
 {  	ALL8%@@	As   '!H	H0H++H0c                 d    t        j                  |       }|r|j                  rt        |       yyy)a?  
    Retrieve and process attendance events from a COSEC biometric device.

    This function fetches attendance events from the specified COSEC biometric device
    and processes them to record clock-in and clock-out events for employees.

    Args:
        device_id (uuid): The ID of the COSEC biometric device.
    N)r3   r4  r  r  rk  s     r>   r  r  `	  s.     ""9-F&%%'/ &vrI   c                 .   | j                   rC| j                  r7t        j                  | j                   | j                        t	        d      z   nCt        j                  t        j
                         t        j                  j                               }t        | j                  | j                  | j                        }|j                  |      }|j                  d      dk(  r|j                  dg       D ]%  }|j                  d      }|st        j                  j!                  || 	      j#                         }|sJ|j                  d
      }t%        j&                  t(              }|j+                  |      }t,        j                  j!                  |j.                  d      j1                  d      j3                         }	t5        |j.                  j6                  |j9                         |j                         |      }
|	rt;        |
       t=        |
       ( |j                  d      rH|d   d   }|d
   j9                         | _         |d
   j                         | _        | j?                          tA        |j                  dg             S y)a  
    Retrieves logs from a Dahua biometric device and marks attendance in Worksol.

    This function fetches biometric logs from the specified device, processes the attendance records,
    and updates the attendance system in Worksol. If an employee has an active clock-in record,
    it marks their clock-out; otherwise, it registers a new clock-in entry.

    Args:
        device_id (int): The unique identifier of the biometric device.

    Returns:
        None
    r%   r  rg  )
start_timerj  rk  r^  rz   ry   create_timeNr   r   in_datetimer   r   r   )!r   r   r   r   r   rb   minrF   r)   rl   r   r   get_control_card_recr   r4   r   r   r   pytzr   r$   ru  r   r   r   lastr   r   r   r   r   r   rX   )r   ro  r  logslogrz   r   r   user_tzlast_none_activityr   last_logs               r>   r  r  o	  s:   " !!f&<&< 	//1G1GH
A
	 hnn.0A0A0CD	  v':':VEXEXE %%%<Dxx#%88Ir* "	'Cggi(G)11886 9 eg  "%''-"8mmI.G"5"@"@"I #**11 ( 4 4" 2  -(  #))::(--/(--/,	L ",'&E"	'H 88IIr*H%-m%<%A%A%CF"%-m%<%A%A%CF"KKM488Ir*++rI   c                 d    t        j                  |       }|r|j                  rt        |       yyy)zN
    Schedules the attendance log retrieval for a Dahua biometric device.
    N)r3   r4  r  r  rk  s     r>   r  r  	  r}  rI   c                    t        j                         }t        | j                  | j                        }| j
                  rE| j                  r9t        j                  | j
                  | j                        t        d      z   dn|dd}|d}|j                  ||      }|j                  d      d	k7  ry
|j                  dg       }|syt        j                  t              }t        j                  j!                  |       D ci c]  }|j"                  | }	}t%        |      D ]  }
|
j                  d      }|r||	vr|	|   }|
d   j'                  |      }t)        |j*                  j,                  |j/                         |j1                         |      }t2        j                  j!                  |j*                  d      j5                  d      j7                         }|rt9        |       t;        |        |d   }|d   j/                         |d   j1                         c| _        | _        | j=                          t?        |      S c c}w )zW
    Retrieves and processes attendance logs from an eTimeOffice biometric device.
    ro  r%   )minutesz%d/%m/%Y_%H:%Mr   rr  rs  rv  rw  r   	PunchDatar   r   Empcode	PunchDater   Nr  r  ) r   rz  r*   r   r   r   r   r   r   r  r   r  r   r$   r4   r   r   rz   reversedru  r   r   r   r   rF   r   r   r  r   r   r   rX   )r   rz  r  rt  ru  r  
punch_datar  empemployee_mapr  rz   r   r   r   r  r  s                    r>   r  r  	  s@    ,,.C $$$$K !!f&<&< V33V5K5KLyabOcces
tH~V$ 
 ^$G**Y*PDxx)#+r*JmmI&G %7$>$>$E$EPV$E$W SL  
# #'')$'5(!+.99'B%%66$))+$))+(	
 &&--$00 .  Xm$TV 	 l#\"7#: !}H""$""$ 3FF2 KKMz?Qs   	Ic                 d    t        j                  |       }|r|j                  rt        |       yyy)zU
    Schedules the attendance log retrieval for an eTimeOffice biometric device.
    N)r3   r4  r  r  rk  s     r>   r  r  
  s.     ""9-F&%%-f5 &vrI   F)r   T)r  r  c                  4    t        t        j                        S r9   )r  r   r   r:   rI   r>   r  r  
       DVYY O rI   r  r  r   c                  4    t        t        j                        S r9   )r  r   r   r:   rI   r>   r  r   
  s     A&)) L rI   
biometric_)r	  r   r  c                  4    t        t        j                        S r9   )r  r   r   r:   rI   r>   r  r  )
  r  rI   r   c                  4    t        t        j                        S r9   )r  r   r   r:   rI   r>   r  r  2
  r  rI   r  c                  4    t        t        j                        S r9   )r  r   r   r:   rI   r>   r  r  ;
  s     J699 U rI   )   r9   )r   r  loggingr   r   	threadingr   r   urllib.parser   r   r  !apscheduler.schedulers.backgroundr	   django.contribr
   django.httpr   r   django.shortcutsr   r   django.template.loaderr   django.utilsr   r   django.utils.translationr   __r   r+  r   r   r   rM  attendance.methods.utilsr   attendance.modelsr   attendance.views.clock_in_outr   r   base.methodsr   r   employee.modelsr   r   horilla.decoratorsr   r   r    r!   horilla.filtersr"   horilla.horilla_settingsr#   horilla.settingsr$   r  r'   r   r(   r  r)   r  r*   filtersr+   formsr,   r-   r.   r/   r0   r1   r2   modelsr3   r4   r5   	getLoggerr   r   rH   rO   r`   rf   rh   r   r   r'  r,  r0  r5  r:  r=  rB  rO  r_  re  rm  r  r  r  r  r  r  rd  rK  r  r  r  r  r  r  r  r  r/  r3  r=  r@  rL  rN  rT  r  r  r  r  r  r  r  r  r  r  r   allupdater   r   r   r   r  r#  r  r   r   r:   rI   r>   <module>r     s     ( # *  A # 2 - 3 4 2 6  ( , 0 = : =  - 7 & $ !  ' *   S R			8	$F2a*f a*Hkv k\ 67- 8  -` 89JL :  JLZ 89K :   K& 56L 7   L2 89L :   L: 89K :   K  89K :   K.
N&R#
L
2
,$
N 67"  8   " J 67%  8   % P 67i  8   i XBJ F JF"J 89M, :  M,` 89J. :   J.Z :;#" <  #"L :;K <  K@ :;[
 <   [
| :;*" <  *"Z :;(1 <  (1V :;$1 <  $1N 78A 9   AH !   !H <Q   <Q~ 
%   
% &    & R :;%  <   % P 89q  :   q hoLd-4+n0?D0FR0CL63	&&**,33E3BG"**11t1D / 9 9:Q>&&'1 3 5I%%O" 01J1J K & 
 OO%((D0 3 5I%%L" 01J1J K'		{3	 &  OO%((G3 3 5I%%O" 01J1J K & 
 OO%((G3 3 5I%%O" 01J1J K & 
 OO%((M9 3 5I%%U" 01J1J K & 
 OO%_/`	s   AW( +W( F%W( (W,