
    siI                     n   d Z ddlZddlZddlmZ  ej
                  e      Zddlm	Z	mZm
Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZmZmZmZ dd
lmZmZmZmZm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z. d Z/d Z0d Z1e,e+d               Z2ddZ3d Z4d Z5e,e+d               Z6y)zc
clock_in_out.py

This module is used register endpoints to the check-in check-out functionalities
    N)render)datedatetime	timedelta)messages)Q)HttpResponse)gettext_lazy)activity_datetimeemployee_existsformat_timeovertime_calculationshift_schedule_todaystrtime_seconds)
AttendanceAttendanceActivityAttendanceGeneralSettingAttendanceLateComeEarlyOut	GraceTime)attendance_validate)#enable_late_come_early_out_trackingtimerunner_enabled)AttendanceAllowedIPCompanyEmployeeShiftDay)hx_request_requiredlogin_required)_thread_localsc                 6   t         j                  j                  d|       j                         r0t         j                  j                  d|       j	                         }n
t               }d|_        | |_        | j                  |_        |j                          |S )zZ
    used to create late come report
    args:
        attendance : attendance object
    	late_cometypeattendance_id	r   objectsfilterexistsfirstr"   r#   employee_idsave
attendancelate_come_objs     A/var/www/python-projects/worksol/attendance/views/clock_in_out.pylate_come_creater/   -   s     "))00
 1 fh 3::AAJ B 

%' 	 34$M",M * 6 6M    c                    t        d      j                  d      syt        t        dd      }t	        | j
                  j                  d            }t	        d      }|rX|j                  rL|j                  j                  dk(  r|j                  j                  dk(  r||j                  j                  z  }n{t        j                  j                  dd      j                         rKt        j                  j                  dd      j                         }|j                  r||j                  z  }n	 ||kD  r'||k7  r"||k  rt!        |        y||kD  rt!        |        y||k  rt!        |        y)z
    this method is used to mark the late check-in  attendance after the shift starts
    args:
        attendance : attendance obj
        start_time : attendance day shift start time
        end_time : attendance day shift end time

    Ntrackingrequest%H:%M12:00T
is_default	is_active)r   getgetattrr   r   attendance_clock_instrftimegrace_time_idr8   allowed_clock_inallowed_time_in_secsr   r%   r&   r'   r(   r/   )r,   
start_timeend_timeshiftr3   now_secmid_day_sec
grace_times           r.   r    r    D   sY    /t488Dni6Gj<<EEgNOG!'*K $$ ))T1##44< u**???G				!	!TT	!	B	I	I	K&&-- . 
 %' 	
 &&z666GHx!7[ Z(  z!Z(  
g	$r0   c
                 r   t         j                  j                  | |||d      j                         }
|
r*|
j                  s|	|
_        ||
_        |
j                          t         j                  j                  | ||||	|	      }t        j                  j                  | |      }|j                         st               }| |_
        ||_        |j                  j                  j                  |_        ||_        ||_        ||_        ||_        ||_        |j                          t        j&                  |j(                        }t+        ||||       |S |d   }d|_        d|_        |j                          |j0                  j                  d      }|j                         r|d   j3                          |S )	a  
    This method is used to create attendance activity or attendance when an employee clocks-in
    args:
        employee        : employee instance
        date_today      : date
        attendance_date : the date that attendance for
        day             : shift day
        now             : current time
        shift           : shift object
        minimum_hour    : minimum hour in shift schedule
        start_time      : start time in shift schedule
        end_time        : end time in shift schedule
    N)r)   attendance_dateclock_in_date	shift_day	clock_out)r)   rG   rH   rI   clock_inin_datetime)r)   rG   r,   r@   rA   rB   r   	early_outr"   )r   r%   r&   r(   rJ   clock_out_dater*   creater   r'   r)   shift_idemployee_work_infowork_type_idrG   attendance_dayr;   attendance_clock_in_dateminimum_hourfindidr    attendance_clock_outattendance_clock_out_datelate_come_early_outdelete)employee
date_todayrG   daynowrB   rW   r@   rA   rL   activitynew_activityr,   early_out_instances                 r.    clock_in_attendance_and_activityre   t   s   6 "))00'  1  eg  **(",%--44'  5 L ##**o + J \
!)
#
","8"8"K"K"X"X
%4
"$'
!),
&.8
+".
  __Z]]3
!j8SX	
   ]
*.
'/3
,';;BBBT$$&q!((*r0   c                    | j                   j                  d      }|dk(  r/t        j                  j	                  d      j                         }n\t        j                  j	                  |      j                         }t        j                  j	                  |      j                         }|r|j                  s| j                  j                  d      rt        j                  j                         }| j                  j                  d      s|r|j                  r| j                  j                  d      }| j                  j                  d      }|r|j                  d	      d
   }|j                  j                  dg       }d}|D ]3  }		 t        j                  |      t        j                   |	d      v rd} n5 |st%        t'        d            S t)        |       \  }
}t+        j,                         }| j                  j                  d      r| j*                  }|
r||j.                  }t1        j2                         }| j                  j                  d      r| j0                  }|}|j5                  d      j7                         }t8        j                  j                  |      }t+        j,                         j5                  d      }| j                  j                  d      r| j:                  j5                  d      }t=        |      }t=        d      }t?        ||      \  }}}||kD  rh||kD  rc|tA        d      z
  }|j5                  d      j7                         }t8        j                  j                  |      }t?        ||      \  }}}|}|}tC        |
|||||||||
      }tE        | dddi      S t%        t'        d            S tG        jH                  | t'        d             t%        d      S # t"        $ r Y ~w xY w)zg
    This method is used to mark the attendance once per a day and multiple attendance activities.
    selected_companyallN
company_idrY   r   HTTP_X_FORWARDED_FORREMOTE_ADDR,r   allowed_ipsF)strictTz,You cannot mark attendance from this networkr   %Ar`   r4   timer5   r`   rB      days)
r^   r_   rG   r`   ra   rB   rW   r@   rA   rL   +attendance/components/in_out_component.htmlrunzOYou Don't have work information filled or your employee detail neither entered *Check in/Check out feature is not enabled.#<script>location.reload();</script>)%sessionr9   r   r%   r&   r(   r   enable_check_in__dict__r   
is_enabledMETAsplitadditional_data	ipaddress
ip_address
ip_network
ValueErrorr	   _r   r   ra   rR   r   todayr<   lowerr   rs   r   r   r   re   r   r   error)r3   rg   attendance_general_settingscompanyallowed_attendance_ipsx_forwarded_foripro   
ip_allowed
allowed_ipr^   	work_infodatetime_nowrB   r_   rG   r`   ra   rC   rD   rW   start_time_secend_time_secdate_yesterdayday_yesterdayr,   s                             r.   rK   rK      s    **+=>5 &>&F&F&M&M 'N '

%' 	$ //((,<(=CCE&>&F&F&M&M 'N '

%' 	$
 	$'77
+!4!<!<!B!B!D   $$Z0&&11 &ll../EFO!!-0B$**3/20@@DD]TVWKJ) 
 ++B/93G3G"54  &*
	 #A&T$UVV-g6)||~
+"++L	-&&EJ##F+$\\
(O%%d+113C"**..3.7C,,.))'2C##F+ll++G4%c*G)'2K9Mu:6L., , ( &0)2C%CN$2$;$;D$A$G$G$IM$4$<$<$@$@]$@$SMAU)B>L., '5O'C9!% /))%(J FPQ
  a
 	
 	w"N OPABBC " s   .O;;	PPc                 .   t         j                  j                  |       j                  dd      }d}|j                  d      j	                         r-|j                  d      j                         }||_        ||_        ||_        |j                          |j                  |j                        }d}|D ]=  }t        |      \  }}||z
  }	|	j                  d	z  d
z  }
|	j                  }|
|z   }||z   }? t        |      }t        j                  j                  |       j                  dd      d   }|dz   |_        ||_        ||_        t'        |      |_        t+        |      |_        |j                          |S t.        j1                  d       y)z
    Clock out the attendance and activity
    args:
        employee    : employee instance
        date_today  : today date
        now         : now
    r)   rG   rY   NT)clock_out__isnull)rG   r      i  z-attendance_datez-idz:00z>No attendance clock in activity found that needs clocking out.)r   r%   r&   order_byr'   lastrJ   rP   out_datetimer*   rG   r   rw   secondsr   r   rZ   r[   attendance_worked_hourr   attendance_overtimer   attendance_validatedloggerr   )r^   r_   ra   r   attendance_activitiesattendance_activitydurationrb   rL   
differencedays_secondr   total_secondsr,   s                 r.   !clock_out_attendance_and_activityr   6  s    /66== > h $'  ##d#;BBD3::" ; 

$& 	 )5%-7*+7(  " 5 < </?? != !

 - 	0H(9((C%K%3J$//B.5K ((G''1M-/H	0 x(''..8.DMM


 +.+
'/9
,,4
))=j)I
& +>j*I
'
LLQR
r0   c                 6   t         j                  j                  d|       j                         r0t         j                  j                  d|       j	                         }n
t               }d|_        | |_        | j                  |_        |j                          |S )zW
    Used to create early out report
    args:
        attendance : attendance obj
    rN   r!   r$   r+   s     r.   early_out_creater   o  s     "))00
 1 fh 3::AAJ B 

%' 	 34$M",M * 6 6Mr0   c                     t        d      j                  d      sy| j                  }t        |t              rt        j                  |d      }t        |j                  d            }t        d      }|rX|j                  rL|j                  j                  dk(  r|j                  j                  dk(  r||j                  j                  z  }n{t        j                  j                  dd      j!                         rKt        j                  j                  dd      j#                         }|j                  r||j                  z  }n	 ||kD  r"||k  r||k  rt%        |        yt%        |        y||kD  rt%        |        y)z
    This method is used to mark the early check-out attendance before the shift ends
    args:
        attendance : attendance obj
        start_time : attendance day shift start time
        start_end : attendance day shift end time
    Nr2   z%H:%M:%Sr4   r5   Tr6   )r   r9   rZ   
isinstancestrr   strptimer   r<   r=   r8   allowed_clock_outr?   r   r%   r&   r'   r(   r   )r,   r@   rA   rB   clock_out_timerC   rD   rE   s           r.   rN   rN     s[    /t488D44N.#&!**>:Fn55g>?G!'*K$$))T1##55=u**???G				!	!TT	!	B	I	I	K&&-- . 
 %' 	
 ''z666GH[ ! , 	 Z('$
r0   c                    | j                   j                  d      }|dk(  r/t        j                  j	                  d      j                         }n\t        j                  j	                  |      j                         }t        j                  j	                  |      j                         }|r|j                  s| j                  j                  d      rYt        j                         }| j                  j                  d      r| j                  }t        |       \  }}|j                  }t        j                         }| j                  j                  d      r| j                  }|j                  d      j!                         }	t"        j                  j                  |		      }	t$        j                  j	                  |
      j'                  dd      j)                         }
|
|
j*                  }	t        j                         j                  d      }| j                  j                  d      r| j,                  j                  d      }t/        |	|      \  }}}t1        ||||      }
|
r|
j2                  j	                  d      }|
j5                         }|
j6                  t9        d      z   }|j;                         s_|r?t=        |      }t=        d      }|
j6                  |k(  s
||k\  r3||k(  r.t?        |
|||       n|
j6                  |k(  rt?        |
|||       tA        | dddi      S tC        jD                  | tG        d             tI        d      S )za
    This method is used to set the out date and time for attendance and attendance activity
    rg   rh   Nri   rk   r   r   rq   rr   r   rY   rG   r4   rs   rt   )r^   r_   ra   r   rN   rO   ru   rv   r5   rM   rx   ry   rz   r{   )%r|   r9   r   r%   r&   r(   r   r}   r~   r   ra   r   rR   r   r   r<   r   r   r   r   r   rU   rs   r   r   r\   is_night_shiftrG   r   r'   r   rN   r   r   r   r   r	   )r3   rg   r   r   r   r^   r   rB   r_   r`   r,   ra   rW   r   r   rd   r   	next_daterC   mid_secs                       r.   rJ   rJ     s    **+=>5 &>&F&F&M&M 'N '

%' 	$ //((,<(=CCE&>&F&F&M&M 'N '

%' 	$ 	$'77
+||~
+"++L-g6)""ZZ\
' J!!$'--/&&**s*3%%(%;Xd-.TV 	
 !++Clln%%g.',,''0C5I56
2nl 7*#L

 !+!?!?!F!FK!F!X'668N"22YA5FFI%,,.!-c2G-g6G"22j@7*&)3!'1'5%1"'	  //:=#-#1!-#	 BUAJ
 	

 	w"N OPABBr0   )N)7__doc__r   loggingdjango.shortcutsr   	getLogger__name__r   r   r   r   django.contribr   django.db.modelsr   django.httpr	   django.utils.translationr
   r   attendance.methods.utilsr   r   r   r   r   r   attendance.modelsr   r   r   r   r   attendance.views.viewsr   base.context_processorsr   r   base.modelsr   r   r   horilla.decoratorsr   r   horilla.horilla_middlewaresr   r/   r    re   rK   r   r   rN   rJ    r0   r.   <module>r      s      #			8	$ . . #  $ 6   7 G F B 6.-`Nb lC  lC^6r*-` PC  PCr0   