
    vg6                     R    d Z ddlmZmZ ddlmZ ddlmZ ddlmZ  G d de      Z	y)	zQ
Middleware to automatically trigger employee clock-out based on shift schedules
    )datetime	timedelta)timezone)MiddlewareMixin)Requestc                       e Zd ZdZd Zd Zy)AttendanceMiddlewarez
    This middleware checks for employees who haven't clocked out by the end of their
    scheduled shift and automatically performs the clock-out action if the auto punch-out
    is enabled for their shift. It processes this during each request.
    c                 $    | j                          y)zB
        Triggers the `trigger_function` on each request.
        N)trigger_function)selfrequests     )/var/www/horilla/attendance/middleware.pyprocess_requestz$AttendanceMiddleware.process_request   s     	    c           
      @   ddl m}m} ddlm} ddlm} |j                  j                  d      }|D ]E  }|j                  j                  |j                  dd      j                  d	      }|D ]  }|j                  j                  |j                  dd|j                  |j                  |j                  
      j                         }	|	s^|j                  }
|j                  r|
t!        d      z  }
t#        j$                  t'        j(                  |
|j*                              }t#        j,                         }||k  s	  |t/        |	j                  j0                  |
|j*                  |              H y# t2        $ r}t5        |        Y d}~(d}~ww xY w)aE  
        Retrieves shift schedules with auto punch-out enabled and checks if there are
        any attendance activities that haven't been clocked out. If the scheduled
        auto punch-out time has passed, the function attempts to clock out the employee
        automatically by invoking the `clock_out` function.
        r   )
AttendanceAttendanceActivity)	clock_out)EmployeeShiftScheduleT)is_auto_punch_out_enabledN)	shift_dayclock_out_dater   z-created_at)employee_idattendance_clock_outattendance_clock_out_dateshift_idattendance_dayattendance_date   )days)userdatetimer   )attendance.modelsr   r   attendance.views.clock_in_outr   base.modelsr   objectsfilterdayorder_byr   r   r   firstis_night_shiftr   r   
make_awarer   combineauto_punch_out_timenowr   employee_user_id	Exceptionprint)r   r   r   r   r   automatic_check_out_shiftsshift_schedule
activitiesactivity
attendancer"   combined_datetimecurrent_timees                 r   r   z%AttendanceMiddleware.trigger_function   s    	E;5%:%B%B%I%I&* &J &
" 9 &	*N+33::(,,# ;  h}%	  ' *'//66 ( 4 4)-.2+44#1#5#5$,$<$< 7  %'  #33D%44	q 11(0(;(; ((~/Q/QR)% $,<<>L(<7
*% ')3)?)?)P)P)-)7)K)K->	!"-*&	*J  ) *!QCMM*s   3E==	FFFN)__name__
__module____qualname____doc__r   r    r   r   r	   r	      s     5*r   r	   N)
r?   r   r   django.utilsr   django.utils.deprecationr   attendance.methods.utilsr   r	   r@   r   r   <module>rD      s&    ) ! 4 ,B*? B*r   