
    vgc                        d Z ddlZddl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mZ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  G d dej2                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Z G d d      Z  G d d      Z! G d d      Z" G d d      Z#d Z$ejJ                  Z&d  Z'e'e_%        y)!zI
filters.py

This page is used to register filter for attendance models

    N)forms)DateTimeInput)gettext_lazy)
AttendanceAttendanceActivityAttendanceLateComeEarlyOutAttendanceOverTimestrtime_seconds)	FilterSet)EmployeeFilter)Employee)filter_by_namec                       e Zd ZdZd Zy)DurationInSecondsFilterzG
    Custom CharFilter class that applies specific filter process.
    c                     |rpg d}t        d t        |t        t        |j	                  d                  D              }| j
                  xs d} |j                  di | j                   d| |iS |S )z
        FilterSet filter method

        Args:
            qs (self): FilterSet instance
            value (str): duration formatted string

        Returns:
            qs: queryset object
        )i  <      c              3   ,   K   | ]  \  }}||z    y w)N ).0abs      &/var/www/horilla/attendance/filters.py	<genexpr>z1DurationInSecondsFilter.filter.<locals>.<genexpr>/   s     VAq1uVs   :exact__r   )sumzipmapintsplitlookup_exprfilter
field_name)selfqsvalueftrduration_seclookups         r   r$   zDurationInSecondsFilter.filter"   sq     CVS#c5;;sCS:T1UVVL%%0F299N4??"32fX >MNN	    N)__name__
__module____qualname____doc__r$   r   r,   r   r   r      s    r,   r   c                   
    e Zd ZdZd ed      fd ed      fd ed      fd ed      fd ed      fd ed      f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                  e      Z	 e
dd      Z e
dd      Z e
dd      Z e
dd      Z e
dd      Z e
dd      Z ej"                  ed      Z ej                  dd      Z G d d      Zdddd fdZ xZS )AttendanceOverTimeFilterz
    Filter set class for AttendanceOverTime model

    Args:
        FilterSet (class): custom filter set class to apply styling
    JanuaryFebruaryMarchAprilMayJuneJulyAugust	SeptemberOctoberNovemberDecembermethodhour_account_secondgter%   r#   ltehour_pending_secondovertime_second	icontains)choicesr#   :employee_id__employee_work_info__department_id__departmentc                       e Zd ZdZeZg dZy)AttendanceOverTimeFilter.Meta6
        Meta class to add additional options
        )employee_idmonthovertimeworked_hoursyeardepartment_name.employee_id__employee_work_info__department_id+employee_id__employee_work_info__company_id0employee_id__employee_work_info__job_position_id)employee_id__employee_work_info__location5employee_id__employee_work_info__reporting_manager_id)employee_id__employee_work_info__shift_id-employee_id__employee_work_info__work_type_idN)r-   r.   r/   r0   r	   modelfieldsr   r,   r   MetarK   e   s    	 #
r,   r\   Nrequestprefixc                   t         |   ||||       | j                  j                  j	                         D ]E  }t        j                          | j                  j                  |   j                  j                  d<   G y N)dataquerysetr^   r_   id	super__init__formr[   keysuuiduuid4widgetattrsr&   rb   rc   r^   r_   field	__class__s         r   rg   z!AttendanceOverTimeFilter.__init__{   j    dXwvVYY%%**, 	KE<@JJL>DIIU#**006	Kr,   NN)r-   r.   r/   r0   _MONTH_CHOICESdjango_filters
CharFilterr   searchr   worked_hours__gteworked_hours__ltepending_hours__ltepending_hours__gteovertime__gteovertime__lteChoiceFilterrN   rR   r\   rg   __classcell__rp   s   @r   r2   r2   5   sk    
AiL!	Qz]#	!G*	!G*	%	6	6	1X;	an%	AiL!	Qz]#	Qz]#M '^&&n=F/(e 0(e 1(e 1(e ,$%M ,$%M (N'';WE/n//OO

 
,KD K Kr,   r2   c                       e Zd ZdZ ej
                  e      Z ej                  e	j                  j                          ej                               Z ej                  dd ej                   ddi      	      Z ej                  dd
 ej                   ddi      	      Z ej&                  d ej(                  ddi      d
      Z ej&                  d ej(                  ddi      d      Z ej&                  d ej(                  ddi      d      Z ej&                  d ej(                  ddi      d
      Z ej&                  d ej(                  ddi      d      Z ej&                  d ej(                  ddi      d      Z ej                  d ej                   ddi            Z edd
      Z edd      Z edd
      Z edd      Z  ej
                  dd      Z! ej
                  dd      Z" ej
                  dd      Z# ej
                  dd      Z$ G d d      Z%dddd fdZ& xZ'S )LateComeEarlyOutFilterz&
    LateComeEarlyOutFilter class
    r?   rc   rl   attendance_id__attendance_daterB   typedaterm   r%   r#   rl   rD   "attendance_id__attendance_clock_intimer%   rl   r#   #attendance_id__attendance_clock_outr   r%   rl   attendance_id__overtime_secondrC   attendance_id__at_work_secondrI   rG   rQ   rN   weekc                       e Zd ZdZeZg dZy)LateComeEarlyOutFilter.Metaz*
        Meta class for additional options)rM   r   attendance_id__minimum_hour%attendance_id__attendance_worked_hour*attendance_id__attendance_overtime_approve#attendance_id__attendance_validatedrS   rT   rU   rV   rW   attendance_id__shift_idattendance_id__work_type_idattendance_date__gteattendance_date__lteattendance_clock_in__lteattendance_clock_in__gteattendance_clock_out__gteattendance_clock_out__lteattendance_clock_inattendance_clock_outattendance_date
departmentrQ   rN   r   N)r-   r.   r/   r0   r   rZ   r[   r   r,   r   r\   r      s    	- +
r,   r\   Nr]   c                   t         |   ||||       | j                  j                  j	                         D ]E  }t        j                          | j                  j                  |   j                  j                  d<   G y ra   re   rn   s         r   rg   zLateComeEarlyOutFilter.__init__   rq   r,   rr   )(r-   r.   r/   r0   ru   rv   r   rw   ModelMultipleChoiceFilterr   objectsallr   SelectMultiplerM   
DateFilter	DateInputr   r   
TimeFilter	TimeInputr   r   r   r   r   r   r   r   overtime_second__lteovertime_second__gteat_work_second__lteat_work_second__gter   rQ   rN   r   r\   rg   r   r   s   @r   r   r      s    '^&&n=F:.::!!%%'#u##%K 5>443uff%56
 5>443uff%56
  9~887uff%56 
  9~887uff%56 
 !: 9 98uff%56!
 !: 9 98uff%56!
 4.337uff%56
 5>448uff%56
 0n//3uff%56O 33 33 22 22 +**OJ %>$$3D &N%%3E %>$$3D 
  
DKD K Kr,   r   c                   0    e Zd ZdZ ej
                  e      Z ej                  d e	j                  ddi            Z ej                  dd e	j                  ddi      	      Z ej                  dd
 e	j                  ddi      	      Z ej                  dd e	j                  ddi      	      Z ej                  dd e	j                  ddi      	      Z ej                  dd
 e	j                  ddi      	      Z ej                  dd
 e	j                  ddi      	      Z ej                  d e	j                  ddi            Z ej                  d e	j                  ddi            Z G d d      Zdddd fdZ xZS )AttendanceActivityFilterz
    Filter set class for AttendanceActivity model

    Args:
        FilterSet (class): custom filter set class to apply styling
    r?   r   r   r   r   r   rB   r   rD   clock_inr   	clock_outclock_in_dateclock_out_datec                       e Zd ZdZg dZeZy)AttendanceActivityFilter.MetarL   )rM   r   attendance_date_fromattendance_date_tillin_fromin_tillout_from	shift_dayout_tillr   r   rS   rT   rX   rY   rU   rV   rW   N)r-   r.   r/   r0   r[   r   rZ   r   r,   r   r\   r   +  s    	
( #r,   r\   Nr]   c                   t         |   ||||       | j                  j                  j	                         D ]E  }t        j                          | j                  j                  |   j                  j                  d<   G y ra   re   rn   s         r   rg   z!AttendanceActivityFilter.__init__F  rq   r,   rr   )r-   r.   r/   r0   ru   rv   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r\   rg   r   r   s   @r   r   r      s    '^&&n=F/n//$_U__FFCS-TO 5>44$uff%56
 5>44$uff%56
 (n''uff%56G
 )~((uff%56H
 (n''uff%56G
 )~((uff%56H
 .N--"?5??&&AQ+RM /^..#OEOO66BR,SN# #6KD K Kr,   r   c                   6    e Zd ZdZ ej
                  d      Z ej                  d      Z ej                  d      Z	 ej                  d      Z ej                  ej                  j                          ej"                               Z ej                  dd	 ej&                  d
di            Z ej                  dd ej&                  d
di            Z ej,                  d ej.                  d
di      d      Z ej,                  d ej.                  d
di      d	      Z ej,                  d ej.                  d
di      d	      Z ej,                  d ej.                  d
di      d      Z ej,                  d ej.                  d
di            Z ej,                  d ej.                  d
di            Z ej                   ej&                  d
di            Z ed      Z  ed      Z! edd      Z" edd	      Z# edd      Z$ edd	      Z% ej                  dd      Z& ej                  dd      Z' ej                  dd      Z( ej                  dd      Z)d Z* G d d       Z+d%d!d!d" fd#Z,d$ Z- xZ.S )&AttendanceFiltersz~
    Filter set class for Attendance model

    Args:
        FilterSet (class): custom filter set class to apply styling
    rd   )r%   r   r?   employee_id__idr   r   rB   r   r   r   r   rD   r   r   r   r   r   )rl   filter_pending_hourat_work_secondrC   rF   rQ   rN   r   rI   rG   c                 @   |nt        |      }g }|D ]\  }t        |j                        }|j                  }||z
  }|dk(  r||k\  s4|j                  |       F||k  sL|j                  |       ^ |j	                  D cg c]  }|j
                   c}      S c c}w )a  
        This method calculates the pending hours for each attendance record in the
        queryset and filters the records based on whether the pending hours are less
        than or equal to (`pending_hour__lte`) or greater than the specified value.
        pending_hour__lteid__in)r
   minimum_hourr   appendr$   rd   )	r&   rc   namer(   filtered_attendance
attendanceminimum_hour_secondworked_hour_secondpending_hour_seconds	            r   r   z%AttendanceFilters.filter_pending_hour  s     #E*E"$& 	?
&5j6M6M&N#%/%>%>"&9<N&N#.. 33+22:> 33+22:>	? 4GHjJMMH  
 	
Hs    Bc                   6    e Zd ZdZeZg dZd eddi      iZy)AttendanceFilters.MetarL   ) rd   rM   rS   rT   rU   rV   rW   attendance_dayr   work_type_idshift_idr   attendance_validatedr   r   r   rF   late_come_early_out__typeattendance_overtime_approver   is_validate_requestis_validate_request_approvedis_bulk_requestr   r   r   r   rF   r   rN   rQ   batch_attendance_idr   r   r   r   N)	r-   r.   r/   r0   r   rZ   r[   r   widgetsr   r,   r   r\   r     s.    	 !
H }FF3CD
r,   r\   Nr]   c                   t         |   ||||       | j                  j                  j	                         D ]E  }t        j                          | j                  j                  |   j                  j                  d<   G y ra   re   rn   s         r   rg   zAttendanceFilters.__init__  rq   r,   c                    ddddddd}| j                   j                  d      }|s~|j                         }|d	   }t        |      d
kD  rdj	                  |d
d       nd}|r|r|j                  ||      }|S |r|j                  |      }|S |r|j                  |      }|S |j                  |      }	 |j
                  di |	|i}|S )a  
        This method allows filtering by the employee's first and/or last name or by other
        fields such as day, shift, work type, department, job position, or company, depending
        on the value of `search_field` provided in the request data.
        attendance_day__day__icontains#shift_id__employee_shift__icontains"work_type_id__work_type__icontainsEemployee_id__employee_work_info__department_id__department__icontainszYemployee_id__employee_work_info__                job_position_id__job_position__icontains?employee_id__employee_work_info__company_id__company__icontains)dayshift	work_typer   job_positioncompanysearch_fieldr   r    N )+employee_id__employee_first_name__icontains*employee_id__employee_last_name__icontains)r   )r   r   )rb   getr"   lenjoinr$   )
r&   rc   r   r(   filter_methodr   parts
first_name	last_namer$   s
             r   r   z AttendanceFilters.filter_by_name  s    4:=a:X
 yy}}^4KKMEqJ/25zA~qr+2I i#??@J?H +    #??@J +   #???H +   #&&|4F&x9&%9Hr,   rr   )/r-   r.   r/   r0   ru   NumberFilterrd   rv   rw   employeer   date_attendancer   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   pending_hour__gter   r   r   r   rQ   rN   r   r   r   r\   rg   r   r   r   s   @r   r   r   L  s    
%	$	$	5B&^&&.>?F(~((4EFH/n//;LMO:.::!!%%'#u##%K
 5>44$uff%56
 5>44$uff%56  9~88(uff%56 
  9~88(uff%56 
 !: 9 9)uff%56!
 !: 9 9)uff%56!
 4.33(uff%56 5>44)uff%56
 0n//uff%56O 0$ 0$ 2# 2# 3$% 3$% %>$$0AvVD%N%%1BPWXE$>$$0AvVD***OJ

.+
 +
ZKD K
(r,   r   c                       e Zd ZdZg dZy)LateComeEarlyOutReGroup:
    Class to keep the field name for group by option
    )r   SelectrM   r   )r   Type)r   Attendance Date)r   Shift)r   	Work Type)r   Minimum Hour)#attendance_id__employee_id__countryCountry)Dattendance_id__employee_id__employee_work_info__reporting_manager_idReporting Manager)=attendance_id__employee_id__employee_work_info__department_id
Department)?attendance_id__employee_id__employee_work_info__job_position_idJob Position)@attendance_id__employee_id__employee_work_info__employee_type_idEmployment Type):attendance_id__employee_id__employee_work_info__company_idCompanyNr-   r.   r/   r0   r[   r   r,   r   r   r     s    Fr,   r   c                       e Zd ZdZg dZy)AttendanceReGroupr   )r   r   r   Batchr   r  )r   r  )r   r  )r   r  employee_id__countryr  rW   r  rS   r
  rU   r  1employee_id__employee_work_info__employee_type_idr  rT   r  Nr  r   r,   r   r  r  5  s    Fr,   r  c                       e Zd ZdZg dZy)AttendanceOvertimeReGroupr   )r   r   )rN   Month)rQ   Yearr  r  rX   r  rY   r  r  r  r  r  Nr  r   r,   r   r   r   K  s    Fr,   r   c                       e Zd ZdZg dZy)AttendanceActivityReGroupr   )r   r   r  )r   In Date)r   Out Date)r   z	Shift Dayr  r  r#  r$  r  r  r  r  Nr  r   r,   r   r&  r&  `      Fr,   r&  c                       e Zd ZdZg dZy)AttendanceRequestReGroupr   )r   r   r  )r   r  )attendance_clock_in_dater'  )attendance_clock_out_dater(  r  r  r#  r$  r  r  r  r  Nr  r   r,   r   r+  r+  w  r)  r,   r+  c                 F   t         j                   j                         j                         }|t        j                  d      z
  }t        j
                  j                  ||d      j                  dd      }|r| j                  |      } | S | j                  |      } | S )Nr   )daysT)r   r   !attendance_clock_out_date__isnullrM   )flatr   )	datetimenowr   	timedeltar   r   r$   values_listexclude)rc   _namer(   today	yesterdayworking_employeess         r   get_working_todayr;    s    !!#((*E**22I"**11&"*. 2  k-dk+	  ??*;?< O ##+<#=Or,   c                 8   t        | g|i | t        j                  dt              }|| j                  d<   |j
                  | j                  j                  d<   | j                  j                  d   j                  j                  j                  ddi       y )NWorking)labelr@   working_todayclasszoh-select oh-select-2 w-100)og_initru   BooleanFilterr;  filtersro   rh   r[   rl   rm   update)r&   argskwargscustom_fields       r   online_initrH    s    D"4"6"!// 1L %1DLL!(4(:(:DII_%II_%,,22992	
r,   )(r0   r2  rj   ru   djangor   django.formsr   django.utils.translationr   rs   attendance.modelsr   r   r   r	   r
   base.filtersr   employee.filtersr   employee.modelsr   horilla.filtersr   rv   r   r2   r   r   r   r   r  r   r&  r+  r;  rg   rA  rH  r   r,   r   <module>rQ     s        & 6  # + $ *n77 0IKy IKXuKY uKpPKy PKfG	 GT > , * . ." 
!
! & r,   