
    si                     :   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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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* ddl+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 e Z7 G d de0      Z8 G d de0      Z9 G d de0      Z: G d de0      Z; G d de0      Z< G d de0      Z= G d  d!e0      Z> G d" d#e0      Z? G d$ d%e0      Z@ G d& d'e0      ZA G d( d)ej                        ZCy)*zH
models.py

This module is used to register models for recruitment app

    N)datedatetime	timedelta)apps)ValidationError)models)Q)reverse)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WorkType)Employee)get_horilla_model_class)HorillaModelupload_path)HorillaAuditInfoHorillaAuditLogrender_templatec                   \   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d Zd Zd Zd Zd Z d Z!d Z"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     5/var/www/python-projects/worksol/attendance/models.pyMetar3   R   s    	 Xr>   r@   c           
      
   dt        d      fdt        d      fdt        d      fdt        d      fd	t        d
      fdt        d      fdt        d      fg}t        |      j                  | j                  j                        S )z 
        Display status
        mondayMondaytuesdayTuesday	wednesday	WednesdaythursdayThursdayfridayFridaysaturdaySaturdaysundaySunday)_dictget	shift_dayday)selfDAYs     r?   
get_statuszAttendanceActivity.get_statusY   s~     q{#)%!K.):'q{#:'q{#
 Cy}}T^^//00r>   c                      t        dd| i      S )#
        for delete button
        z*cbv/attendance_activity/delete_action.htmlinstancepathcontextr"   rU   s    r?   get_delete_attendancez(AttendanceActivity.get_delete_attendancei       
 =&
 	
r>   c                     | j                   j                  j                   d| j                   j                  j                   S zZ
        Return subtitle containing both department and job position information.
        z / employee_idemployee_work_infodepartment_idjob_position_idr^   s    r?   attendance_detail_subtitlez-AttendanceActivity.attendance_detail_subtitles   ;     ""55CCDCHXHXHkHkH{H{G|}}r>   c                 8    t        dd| j                  i      }|S )z)
        for detail view of page
        zattendance-activity-single-viewpkkwargsr
   rk   rU   urls     r?   attendance_detail_viewz)AttendanceActivity.attendance_detail_viewy        7twwP
r>   c                 $    | j                   d k(  ryy )N!style="background-color: #FFE4B3")	clock_outr^   s    r?   	diff_cellzAttendanceActivity.diff_cell   s    >>T!6 "r>   c                      t        dd| i      S )rY   z1cbv/attendance_activity/detail_delete_action.htmlrZ   r[   r"   r^   s    r?   detail_view_delete_attendancez0AttendanceActivity.detail_view_delete_attendance   s    
 D&
 	
r>   c                 |    t        |dz        }t        |dz  dz        }t        |dz  dz        }|dd|dd|dS )zz
        This method is used to format seconds to H:M:S and return it
        args:
            seconds : seconds
          <   02d:)int)rU   secondshourminutess       r?   duration_format_timez'AttendanceActivity.duration_format_time   sU     7d?#w~",-w~+,s1WSM73-88r>   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
        )ru   clock_out_dater   todayr   nowtimecombineclock_in_dater7   total_seconds)rU   clock_in_datetimeclock_out_datetimetime_differences       r?   durationzAttendanceActivity.duration   s    
 ~~T%8%8"*.."2"7"7"9D%\\^002DN$,,T-?-?O%--d.A.A4>>R,/@@,,..r>   c                 H    | j                         }| j                  |      }|S )zB
        Function to return the duration time in hh:mm:ss
        )r   r   )rU   r   formatted_durations      r?   duration_formatz"AttendanceActivity.duration_format   s&     !66}E!!r>   c                 l    | j                    d| j                   d| j                   d| j                   S )N - )rd   attendance_dater7   ru   r^   s    r?   __str__zAttendanceActivity.__str__   s9    ""#3t';';&<CcRVR`R`Qabbr>   N)%r8   r9   r:   r;   r   
ForeignKeyr   PROTECTrP   rd   	DateFieldr   r   r   
DO_NOTHINGrS   DateTimeFieldin_datetimer   	TimeFieldr7   r   out_datetimeru   r   objectsr@   rW   r_   rh   rq   rv   rx   r   r   r   r   r=   r>   r?   r%   r%   2   sP    $&##..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1 
~7
	9/ "cr>   r%   c                   J    e Zd ZdZ ej
                  d ed            Zd Zy)BatchAttendancez 
    Batch attendance model
       Title
max_lengthr*   c                 8    | j                    d| j                   S N-)titleidr^   s    r?   r   zBatchAttendance.__str__   s    **Qtwwi((r>   N)	r8   r9   r:   r;   r   	CharFieldrP   r   r   r=   r>   r?   r   r      s&     F!G*EE)r>   r   c            	       n    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j0                  d	 ed       ed            Z ej                  d	 ed            Z ej0                  d	 ed       ed            Z ej8                  d	ddeg ed             Z ej8                  ddeg ed!      "      Z ej                  e d	d	ej                   ed#            Z! ej8                  degd ed$      %      Z" ejF                  d ed&      '      Z$ ejF                  d ed(      '      Z% ejL                  d	d	)      Z' ejL                  d	d	 ed*      +      Z( ejL                  d,-      Z) ejF                  d ed.      '      Z* ejF                  dd/      Z+ ejF                  d ed0      '      Z, ejZ                  d	d1 ed2      3      Z. ej8                  d4d	ed5      Z/ ejF                  d-      Z0 ejb                  d	d6      Z2 ej                  e	ej                  d	d	 ed7      d8      Z3 e4d9:      Z5 e6d;e7g<      Z8d= Z9d> Z:d? Z;d@ Z<dA Z=dB Z> G dC dD      Z?dE Z@dF ZAdGeBfdHZCdI ZDdJ ZEdK ZFdL ZGdM ZHdN ZIdO ZJdP ZKdQ ZLdR ZMdS ZNdT ZOdU ZPdV ZQdW ZRdX ZSdY ZTdZ ZDd[ ZUdhd\ZVd] ZWd^ ZXd_ ZYd` ZZda Z[ fdbZ\dc Z] fddZ^de Z_df Z` fdgZa xZbS )i
Attendancez
    Attendance model
    c                 $    t        |   |i | y N)super__init__rU   argsrm   	__class__s      r?   r   zAttendance.__init__   s    $)&)r>   create_requestzCreate Requestupdate_requestzUpdate Requestcreated_requestzCreated 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 Approver   r*   zAttendance Validater,   r   zOvertime In Second)r,   r   r*   r   r   zIs validate request)r   editablezIs validate request approved   zRequest Description)r,   r   r*      )r   r,   choicesr   )r,   r   zApproved By)r(   r,   r   r*   r   r/   r0   history_set)r)   basesc                     | j                   S r   r   r^   s    r?   get_instance_idzAttendance.get_instance_id=      wwr>   c                 $    | j                   dk(  ryy )Nr   rt   )request_typer^   s    r?   rv   zAttendance.diff_cell@  s     116 2r>   c                      t        dd| i      S )zA
        This method for get custome coloumn for rating.
        z"cbv/attendance_request/status.htmlrZ   r[   r"   r^   s    r?   
status_colzAttendance.status_colD      
 5&
 	
r>   c                     | j                   j                  j                   d| j                   j                  j                   S z&
        Detail view subtitle
        z /
          rc   r^   s    r?   my_attendance_subtitlez!Attendance.my_attendance_subtitleN  F    
 $$77EEG H//??
@D 	Dr>   c                 8    t        dd| j                  i      }|S )
        detail view
        zmy-attendance-detailrk   rl   rn   ro   s     r?   my_attendance_detailzAttendance.my_attendance_detailV  s    
 ,dDGG_E
r>   c                 8    t        dd| j                  i      }|S )r   zattendances-tab-detail-viewrk   rl   rn   ro   s     r?   rq   z!Attendance.attendance_detail_view_  s    
 3T477OL
r>   c                   D    e Zd ZdZdZddgZg dZ ed      Z ed      Z	y)	Attendance.Metar4   )rd   r   )change_validateattendancezValidate Attendance)change_approveovertimezChange Approve Overtime)r5   r6   attendance_clock_inr   AttendancesN)
r8   r9   r:   r;   unique_togetherpermissionsr<   rP   r*   verbose_name_pluralr=   r>   r?   r@   r   h  s8    	 =@A


 .r>   r@   c                      y)z?
        Method to check the min ot for the attendance
        Nr=   r^   s    r?   check_min_otzAttendance.check_min_otz  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)rU   rT   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             r   )rd   employee_first_nameemployee_last_namer   r^   s    r?   r   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
        r   rd   )querycountr%   r   r   r   rd   r   rU   
activitiess     r?   r   zAttendance.activities  F     (//66 00d>N>N 7 

 $j.>.>.@AAr>   c                      t        dd| i      S )z;
        method for rendering actions(edit,delete)
        z'cbv/attendances/attendance_actions.htmlrZ   r[   r"   r^   s    r?   attendance_actionszAttendance.attendance_actions  s    
 :&
 	
r>   c                      t        dd| i      S )A
        This method for get custom coloumn for comment.
        z#cbv/attendance_request/comment.htmlrZ   r[   r"   r^   s    r?   comment_colzAttendance.comment_col  s    
 6&
 	
r>   c                      t        dd| i      S )zZ
        this method is used to return attendance detail view activity custom col
        z-cbv/attendances/detail_view_activity_col.htmlrZ   r[   r"   r^   s    r?   attendance_detail_activity_colz)Attendance.attendance_detail_activity_col      
 @&
 	
r>   c                      t        dd| i      S )r   z+cbv/attendance_request/request_actions.htmlrZ   r[   r"   r^   s    r?   request_actionszAttendance.request_actions      
 >&
 	
r>   c                 8    t        dd| j                  i      }|S )-
        detail view of validate tab
        zvalidate-detail-viewrk   rl   rn   ro   s     r?   validate_detail_viewzAttendance.validate_detail_view  s     ,dDGG_E
r>   c                 8    t        dd| j                  i      }|S )r	  zindividual-validate-detail-viewrk   rl   rn   ro   s     r?   individual_validate_detail_viewz*Attendance.individual_validate_detail_view  rr   r>   c                 8    t        dd| j                  i      }|S )z'
        detail view of OT tab
        zot-detail-viewrk   rl   rn   ro   s     r?   ot_detail_viewzAttendance.ot_detail_view  s     &dgg?
r>   c                 8    t        dd| j                  i      }|S )z.
        detail view of validated tab
        zvalidated-detail-viewrk   rl   rn   ro   s     r?   validated_detail_viewz Attendance.validated_detail_view  s     -tTWWoF
r>   c                 8    t        dd| j                  i      }|S )z6
        deteil view of requested attendances
        zvalidate-attendance-requestattendance_idrl   rn   ro   s     r?   detail_viewzAttendance.detail_view  s     3_dgg<VW
r>   c                 8    t        dd| j                  i      }|S )
        Edit url
        zupdate-attendance-requestrk   rl   rn   ro   s     r?   change_attendancezAttendance.change_attendance  s     14/J
r>   c                     t        d      }t        j                  j                         }|t        |j                        }t        d| |d      S )z1
        method for rendering approve OT
        00:30z$cbv/attendances/ot_confirmation.htmlrZ   minotr[   r   AttendanceValidationConditionr   r   minimum_overtime_to_approver#   rU   r  	conditions      r?   
ot_approvezAttendance.ot_approve  sP      (199??A	 #I$I$IJE7!%6
 	
r>   c                      t        dd| i      S )z5
        detail view actions of validate tab
        z(cbv/attendances/validate_tab_action.htmlrZ   r[   r"   r^   s    r?   validate_detail_actionsz"Attendance.validate_detail_actions      
 ;&
 	
r>   c                     t        d      }t        j                  j                         }|t        |j                        }t        d| |d      S )z/
        detail view actions of OT tab
        r  z"cbv/attendances/ot_tab_action.htmlr  r[   r  r  s      r?   ot_detail_actionszAttendance.ot_detail_actions  sP    
  (199??A	 #I$I$IJE5!%6
 	
r>   c                      t        dd| i      S )6
        detail view actions of validated tab
        z)cbv/attendances/validated_tab_action.htmlrZ   r[   r"   r^   s    r?   validated_detail_actionsz#Attendance.validated_detail_actions  s    
 <&
 	
r>   c                      t        dd| i      S )r'  z$cbv/attendances/validate_button.htmlrZ   r[   r"   r^   s    r?   validate_buttonzAttendance.validate_button  s    
 7&
 	
r>   c                     | j                   j                  j                   d| j                   j                  j                   S rb   rc   r^   s    r?   attendances_detail_subtitlez&Attendance.attendances_detail_subtitle%  ri   r>   c                     t         j                  j                  | j                  | j                        }||j                         dS r   r   r   s     r?   r   zAttendance.activities+  r   r>   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)rU   r3  datadiffss       r?   requested_fieldszAttendance.requested_fields4  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
        )rd   r   clock_out__isnullr   )r%   r   r   rd   r   order_bylast)rU   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   )r   minutesecond)r%   r   r   r   rd   r9  r   r   ru   r   dtr   r   r>  r?  r   r7   r   r   )
rU   r   at_work_secondsr   activityout_timecombined_outin_timecombined_inr5  s
             r?   get_at_work_from_activitiesz&Attendance.get_at_work_from_activitiesJ  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   r   )r   minimum_hourattendance_worked_hourr   )rU   minimum_hoursworked_hourpending_secondspending_hourss        r?   hours_pendingzAttendance.hours_pendinga  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.
        r   TN)r   r   r   rI  r^   s    r?   adjust_minimum_hourzAttendance.adjust_minimum_hourm  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   rJ  rI  attendance_overtimeat_work_secondovertime_secondr^   s    r?   update_attendance_overtimez%Attendance.update_attendance_overtimeu  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)r  r   r   is_validate_requestis_validate_request_approvedattendance_validatedovertime_cutoffr   rV  r   rT  auto_approve_otr  attendance_overtime_approve)rU   r  cutoff_secondss      r?   handle_overtime_conditionsz%Attendance.handle_overtime_conditions  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)rT   F)rk   %Bmonthyearr   )rW  r   r   rR   r   strftimelowerr   rQ  r`  rk   r   r^  rd   employee_overtimer   re  r   	update_ot	create_otrV  approved_overtime_secondr   overtimesaver   
first_save)
rU   r   rm   prev_attendance_approved
prev_stateemployee_otapprovedattendance_accounttotal_ot_secondsr   s
            r?   rm  zAttendance.save  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
         )rd   r   attendance_clock_in_dater   attendance_clock_outattendance_clock_out_dater   work_type_idrJ  rI  batch_attendance_id)rd   r   strr   rw  r   rx  ry  r   rz  rJ  rI  r{  )rU   serialized_datas     r?   r2  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   rb  %Yrc  )
contextlibsuppress	Exceptionr%   r   r   r   rd   deleterh  rf  rg  existsri  r   r   )rU   r   rm   rq  r   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.
        rb  rd   rd  re  )AttendanceOverTimer   get_or_createrd   r   rf  rg  re  r^  rT  rl  r[  rJ  hour_accountrm  )rU   rq  createds      r?   rj  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
        leaverr  )start_date__lteend_date__gtestatus)attendance_date__rangeT)rd   attendance_date__monthattendance_date__yearr[  rI  rU  r   )r   is_installedrd   leaverequest_setr   r   r	   
start_dateend_dater   r   rd  re  excludevaluesr   minr   worked_hoursrN  rm  )rU   rq  approved_leave_requestsexclude_conditionr  month_attendanceshour_balanceminimum_hour_second
attendancerequired_work_secondrU  rO  s               r?   ri  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                 d   t        |   |i | t        j                         j	                         }t        j
                         j                         }t        | j                  t              r/t        j                  | j                  d      j	                         }n| 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 )Nz%H:%M:%Srw  z?Attendance check-in date cannot be earlier than attendance datery  z>Attendance check-out date cannot be earlier than check-in daterx  z&Check-out time cannot be in the future)r   cleanr   r   r   r   r   
isinstancerx  r|  strptimerw  r   r   ry  )rU   r   rm   r   r   rC  r   s         r?   r  zAttendance.clean<  s"   t&v&lln!!# %%' d//5(()B)BJOTTVH00H((4+?+??!.0q  **..1N1NN!/1q  ))d.L.LPU.U#~%+-UV   /V)r>   )F)cr8   r9   r:   r;   r   rP   r  r   r   r   r   rd   r   r   r   r   SET_NULLr   r   rz  r   r   r   rw  r   r   ry  rx  r   r   rJ  rI  r   r{  rT  BooleanFieldr^  r[  IntegerFieldrU  rV  rk  rY  is_bulk_requestrZ  	TextFieldrequest_descriptionr   r   	JSONFieldr/  approved_byr   r   r!   r    historyr   rv   r   r   r   rq   r@   r   r   r|  r   r   r   r  r  r  r
  r  r  r  r  r  r   r"  r%  r(  r*  r,  r6  r<  rG  rO  rQ  rW  r`  rm  r2  r  rj  ri  r  __classcell__r   s   @r?   r   r      s   * 
1-./	1-./	A/01F $&##..+z]K 'f&&,-()O
 !v  t!G*H %6$$//{^L 'V&&##'(	N  0v/// 2  +&***;P9Q !1 0 0"2 3! ,6+++!<Q:R .V--()~& $6##()~&	L ,&++..)* +&**()z]	 #6&"5"5A&8$9# /6..A&;$< )V((d$?N)f))A.B,CO  3v221=-&--A&;$< *f))%%HO#66#6#6A&D$E$  +&**c2G0H $6##D&:JL %$$U3J%V%%4%@N#&##..}%K $KG "
G7
D/ /$

'L LB








~B		!.
#I 8&+ Z2($.3j   r>   r   c                   2    e Zd Z ej                  e      Zy)AttendanceRequestFile)	upload_toN)r8   r9   r:   r   	FileFieldr   filer=   r>   r?   r  r  _  s    6k2Dr>   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)r   Commentr   )r,   r*   r   r   c                     | j                    S r   )commentr^   s    r?   r   z AttendanceRequestComment.__str__m  s    ,, r>   N)r8   r9   r:   r;   r   r   r   CASCADE
request_idr   rd   ManyToManyFieldr  filesr  rP   r  r|  r   r=   r>   r?   r  r  c  su     #"":HJ#&##HGK"F""#8EEfDq|PSTG! !r>   r  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!d" Z"d# Z#d$ Z$d% Z%d& Z&d' Z'd( Z( fd)Z) xZ*S )*r  z"
    AttendanceOverTime model
    rh  r   r'   r   Monthr   r   r   r  TYear)r   r,   r   r*   r   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 ed      Z ed      Zy)AttendanceOverTime.Metar4   r  z-yearz-month_sequencezHour AccountzHour AccountsN)	r8   r9   r:   r;   r   r<   rP   r*   r   r=   r>   r?   r@   r    s/    	 A./(0r>   r@   c                 6    | j                   j                         S )z"
        capitalize month
        )rd  
capitalizer^   s    r?   get_month_capitalizedz(AttendanceOverTime.get_month_capitalized  s     zz$$&&r>   c                 8    t        dd| j                  i      }|S )r  zattendance-overtime-updateobj_idrl   rn   ro   s     r?   edit_url_overtimez$AttendanceOverTime.edit_url_overtime      
 2Hdgg;NO
r>   c                 8    t        dd| j                  i      }|S )z
        delete url
        zattendance-overtime-deleter  rl   rn   ro   s     r?   delete_url_overtimez&AttendanceOverTime.delete_url_overtime  r  r>   c                      t        dd| i      S )*
        actions in hour account

        z"cbv/hour_account/hour_actions.htmlrZ   r[   r"   r^   s    r?   hour_actionszAttendanceOverTime.hour_actions  s     5&
 	
r>   c                     | j                   j                  j                   d| j                   j                  j                   S r   rc   r^   s    r?   hour_account_subtitlez(AttendanceOverTime.hour_account_subtitle  r   r>   c                 8    t        dd| j                  i      }|S )r   zhour-account-detail-viewrk   rl   rn   ro   s     r?   hour_account_detailz&AttendanceOverTime.hour_account_detail  s    
 0$I
r>   c                      t        dd| i      S )z6
        actions in hour account detail view

        z(cbv/hour_account/hour_detail_action.htmlrZ   r[   r"   r^   s    r?   hour_detail_actionsz&AttendanceOverTime.hour_detail_actions  s     ;&
 	
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  re  z3Year must be an integer value between 1900 and 2100)r~   re  r   rP   
ValueError	TypeError)rU   re  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_sequencer~   re  r   r   )rU   rd  re  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  rd   r[  rU  Tflatsumlistr   r   r   r   rd  re  rd   values_listr   )rU   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  rd   r[  r^  rV  r  r  )rU   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   rd  r^   s    r?   get_month_indexz"AttendanceOverTime.get_month_index2  s     TZZ((r>   c                 0   t        | j                        | _        t        | j                        | _        t        | j
                        | _        | j                  j                  d      d   }g d}|j                  |      | _
        t        | 0  |i | y )Nr   r   )januaryfebruarymarchaprilmayjunejulyaugust	septemberoctobernovemberdecember)r   r  hour_account_secondrN  hour_pending_secondrl  rV  rd  splitindexr  r   rm  )rU   r   rm   
month_namemonthsr   s        r?   rm  zAttendanceOverTime.save8  s~    #243D3D#E #243E3E#F .t}}=ZZ%%c*1-

 %ll:6d%f%r>   )+r8   r9   r:   r;   r   r   r   r   rP   rd   r   rd  PositiveSmallIntegerFieldr  r   r   rf  re  r   r  rN  rl  r  r  r  rV  r   r   r@   r  r  r  r  r  r  r  r  r  r  r  r  rm  r  r  s   @r?   r  r  q  s    $&##..(z]	K FwZE 6V55a@N6''-vY	D $6##()~&L %F$$()'M  v()'(	H .&--'(
 .&--()
 *f)))*O
 $KG1 1'	
D	

$, +")& &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 Zd Zd Zd Zd Zd Zd ZdefdZ xZ S )!AttendanceLateComeEarlyOutz*
    AttendanceLateComeEarlyOut model
    	late_come	Late Come	early_out	Early Outlate_come_early_outr   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   r^   s    r?   get_penalties_countz.AttendanceLateComeEarlyOut.get_penalties_countm  s     ''--//r>   r   c                 x    t        |   |i | | j                  j                  | _        t        |   |i | y r   )r   rm  r  rd   r   s      r?   rm  zAttendanceLateComeEarlyOut.saves  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   r  typez-attendance_id__attendance_dateN)r8   r9   r:   r;   r   r<   r=   r>   r?   r@   r  x  s    	 ,v756r>   r@   c                 ~    dt        d      fdt        d      fg}t        |      j                  | j                        S )z#
        Display work type
        r  r  r  r  )rP   rQ   rR   r  )rU   r   s     r?   get_typez#AttendanceLateComeEarlyOut.get_type  s>    
 !K.)!K.)
 G}  ++r>   c                      t        dd| i      S )z$
        To get penalities

        z)cbv/late_come_and_early_out/penality.htmlrZ   r[   r"   r^   s    r?   penalities_columnz,AttendanceLateComeEarlyOut.penalities_column  s     <&
 	
r>   c                      t        dd| i      S )r  z/cbv/late_come_and_early_out/actions_column.htmlrZ   r[   r"   r^   s    r?   actions_columnz)AttendanceLateComeEarlyOut.actions_column  s     B&
 	
r>   c                      t        dd| i      S )r  z.cbv/late_come_and_early_out/detail_action.htmlrZ   r[   r"   r^   s    r?   detail_actionsz)AttendanceLateComeEarlyOut.detail_actions  s     A&
 	
r>   c                     | j                   j                  j                   d| j                   j                  j                   S r   rc   r^   s    r?   late_come_subtitlez-AttendanceLateComeEarlyOut.late_come_subtitle  r   r>   c                 8    | j                   j                  dk(  ryy)NTYesNo)r  r[  r^   s    r?   attendance_validated_checkz5AttendanceLateComeEarlyOut.attendance_validated_check  s    22d:r>   c                 8    t        dd| j                  i      }|S )r   zlate-in-early-out-single-viewrk   rl   rn   ro   s     r?   late_come_detailz+AttendanceLateComeEarlyOut.late_come_detail  s    
 5tTWWoN
r>   c                     | j                   j                  j                   d| j                   j                  j                   d| j                   S r   )r  rd   r   r   r  r^   s    r?   r   z"AttendanceLateComeEarlyOut.__str__  sN    $$00DDE F++>>?s499+O 	Or>   )r   N)!r8   r9   r:   r;   rP   r   r   r   r   r   r  r   r   rd   r   r  r   r   r   
created_atr  rm  r@   r  r  r  r  r  r  r!  r|  r   r  r  s   @r?   r  r  O  s   
 
an%	an%G
 &F%%..*|_	M $&####*z]K 6r76SD#KG &%%4dCJ0&
7 7,	
	
	
DO Or>   r  c                   4    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d Z xZS )r  z-
    AttendanceValidationCondition model
    r   zWorked Hours Auto Approve Till)r   r-   r*   T)r   r,   r   r-   FzAuto Approve OTr   r   r   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)r   r  r   r  r   r  r   rP   rU   r   s    r?   r  z#AttendanceValidationCondition.clean  s@     	ww8@@GGI!!$E"FGG Jwr>   c                      t        dd| i      S )r  z$cbv/settings/break_point_action.htmlrZ   r[   r"   r^   s    r?   break_point_actionsz1AttendanceValidationCondition.break_point_actions  s     7&
 	
r>   )r8   r9   r:   r;   r   r   r   rP   validation_at_workr  r\  r  r]  r  r   
company_idr   r   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	
r>   r  c                       e Zd ZdZ ej
                  degd ed            Z ej                         Z
 ej                  d ed       ed      	      Z ej                  d
 ed       ed      	      Z ej                  d
      Z ej                  ed ed            Z e       ZdefdZd Zd Zd Zd Zd Zd Z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 AttendancezAllowed Clock-In)r   r   r*   Fz2Allcocate this grace time for Check-Out AttendancezAllowed Clock-Outr   r   r%  r   c                 2    t        | j                   d      S )Nz - Hours)r|  allowed_timer^   s    r?   r   zGraceTime.__str__  s    d''(122r>   c                     | j                   S r   r   r^   s    r?   r   zGraceTime.get_instance_id  r   r>   c                      t        dd| i      S )7
        This method for get custome coloumn .
        z*cbv/settings/is_active_col_grace_time.htmlrZ   r[   r"   r^   s    r?   is_active_colzGraceTime.is_active_col  r`   r>   c                      | j                    dS )z"
        Allowed time col
        z Hoursr0  r^   s    r?   allowed_time_colzGraceTime.allowed_time_col  s     ##$F++r>   c                      t        dd| i      S )r3  z+cbv/settings/grace_time_default_action.htmlrZ   r[   r"   r^   s    r?   
action_colzGraceTime.action_col"  r  r>   c                      t        dd| i      S )5
        This method for get custom column .
        z,cbv/settings/applicable_on_clock_in_col.htmlrZ   r[   r"   r^   s    r?   applicable_on_clock_in_colz$GraceTime.applicable_on_clock_in_col,  s    
 ?&
 	
r>   c                      t        dd| i      S )r;  z-cbv/settings/applicable_on_clock_out_col.htmlrZ   r[   r"   r^   s    r?   applicable_on_clock_out_colz%GraceTime.applicable_on_clock_out_col6  r  r>   c                      t        dd| i      S )r;  z"cbv/settings/grace_time_shift.htmlrZ   r[   r"   r^   s    r?   get_shifts_displayzGraceTime.get_shifts_display@  r   r>   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_defaultr   z2There is already a default grace time that exists.r6  r0  z?There is already an existing grace time with this allowed time.N)r   r  rB  r.  r   r   r  r   r  r   rP   r0  )rU   r0  rB  exclude_defaultr   s       r?   r  zGraceTime.cleanJ  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 )Nr}   r|   rz   r{   )r0  r  r~   allowed_time_in_secsr   rm  )rU   r   rm   r0  hoursr   secs	hours_intminutes_intsecs_int	hours_strminutes_strsecs_strr   s                r?   rm  zGraceTime.saveg  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   r   r   rP   r0  r  rE  r  allowed_clock_inallowed_clock_outrB  r  r   r,  r   r   r|  r   r   r4  r7  r9  r<  r>  r@  r  rm  r  r  s   @r?   r.  r.    s	    $6##,-~&	L /6..0*v**GH)*
 ,++HI*+
 %$$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d Zd	 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*   r   )r(   r,   c                 H    | j                   r| j                   j                  S y)NzAll Company)r,  companyr^   s    r?   company_colz$AttendanceGeneralSetting.company_col  s    ????*** r>   c                      t        dd| i      S )z6
        This method for get custom coloumn .
        z(cbv/settings/check_in_check_out_col.htmlrZ   r[   r"   r^   s    r?   check_in_check_out_colz/AttendanceGeneralSetting.check_in_check_out_col  r#  r>   N)r8   r9   r:   r;   r   r  time_runnerrP   enable_check_inr   r   r  r,  r   r   rT  rV  r=   r>   r?   rQ  rQ  x  sr     &&%%d3K)f))23t
O #""7fnn4PJ#%G!
r>   rQ  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j                  eej8                  dd      Z ej2                  d      Z ej@                  d       r  ej                  d!ej8                  dd      Z! ej                  e"ej8                  dd      Z# ejH                  d      Z% ejL                  dd      Z' e(d"      Z)d# Z* fd$Z+ fd%Z,d& Z- G d' 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,   r   r   )r   r,   r   r   )r(   r*   r   r   )r,   r   r-   r   r   r   )r,   r   r   r   )r      Fr   )r(   r   r,   r  zleave.LeaveRequestr/   c                     | j                   }|dk(  r1t        j                  d      r|d| j                  j                   z  }|S )NLeaver  z | )messager   r  leave_request_idleave_type_id)rU   title_messages     r?   rl  zWorkRecords.title_message  sE    G#  )3t'<'<'J'J&K!LLr>   c                 V    t        j                         | _        t        |   |i | y r   )r   r   last_updater   rm  r   s      r?   rm  zWorkRecords.save  s"    #<<>d%f%r>   c                 x    t         |           d| j                  cxk  rdk  sn t        t	        d            y )Ng        g      ?z*Day percentage must be between 0.0 and 1.0)r   r  day_percentager   rP   r(  s    r?   r  zWorkRecords.clean  s5    d))0S0!!$P"QRR 1r>   c                     | j                   | j                   S | j                   d| j                   d| j                   S r   )record_namework_record_typer   rd   r^   s    r?   r   zWorkRecords.__str__  sM     + 	
 ))*!DII;a8H8H7IJ	
r>   c                   ,    e Zd Z ed      Z ed      Zy)WorkRecords.MetazWork RecordzWork RecordsN)r8   r9   r:   rP   r*   r   r=   r>   r?   r@   ru    s    '/r>   r@   )0r8   r9   r:   r;   rP   r   r   r   rr  rs  r   r   r  rd   r   r   r   at_workmin_hourr  rU  min_hour_secondr  noteri  r  is_attendance_recordr   r  r  is_leave_recordr   r  rj  r   r   
FloatFieldrp  r   rn  r   r   rl  rm  r  r   r@   r  r  s   @r?   rZ  rZ    sW   
 
)	$%&	(	q()*	:	'
G #&""cDIK'v''2D'R#&##FNN:K 6T2Df 
 G  v 
 H )V((d$JN)f))t4KO6s+Df"4tDG.6..u=%F%%fooTM *f))%8Ot!,6,, oo	
 !v  4H 'V&&q1N&&&&D=K#$QRG&
S

0 0r>   rZ  )Dr;   r  r   r@  r0  r   r   pandaspddjango.appsr   django.core.exceptionsr   	django.dbr   django.db.modelsr	   django.urlsr
   django.utilsr   django.utils.translationr   rP   attendance.methods.utilsr   r   r   r   r   r   r   r   base.horilla_company_managerr   base.methodsr   r   base.modelsr   r   r   r   employee.modelsr   horilla.methodsr   horilla.modelsr   r   horilla_audit.modelsr    r!   horilla_views.cbv_methodsr#   _validate_time_in_minutesr%   r   r   r  r  r  r  r  r.  rQ  ModelrZ  r=   r>   r?   <module>r     s      . .   2    ! 6	 	 	 ? 5 J J $ 4 4 B 5 5 Ac AcH)l )[
 [
|3L 3!| ![& [&|uO uOp'
L '
TD& D&N
| 
BW0&,, W0r>   