
    vgW                     b   d Z ddlZddl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 Z.d Z/e*e)d               Z0ddZ1d Z2d Z3e*e)d               Z4y)zc
clock_in_out.py

This module is used register endpoints to the check-in check-out functionalities
    N)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     1/var/www/horilla/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_idr7   allowed_clock_inallowed_time_in_secsr   r$   r%   r&   r'   r.   )r+   
start_timeend_timeshiftr2   now_secmid_day_sec
grace_times           r-   r   r   B   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	$r/   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(   rF   rG   rH   clock_inin_datetime)r(   rF   r+   r?   r@   rA   r   	early_outr!   )r   r$   r%   r'   rI   clock_out_dater)   creater   r&   r(   shift_idemployee_work_infowork_type_idrF   attendance_dayr:   attendance_clock_in_dateminimum_hourfindidr   attendance_clock_outattendance_clock_out_datelate_come_early_outdelete)employee
date_todayrF   daynowrA   rV   r?   r@   rK   activitynew_activityr+   early_out_instances                 r-    clock_in_attendance_and_activityrd   r   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!((*r/   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      rUt        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        |
|||||||||
      }d}d}tE        |       d   }d}d}|r)djG                  |
jI                         d         }d}d }d!}t%        d"jG                  t'        d#      ||||$            S t%        t'        d%            S tK        jL                  | 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rX   r   HTTP_X_FORWARDED_FORREMOTE_ADDR,r   allowed_ipsF)strictTz,You cannot mark attendance from this networkr   %Ar_   r3   timer4   r_   rA      days)
r]   r^   rF   r_   r`   rA   rV   r?   r@   rK    enabled_timerunnerz
                <script>
                        $(".time-runner").removeClass("stop-runner");
                        run = 1;
                        at_work_seconds = {at_work_seconds_forecasted};
                    </script>
                    forecasted_at_work_secondsat_work_seconds_forecastedz6
                style="display:none"
                zS onmouseenter = "$(this).find('span').show();$(this).find('.time-runner').hide();" zS onmouseleave = "$(this).find('span').hide();$(this).find('.time-runner').show();" a  
                <button class="oh-btn oh-btn--warning-outline check-in mr-2"
                {mouse_in}
                {mouse_out}
                    hx-get="/attendance/clock-out"
                        hx-target='#attendance-activity-container'
                        hx-swap='innerHTML'><ion-icon class="oh-navbar__clock-icon mr-2
                        text-warning"
                            name="exit-outline"></ion-icon>
                <span {hidden_label} class="hr-check-in-out-text">{check_out}</span>
                    <div class="time-runner"></div>
                </button>
                {script}
                z	Check-Out)	check_outscripthidden_labelmouse_in	mouse_outzOYou Don't have work information filled or your employee detail neither entered *Check in/Check out feature is not enabled.#<script>location.reload();</script>)'sessionr8   r   r$   r%   r'   r   enable_check_in__dict__r   
is_enabledMETAsplitadditional_data	ipaddress
ip_address
ip_network
ValueErrorr   _r   r   r`   rQ   r   todayr;   lowerr   rr   r   r   r   rd   r   formatget_forecasted_at_workr   error)r2   rf   attendance_general_settingscompanyallowed_attendance_ipsx_forwarded_foriprn   
ip_allowed
allowed_ipr]   	work_infodatetime_nowrA   r^   rF   r_   r`   rB   rC   rV   start_time_secend_time_secdate_yesterdayday_yesterdayr+   r}   r~   time_runner_enabledr   r   s                                  r-   rJ   rJ      sX    **+=>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L"4W"=>R"SHI" /7/N/N/P40     uu	 Fn!!-%'   , a
 	
 	w"N OPABBW " s   .Q	Q"!Q"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(   rF   rX   NT)clock_out__isnull)rF   r      i  z-attendance_datez-idz:00z>No attendance clock in activity found that needs clocking out.)r   r$   r%   order_byr&   lastrI   rO   out_datetimer)   rF   r
   rv   secondsr   r   rY   rZ   attendance_worked_hourr   attendance_overtimer   attendance_validatedloggerr   )r]   r^   r`   r   attendance_activitiesattendance_activitydurationra   rK   
differencedays_secondr   total_secondsr+   s                 r-   !clock_out_attendance_and_activityr   ^  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
r/   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
    rM   r    r#   r*   s     r-   early_out_creater     s     "))00
 1 fh 3::AAJ B 

%' 	 34$M",M * 6 6Mr/   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
    Nr1   z%H:%M:%Sr3   r4   Tr5   )r   r8   rY   
isinstancestrr   strptimer   r;   r<   r7   allowed_clock_outr>   r   r$   r%   r&   r'   r   )r+   r?   r@   rA   clock_out_timerB   rC   rD   s           r-   rM   rM     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('$
r/   c           	      f   | 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                  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?        |
|||       d}d}tA        |       d   }d}d}|r)djC                  |jE                         d         }d}d}d}tG        djC                  tI        d       ||||!            S tK        jL                  | tI        d"             tG        d#      S )$za
    This method is used to set the out date and time for attendance and attendance activity
    rf   rg   Nrh   rj   r   r   rp   rq   r   rX   rF   r3   rr   rs   )r]   r^   r`   r   rM   rN   rt   ru   r4   rL   rw   rx   a?  
                <script>
                $(document).ready(function () {{
                    $('.at-work-seconds').html(secondsToDuration({at_work_seconds_forecasted}))
                }});
                run = 0;
                at_work_seconds = {at_work_seconds_forecasted};
                </script>
            ry   rz   z.
            style="display:none"
            zX onmouseenter="$(this).find('div.at-work-seconds').hide();$(this).find('span').show();" zWonmouseleave="$(this).find('div.at-work-seconds').show();$(this).find('span').hide();" aQ  
                <button class="oh-btn oh-btn--success-outline mr-2"
                {mouse_in}
                {mouse_out}
                hx-get="/attendance/clock-in"
                hx-target='#attendance-activity-container'
                hx-swap='innerHTML'>
                <ion-icon class="oh-navbar__clock-icon mr-2 text-success"
                name="enter-outline"></ion-icon>
                <span class="hr-check-in-out-text" {hidden_label} >{check_in}</span>
                <div class="at-work-seconds"></div>
                </button>
                {script}
                zCheck-In)check_inr}   r~   r   r   r   r   )'r   r8   r   r$   r%   r'   r   r   r   r   r`   r   rQ   r   r   r;   r   r   r   r   r   rT   rr   r   r   r[   is_night_shiftrF   r   r&   r   rM   r   r   r   r   r   r   r   )r2   rf   r   r   r   r]   r   rA   r^   r_   r+   r`   rV   r   r   rc   r   	next_daterB   mid_secr}   r~   r   r   r   s                            r-   rI   rI     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!-#	 09:NO	 +3+J+J+L0,   L vHuI F:)!#  
 	
. 	w"N OPABBr/   )N)5__doc__r   logging	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   rd   rJ   r   r   rM   rI    r/   r-   <module>r      s     			8	$ . . #  $ 6   7 G F B 6.-`Nb VC  VCr6r*-` zC  zCr/   