
    siTD                    n   d dl Z d dl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 d dlmZ d d	lmZ d d
lmZ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* d dl+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; d dl<m=Z= d dl>m?Z?m@Z@mAZAmBZB d dlCmDZD  ej                  eF      ZGej                  ej                  ej                  ej                  ej                  ej                  ej                  dZOd e d      fd e d      fd e d       fgZPd!d"gZQd# e d$      fd% e d&      fd' e d(      fgZRd) e d*      fd+ e d,      fd- e d.      fd/ e d0      fd1 e d2      fd3 e d4      fd5 e d6      fd7 e d8      fd9 e d:      fd; e d<      fd= e d>      fd? e d@      fgZSg dA e dB      fdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`daZTdb e dc      fdd e de      fdf e dg      fgZUdh e di      fdj e dk      fgZVdl e dm      fdn e do      fdp e dq      fgZWdr e ds      fdt e du      fgZXdv e dw      fdx e dy      fdz e d{      fd| e d}      ffZYdv e dw      fdx e dy      fd| e d}      ffZZd~ e d      fd) e d      fd+ e d      fd- e d      fd/ e d      fgZ[d~ e d      fd) e d      fd+ e d      fd- e d      fd/ e d      fd1 e d      fd3 e d      fgZ\ G d de5      Z] G d de5      Z^ G d de5      Z_ G d de5      Z`d Zad Zbd Zc G d de5      Zd G d dej                        Zf G d de5      Zg G d de5      Zh G d de5      Zi G d dej                        Zj G d de5      Zk ej                  d      r G d de5      Zm G d de5      Zn ej                  d      r G d de5      Zo G d de5      Zp ej                  d      r G d ded      Zqyy)    N)datedatetime	timedelta)relativedelta)apps)settings)messages)ValidationError)default_storage)models)QSum)reversereverse_lazy)timezone)gettext_lazy)HorillaCompanyManager)CompanyCompanyLeaves
DepartmentHolidaysJobPositionMultipleApprovalConditionclear_messages)EmployeeEmployeeWorkInformation)horilla_middlewares)_thread_locals)get_horilla_model_class)HorillaModelupload_pathget_diff)HorillaAuditInfoHorillaAuditLogrender_template)calculate_requested_dayscompany_leave_dates_list filter_conditional_leave_requestholiday_dates_list)LeaveClashThread)equalnotequalltgtlege	icontainsfull_dayzFull Day
first_halfz
First Halfsecond_halfzSecond Half)yesYes)noNoyearlyYearlymonthlyMonthlyweeklyWeekly1Jan2Feb3Mar4Apr5May6Jun7Jul8Aug9Sep10Oct11Nov12Declast dayzLast Day)rA   1st)rC   2nd)rE   3rd)rG   4th)rI   5th)rK   6th)rM   7th)rO   8th)rQ   9th)rS   10th)rU   11th)rW   12th)1313th)1414th)1515th)1616th)1717th)1818th)1919th)2020th)2121th)2222th)2323th)2424th)2525th)2626th)2727th)2828th)2929th)3030th)3131thdayDaymonthMonthyearYearpaidPaidunpaidUnpaidno carryforwardzNo Carry ForwardcarryforwardzCarry Forwardcarryforward expirezCarry Forward with Expirejob_positionJob Positionjob_rolezJob Role	requested	RequestedapprovedApproved	cancelled	CancelledrejectedRejected0z
First WeekzSecond Weekz
Third WeekzFourth Weekz
Fifth WeekMondayTuesday	WednesdayThursdayFridaySaturdaySundayc            
           e Zd Z ej                  dde ed            Z ej                  dd ed            Z	 ej                  dd ed      	      Z
 ej                  ded
 ed            Z ej                  dd      Z ej                  ded      Z ej"                  d ed            Z ej                  dd      Z ej"                  d ed            Z ej"                  d ed            Z ej                  dedd ed            Z ej                  ded ed            Z ej                  dedd ed            Z ej                  ddde ed            Z ej                  ded ed            Z ej                  dd ed       !      Z  ejB                  dd ed"      !      Z" ej                  dedd ed#            Z# ejH                  dd ed$      !      Z% ej                  de&ddd% ed&      '      Z' ej                  de&d(dd ed)      *      Z( ej                  de&d( ed+            Z) ej                  de&d( ed,            Z* ej"                  d-      Z+ ejX                  e-ddej\                  .      Z/ e0d/0      Z1 G d1 d2      Z2d3 Z3d4 Z4d5 Z5 fd6Z6 fd7Z7d8 Z8d9 Z9d: Z:d; Z;d< Z<d= Z=d> Z>d? Z?d@ Z@dA ZAdB ZB xZCS )C	LeaveTypeTIconnullblank	upload_toverbose_name   FName
max_lengthr   r   Color)r   r   r   r   zIs Paidr   choicesdefaultr      )r   r   r   r   r   r   zLimit Leave Daysr   r   ResetzIs EncashablezReset Period)r   r   r   r   r   zReset Month)r   r   r   r   z	Reset Day)r   r   r   r   r   
   zReset Weekday)r   r   r   r   r   r   zCarryforward TypezCarryforward Maxr   r   r   zCarryforward Expire InzCarryforward Expire PeriodzCarryforward Expire Dater7   zRequire Approval)r   r   r   r   r   r   r9   zRequire Attachment)r   r   r   r   r   r   zExclude Company HolidayszExclude Holidaysr   r   editable	on_delete
company_idrelated_company_fieldc                       e Zd ZdgZy)LeaveType.Meta-idN__name__
__module____qualname__ordering     0/var/www/python-projects/worksol/leave/models.pyMetar      s	    7r   r   c                     d| j                    d}| j                  rA| j                  j                   }t        j                  |      r| j                  j                  }|S zV
        Method will retun the api to the avatar or path to the profile image
        !https://ui-avatars.com/api/?name=&background=random)nameiconr   existsurlselfr   full_filenames      r   
get_avatarzLeaveType.get_avatar  sK     2$))<NO99 IINNM%%m4iimm
r   c                    t        j                         j                         | j                  r| j                  sy fd}| j
                  dk(  rt        | j                         |t        | j                        | j                        }}t        j                  t        j                  ||      j                         k  z   ||      j                         }|S | j
                  dk(  rj                  }t        j                  | ||| j                              j                         }|k  rF|dz  dz   }j                  |dk(  z   }t        || ||| j                              j                         }|S | j
                  dk(  r>t        | j                     }|j                         z
  dz  xs d}t        |      z   }|S d }|S )	Nc                 j    |dk(  r#t        j                  j                  |       d   S t        |      S )NrY   r   )calendar
monthranger   int)r   r   todays     r   get_reset_dayz;LeaveType.leave_type_next_reset_date.<locals>.get_reset_day  s;     *$ ##EJJ6q9 Xr   r;   r=      r   r?      days)r   nowr   reset	reset_dayreset_basedr   reset_monthr   r   	WEEK_DAYSweekdayr   )	r   r   r   r   
reset_dater   target_weekdaydays_until_resetr   s	           @r   leave_type_next_reset_datez$LeaveType.leave_type_next_reset_date  s   ##%zz	 x'T--.D$$%t~~13E "

huzz5#>CCEMN df	 4 ) *KKE!

E=#Gdf  E!q(zzUaZ0%%udnn!E$&   )&t~~6N . @AEJ0@!AAJ
  Jr   c                     | j                   }| j                  dk(  r|t        |      z   }|S | j                  dk(  r|t        |      z   }|S |t        |      z   }|S )Nr   r   r   monthsyears)carryforward_expire_incarryforward_expire_periodr   )r   assigned_dateperiodexpired_dates       r   set_expired_datezLeaveType.set_expired_date;  sp    ,,**e3(=f+EEL  ,,7(=+GGL  )=v+FFLr   c                     t         |   |        | j                  r_t        j                  j                  d      j                  | j                        j                         rt        dt        d      i      y y )NTis_compensatory_leave)pkr   z*Compensatory Leave Request already exists.)supercleanr  r   objectsfilterexcluder  r   r
   _r   argskwargs	__class__s      r   r  zLeaveType.cleanF  sr    d%%!!((t(DDGG$ &QKLM 	 &r   c                    t        t        j                  dd       }|j                  j	                  d      }| j
                  s-| j                  s!|r|dk7  rt        j                  |      | _        | j                  dk7  r!| j                  t        j                  | _
        | j                  r;t        j                  j	                  | j                        j                   rd| _        | j                  dk(  r.| j"                  s"| j%                  | j&                        | _        n| j                  dk7  rd | _        t(        | U          y )	Nrequestselected_companyallr   idTr   )r   )getattrr   r   sessiongetr  r   r   findcarryforward_typecarryforward_maxmathinfr  r   r  r  carryforward_expire_dater  
created_atr  save)r   r  r  r  r  r  s        r   r   zLeaveType.saveR  s   -<<iN"??../ABOO  E)%ll+;<DO ""&77%%-$(HHD!77y((,,,8NN)-D& ""&;;11,0,A,A"oo -B -D) ##'<<,0D)r   c                     | j                   S Nr   r   s    r   __str__zLeaveType.__str__q      yyr   c                      t        dd| i      S )z'
        actions for list view
        z,cbv/leave_types/leave_type_list_actions.htmlinstancepathcontextr&   r$  s    r   leave_list_actionszLeaveType.leave_list_actionst      
 ?&
 	
r   c                      t        dd| i      S )z*
        reset col in detail view
        z'cbv/leave_types/leave_detail_reset.htmlr(  r)  r&   r$  s    r   leave_detail_resetzLeaveType.leave_detail_reset~  s     :ZQUDV
 	
r   c                      t        dd| i      S )z1
        carryforward col in detail view
        z.cbv/leave_types/leave_detail_carryforward.htmlr(  r)  r&   r$  s    r   leave_detail_carryforwardz#LeaveType.leave_detail_carryforward       A&
 	
r   c                     t        d      }|S )z/
        This method to get create url
        ztype-creationr   r   r   s     r   get_create_urlzLeaveType.get_create_url  s    
 ?+
r   c                 8    t        dd| j                  i      }|S )z/
        This method to get assign url
        z
assign-oner  r  r   r  r5  s     r   get_assign_urlzLeaveType.get_assign_url  s    
 <tww@
r   c                 8    t        dd| j                  i      }|S )z'
        for to get update url
        ztype-updater  r8  r9  r5  s     r   get_update_urlzLeaveType.get_update_url  s    
 =$A
r   c                     t        d      }|S )z/
        This method to get delete url
        zgeneric-deleter4  r5  s     r   get_delete_urlzLeaveType.get_delete_url  s     +,
r   c                 8    t        dd| j                  i      }|S )
        detail view
        zleave-type-detail-viewr  r8  r   r  r5  s     r   leave_detail_viewzLeaveType.leave_detail_view  s    
 .dggG
r   c                 "    | j                   rdS dS )z&
        encashable condition
        r8   r:   )is_encashabler$  s    r   
encashablezLeaveType.encashable  s     **u44r   c                      t        dd| i      S )%
        detail view actions
        z#cbv/leave_types/detail_actions.htmlr(  r)  r&   r$  s    r   detail_view_actionszLeaveType.detail_view_actions  s     6T@R
 	
r   )Dr   r   r   r   
ImageFieldr!   r  r   	CharFieldr   colorPAYMENTpayment
FloatFieldcountTIME_PERIOD	period_inBooleanFieldlimit_leave
total_daysr   rD  RESET_BASEDr   MONTHSr   DAYSr   r   reset_weekendCARRYFORWARD_TYPEr  r  IntegerFieldr   r   	DateFieldr  CHOICESrequire_approvalrequire_attachmentexclude_company_leaveexclude_holidayr  
ForeignKeyr   PROTECTr   r   r  r   r   r   r  r  r   r%  r,  r/  r1  r6  r:  r<  r>  rB  rE  rH  __classcell__r  s   @r   r   r      s   61V9D 6rAfINDF$2AgJOEfwq|G F43E   BUSI%&%%dCUAVWK"""a8JFAgJGE'F''AoDVWM"&""~&K #&""vT-@PK !  t$d;I %F$$'M )((!!*+	 )v((A.@,A 1V00A.F,G "2!1!134"  0v//A.H,I  (v'')* *))+, -F,,12	 'f&&w1EWCXO 0F//>"""dUfnnJ $,GG 
+Z	
>


"5
r   r   c                   N   e Zd Z ej                  dd ed            Z ej                   ed            Z ej                  dd ed      	      Z	 ej                  d ed
            Z ej                  eddej                        Z ed      Zd Zd Zd Zd Zd Zy)Holidayr   Fr   r   
Start Dater   TEnd Dater   	Recurringr   r   r   r   c                     | j                   S r"  r#  r$  s    r   r%  zHoliday.__str__  r&  r   c                 8    t        dd| j                  i      }|S )r@  zholiday-detail-viewr  r8  rA  r5  s     r   detail_viewzHoliday.detail_view  s    
 +T477OD
r   c                      t        dd| i      S )rG  z%cbv/holidays/detail_view_actions.htmlr(  r)  r&   r$  s    r   rH  zHoliday.detail_view_actions  s     8&
 	
r   c                 "    | j                   rdS dS )z 
        recurring data
        r8   r:   )	recurringr$  s    r   get_recurring_statuszHoliday.get_recurring_status  s     u0D0r   c                      t        dd| i      S )z;
        method for rendering actions(edit,delete)
        z"cbv/holidays/holidays_actions.htmlr(  r)  r&   r$  s    r   holidays_actionszHoliday.holidays_actions  s    
 5&
 	
r   N)r   r   r   r   rJ  r  r   r[  
start_dateend_daterR  rp  ra  r   rb  r   r   r  r%  rm  rH  rq  rs  r   r   r   rf  rf    s    6rAfIND!!!q?JvTAjMRH###E+OI"""dUfnnJ $,GG
1
r   rf  c                       e Zd Z ej                  dedd      Z ej                  de      Z ej                  e
ddej                        Z ed      Z G d	 d
      Zd Zd Zd Zd Zd Zd Zd Zd Zy)CompanyLeaved   T)r   r   r   r   )r   r   Fr   r   r   c                       e Zd ZdZy)CompanyLeave.Meta)based_on_weekbased_on_week_dayNr   r   r   unique_togetherr   r   r   r   rz    s    @r   r   c                     t        t              j                  | j                         dt        t              j                  | j
                         S Nz | )dictr   r  r|  WEEKSr{  r$  s    r   r%  zCompanyLeave.__str__  s>    y/%%d&<&<=>c$u+//RVRdRdBeAfggr   c                 *    t        d| t        d      S )z%
        custom based on col
        zcbv/company_leaves/on_week.htmlr(  weeksr)  r'   r  r$  s    r   custom_based_on_weekz!CompanyLeave.custom_based_on_week  s    
 2!%6
 	
r   c                 
    d}|S )z"
        for return title
        zCompany Leavesr   )r   titles     r   get_detail_titlezCompanyLeave.get_detail_title  s    
 !r   c                      t        dd| i      S )rG  z+cbv/company_leaves/detail_view_actions.htmlr(  r)  r&   r$  s    r   rH  z CompanyLeave.detail_view_actions   s     >&
 	
r   c                 *    t        d| t        d      S )z.
        custom based on week day col
        z#cbv/company_leaves/on_week_day.html)r(  	week_daysr)  )r'   r   r$  s    r   based_on_week_day_colz"CompanyLeave.based_on_week_day_col)  s    
 6!%I>
 	
r   c                 *    t        d| t        d      S )z$
        custom actions col
        z-cbv/company_leaves/company_leave_actions.htmlr  r)  r  r$  s    r   company_leave_actionsz"CompanyLeave.company_leave_actions3  s    
 @!%6
 	
r   c                 8    t        dd| j                  i      }|S )r@  zcompany-leave-detail-viewr  r8  rA  r5  s     r   rm  zCompanyLeave.detail_view=  s    
 14/J
r   c                     d| j                          d}| j                  rRt        j                  | j                  j                  z   }t        j                  |      r| j                  j                  }|S )zV
        Method will rerun the api to the avatar or path to the profile image
        r   r   )get_full_nameprofiler   
MEDIA_ROOTr   r   r   r   r   s      r   r   zCompanyLeave.get_avatarE  se    
 00B0B0D/EEWX 	 <<$//$,,2C2CCM%%m4ll&&
r   N)r   r   r   r   rJ  r  r{  r   r|  ra  r   rb  r   r   r  r   r%  r  r  rH  r  r  rm  r   r   r   r   rw  rw    s    $F$$TM )((CK"""dUfnnJ $,GGA Ah



r   rw  c            
           e Zd Z ej                  eej                  d ed            Z ej                  e	ej                  ddd ed            Z ej                  d ed	      
      Z ej                  d ed      
      Z ej                  d ed      
      Z ej                   ej$                   ed      
      Z ej                   dd ed            Z ej                   dd ed            Z ed      Z edeg      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&d# Z' fd$Z( xZ)S )%AvailableLeaveavailable_leaver   )r   related_namer   employee_available_leaveT
Leave type)r   r  r   r   r   r   zAvailable Daysr   zCarryforward DayszTotal Leave DayszAssigned DatezLeave Reset Dater   r   r   zCarryForward Expired Date+employee_id__employee_work_info__company_idr   history_setr  basesc                       e Zd ZdZy)AvailableLeave.Metaleave_type_idemployee_idNr}  r   r   r   r   r  |  s    :r   r   c                 8    | j                    d| j                   S r  r  r  r$  s    r   r%  zAvailableLeave.__str__  s!    ""#3t'9'9&:;;r   c                      t        dd| i      S )z<
        method for edit and delete actions coloumn
        z.cbv/assigned_leave/assigned_leave_actions.htmlr(  r)  r&   r$  s    r   assigned_leave_actionsz%AvailableLeave.assigned_leave_actions  r2  r   c                      t        dd| i      S )z@
        method for detail view edit and delete actions
        z5cbv/assigned_leave/assigned_leave_detail_actions.htmlr(  r)  r&   r$  s    r   assigned_leave_detail_actionsz,AvailableLeave.assigned_leave_detail_actions  s     H&
 	
r   c                 8    t        dd| j                  i      }|S )r@  zavailable-leave-single-viewr  r8  rA  r5  s     r   assigned_leave_detail_viewz)AvailableLeave.assigned_leave_detail_view       3T477OL
r   c                     | j                    S zB
        Return subtitle containing both name and emp id.
        r  r$  s    r   #assigned_leave_detail_name_subtitlez2AvailableLeave.assigned_leave_detail_name_subtitle       ""#$r   c                     | j                   j                  j                   d| j                   j                  j                   S zZ
        Return subtitle containing both department and job position information.
         / r  employee_work_infodepartment_idjob_position_idr$  s    r   &assigned_leave_detail_postion_subtitlez5AvailableLeave.assigned_leave_detail_postion_subtitle  ;     ""55CCDCHXHXHkHkH{H{G|}}r   c                    i }| j                   j                  dk(  rt        j                         }t	        dd      D ]  }|t        |      z   }| j                   j                  rW| j                  t        | j                   j                  | j                   j                  |z        z   ||j                  d      <   | j                  | j                   j                  |z  z   ||j                  d      <    |S )Nr=   r   r   r   z%Y-%m)r  r   r   r   ranger   r  available_daysminrT  strftime)r   forecasted_leaver   i
next_months        r   forcasted_leaveszAvailableLeave.forcasted_leaves  s    ))Y6LLNE1a[ "]!%<<
%%66++ ..??!//::Q> %Z%8%8%AB ++t/A/A/L/Lq/PQ %Z%8%8%AB  r   c                     t        |t              r$t        j                  |d      j	                         }| j
                  j                         }|r||k  r| j
                  j                  S y)N%Y-%m-%dr   )
isinstancestrr   strptimer   r  r   rT  )r   r   next_reset_dates      r   r  zAvailableLeave.forcasted_leaves  sZ    dC $$T:6;;=D,,GGI$6%%000r   c                    | j                   j                  dk7  rP| j                   j                  | j                  k\  r| j                  | _        n| j                   j                  | _        | j                   j
                  | _        y )Nr   )r  r  r  total_leave_dayscarryforward_daysrT  r  r$  s    r   update_carryforwardz"AvailableLeave.update_carryforward  sg    //3DD!!22d6K6KK)-)>)>&)-););)L)L&"00;;r   c                 x   |j                   j                  dk(  r|j                   j                  }|dk(  r+|t        dd      z   }||k  r|}|S |t        dd      z   }|S |t        dt	        |            z   }||k  r|}|S |t        dt	        |            z   }|S |j                   j                  dk(  r`d|j                         t	        |j                   j                        z
  dz
  z
  }|dk7  r|t        |dz  	      z   }|S |t        d	      z   }|S t	        |j                   j                        }|j                   j                  }|dk(  r-|t        d|d
      z   }||k  r|}|S |t        d|d
      z   }|S |t        d|t	        |      
      z   }||k  r|}|S |t        d|t	        |      
      z   }|S )Nr=   rY   r      )r   r   r   r?   r   r   )r   r   r   )r  r   r   r   r   
isoweekdayrX  r   )r   r   r  r   	temp_dater   tempr   s           r   set_reset_datezAvailableLeave.set_reset_date  s0   ((44	A'55??IJ&)M,KK	 9,!*Jb _ "/aR1P!PJ^ Y *MI,WW	 9,!*JT Q "/ c)n2 "JP I **66(B((*o33AABCD
 qy*]-KK
: 7 +]-BB
6 3 o;;GGHK'55??IJ&)M;B- 	 !9,!*J"  "/{2 "J  *M;C	N- 	 !9,!*J 	 "/{I2 "J r   c                     t         j                  j                  | j                  | j                  d      j                  t        d            }|d   r|d   S dS )z*
        taken leaves calculation
        r   )r  r  statusrequested_days)	total_sumr  r   )LeaveRequestr  r	  r  r  	aggregater   )r   leave_takens     r   r  zAvailableLeave.leave_taken	  sd     #**11,,(( 2 
 )c"23)
4	 	 ,7{+C{;'JJr   c                 8   |j                   j                  }|j                   j                  dk(  r|t        |      z   }n8|j                   j                  dk(  r|t        |      z   }n|t        |      z   }d|_        |j                   j
                  |_        |S )Nr   r   r   r   r   r   )r  r   r   r   r  rT  r  )r   r  r   r   r   s        r   r  zAvailableLeave.set_expired_date  s     ..EE((CCuL(=f+EEL**EEP(=+GGL(=v+FFL,-))8)F)F)Q)Q&r   c                    | j                   8| j                  j                  r"| j                  | j                  |       | _         | j                  j
                  dk(  r?| j                  }| j                  j                  r| j                  j                  }|| _        t        t        | j                  | j                  z   d      d      | _        t        t        | j                  d      d      | _        y)zK
        Reusable method to compute fields normally set in save().
        N)r   r  r   r      )r   r  r   r  r   r  r  r   roundmaxr  r  r  )r   expiry_dates     r   pre_save_processingz"AvailableLeave.pre_save_processing#  s    
 ??"t'9'9'?'?"11"00$ 2 DO
 //3HH,,K!!::"00II +D !&##d&<&<<a@!!
 "'s4+A+A1'Eq!Ir   c                 D    | j                          t        |   |i | y r"  )r  r  r   r  s      r   r   zAvailableLeave.save:  s       "d%f%r   )*r   r   r   r   ra  r   CASCADEr  r  r   rb  r  rN  r  r  r  r[  r   r   r   r   r   r   r  r%   r$   historyr   r%  r  r  r  r  r  r  r  r  r  r  r  r   rc  rd  s   @r   r  r  U  s   #&##..&z]	K &F%%../|_M 'V&&qqAQ?RSN)))"5 6 )v((CUAVW$F$$1_+=M "!!A.@,AJ $6##A.I,JL $KG "
G; ;<

%~ (<7r
KJ.& &r   r  c                 
   g }t         j                  j                  |       }|D ][  }|j                  }|j                  }||z
  }t        |j                  dz         D ]!  }|t        |      z   }|j                  |       # ] |S )Nr  r   )	RestrictLeaver  r	  rt  ru  r  r   r   append)restrirestricted_datesrestricted_daysr  restrict_start_daterestrict_end_daterT  r   s           r   restrict_leavesr  ?  s    #++22f2=O *llJJ&)<<
z*+ 	*A&15D##D)	*	* r   c                 ~    || }t        || z
  j                  dz         D cg c]  }| t        |      z    c}S c c}w )zF
    Returns a list of dates from the start date to the end date.
    r   )r  r   r   )rt  ru  r  s      r   leave_requested_datesr  M  sB     /4h6K5Q5QTU5U/VW!J1%WWWs   :c                   	
 t        | |      }t        t        t        j                  j                                     	t        t        t        j                  j                         |             |j                  dk(  r9|j                  dk(  r*	j                        
t        
fd|D              }||z
  S |j                  dk(  rt        	fd|D              }||z  }|j                  dk(  rt        fd|D              }||z  }|S )zn
    Calculates the effective requested leave days by accounting for
    holidays and company leave days.
    r7   c              3   &   K   | ]  }|v  
 y wr"  r   ).0r   total_leavess     r   	<genexpr>z/cal_effective_requested_days.<locals>.<genexpr>f  s     Q 4Q   c              3   &   K   | ]  }|v  
 y wr"  r   )r  r   holidayss     r   r  z/cal_effective_requested_days.<locals>.<genexpr>j  s     IDH,Ir  c              3   &   K   | ]  }|v  
 y wr"  r   )r  r   company_leave_datess     r   r  z/cal_effective_requested_days.<locals>.<genexpr>n  s      "
,0D''"
r  )r  setr+   r   r  r  r)   r   r_  r`  unionsum)rt  ru  r  r  requested_datestotal_leave_countholiday_countcompany_leave_countr  r  r  s           @@@r   cal_effective_requested_daysr  V  s    
 ,JAO%h&6&6&:&:&<=>H !6!6!:!:!<jI
 	++u4))U2~~&9:QQQ 111$$-III-'**e3! "
4C"
 
 	--r   c            	           e Zd Z ej                  eej                   ed            Z ej                  e	ej                   ed            Z ej                  d ed            Z ej                  ded ed	      
      Z ej                  dd ed            Z ej                  ded ed      
      Z ej&                  dd ed            Z ej*                  d ed            Z ej.                   ed      d      Z ej2                  dde ed            Z ej                  ded ed      
      Z ej                  ej>                   ed            Z  ej&                  d      Z! ej&                  d      Z" ej.                  d ed      d      Z# e$de%g       Z& ej                  e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- Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d5 Z8d6 Z9d7 Z:d8 Z;d9 Z<d: Z=d; Z>d< Z?d= Z@d> ZAd? ZBd@ ZCdA ZDdB ZEdC ZFdTdDZGdE ZHdF ZIdG ZJdH ZKdI ZL fdJZM fdKZNdL ZOdM ZPdN ZQdO ZRdP ZS fdQZTdR ZUdS ZV xZWS )Ur  r   r   r   z
Leave TypeFrg  )r   r   r   r4   zStart Date Breakdownr   Tri  r   zEnd Date BreakdownzRequested Daysr  r   zLeave Clashes Countr   Description   )r   r   
Attachmentr   r   StatuszCreated Dater   zReject Reason)r   r   r   r  r  leave_request_createdz
Created By)r   r   r  r   r  r   c                       e Zd ZdgZdZdZy)LeaveRequest.Metar   zLeave RequestzLeave RequestsN)r   r   r   r   r   verbose_name_pluralr   r   r   r   r    s    7&.r   r   c                      t        dd| i      S )z5
        method for rendering comment action
        z!cbv/my_leave_request/comment.htmlr(  r)  r&   r$  s    r   comment_actionzLeaveRequest.comment_action  s    
 4&
 	
r   c                 J    t        j                         }t        d| |d      S )4
        method for rendering cancel action
        z(cbv/my_leave_request/confirm_cancel.htmlr(  current_dater)  r   r   r'   r   r  s     r   cancel_confirmation_actionz'LeaveRequest.cancel_confirmation_action  s'    
 zz|;!%|D
 	
r   c                      t        dd| i      S )r  z'cbv/my_leave_request/leave_actions.htmlr(  r)  r&   r$  s    r   leave_actionszLeaveRequest.leave_actions  s    
 :&
 	
r   c                      t        dd| i      S )9
        method for rendering detail view action
        z.cbv/my_leave_request/detail_leave_actions.htmlr(  r)  r&   r$  s    r   detail_leave_actionsz!LeaveRequest.detail_leave_actions  s    
 A&
 	
r   c                 8    | j                    d| j                   S )Nz to )rt  ru  r$  s    r   
get_periodzLeaveRequest.get_period  s    //"$t}}o66r   c                    t         j                  j                  t        | j                  j
                  j                        t        | j                  j
                  j                        z  | j                  | j                        }|j                  | j                  j
                  j                        }|j                  | j                  j
                  j                        }t        d| ||d      S )zD
        method for rendering clashed_due_to col in clashes
        .employee_id__employee_work_info__department_id0employee_id__employee_work_info__job_position_idstart_date__lteend_date__gtez&cbv/leave_requests/clashed_due_to.html)r(  clashed_due_to_departmentclashed_due_to_job_positionr)  )r  r  r	  r   r  r  r  r  ru  rt  r'   )r   overlapping_requestsr&  r'  s       r   clashed_due_tozLeaveRequest.clashed_due_to  s      ,33::?C?O?O?b?b?p?p AEAQAQAdAdAtAt !MM//  ; 	 
 %9$?$?;?;K;K;^;^;l;l %@ %
! ';&A&A=A=M=M=`=`=p=p 'B '
# 9 -F/J
 	
r   c                    g }d| i}t        j                  d      rbt        dd      }|j                  j	                  | j
                  | j                  | j                  g      }|r|j                  |       | |d}t        d|      S )	z'
        leave type custom col
        r(  recruitmentinterviewschedule	app_labelmodelr  interview_date__ranger(  leave_requests_with_interviewz(cbv/my_leave_request/leave_type_col.htmlr)  
r   is_installedr   r  r	  r  rt  ru  r  r'   r   r3  r+  Schedule
interviewss        r   leave_type_customzLeaveRequest.leave_type_custom  s     )+%t$]+.'/BH "))00 ,,OOMM' 1 J -44Z@ !1NG ;W
 	
r   c                 $    | j                   dk(  ryy)zB
        method to change background if they are rejected
        r   3style="background-color: rgba(255, 166, 0, 0.158);"Nr  r$  s    r   is_rejectedzLeaveRequest.is_rejected$  s    
 ;;*$H %r   c                     | j                   j                  j                   d| j                   j                  j                   S r  r  r$  s    r    my_leave_request_detail_subtitlez-LeaveRequest.my_leave_request_detail_subtitle,  r  r   c                 8    t        dd| j                  i      }|S )r@  zmy-leave-request-detail-viewr  r8  rA  r5  s     r   my_leave_request_detail_viewz)LeaveRequest.my_leave_request_detail_view2  s     4dDGG_M
r   c                      t        dd| i      S )z6
        method for rendering rejected action
        z)cbv/my_leave_request/rejected_action.htmlr(  r)  r&   r$  s    r   rejected_actionzLeaveRequest.rejected_action9      
 <&
 	
r   c                      t        dd| i      S )z7
        method for rendering cancelled action
        z*cbv/my_leave_request/cancelled_action.htmlr(  r)  r&   r$  s    r   cancelled_actionzLeaveRequest.cancelled_actionC  s    
 =&
 	
r   c                      t        dd| i      S )z8
        method for rendering attachment action
        z+cbv/my_leave_request/attachment_action.htmlr(  r)  r&   r$  s    r   attachment_actionzLeaveRequest.attachment_actionM  s    
 >&
 	
r   c                      t        dd| i      S )z?
        method for rendering multiple approval action
        z0cbv/leave_requests/multiple_approval_action.htmlr(  r)  r&   r$  s    r   multiple_approval_actionz%LeaveRequest.multiple_approval_actionW  s    
 C&
 	
r   c                 t    t        t        d      }t        |      j                         }t	        d| |d      S )z8
        method for rendering custom status col
        r  z)cbv/leave_requests/custom_status_col.html)r(  multiple_approvalsr)  )r  r   r*   distinctr'   )r   r  rL  s      r   custom_status_colzLeaveRequest.custom_status_cola  s=     .)4=gFOOQ<!%=OP
 	
r   c                      t        dd| i      S )r  4cbv/leave_requests/leave_request_detail_actions.htmlr(  r)  r&   r$  s    r   leave_request_detail_actionz(LeaveRequest.leave_request_detail_actionm  s    
 G&
 	
r   c                      t        dd| i      S )z,
        method for comment sidebar
        z&cbv/leave_requests/comment_action.htmlr(  r)  r&   r$  s    r   comment_sidebarzLeaveRequest.comment_sidebarw  s     9&
 	
r   c                      t        dd| i      S )z0
        method for leave clash coloumn
        z#cbv/leave_requests/leave_clash.htmlr(  r)  r&   r$  s    r   leave_clash_colzLeaveRequest.leave_clash_col       6&
 	
r   c                      t        dd| i      S )z-
        method for penality coloumn
        z cbv/leave_requests/penality.htmlr(  r)  r&   r$  s    r   penality_colzLeaveRequest.penality_col  s     3&
 	
r   c                      t        dd| i      S )z,
        method for actions coloumn
        z#cbv/leave_requests/actions_col.htmlr(  r)  r&   r$  s    r   actions_colzLeaveRequest.actions_col  rV  r   c                 `    t        j                         }t        d| || j                  d      S )z8
        method for confirmation button coloumn
        z$cbv/leave_requests/confirmation.html)r(  r  ru  r)  )r   r   r'   ru  r  s     r   confirmation_colzLeaveRequest.confirmation_col  s1     zz|7  , MM
 	
r   c                 $    | j                   dk(  ryy)zC
        method to change background if they are cancelled
        r   z#style="background-color: lightgrey"Nr<  r$  s    r   is_attendance_request_cancelledz,LeaveRequest.is_attendance_request_cancelled  s    
 ;;+%8 &r   c                 8    t        dd| j                  i      }|S )r@  zleave-requests-detail-viewr  r8  rA  r5  s     r   leave_requests_detail_viewz'LeaveRequest.leave_requests_detail_view  s     2D$''?K
r   c                 J    t        j                         }t        d| |d      S )z8
        method for detail view actions coloumn
        rP  r  r)  r  r  s     r   "leave_requests_detail_view_actionsz/LeaveRequest.leave_requests_detail_view_actions  s'     zz|G!%|D
 	
r   c                    g }d| i}t        j                  d      rbt        dd      }|j                  j	                  | j
                  | j                  | j                  g      }|r|j                  |       | |d}t        d|      S )	z2
        custom emp col in leave requests
        r(  r+  r,  r-  r0  r2  z-cbv/leave_requests/leave_request_emp_col.htmlr)  r4  r6  s        r   leave_requests_custom_emp_colz*LeaveRequest.leave_requests_custom_emp_col  s     )+%t$]+.'/BH "))00 ,,OOMM' 1 J -44Z@ 1NG
 @'
 	
r   c                     | j                    S r  r  r$  s    r   leave_requests_detail_subtitlez+LeaveRequest.leave_requests_detail_subtitle  r  r   c                     t        |       S r"  r"   r$  s    r   trackingzLeaveRequest.tracking      ~r   c                 R    | j                    d| j                   d| j                   S r  )r  r  r  r$  s    r   r%  zLeaveRequest.__str__  s+    ""#3t'9'9&:#dkk]KKr   c                     | t        j                         n| } t        j                  j	                  | |       }||j	                  |      }|S )a  
        Retrieve employees who are on leave on a given date (default is today).

        Args:
            today (date, optional): The date to check. Defaults to the current date
                                    in the server's local timezone.
            status (str, optional): The status to filter leave requests. If None, no filtering by status is applied.

        Returns:
            QuerySet: A queryset of LeaveRequest instances where employees are on leave on the specified date.
        r#  r<  )r   r   r  r  r	  )r   r  querysets      r   employees_on_leave_todayz%LeaveRequest.employees_on_leave_today  sR     !&

5''..! / 
 f5Hr   c                 6    | j                   j                         S )z^
        This method is used to return the total penalties in the late early instance
        )penaltyaccounts_setrO  r$  s    r   get_penalties_countz LeaveRequest.get_penalties_count  s     ''--//r   c                     | j                   }| j                  }|| j                   }||z
  }g }t        |j                  dz         D ]!  }|t	        |      z   }|j                  |       # |S )z^
        :return: this functions returns a list of dates from start date to end date.
        r   )rt  ru  r  r   r   r  )r   request_start_daterequest_end_dater  r  r  r   s          r   r  zLeaveRequest.requested_dates  s{     "__==##),>>~**Q./ 	)A%	!4D""4(	) r   c                    g }t         j                  j                         }|D ]_  }|j                  }|j                  }||}||z
  }t        |j                  dz         D ]!  }|t        |      z   }|j                  |       # a |S )zN
        :return: this functions returns a list of all holiday dates.
        r   )	r   r  r  rt  ru  r  r   r   r  )	r   holiday_datesr  holidayholiday_start_dateholiday_end_dateholiday_daysr  r   s	            r   ru  zLeaveRequest.holiday_dates  s     ##'') 	+G!(!3!3&//'#5 +.@@L<,,q01 +)IaL8$$T*+	+ r   c                    ddl m} t        j                  j	                         }g }|D ]  }| r| j
                  j                  }n |j                         j                  }|j                  }|j                  }t        dd      D ]U  }|dk7  rt        j                  d       t        j                  ||      }		 |	t        |         }
|
D cg c]
  }|dk7  s	| }}|D ]d  }t        j                  | d|dd|dd	      j                         } |j                          t        |      k(  sO||vsT|j#                  |       f t        j                  d       t        j                  ||      }	|	D ]d  }|t        |         dk7  st        j                  | d|dd|t        |         dd	      j                         }||vsT|j#                  |       f X  |S c c}w # t$        $ r Y ow xY w)
zI
        :return: This function returns a list of all company leave datesr   )r   r      N   -02r  )r   r   r   r  r  rt  r   r   r{  r|  r  r   setfirstweekdaymonthcalendarr   r  r   r  
IndexError)r   r   company_leavesr  company_leaver   r{  r|  r   month_calendarr  r   weekdays_in_weeksweeks                 r   r  z LeaveRequest.company_leave_dates'  s    	"&..224 + %	AM++!tzz|(()77M - ? ?q" A D(,,Q/%-%;%;D%%HN .s=/A B<A,NSSAXS,N),N#4 AC#+#4#4#'&%1SH =z$"df ! !-#6G2H H$(0C$C 3 : :4 @A ,,Q/%-%;%;D%%HN . A$5 671<#+#4#4#'&%1T#>O:P5QRT4U V *$ #df !  $+>> 3 : :4 @A/A%	AL #"1 -O & s7   :G(
G#G#AG(.G(3G(#G((	G54G5c                    t         j                  j                  | j                  | j                  | j
                        j                  | j                        }|j                         r|j                         }|j
                  | j
                  k(  rn|j                  dk7  r_| j                  dk7  rP|j                  | j                  k7  r7|j                  | j                  k7  rt         j                  j                         S |S )zk
        Checks for overlapping leave requests based on the current instance's dates and employee.
        )r  r$  r%  r  r4   )r  r  r	  r  ru  rt  r
  r  r   firststart_date_breakdownend_date_breakdownnone)r   r(  existing_leaves      r   leaveoverlappingzLeaveRequest.leaveoverlappingV  s      ,33::(( MM//  ;  
 'TWW'
	 	  &&(1779N ))T__<"77:E--;"774;T;TT"559P9PP#++0022##r   c                    t        | j                  | j                  | j                  | j                        | _        | j                  j                  dk(  r*| j                  j                  dk(  r| j                          n| j                          | j                  dv rd| _        n| j                         | _        t        | <  |i | | j!                          t"        j$                  j'                  | j(                        }d }d }|j+                         r@| j(                  j,                  j.                  }| j(                  j,                  j0                  }| j
                  }d}|d k7  r5d k7  r0t2        j$                  j'                  ||      j5                  d      }|d k7  r|D ]  }	|	j6                  }
|
dk(  r=t9        |	j:                        }t9        |	j<                        }||cxk  r|k  sJn M|	} nIt>        jA                  |	j6                        } tC        |      |	jD                        } |||      s|	} n |r| j                  d	k(  rtF        j$                  j'                  | 
      jI                          d}|jK                         }|D ]\  }tM        |tN              s tQ        | j(                  j,                  |      }|s6|dz  }tF        j$                  jS                  || |       ^ y y y )Nr7   r   r   r   r  F)
departmentr   condition_valuer  r   leave_request_idr   )sequencer  
manager_id)*r(   rt  ru  r  r  r  r  r_  r`  exclude_all_leavesexclude_leavesr  leave_clashes_countcount_leave_clashesr  r   update_leave_clashes_countr   r  r	  r  r   r  r  r   r   order_bycondition_operatorfloatcondition_start_valuecondition_end_valueoperator_mappingr  typer  LeaveRequestConditionApprovaldeleteapproval_managersr  r   r  create)r   r  r  	work_infor  
conditionsemp_comp_idr  applicable_condition	conditionoperatorstart_value	end_valueoperator_funcr  r  managersmanagerr  s                     r   r   zLeaveRequest.saveo  s   6OOMM%%##	
 44=""22e;##%!;;33'(D$'+'?'?'AD$d%f%'')+33::tGWGW:X	
 ,,??MMM**==HHK,,$D [D%82::AA([ B h()  ' 	$77w&"'	(G(G"HK %i&C&C DI"nA	A/8,$4$8$89U9U$VM&:d>&:9;T;T&UO$^_E/8,  DKK;$>)1188$8OVVXH+==?H# 	!'84%d&6&6&I&I7SGMH199@@!))-#* A 		 %?r   c                    t         |          }t        | dd       }|sy t        | dd       }t        | j	                               }t
        j                  j                         }t        t        j                  dd       }t        j                  j                  | j                  |      j                         st        dt        d      i      | j                   | j"                  kD  rt        t        d            | j                   | j"                  k(  r-| j$                  | j&                  k7  rt        t        d            |r'|j(                  dk(  r|st        dt        d	      i      | j                   rl| j"                  r`| j+                         j-                  | j.                  
      }|j-                  ddg      j                         rt        t        d            |j0                  j2                  st4        j                  j                  d      j                         rUt4        j                  j7                         }|r5| j                   t9        j:                         k  rt        t        d            t        j                  j=                  | j                  |      }	t?        | j                   | j"                  | j$                  | j&                        }
tA        | j                   | j"                  ||
      }tC        | j                   | j"                        }|D cg c]#  }|jD                   d |jF                  d       % }}tI        j:                         }tK        t        |            }|jL                   d|jD                   |v r/|jO                  |jG                  d       d|jD                          |	jQ                  | j                         }|	jR                  xs d}|	jT                  xs d}|	jV                  jX                  xs d}|	jV                  jZ                  }|dv rt]        ||      }n|dk(  rd}||z   |z   }||k  st        t        d            t^        j                  j                  | j                        j7                         }|r|j`                  nd }|r|jb                  nd }|j0                  j2                  r|S |D ]  }t        |jd                  j                               }t        |jf                  j                               }d}|jh                  r|sd}n|r||vrd}n||v rd}|snt        tk        |j.                              }|s||z  s|jl                  |k(  r|jn                  j                         r |s||jn                  j                         v st        d       |S c c}w )Nr  
attachmentr  r  z9The selected leave type is not assigned to this employee.z,End date should not be less than start date.z4Mismatch in the breakdown of the start and end date.r7   z0An attachment is required for this leave requestr  r   r   )
status__inz9Employee already has a leave request for this date range.T)enabledz'Requests cannot be made for past dates.)rt  ru  r  r  r}  z%mr   )r   r   r   z?Does not have sufficient leave balance for the requested dates.r  FzgYou cannot request leave for this date range. The requested dates are restricted. Please contact admin.)8r  r  r  r  r  r  r  r  r   r   r  r	  r  r   r
   r  rt  ru  r  r  r^  r  r
  r  useris_superuserEmployeePastLeaveRestrictr  r   r   r  r(   r  r  r   r  r   listr   remover  r  r  r  r  r  r  r   r  r  exclued_leave_typesspesific_leave_typesinclude_allr  r  r   )r   cleaned_data
leave_typer  	requ_daysrestricted_leavesr  r(  restrictr  r  effective_requested_daysleave_datesr   
month_yearr   unique_datesforcated_daysr  r  r  r  r  r  emp_depemp_jobexclued_typesspecific_typesis_restrictedrestri_daysr  s                                 r   r  zLeaveRequest.clean  sO   w}T?D9
T<6
,,./	)11557-<<iN %%,,((
 - 

&( "#QS&  ??T]]*!!$R"STT OOt}},))T-D-DD!HI 
 *775@!q!STU 
 ??t}}#'#8#8#:#B#Bdgg#B#N #++'4 , fh &QR  )))11888FMMO088>>@HDOOdjjl:%a(Q&RSS )0044((
 5 
 2OOMM%%##	
 $@]]$)	$
  ,DOOT]]KGRSt1]T]]4%8$9:S
S C
O,kk]!EJJ<(L85>>$#7"8%** FG'88I(77<1+==B*88IINQ+99KK GG #$57G H"33 !),==M'+;;!ST 
 ,33::(( ; 

%' 	 .7)))D/8)++d <<$$ * 	H < < @ @ BCM !>!>!B!B!DEN!M ##M $:]#B $~- $ ohkk:;K ;&''72$1188:'X-B-B-F-F-H"H) B 7	> I Ts   #(Wc                    | j                         }| j                         }| j                         }t        t	        ||z               t        fd|D              }| j                  v s| j                  v r3| j                  dk(  s| j                  dk(  r| xj                  dz  c_
        | j                  |z
  | _
        y )Nc              3   &   K   | ]  }|v  
 y wr"  r   )r  requested_dater  s     r   r  z2LeaveRequest.exclude_all_leaves.<locals>.<genexpr>I  s       
/=Nl* 
r  r6   r5   g      ?)r  ru  r  r  r  r  rt  ru  r  r  r  )r   r  ru  r  r   r  s        @r   r  zLeaveRequest.exclude_all_leavesD  s    ..0**,"668C0C CDE  
AP 
 
 OO|+t}}/L%%6&&,63&"114EEr   c                    d}| j                   j                  dk(  rE| j                         }| j                         }|D ]  }||v s|dz  } | j                  |z
  | _        | j                   j
                  dk(  rI| j                         }| j                         t        fd|D              }| j                  |z
  | _        y y )Nr   r7   r   c              3   &   K   | ]  }|v  
 y wr"  r   )r  r  r  s     r   r  z.LeaveRequest.exclude_leaves.<locals>.<genexpr>a  s      &" "55&r  )r  r`  r  ru  r  r_  r  r  )r   r  r  ru  r  r  r  s         @r   r  zLeaveRequest.exclude_leavesU  s    --6"224O ..0M"1 '!]2!Q&M' #'"5"5"ED33u<"224O"&":":"<"% &&5& # #'"5"58K"KD =r   c                    | j                   }| j                  }t        j                  j	                  ||      }| j
                  |j                  kD  rM| j
                  |j                  z
  }|j                  | _        d|_        |j                  |z
  |_        || _	        n/|j                  | j
                  z
  |_        | j
                  | _        d| _
        |j                          y )Nr  r   r   )r  r  r  r  r  r  r  approved_available_daysr  approved_carryforward_daysr  r   )r   r  r  r  leaves        r   no_approvalzLeaveRequest.no_approvalg  s    &&**(0044'[ 5 
 !?!??''/*H*HHE+:+I+ID(-.O*11E9 - /4D+  ..1D1DD * ,0+>+>D( r   c                 H   t         j                  j                  |       }|j                  d      j                  d      }|j                  d      j                  d      }g }|D ]  }|j	                  |j
                          |j                         r	||||d}|S d}|S )Nr  F)is_approvedr  T)r  r   r   	approvals)r  r  r	  r  r  r  r   )	r   r  r  r  requested_queryapproved_queryr  r  results	            r   rL  zLeaveRequest.multiple_approvals}  s    199@@RV@W	#**u*=FFzR"))d);DDZP  	0GOOG../	0$*,&	F  Fr   c                 @   t        t        j                  dd       }|rt        j                  j                  |j                        j                         }t        j                  j                  | |j                        j                         }|r|j                   S yy )Nr  )employee_user_id)r  r  T)r  r   r   r   r  r	  r  r  r  r  r  )r   r  employeecondition_approvals       r   r  zLeaveRequest.is_approved  s    -<<iN''...MSSUH!>!F!F!M!M!%(++ "N "eg  "-9999 r   c                 &   | j                   dk(  r t        |   |i | | j                          y t	        t
        j                  dd       }|rDt        |       t        j                  |t        d      j                  | j                                y y )Nr   r  z(The {} leave request cannot be deleted !)r  r  r  r  r  r   r   r   r	   warningr  format)r   r  r  r  r  s       r   r  zLeaveRequest.delete  s|    ;;+%GND+F+ ++-1@@)TRGw'  @AHHU r   c                    t         j                  j                  t        | j                        t        d      z  t        d      z        }|D ]  }|j                         |_         t         j                  j                  |dg       y)zH
        Update the leave clashes count for all leave requests.
        r  r   r<  r   r  N)r  r  r
  r   r  r  r  bulk_update)r   leave_requests_to_updateleave_requests      r   r  z'LeaveRequest.update_leave_clashes_count  s}     $0#7#7#?#?MA[11AZ4HH$
  6 	TM0=0Q0Q0SM-	T 	(($'<&=	
r   c                    t         j                  j                  | j                        }|j	                         r| j
                  dvrt        j                  j                  | j                        j                  t        | j                  j                  j                        t        | j                  j                  j                        z  t        | j                  j                  j                        z  | j                  | j                        j                  t        d	      t        d
	      z        }|j!                         S y)z
        Method to count leave clashes where this employee's leave request overlaps
        with other employees' requested dates.
        r  r  r  r  r!  )r  r#  r   r<  r   r   )r   r  r	  r  r   r  r  r
  r  r   r  r  r  r   ru  rt  rO  )r   r  r(  s      r   r  z LeaveRequest.count_leave_clashes  s   
 ,33::tGWGW:X	$++5N"N$$,,,8KOK[K[KnKnK|K| MQM]M]MpMp  NA  NA DHDTDTDgDgDrDr
 %)MM"&//   +.*1EEF# !( (--//r   )NN)Xr   r   r   r   ra  r   r  r  r  r   rb  r  r[  rt  rJ  	BREAKDOWNr  ru  r  rN  r  rZ  r  	TextFielddescription	FileFieldr!   r  LEAVE_STATUSr  r   r   r  r  r  reject_reasonr%   r$   r  
created_byr   r  r   r  r  r  r  r  r)  r9  r=  r?  rA  rC  rF  rH  rJ  rN  rQ  rS  rU  rX  rZ  r\  r^  r`  rb  rd  rf  rh  r%  rm  rp  r  ru  r  r  r   r  r  r  r  rL  r  r  r  r  rc  rd  s   @r   r  r  v  s   #&##FNN:K &F%%V^^!L/M "!!u1\?KJ+6++-.	  vTAjMRH)))+,	 'V&&A.>,?N .&--"7 8 #&""-0@SQK!!!|_	J Vx[	F &V%%1^+<N 0f//:!2!2!21!=$F$$?!3M "
G #""..,|_J $KG/ /

	


7
>
8I~











9

8%L,0"-#^$2@DQfF"L$,$

 r   r  c                   2    e Zd Z ej                  e      Zy)LeaverequestFile)r   N)r   r   r   r   r  r!   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
)LeaverequestCommentz#
    LeaverequestComment Model
    r   Tr   Commentr  r   r   r   returnc                     | j                    S r"  commentr$  s    r   r%  zLeaverequestComment.__str__      ,, r   N)r   r   r   __doc__r   ra  r  r  
request_idr   r  ManyToManyFieldr  filesr  r  r  r  r%  r   r   r   r  r    su     #""<6>>JJ#&##HGK"F""#34@EfDq|PSTG! !r   r  c                       e Zd Z ej                  eej                   ed            Z ej                  e	ej                   ed            Z ej                  dd ed            Z ej                  ej                         Z ej$                  d ed	      
      Z ej(                  dde ed            Z ej.                  ded      Z ej$                  dd      Z edeg      Z ed      Z G d d      Z d Z! fdZ"d Z# f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& Z0d' Z1d( Z2d) Z3 xZ4S )*LeaveAllocationRequestr  r  r   TzRequested daysr  r   r  r  r   r   r  r   r   r   r   r   r   r  r  r  r   c                   2    e Zd ZdgZ ed      Z ed      Zy)LeaveAllocationRequest.Metar   zLeave Allocation RequestzLeave Allocation RequestsN)r   r   r   r   r  r   r  r   r   r   r   r    s     734 ;<r   r   c                 R    | j                    d| j                   d| j                   S Nz| r  r  r  r$  s    r   r%  zLeaveAllocationRequest.__str__  s+    ""#2d&8&8%9DGG9EEr   c                 $    t        |   |i | y r"  )r  r   r  s      r   r   zLeaveAllocationRequest.save  s    d%f%r   c                 J    | j                   dk7  rt        t        d            y )Nr   zFThis form cannot be edited because the status is Requested / Rejected.)r  r
   r  )r   r  r  s      r   r  zLeaveAllocationRequest.clean  s+    ;;+%!\  &r   c                 2    t        |   |i | d| _        y )NF)r  __init__skip_historyr  s      r   r  zLeaveAllocationRequest.__init__  s    $)&)!r   c                     t        |       S r"  r"   r$  s    r   rh  zLeaveAllocationRequest.tracking"  ri  r   c                     	 t        |       dd }|D ]#  }|d   dk(  s|d   D ]  }|d   dk(  s|c c S  % y#  Y yxY w)zS
        This method is used to return the tracked history of the instance
        N   r  Changeschanges
field_namer  r"   )r   	historiesr  updates       r   allocate_trackingz(LeaveAllocationRequest.allocate_tracking%  sc    
	 r*I$ *6?i/"))"4 *!,/3CC#)M**
	s   9 9 9 9 =c                 R    t        t              j                  | j                        S )z 
        Display status
        r  LEAVE_ALLOCATION_STATUSr  r  r$  s    r   
get_statusz!LeaveAllocationRequest.get_status4  s     +,00==r   c                      t        dd| i      S )z$
        For comment column
        z)cbv/leave_allocation_request/comment.htmlr(  r)  r&   r$  s    r   r  zLeaveAllocationRequest.comment:  rD  r   c                      t        dd| i      S )#
        For action column
        z/cbv/leave_allocation_request/action_column.htmlr(  r)  r&   r$  s    r   
action_colz!LeaveAllocationRequest.action_colD      
 B&
 	
r   c                      t        dd| i      S )r  z/cbv/leave_allocation_request/detail_action.htmlr(  r)  r&   r$  s    r   detail_actionz$LeaveAllocationRequest.detail_actionN  r!  r   c                      t        dd| i      S )r  z5cbv/leave_allocation_request/leave_detail_action.htmlr(  r)  r&   r$  s    r   leave_detail_actionz*LeaveAllocationRequest.leave_detail_actionX  s    
 H&
 	
r   c                      t        dd| i      S )z'
        For attachment column
        z,cbv/leave_allocation_request/attachment.htmlr(  r)  r&   r$  s    r   attachment_colz%LeaveAllocationRequest.attachment_colb  r-  r   c                      t        dd| i      S )z$
        For history column
        z)cbv/leave_allocation_request/history.htmlr(  r)  r&   r$  s    r   history_colz"LeaveAllocationRequest.history_coll  rD  r   c                      t        dd| i      S )z$
        For rejeect column
        z(cbv/leave_allocation_request/reject.htmlr(  r)  r&   r$  s    r   
reject_colz!LeaveAllocationRequest.reject_colv  s    
 ;&
 	
r   c                      t        dd| i      S )r  z/cbv/leave_allocation_request/confirmations.htmlr(  r)  r&   r$  s    r   confirm_colz"LeaveAllocationRequest.confirm_col  r!  r   c                 $    | j                   dk(  ryy )Nr   r;  r<  r$  s    r   	diff_cellz LeaveAllocationRequest.diff_cell  s    ;;*$H %r   c                     | j                   j                  j                   d| j                   j                  j                   S r  r  r$  s    r   (leave_request_allocation_detail_subtitlez?LeaveAllocationRequest.leave_request_allocation_detail_subtitle  r  r   c                 8    t        dd| j                  i      }|S )r@  zdetail-leave-allocation-requestr  r8  rA  r5  s     r   $leave_request_allocation_detail_viewz;LeaveAllocationRequest.leave_request_allocation_detail_view  s     7twwP
r   c                 8    t        dd| j                  i      }|S )r@  z$leave-allocation-request-detail-viewr  r8  rA  r5  s     r   $detail_view_leave_request_allocationz;LeaveAllocationRequest.detail_view_leave_request_allocation  s     <dDGG_U
r   )5r   r   r   r   ra  r   rb  r  r  r   r  r  rN  r  r[  r   r   r  r  r  r  r!   r  rJ  r  r  r  r%   r$   r  r   r  r   r%  r   r  r  rh  r  r  r  r   r#  r%  r'  r)  r+  r-  r/  r1  r3  r5  rc  rd  s   @r   r  r    s}   %F%%V^^!L/M $&##FNN:K 'V&&A.>,?N &V%%hll;N"&""c-@PQK!!!|_	J V6F %F$$4C@M"
G $KG= =
F&">







I~r   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
)LeaveallocationrequestCommentz-
    LeaveallocationrequestComment Model
    r  Tr  r  r  r  r  c                     | j                    S r"  r  r$  s    r   r%  z%LeaveallocationrequestComment.__str__  r  r   N)r   r   r   r  r   ra  r  r  r  r   r  r   r  r  r  r  r  r  r%  r   r   r   r7  r7    sv     #""#9V^^TJ#&##HGK"F""#34@EfDq|PSTG! !r   r7  c                       e Zd Z ej                         Z ej                  d      Z ej                  d      Z ej                  e
ej                        Z ej                  eej                        Zy)r  Fr   r  N)r   r   r   r   rZ  r  rR  r  r=  ra  r  r  r  r   r  r   r   r   r  r    sg    "v""$H%&%%e4K%&%%e4K(v((P"""8v~~FJr   r  c            	          e Zd Z ej                  d ed            Z ej                   ed            Z ej                   ed            Z	 ej                  e ed      ej                        Z ej                  e ed	      d
 ed            Z ej"                  d
 ed       ed            Z ej                  e ed      dd
 ed            Z ej                  e ed      dd
 ed            Z ej,                  d
 ed      d      Z ej                  ed
d
ej                   ed            Z ed      ZdefdZd  Zd! Zd" Z d# Z!d$ Z"y%)&r     Titler  rg  rh  ri  r   )r   r   r   TzxIf no job positions are specifically selected, the system will consider all job positions under the selected department.)r   r   	help_textz!Enable to select all Leave types.zInclude All)r   r=  r   zSpecific Leave Typesspesific_leave_typez(Choose specific leave types to restrict.)r   r  r   r=  zExclude Leave Typesexcluded_leave_typez/Choose leave types to exclude from restriction.r  r  r  r   )r   r   r   r   r   r   r  c                     | j                    S r"  r  r$  s    r   r%  zRestrictLeave.__str__  s    **r   c                      t        dd| i      S )z)
        For job position column
        z%cbv/restricted_days/job_position.htmlr(  r)  r&   r$  s    r   job_position_colzRestrictLeave.job_position_col  s    
 8&
 	
r   c                      t        dd| i      S )r  z cbv/restricted_days/actions.htmlr(  r)  r&   r$  s    r   rZ  zRestrictLeave.actions_col  s    
 3&
 	
r   c                      t        dd| i      S )r  z&cbv/restricted_days/detail_action.htmlr(  r)  r&   r$  s    r   r#  zRestrictLeave.detail_action  s    
 9&
 	
r   c                 &    d| j                    d}|S r   rA  r5  s     r   r   zRestrictLeave.get_avatar	  s     2$**=OP
r   c                 8    t        dd| j                  i      }|S )r@  zrestricted-days-detail-viewr  r8  rA  r5  s     r   restricted_days_detail_viewz)RestrictLeave.restricted_days_detail_view  r  r   N)#r   r   r   r   rJ  r  r  r[  rt  ru  ra  r   r  r  r   r   r   rR  r  r   r  r  r  r  r   r   r   r  r  r%  rC  rZ  r#  r   rH  r   r   r   r  r    s   F!G*EE!!!q?JvQz];H"""<FNNJ *6))~& G
	L &&%%78}%K
 2611-.*>? 1&00,-*EF #&""- 0SK #""..y\J $,GG 


r   r  
attendancec                   >    e Zd Z ej                  eej                  d      Z ej                  eej                  d      Z
 ej                  dd      Z ej                  dd      Z ej                  ej"                  	      Z ej&                  d
      Z ej*                  ded      Z ej&                  dd
      Z edeg      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*d" Z+d# Z,d$ Z-d% Z,d& Z. fd'Z/ xZ0S )(CompensatoryLeaveRequestr  r  r   zattendance.Attendance
Attendancerh  T)r   r   r   r  )r   r   r   r   r  r  r  r  r   c                       e Zd ZdgZy)CompensatoryLeaveRequest.Metar   Nr   r   r   r   r   rN  6  s	    wHr   r   c                 R    t        t              j                  | j                        S )z 
            status
            r  r$  s    r   status_displayz'CompensatoryLeaveRequest.status_display9  s     /044T[[AAr   c                      t        dd| i      S )z-
            comment sidebar col
            z0cbv/compensatory_leave/compensatory_comment.htmlr(  r)  r&   r$  s    r   compensatory_commentz-CompensatoryLeaveRequest.compensatory_comment?       #G#T* r   c                      t        dd| i      S )z"
            date col
            z'cbv/compensatory_leave/custom_date.htmlr(  r)  r&   r$  s    r   compensatory_datez*CompensatoryLeaveRequest.compensatory_dateH  s     #>#T* r   c                      t        dd| i      S )z1
            edit and delete options
            z0cbv/compensatory_leave/compensatory_actions.htmlr(  r)  r&   r$  s    r   compensatory_optionsz-CompensatoryLeaveRequest.compensatory_optionsQ  rS  r   c                      t        dd| i      S )z4
            approve and reject options
            z5cbv/compensatory_leave/compensatory_confirmation.htmlr(  r)  r&   r$  s    r   compensatory_confirm_actionsz5CompensatoryLeaveRequest.compensatory_confirm_actionsZ  s     #L#T* r   c                     | j                    S )zJ
            Return subtitle containing both name and emp id.
            r  r$  s    r   !compensatory_detail_name_subtitlez:CompensatoryLeaveRequest.compensatory_detail_name_subtitlec  s     &&'(r   c                     | j                   j                  j                   d| j                   j                  j                   S )zb
            Return subtitle containing both department and job position information.
            r  r  r$  s    r   compensatory_detail_subtitlez5CompensatoryLeaveRequest.compensatory_detail_subtitlei  sE     &&99GGHDL\L\LoLoLL  LA  B  Br   c                      t        dd| i      S )z=
            my compensatory detail view actions
            z9cbv/compensatory_leave/my_compensatory_detail_action.htmlr(  r)  r&   r$  s    r   my_compensatory_detail_actionsz7CompensatoryLeaveRequest.my_compensatory_detail_actionso  s     #P#T* r   c                      t        dd| i      S )z:
            compensatory detail view actions
            z7cbv/compensatory_leave/compensatory_detail_actions.htmlr(  r)  r&   r$  s    r   compensatory_detail_actionsz4CompensatoryLeaveRequest.compensatory_detail_actionsx  s     #N#T* r   c                      t        dd| i      S )zC
            compensatory reject reason in detail view
            z0cbv/compensatory_leave/detail_reject_reason.htmlr(  r)  r&   r$  s    r   !compensatory_detail_reject_reasonz:CompensatoryLeaveRequest.compensatory_detail_reject_reason  rS  r   c                 8    t        dd| j                  i      }|S )z<
            detail view of my compensatory tab
            zmy-compensatory-detail-viewr  r8  rA  r5  s     r   my_compensatory_detail_viewz4CompensatoryLeaveRequest.my_compensatory_detail_view  s     7twwPCJr   c                 8    t        dd| j                  i      }|S )z9
            detail view of compensatory tab
            zcompensatory-detail-viewr  r8  rA  r5  s     r   compensatory_detail_viewz1CompensatoryLeaveRequest.compensatory_detail_view  s     4dDGG_MCJr   c                 2    d}| j                   dk(  rd| dS y)zJ
            method to change background if they are rejected
            	lightgreyr   z\style="background-color: rgba(255, 166, 0, 0.158);"onmouseover="this.style.backgroundColor='zG';" onmouseout="this.style.backgroundColor='rgba(255, 166, 0, 0.158)';"Nr<  )r   hoverings     r    is_compensatory_request_rejectedz9CompensatoryLeaveRequest.is_compensatory_request_rejected  s7     #H{{j(AAI
 K\] )r   c                     t         j                  j                  | j                  | j                        \  }}|xj
                  | j                  z  c_        |j                          y Nr  r  r  get_or_creater  r  r  r  r   r   r  createds      r   assign_compensatory_leave_typez7CompensatoryLeaveRequest.assign_compensatory_leave_type  Z    '5'='='K'K ,,"00 (L ($OW **d.A.AA*  "r   c                 R    | j                    d| j                   d| j                   S r	  r
  r$  s    r   r%  z CompensatoryLeaveRequest.__str__  s+    &&'r$*<*<)=RyIIr   c                     t         j                  j                  | j                  | j                        \  }}|xj
                  | j                  z  c_        |j                          y rm  rn  rp  s      r   rr  z7CompensatoryLeaveRequest.assign_compensatory_leave_type  rs  r   c                    t         j                  j                  | j                  | j                        j                         rt         j                  j                  | j                  | j                        j                         }|j                  | j                  k  r=d|_        t        d|j                  | j                  |j                  z
  z
        |_
        n|xj                  | j                  z  c_        |j                          y y )Nr  r   )r  r  r	  r  r  r   r  r  r  r  r  r   )r   r  s     r   exclude_compensatory_leavez3CompensatoryLeaveRequest.exclude_compensatory_leave  s    %%,, ,,"00 -  fh #1"8"8"?"? $ 0 0"&"4"4 #@ # %'   #11D4G4GG56O28;'99..1O1OOQ9O5 $22d6I6II2$$&#r   c                     t         j                  j                  d      j                         | _        t        |   |i | y )NTr  )r   r  r	  r  r  r  r   r  s      r   r   zCompensatoryLeaveRequest.save  sA    !*!2!2!9!9&* ": "eg  GL$)&)r   )1r   r   r   r   ra  r   rb  r  r   r  r  r   attendance_idrN  r  r[  r   r   r  r  r  rJ  r  r  r  r%   r$   r  r   r  r   rP  rR  rU  rW  rY  r[  r]  r_  ra  rc  re  rg  rk  rr  r%  rw  r   rc  rd  s   @r   rK  rK    sW   )))l
 (f''Z
 /..#%
 +**DA)))(,,?&f&&#6!!!#:K
 )((tD!& 
 ("O
	 		B					)	B					
		#	J	#	'(	* 	*r   rK  c                       e Zd ZdZ ej
                  d      Z ej                         Z ej                  e
ej                  d      Zy)LeaveGeneralSettingz
    LeaveGeneralSettings
    Tr   )r   r   N)r   r   r   r  r   rR  compensatory_leaveManagerr  ra  r   r  r   r   r   r   r{  r{    sD     -,,T:fnnG"""7fnn4PJr   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
)CompensatoryLeaverequestCommentz7
        CompensatoryLeaverequestComment Model
        r  Tr  r  r  r  r  c                     | j                    S r"  r  r$  s    r   r%  z'CompensatoryLeaverequestComment.__str__  s    ll^$r   N)r   r   r   r  r   ra  rK  r  r  r   r  r   r  r  r  r  r  r  r%  r   r   r   r  r    sz    	 'V&&$

 (f''FNNK&&&'7tD"&""1Y<TWX	%S 	%r   r  c                   2    e Zd Z ej                  d      Zy)r  Tr   N)r   r   r   r   rR  r  r   r   r   r  r    s    !f!!$/Gr   r  c                       e Zd ZdZy)OverrideLeaveRequestsz@
        Class to override Attendance model save method
        N)r   r   r   r  r   r   r   r  r    s    	 	r   r  )rr   loggingr  r  	threadingr   r   r   dateutil.relativedeltar   django.appsr   django.confr   django.contribr	   django.core.exceptionsr
   django.core.files.storager   	django.dbr   django.db.modelsr   r   django.urlsr   r   django.utilsr   django.utils.translationr   r  base.horilla_company_managerr   base.modelsr   r   r   r   r   r   r   employee.modelsr   r   horillar   horilla.horilla_middlewaresr   horilla.methodsr   horilla.modelsr    r!   horilla_audit.methodsr#   horilla_audit.modelsr$   r%   horilla_views.cbv_methodsr'   leave.methodsr(   r)   r*   r+   leave.threadingr,   	getLoggerr   loggereqner/   r0   r1   r2   containsr  r  r\  rU  rV  rW  rP  rL  rY  ACCRUAL_PLANr  r  r  r   r   rf  rw  r  r  r  r  r  Modelr  r  r  r7  r  r  r5  rK  r{  r  r  r  r   r   r   <module>r     s        . . 0    # 2 5  # - ! 6 >   > ' 6 3 4 * B 5  -			8	$ [[
++
++
++
++""  :1\?#Am$%	
 <
( q{)q{ 	!E(O!E(O!E(O!E(O!E(O!E(O!E(O!E(O!E(O	1U8	1U8	1U8
!:!! ! 	!
 ! ! ! ! ! ! ! ! ! ! !  !!" #!$ %!& '!( )!* +!, -!. /!0 1!2 3!4 5!6 7!8 9!: ;!< =!> ?!@ A!H qx 7AgJ"7&!F)9LMAfI1X; 7
8 ,-.Q'(A9:;   >!23j!J-5PQ !K.!:!K.!:	 !K.!::  	!L/!M
!L/!M
!L/	 	!H+!I,!K.!J-!H+!J-!H+	e
 e
P	,
l ,
^T< Tng&\ g&TX@`< `F3v|| 3!, !u\ up!L !GFLL G\L \~ 4\"u*< u*pQ, Q 4\"%, % 0 0 4\"  #r   