
    vg                        d Z ddlZddlZddl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 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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*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 e!Z6 G d de2      Z7 G d de2      Z8 G d de2      Z9 G d de2      Z: G d de2      Z; G d de2      Z< G d  d!e2      Z= G d" d#e2      Z> G d$ d%e2      Z? G d& d'e2      Z@ ej                  d(      r- ej                  d)      r G d* d+e2      ZB eeeB,      d-        ZC G d. d/ej                        ZE G d0 d1e9      ZFy)2zH
models.py

This module is used to register models for recruitment app

    N)datedatetime	timedelta)apps)ValidationError)models)Q)	post_save
pre_delete)receiver)timezone)gettext_lazy)MONTH_MAPPINGattendance_date_validateformat_timeget_diff_dictstrtime_secondsvalidate_hh_mm_ss_formatvalidate_time_formatvalidate_time_in_minutes)HorillaCompanyManager)is_company_leave
is_holiday)CompanyEmployeeShiftEmployeeShiftDayHolidaysWorkType)Employee)get_horilla_model_class)HorillaModel)HorillaAuditInfoHorillaAuditLogc                   &   e Zd ZdZ ej
                  eej                  d ed            Z	 ej                  deg ed            Z ej
                  edej                   ed      	      Z ej                   d
      Z ej                  d ed            Z ej&                   ed            Z ej                  d ed            Z ej                   d
      Z ej&                  d ed            Z ed      Z G d d      Zd Zy)AttendanceActivityz"
    AttendanceActivity model
    employee_attendance_activitiesr   	on_deleterelated_nameverbose_nameTzAttendance Datenull
validatorsr*   z	Shift Day)r,   r(   r*   )r,   zIn Dater,   r*   zCheck In)r*   zOut Datez	Check Out+employee_id__employee_work_info__company_idrelated_company_fieldc                       e Zd ZdZg dZy)AttendanceActivity.Meta;
        Meta class to add some additional options
        )-attendance_date employee_id__employee_first_nameclock_inN)__name__
__module____qualname____doc__ordering     %/var/www/horilla/attendance/models.pyMetar3   O   s    	 Xr>   r@   c                    | j                   r| j                  sNt        j                         j	                         | _        t        j
                         j                         | _         t        j                  | j                  | j                        }t        j                  | j                  | j                         }||z
  }|j                         S )z1
        Duration calc b/w in-out method
        )	clock_outclock_out_dater   todayr   nowtimecombineclock_in_dater7   total_seconds)selfclock_in_datetimeclock_out_datetimetime_differences       r?   durationzAttendanceActivity.durationV   s    
 ~~T%8%8"*.."2"7"7"9D%\\^002DN$,,T-?-?O%--d.A.A4>>R,/@@,,..r>   N)r8   r9   r:   r;   r   
ForeignKeyr   PROTECT_employee_id	DateFieldr   attendance_dater   
DO_NOTHING	shift_dayDateTimeFieldin_datetimerH   	TimeFieldr7   rC   out_datetimerB   r   objectsr@   rN   r=   r>   r?   r%   r%   /   s!    $&##..5z]	K 'f&&,-()O
 "!!##{^	I '&&&D1K$F$$$Qy\JMvQz];H%V%%4a
mLN'6''T2L   d;HI#KGX X/r>   r%   c                   <    e Zd ZdZ ej
                  d      Zd Zy)BatchAttendancez 
    Batch attendance model
       
max_lengthc                 8    | j                    d| j                   S N-)titleidrJ   s    r?   __str__zBatchAttendance.__str__n   s    **Qtwwi((r>   N)r8   r9   r:   r;   r   	CharFieldrd   rg   r=   r>   r?   r]   r]   g   s      F,E)r>   r]   c            	           e Zd ZdZ fdZd ed      fd ed      fd ed      fgZ ej                  e	ej                  d	d
 ed            Z ej                  deg ed            Z ej                  eej                   d	 ed            Z ej                  ed	d	ej                    ed            Z ej                  eej                   d	 ed            Z ej                  d	 ed            Z ej.                  d	 ed       ed            Z ej                  d	 ed            Z ej.                  d	 ed       ed            Z ej6                  d	ddeg ed             Z ej6                  ddeg ed!      "      Z ej                  ed	d	ej                   ed#            Z  ej6                  degd ed$      %      Z! ejD                  d ed&      '      Z# ejD                  d ed(      '      Z$ ejJ                  d	d	)      Z& ejJ                  d	d	 ed*      +      Z' ejJ                  d,-      Z( ejD                  d ed.      '      Z) ejD                  dd/      Z* ejD                  d ed0      '      Z+ ejX                  d	d12      Z- ej6                  d3d	ed4      Z. ejD                  d-      Z/ ej`                  d	d5      Z1 e2d67      Z3 e4d8e5g9      Z6 G d: d;      Z7d< Z8d= Z9d>e:fd?Z;d@ Z<dA Z=dNdBZ>dC Z?dD Z@dE ZAdF ZBdG ZC fdHZDdI ZE fdJZFdK ZGdL ZH fdMZI xZJS )O
Attendancez
    Attendance model
    c                 2    t        |   |i | d| _        y )NT)super__init__
first_saverJ   argskwargs	__class__s      r?   rm   zAttendance.__init__w   s    $)&)r>   create_requestzCreate Requestupdate_requestzUpdate Requestrevalidate_requestzRe-validate RequestTemployee_attendancesr   )r(   r,   r)   r*   FzAttendance dater+   Shift)r(   r,   r*   z	Work Type)r,   blankr(   r*   zAttendance dayzCheck-In Dater.   zCheck-InzFirst Check-In Time)r,   r*   	help_textzCheck-Out Datez	Check-OutzLast Check-Out Time00:00
   Worked Hours)r,   defaultr`   r-   r*   zMinimum hourr`   r}   r-   r*   zBatch AttendanceOvertimer}   r-   r`   r*   zOvertime approvedr}   r*   zAttendance validatedr,   rx   zOvertime In Second)r,   rx   r*   r   r}   zIs validate request)r}   editablezIs validate request approved   )r,   r`      )r`   r,   choicesr}   )r,   r   r/   r0   history_set)r)   basesc                   $    e Zd ZdZdZddgZg dZy)Attendance.Metar4   )rR   rT   )change_validateattendancezValidate Attendance)change_approveovertimezChange Approve Overtime)r5   r6   attendance_clock_inN)r8   r9   r:   r;   unique_togetherpermissionsr<   r=   r>   r?   r@   r      s"    	 =@A

r>   r@   c                      y)z?
        Method to check the min ot for the attendance
        Nr=   rf   s    r?   check_min_otzAttendance.check_min_ot   s    r>   c                     | j                   }|y|j                  j                  | j                        j	                         }|sy|j
                  S )z-
        check is night shift or not
        F)shift_id)attendance_dayday_schedulefilterr   firstis_night_shift)rJ   dayschedules      r?   r   zAttendance.is_night_shift   sO     !!;##**DMM*BHHJ&&&r>   returnc                 z    | j                   j                   d| j                   j                   d| j                   S Nz             z - )rR   employee_first_nameemployee_last_namerT   rf   s    r?   rg   zAttendance.__str__  sD    ""667 8001T5I5I4JL 	Lr>   c                     t         j                  j                  | j                  | j                        }||j                         dS )zl
        This method is used to return the activites and count of activites comes for an attendance
        rT   rR   )querycount)r%   r[   r   rT   rR   r   )rJ   
activitiess     r?   r   zAttendance.activities
  sF     (//66 00d>N>N 7 

 $j.>.>.@AAr>   c                     g }| j                   It        j                  | j                         }t        | j	                         |      }|j                         }|S )zF
        This method will returns the value difference fields
        )requested_datajsonloadsr   	serializekeys)rJ   r   datadiffss       r?   requested_fieldszAttendance.requested_fields  sL     *::d112D!$.."2D9E::<Dr>   c                     t         j                  j                  | j                  | j                  |      j                  d      }|j                         S )zS
        This method is used to get the last attendance activity if exists
        )rR   rT   clock_out__isnullre   )r%   r[   r   rR   rT   order_bylast)rJ   null_activityr   s      r?   get_last_clock_outzAttendance.get_last_clock_out  sQ     (//66(( 00+ 7 
 (4.	 	
   r>   c           	      B   t         j                  j                  | j                  | j                        j                  d      }d}t        j                         }|D ]  }|j                  }|Kt        j                  |t        j                  |j                  |j                  |j                              }n t        j                  |j                  |      }|j                   }t        j                  |j"                  |      }||z
  }	||	j%                         z   } |S )zY
        This method is used to retun the at work calculated from the activities
        r   r7   r   )hourminutesecond)r%   r[   r   rT   rR   r   r   rE   rB   rG   dtrF   r   r   r   rC   r7   rH   rI   )
rJ   r   at_work_secondsrE   activityout_timecombined_outin_timecombined_inr   s
             r?   get_at_work_from_activitiesz&Attendance.get_at_work_from_activities)  s    (//66 00d>N>N 7 

(:
 	 lln" 	FH))H'//chhszz#**U   (//0G0GR''G"**8+A+A7KK ;.E-0C0C0EEO	F r>   c                     t        | j                        }t        | j                        }||z
  }|dk  ryt        |      }|S )ze
        This method will returns difference between minimum_hour and attendance_worked_hour
        r   rz   )r   minimum_hourattendance_worked_hourr   )rJ   minimum_hoursworked_hourpending_secondspending_hourss        r?   hours_pendingzAttendance.hours_pending@  sI     ((9(9:%d&A&AB'+5Q#O4r>   c                 v    t        | j                        st        | j                        rd| _        d| _         yy)zg
        Set minimum_hour to 00:00 if the attendance date falls on a holiday or company leave.
        rz   TN)r   rT   r   r   rf   s    r?   adjust_minimum_hourzAttendance.adjust_minimum_hourL  s5     d**+/?@T@T/U 'D"DO 0Vr>   c           	          t        t        dt        | j                        t        | j                        z
              | _        t        | j                        | _        t        | j
                        | _        y)zN
        Calculate and update attendance overtime and worked seconds.
        r   N)r   maxr   r   r   attendance_overtimeat_work_secondovertime_secondrf   s    r?   update_attendance_overtimez%Attendance.update_attendance_overtimeT  se     $/#D$?$?@%d&7&789$
  .d.I.IJ.t/G/GHr>   c                 v   t         j                  j                         }| j                  rdx| _        | _        |r|j                  r;t        |j                        }| j                  |kD  r|| _        t        |      | _
        |j                  r+| j                  t        |j                        k\  rd| _        y y y y )NFT)AttendanceValidationConditionr[   r   is_validate_requestis_validate_request_approvedattendance_validatedovertime_cutoffr   r   r   r   auto_approve_otminimum_overtime_to_approveattendance_overtime_approve)rJ   	conditioncutoff_secondss      r?   handle_overtime_conditionsz%Attendance.handle_overtime_conditionsd  s    199??A	##LQQD-0I((!01J1J!K''.8+9D(/:>/JD, ((T-A-A_55F . 480.( r>   c                    | j                          t        j                  j                  | j                  j                  d      j                               | _        d}| j                          | j                          | j                  6t        j                  j                  | j                        }|j                  }| j                  j                  j                  | j                  j                  d      j                         | j                  j                         j#                         }|r| j%                  |       n!| j'                         }| j%                  |       | j                  }| j                  j                  j                  | j                  j                  d      j                         | j                  j                         j#                         }|j(                  }|r%|du r!| j(                  | _        || j*                  z   }n|s|| j*                  z
  }d| _        t-        |      |_        |j1                          t3        	| `  |i | d| _        y )Nz%A)r   F)pk%Bmonthyearr   )r   r   r[   getrT   strftimelowerr   r   r   r   rj   r   rR   employee_overtimer   r   r   	update_ot	create_otr   approved_overtime_secondr   overtimesaverl   rn   )
rJ   rp   rq   prev_attendance_approved
prev_stateemployee_otapprovedattendance_accounttotal_ot_secondsrr   s
            r?   r   zAttendance.savew  s   '').66::$$--d399; ; 
 $)   " 	'')77#++//477/;J'1'M'M$ &&88??&&//5;;=%%** @ 
 %' 	 NN;' ..*KNN;'33!--??FF&&//5;;=%%** G 
 %' 	 .==0E9,0,@,@D)/$2O2OO/$2O2OO,-D)&12B&C#!d%f%r>   c                    | j                   j                  t        | j                        t        | j                        t        | j
                        t        | j                        t        | j                        | j                  r| j                  j                  nd| j                  r| j                  j                  nd| j                  | j                  | j                  r| j                  j                  ndd}|S )z7
        Used to serialize attendance instance
         )rR   rT   attendance_clock_in_dater   attendance_clock_outattendance_clock_out_dater   work_type_idr   r   batch_attendance_id)rR   re   strrT   r   r   r   r   r   r   r   r   r   )rJ   serialized_datas     r?   r   zAttendance.serialize  s      ++.."4#7#78(+D,I,I(J#&t'?'?#@$'(A(A$B),T-K-K)L,0MM((r484E4ED--002&*&A&A --/3/G/G((++R
  r>   c                 2   t        j                  t              5  t        j                  j                  | j                  | j                        j                          | j                  j                  j                  | j                  j                  d      j                         | j                  j                  d            }d d d        j                         r| j                  |j                                t        |    |i | y # 1 sw Y   HxY w)Nr   r   %Yr   )
contextlibsuppress	Exceptionr%   r[   r   rT   rR   deleter   r   r   existsr   r   rl   )rJ   rp   rq   r   rr   s       r?   r   zAttendance.delete  s       + 	&&-- $ 4 4$BRBR . fh**<<CC**33D9??A))2248 D K		 NN;,,./''	 	s   B+DDc                 n   t         j                  j                  | j                  | j                  j                  d      j                         | j                  j                        \  }}| j                  r| j                  |_
        | j                  r| j                  |_        |j                          |S )z
        Create a new Hour Account instance if it doesn't exist for a specific month and year.
        Returns:
            AttendanceOverTime: The created or fetched AttendanceOverTime instance.
        r   rR   r   r   )AttendanceOverTimer[   get_or_createrR   rT   r   r   r   r   r   r   r   r   hour_accountr   )rJ   r   createds      r?   r   zAttendance.create_ot  s      299GG((&&//5;;=%%**  H  
W ++#'#;#;K $$'+'B'BK$r>   c                    t        j                  d      r=| j                  j                  j	                  | j
                  | j
                  d      }ng }t               }|r,|D ]'  }|t        |j                  |j                  f      z  }) t        j                  j	                  | j                  | j
                  j                  | j
                  j                  d      j                  |      j                  dd      }d	}d	}|D ])  }t        |d         }	t!        |	|d         }
||
z  }||	z  }+ ||z
  }t#        |      |_        t#        |      |_        |j)                          |S )
z
        Update the hour account for the given employee.

        Args:
            employee_ot (obj): AttendanceOverTime instance
        leaver   )start_date__lteend_date__gtestatus)attendance_date__rangeT)rR   attendance_date__monthattendance_date__yearr   r   r   r   )r   is_installedrR   leaverequest_setr   rT   r	   
start_dateend_daterj   r[   r   r   excludevaluesr   minr   worked_hoursr   r   )rJ   r   approved_leave_requestsexclude_conditionr	  month_attendanceshour_balanceminimum_hour_second
attendancerequired_work_secondr   r   s               r?   r   zAttendance.update_ot  s    W%&*&6&6&G&G&N&N $ 4 4"22! 'O '# ')# C"0 !Q,1,<,<enn+M& ! %% ,,'+';';'A'A&*&:&:&?&?%)	 &  W&'VN$45 	 + 	8J#2:n3M#N  !5zBR7STNN*L#77		8 ,l:#.|#< $/$>!r>   c                    t        |   |i | t        j                         j	                         }t        j
                         j                         }| j                  }| j                  | j                  k  rt        ddi      | j                  r&| j                  | j                  k  rt        ddi      | j                  r#| j                  |k\  r||kD  rt        ddi      y y y )Nr   z;Attendance check-in date never smaller than attendance dater   zEAttendance check-out date never smaller than attendance check-in dater   z&Check out time not allow in the future)rl   cleanr   rE   rF   rD   r   r   r   rT   r   r   )rJ   rp   rq   rE   rD   r   rr   s         r?   r   zAttendance.clean  s    t&v&lln!!# %%',,((4+?+??!.0m  **..1N1NN!/1x 
 ))d.L.LPU.U#~%+-UV   /V)r>   )F)Kr8   r9   r:   r;   rm   rQ   r  r   rO   r   rP   rR   rS   r   rT   r   rU   r   r   r   r   r   r   rY   r   r   r   rh   r   r   r   r]   r   r   BooleanFieldr   r   IntegerFieldr   r   r   r   is_bulk_requestr   	TextFieldrequest_descriptionrequest_typer   	JSONFieldr   r   r[   r#   r"   historyr@   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__rr   s   @r?   rj   rj   r   s   
 
1-./	1-./	q!678F $&##..+z]K 'f&&,-()O
 !v  !2!2AgJH %6$$##{^L 'V&&##'(	N  0v/// 2  +&***;P9Q !1 0 0"2 3! ,6+++!<Q:R .V--()~& $6##()~&	L ,&++..)* +&**()z]	 #6&"5"5A&9$:# /6..A&<$= )V((d$?N)f))A.B,CO  3v221=-&--A&;$< *f))%%HO#66#6#6A&D$E$  +&**E#6##D&:JL %$$U3J%V%%4%@N#KG "
G
 
 

'L LB		!.
#I 8&+ Z2($.3j r>   rj   c                   2    e Zd Z ej                  d      Zy)AttendanceRequestFilezattendance/request_files)	upload_toN)r8   r9   r:   r   	FileFieldfiler=   r>   r?   r,  r,  6  s    6&@ADr>   r,  c                       e Zd ZdZ ej
                  eej                        Z ej
                  e	ej                        Z
 ej                  ed      Z ej                  d ed      d      Zdefd	Zy
)AttendanceRequestCommentz(
    AttendanceRequestComment Model
    )r(   T)rx   Commentr   )r,   r*   r`   r   c                     | j                    S N)commentrf   s    r?   rg   z AttendanceRequestComment.__str__D  s    ,, r>   N)r8   r9   r:   r;   r   rO   rj   CASCADE
request_idr   rR   ManyToManyFieldr,  filesr$  rQ   r5  r   rg   r=   r>   r?   r1  r1  :  su     #"":HJ#&##HGK"F""#8EEfDq|PSTG! !r>   r1  c            	           e Zd ZdZ ej
                  eej                  d ed            Z	 ej                  d ed            Z ej                  d	      Z ej                   ej                         j!                  d
      dd ed            Z ej                  dddeg ed            Z ej                  dddeg ed            Z ej                  ddeg ed            Z ej,                  dd ed            Z ej,                  dd ed            Z ej,                  dd ed            Z ed      Z G d d      Zd Zd Zd Zd  Z d! Z! fd"Z" xZ#S )#r  z"
    AttendanceOverTime model
    r   r   r'   r{   Month)r`   r*   r   r   r   TYear)r}   r,   r`   r*   rz   r|   )r`   r}   r,   r-   r*   zPending Hours   zOvertime Hoursr~   zWorked Seconds)r}   r,   r*   zPending SecondszOvertime Secondsr/   r0   c                        e Zd ZdZg dZddgZy)AttendanceOverTime.Metar4   r  z-yearz-month_sequenceNr8   r9   r:   r;   r   r<   r=   r>   r?   r@   r?    s    	 A./r>   r@   c                     	 t        | j                        }d|cxk  rdk  sn t        dt        d      i      y # t        t
        f$ r t        dt        d      i      w xY w)Nil  i4  r   z3Year must be an integer value between 1900 and 2100)intr   r   rQ   
ValueError	TypeError)rJ   r   s     r?   r   zAttendanceOverTime.clean  sr    		tyy>DD(D(%QTUV  ) I& 	!PQR 	s	   8; &A!c                     | j                   dz   }t        | j                        }t        ||d      }|dk(  r!t        |dz   dd      t	        d      z
  }||fS t        ||dz   d      t	        d      z
  }||fS )z
        this method is used to create new AttendanceOvertime's instance if there
        is no existing for a specific month and year
              )days)month_sequencerB  r   r   r   )rJ   r   r   r  r  s        r?   
month_dayszAttendanceOverTime.month_days  s    
 ##a'499~$q)
B;D1Ha+iQ.??H 8## D%!)Q/)2CCH8##r>   c           
          t        t        t        j                  j	                  t
        | j                     | j                  | j                  d      j                  dd                  }t        |      S )zH
        This method will return not validated hours in a month
        F)r  r  rR   r   r   Tflatsumlistrj   r[   r   r   r   r   rR   values_listr   )rJ   hrs_to_vlaidates     r?   not_validated_hrsz$AttendanceOverTime.not_validated_hrs  sl     ""))+8+D*.)) $ 0 0).	 * 
 +.T+:	
 ?++r>   c                     t        t        t        j                  j	                  t
        | j                     | j                  | j                  dd      j                  dd                  }t        |      S )zK
        This method will return the overtime hours to be approved
        TF)r  r  rR   r   r   r   rL  rN  )rJ   hrs_to_approves     r?   not_approved_ot_hrsz&AttendanceOverTime.not_approved_ot_hrs  so     ""))+8+D*.)) $ 0 0)-05 *  +/d+;

 >**r>   c                 (    t         | j                     S )z@
        This method will return the index of the month
        )r   r   rf   s    r?   get_month_indexz"AttendanceOverTime.get_month_index  s     TZZ((r>   c                 0   t        | j                        | _        t        | j                        | _        t        | j
                        | _        | j                  j                  d      d   }g d}|j                  |      | _
        t        | 0  |i | y )Nrc   r   )januaryfebruarymarchaprilmayjunejulyaugust	septemberoctobernovemberdecember)r   r  hour_account_secondr   hour_pending_secondr   r   r   splitindexrI  rl   r   )rJ   rp   rq   
month_namemonthsrr   s        r?   r   zAttendanceOverTime.save  s~    #243D3D#E #243E3E#F .t}}=ZZ%%c*1-

 %ll:6d%f%r>   )$r8   r9   r:   r;   r   rO   r   rP   rQ   rR   rh   r   PositiveSmallIntegerFieldrI  r   rE   r   r   r   r  r   r   r"  rf  rg  r   r   r[   r@   r   rJ  rS  rV  rX  r   r)  r*  s   @r?   r  r  H  s    $&##..(z]	K FwZE 6V55a@N6''-vY	D $6##()~&L %F$$()'M  v()'(	H .&--'(
 .&--()
 *f)))*O
 $KG0 0
$, +")& &r>   r  c            	       ~    e Zd ZdZd ed      fd ed      fgZ ej                  eej                  d ed            Z
 ej                  eej                  d	d ed
      d      Z ej                  de ed            Z ed      Z ej$                  d	d	      Zd Zd fdZ G d d      ZdefdZ xZS )AttendanceLateComeEarlyOutz*
    AttendanceLateComeEarlyOut model
    	late_comez	Late Come	early_outz	Early Outlate_come_early_outrj   r'   Tr   F)r(   r,   r)   r*   r   r=  Type)r`   r   r*   r/   r0   )auto_now_addr,   c                 6    | j                   j                         S )z^
        This method is used to return the total penalties in the late early instance
        )penaltyaccounts_setr   rf   s    r?   get_penalties_countz.AttendanceLateComeEarlyOut.get_penalties_count  s     ''--//r>   r   c                 x    t        |   |i | | j                  j                  | _        t        |   |i | y r4  )rl   r   attendance_idrR   ro   s      r?   r   zAttendanceLateComeEarlyOut.save	  s9    d%f%--99d%f%r>   c                       e Zd ZdZddgZdgZy)AttendanceLateComeEarlyOut.Metar4   rx  typez-attendance_id__attendance_dateNr@  r=   r>   r?   r@   rz    s    	 ,v756r>   r@   c                     | j                   j                  j                   d| j                   j                  j                   d| j                   S r   )rx  rR   r   r   r{  rf   s    r?   rg   z"AttendanceLateComeEarlyOut.__str__  sN    $$00DDE F++>>?s499+O 	Or>   r   N)r8   r9   r:   r;   rQ   r   r   rO   rj   rP   rx  r   rU   rR   rh   r{  r   r[   rW   
created_atrv  r   r@   r   rg   r)  r*  s   @r?   rn  rn    s    
 
an%	an%G
 &F%%..*|_	M $&####*z]K 6r76SD#KG &%%4dCJ0&
7 7O Or>   rn  c                   .    e Zd ZdZ ej
                  deg ed            Z ej
                  dddeg      Z	 ej
                  dddeg      Z
 ej                  d ed      	      Z ej                  ed ed
            Z e       Z fdZ xZS )r   z-
    AttendanceValidationCondition model
    r{   zWorked Hours Auto Approve Till)r`   r-   r*   T)rx   r,   r`   r-   FzAuto Approve OTr   r   rx   r*   c                     t         |           | j                  s3t        j                  j                         rt        t        d            yy)H
        This method is used to perform some custom validations
        zYou cannot add more conditions.N)rl   r   re   r   r[   r  r   rQ   rJ   rr   s    r?   r   z#AttendanceValidationCondition.clean1  s@     	ww8@@GGI!!$E"FGG Jwr>   )r8   r9   r:   r;   r   rh   r   rQ   validation_at_workr   r   r!  r   r8  r   
company_idr   r[   r   r)  r*  s   @r?   r   r     s     *))()78
 #3&"2"2":N9O# 'f&&":N9OO *f))A&7$8O (''t!I,WJ#%GH Hr>   r   c                   f    e Zd ZdZ ej
                  degd ed            Z ej                         Z
 ej                  d ed            Z ej                  d	 ed
            Z ej                  d	      Z ej                  ed ed            Z e       ZdefdZ fdZ fdZ xZS )	GraceTimez%
    Model for saving Grace time
    z00:00:00r{   zAllowed timer   Tz1Allcocate this grace time for Check-In Attendance)r}   ry   Fz2Allcocate this grace time for Check-Out Attendancer   r   r  r   c                 2    t        | j                   d      S )Nz - Hours)r   allowed_timerf   s    r?   rg   zGraceTime.__str__Q  s    d''(122r>   c                    t         |           | j                  r\t        j                  j                  d      j                  | j                        j                         rt        t        d            | j                  }| j                  }| }t        j                  j                  |      j                  |      j                  | j                        j                         rt        dt        d      i      y)	r  T)
is_default)re   z2There is already a default grace time that exists.)r  r  z?There is already an existing grace time with this allowed time.N)rl   r   r  r  r[   r   r  re   r  r   rQ   r  )rJ   r  r  exclude_defaultrr   s       r?   r   zGraceTime.cleanT  s     	??  ''4'8@@DGG@LSSU%JK  ((__
(. $$,$?WW0WW VX
 ""AY% r>   c                    | j                   }|j                  d      \  }}}t        |      }t        |      }t        |      }	|d}
|d}|	d}|
 d| d| | _         |dz  |dz  z   |	z   | _        t	        |   |i | y )N:02di  <   )r  rh  rB  allowed_time_in_secsrl   r   )rJ   rp   rq   r  hoursminutessecs	hours_intminutes_intsecs_int	hours_strminutes_strsecs_strrr   s                r?   r   zGraceTime.saveq  s    ((+11#6wJ	'lt9 o	$S)s^(k;-q
C$-$4{R7G$G($R!d%f%r>   )r8   r9   r:   r;   r   rh   r   rQ   r  r"  r  r!  allowed_clock_inallowed_clock_outr  r8  r   r  r   r[   r   rg   r   r   r)  r*  s   @r?   r  r  :  s     $6##,-~&	L /6..0*v**"U V ,++#W!X %$$U3J'''t!I,WJ#%G3 3:& &r>   r  c                       e Zd ZdZ ej
                  d      Z ej
                  d ed       ed            Z ej                  e
ej                  d      Z e       Zy)	AttendanceGeneralSettingz#
    AttendanceGeneralSettings
    Tr   zEnable Check in/Check outzfEnabling this feature allows employees to record their attendance using the Check-In/Check-Out button.)r}   r*   ry   )r(   r,   N)r8   r9   r:   r;   r   r!  time_runnerrQ   enable_check_inrO   r   r6  r  r   r[   r=   r>   r?   r  r    sh     &&%%d3K)f))23t
O #""7fnn4PJ#%Gr>   r  r	  payrollc                       e Zd ZdZ ej
                  eej                  dddd      Z ej
                  e	ej                  dd      Z ej
                  ddej                  d	      Z ej
                  d
ej                  ddd      Z ej                  dd      Z ej"                  d      Z ej                  dd      Zd fdZ G d d      Z xZS )PenaltyAccountz0
        LateComeEarlyOutPenaltyAccount
        penalty_setFr   T)r(   r)   r   r*   r,   )r(   r,   r   zleave.LeaveRequest)r,   r(   r   zleave.LeaveTypez
Leave type)r(   rx   r,   r*           )r}   r,   r   c                    t         |           | j                  s"| j                  rt	        dt        d      i      | j                  r"| j                  st	        dt        d      i      | j                  s"| j                  st	        dt        d      i      | j                  s| j                  r"| j                  st	        dt        d      i      y )Nleave_type_idz+Specify the leave type to deduct the leave.minus_leaveszCIf a leave type is chosen for a penalty, minus leaves are required.z3Either minus leaves or a penalty amount is requiredzLeave type is required)rl   r   r  r  r   rQ   penalty_amountdeduct_from_carry_forwardr  s    r?   r   zPenaltyAccount.clean  s    GMO%%$*;*;%$a(U&VW  !!$*;*;%&a)  $$T-@-@%'Q*  !!T%C%C((%:R8S&TUUr>   c                       e Zd ZdgZy)PenaltyAccount.Metaz-created_atN)r8   r9   r:   r<   r=   r>   r?   r@   r    s	    %Hr>   r@   r}  )r8   r9   r:   r;   r   rO   r   rP   rR   rn  r6  late_early_idleave_request_idrU   r  
FloatFieldr  r!  r  r  r   r@   r)  r*  s   @r?   r  r    s    	 (f''nn&#
 *))&nn	
 -6,, tv~~PU
 *))''%
 )v((4@$7F$7$7$F!***3TB	:	' 	'r>   r  senderc                 l   |r1|j                   }|rUt        dd      } |       }|j                  rH|j                  j                          d|_        |j                  j
                  j                  |_        n{|j                  r:d|j                  j                   |_        |j                  j                  |_        n5dt        j                          |_        t        j                         |_        d|_        d|_        |j                   |_        d|_        |j!                          d|_        |j"                  j%                  |j&                         |j!                          |j(                  r|j*                  r|j&                  j,                  j/                  |j(                  	      j1                         }t3        |j*                  d
z        d
z  }|j4                  st7        d|j8                  |z
        |_        nt7        d|j:                  |z
        |_        |j!                          yyyy)z\
        This is post save method, used to create initial stage for the recruitment
        r  	deduction	app_labelmodelz penaltyzLeave penalty zPenalty on FT)r     r   N)r  r    r  get_type_displayrd   rx  rT   one_time_dater  r  r   rD   include_active_employeesis_fixedamountonly_show_under_employeer   specific_employeesaddrR   r  r  available_leaver   r   roundr  r   available_dayscarryforward_days)	r  instancer  rq   r  	Deductionpenalty	availableunits	            r?   create_initial_stager    s    %44N3'{	 $+))#11BBDEXN M !..<<LL ) ..()B)B)K)K(LM M -5,E,E,N,NG)&1(..2B1C$DGM,4NN,<G)380#' !)!8!8370380**..x/C/CD%%(*?*?$00@@GG"*"8"8 H %'  X22Q67!;99/21y7O7ORV7V/XI,25I77$>3I/   +@%? r>   c            	           e Zd ZdZd ed      fd ed      fd ed      fd ed	      fd
 ed      fd ed      fgZ ej                  ddd      Z ej                  dde      Z	 ej                  eej                   ed            Z ej                  dd      Z ej                  ddegdd      Z ej                  ddegdd      Z ej&                  ddd      Z ej&                  ddd      Z ej,                  d      Z ej                  ddd      Z ej2                  d      Z ej2                  d      Z ej8                  d      Z ej<                  dd      Z e d      Z!d  Z" fd!Z# fd"Z$d# Z% xZ&S )$WorkRecordsz
    WorkRecord Model
    FDPPresentHDPzHalf Day PresentABSAbsentHDzHoliday/Company LeaveCONFConflictDFTDraft   T)r`   r,   rx      )r`   r,   r   r   )r(   r*   r   rz   )r,   rx   r-   r}   r`   r   )r,   rx   r}   r   r_      Fr   r/   c                 \   | j                   }|dk(  rt        j                  d      rt        dd      }|j                  j                         rT|j                  j                  | j                  | j                  | j                        j                         j                  nd }|d| z  }|S )Nr  r	  leaverequestr  )rR   r  r
  z | )messager   r  r    r[   r  r   rR   r   r   r  )rJ   title_messageLeaveRequest
leave_types       r?   r  zWorkRecords.title_message:  s    H$  )6%^  $++224 !((//$($4$4&*ii(,		 0 
 UW"]  3zl!33r>   c                 V    t        j                         | _        t        |   |i | y r4  )r   rE   last_updaterl   r   ro   s      r?   r   zWorkRecords.saveO  s"    #<<>d%f%r>   c                 x    t         |           d| j                  cxk  rdk  sn t        t	        d            y )Nr        ?z*Day percentage must be between 0.0 and 1.0)rl   r   day_percentager   rQ   r  s    r?   r   zWorkRecords.cleanT  s5    d))0S0!!$P"QRR 1r>   c                 h    | j                   | j                   S | j                   d| j                   S rb   )record_namework_record_typer   rf   s    r?   rg   zWorkRecords.__str__Y  s@     + 	
 ))*!DII;7	
r>   )'r8   r9   r:   r;   rQ   r   r   rh   r  r  rO   r   rP   rR   rS   r   r   at_workmin_hourr"  r   min_hour_secondr$  noter  r!  is_attendance_recordis_leave_recordr  r  rW   r  r   r[   r  r   r   rg   r)  r*  s   @r?   r  r  
  s   
 
)	$%&	(	q()*	:	'
G #&""cDIK'v''14Q#&##FNN:K 6T2Df 
 G  v 
 H )V((d$JN)f))t4KO6s+Df"4tDG.6..u=)f))%8O&V&&q1N&&&&D=K#$QRG*&
S

r>   r  c                   P    e Zd ZdZ eee      d        Z eee      d        Z	y)OverrideAttendancesz8
    Class to override Attendance model save method
    r  c                 6   |j                   rt        |j                        }t        |j                        }|j                  |k\  rdnd}|j
                  du rdn|}|dk(  rt        d      n
t        d      }|dk(  r||kD  rt        d      n|}t        j                  j                  |j                  d|j                  	      }t        j                  j                  |j                  |j                  
      j                         s
t               nBt        j                  j                  |j                  |j                  
      j                         }|j                  |_        |j                  |_        |j                  |_        |j                  |_        ||_        ||_        ||_        ||_        d|_        |j
                  r||dz  kD  rdnd|_        |j-                          |dk(  r|j.                  rt        d      }n-|dk(  r|j.                  rt        d      }n|dk(  rt        d      }||_        |j-                          |j&                  }|j$                  }|j0                  sd}t        d      }||_        ||_        |j-                          yy)z9
        Overriding Attendance model save method
        r  r  Fr  zValidate the attendance	ValidatedzIncomplete minimum hourT)r   r  rR   )r   rR   r  r  g      ?zHalf day leavezAn approved leave existsr  zCurrently workingN)rn   r   r   r   r   r   rQ   r  r[   r   rT   rR   r  r   r   r  r  r  r  r  r  r  r   r  r   )r  r  rq   r  r   r  r  work_records           r?   attendance_post_savez(OverrideAttendances.attendance_post_saveg  s}   
 -h.C.CDO,X-L-LMN&55HUeF'<<EV6F06&0@+,an  U?'G +, 
 &--44--%)$00 5 K #**11!11 ( 4 4 2  &( 
 !((//!11 ( 4 4 0  %'  '/&:&:K#'77K"*"A"AK#+#8#8K *9K')7K&+1K(")K/3K,,,*_q-@@Dd * ;#>#>,-5[%@%@675I,")K!))G 11F00/0")K+1K(A r>   c                     t         j                  j                  |j                  d|j                        j                          y)z;
        Overriding Attendance model delete method
        T)rR   r  r   N)r  r[   r   rR   rT   r   )r  r  _kwargss      r?   attendance_pre_deletez)OverrideAttendances.attendance_pre_delete  s;     	"" ,,!%)) 	# 	
 &(r>   N)
r8   r9   r:   r;   r   r
   rj   r  r   r  r=   r>   r?   r  r  a  s@    
 i
+D ,DL j,
 -
r>   r  )Gr;   r   r   r   r   r   r   django.appsr   django.core.exceptionsr   	django.dbr   django.db.modelsr	   django.db.models.signalsr
   r   django.dispatchr   django.utilsr   django.utils.translationr   rQ   attendance.methods.utilsr   r   r   r   r   r   r   r   base.horilla_company_managerr   base.methodsr   r   base.modelsr   r   r   r   r   employee.modelsr   horilla.methodsr    horilla.modelsr!   horilla_audit.modelsr"   r#   _validate_time_in_minutesr%   r]   rj   r,  r1  r  rn  r   r  r  r  r  r  Modelr  r  r=   r>   r?   <module>r     sY      . .  2   : $ ! 6	 	 	 ? 5 T T $ 3 ' B 5 5/ 5/p)l )A AHBL B!| !Z& Z&z3O 3OlHL H>E& E&P&| &" 4W"3$"3"3I">?' ?'B i/0! 00!fT
&,, T
nX* Xr>   