
    vgA                        d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z ddlmZmZmZ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mZ ddlmZmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z% e"e! e#d      d                      Z&e"d        Z'e"e!d               Z(e"e!d               Z)e"e!d               Z*e"e!d               Z+e"e!d               Z,e"d        Z-y)zf
search.py

This is moduel is used to register end point related to the search filter functionalities
    N)datetime)parse_qs)JsonResponse)render)gettext_lazy)AttendanceActivityFilterAttendanceFiltersAttendanceOverTimeFilterLateComeEarlyOutFilter)AttendanceOverTimeForm)
AttendanceAttendanceActivityAttendanceLateComeEarlyOutAttendanceOverTimeAttendanceValidationCondition)paginator_qrystrtime_seconds)filtersubordinatesget_key_instancessortby)hx_request_requiredlogin_requiredmanager_can_enter)group_by_querysetattendance.view_attendancec                    d}g d}g }|t        | j                  j                               k(  r|D cg c]
  }|dk7  s	| }}| j                  j                         }| j                  j	                  d      }t        d      }t        j                  j                         }t        j                  j                         }	| j                  j	                  d      rt        | |	d      }	|!|j                  t        |j                        }|	j                  d      }
|	j                  d	      }|	j                  d
d	      }t        | j                  |
      j                  }
t        | j                  |      j                  }t        | j                  |      j                  }| j                   j#                  d      s't%        | |d      }t%        | |
d      }
t%        | |d      }t'        |      }t)        t        |       |j+                         D cg c]  \  }}|dgk(  s||v r| }}}|D ]  }|j-                  |        |t        | j                  j                               k(  rS|x}}
d}|sKt/        j0                  | j                  j	                  d      d      }t3        |j5                  d            }d}d}g g g }}}|dk7  r|t7        ||| j                  j	                  d      d      }|D cg c]  }|d   	 }}g }|D ].  }|j8                  D ]  }|j;                  |j<                          0 t?        j@                  t        |            }t7        |
|| j                  j	                  d      d      }
|
D cg c]  }|d   	 }}g }|D ].  }|j8                  D ]  }|j;                  |j<                          0 t?        j@                  t        |            }t7        ||| j                  j	                  d      d      }|D cg c]  }|d   	 }}g }|D ].  }|j8                  D ]  }|j;                  |j<                          0 t?        j@                  t        |            }d}ntC        |
| j                  j	                  d            }
tC        || j                  j	                  d            }tC        || j                  j	                  d            }t?        j@                  |
j8                  D cg c]  }|j<                   c}      }t?        j@                  |j8                  D cg c]  }|j<                   c}      }t?        j@                  |j8                  D cg c]  }|j<                   c}      }tE        | ||
||||||||||d      S c c}w c c}}w c c}w c c}w c c}w c c}w c c}w c c}w )z3
    This method is used to search attendances
     employee_idattendance_validatedattendance_date__gteattendance_date__lter   fieldz00:00r   F)r    Tr   )overtime_second__gtr    r   unknownz.attendance/attendance/validate_attendance.htmlr!   z%Y-%m-%dz%Bz4attendance/attendance/validate_attendance_empty.htmlz&attendance/attendance/tab_content.htmlpagelistvpageopagez#attendance/attendance/group_by.html)validate_attendancesattendancesovertime_attendancesvalidate_attendances_idsot_attendances_idsattendances_idspdr#   filter_dict
month_nameminot)#r'   GETkeys	urlencodegetr   r   objectsfirstr   allr   minimum_overtime_to_approvefilterr	   qsuserhas_permr   r   r   itemspopr   strptime_strftimer   object_listappendidjsondumpsr   r   )requestr2   paramsremove_paramsparamprevious_datar#   r3   	conditionall_attendancesr*   r+   ot_attendances	data_dictkeyvaluekeys_to_removetemplatedate_objectr-   r.   r/   entrylist_valuesid_listinstances                             +/var/www/horilla/attendance/views/search.pyattendance_searchr]   #   s    JF Mgkk&&()),2M5e}6LMMKK))+MKKOOG$EG$E-55;;=I ((,,.O{{x  /8D!F!F!R	 E EF*11u1M!((d(CK$++! , N
 -W[[:NORR#GKK=@@K&w{{NCFFN<<  !=>([">
  2)+G 
 ,^%A
 'Ij), $//+CYK3-#7 	N 
  cgkk&&())0;;-C"++ 67K ;//56JMH7HDFB/0{u(' 7
 3>>uV}>>  	,E!-- ,x{{+,	, **T']30 %)A7 
 3GGuV}GG  	,E!-- ,x{{+,	, $(::d7m#< *E7;;??7#;W
 3AAuV}AA  	,E!-- ,x{{+,	, "ZZW68, '++//'": 
 '~w{{w7OP#K1HI#'::)=)I)IJXX[[J$
  "ZZ)7)C)CDXX[[D
 **)4)@)@AXX[[A
 $8&$2(@"4.$$	
 K NF. ? H B K E Bs4   
V"V"V'8V-V2V7V<8W/Wc           
         | j                   j                  d      }| j                   j                         }t        | j                         j                  }t               }d}|j                  | j                        }t        | |d      }t        | |d      }||z  }|j                         }t        |      }t        t        |       |j                         D 	cg c]  \  }}	|	dgk(  s| }
}}	|
D ]  }|j                  |        |dk7  r,|*t!        ||| j                   j                  d      d      }d	}n%t#        || j                   j                  d            }t%        | ||||||d
      S c c}	}w )zP
    This method is used to search attendance overtime account by employee.
    r#   z0attendance/attendance_account/overtime_list.htmlemployee_id__employee_user_idr   "attendance.view_attendanceovertimer%   r   r&   z+attendance/attendance_account/group_by.html)accountsformr0   r#   r1   )r4   r7   r6   r
   r=   r   r<   r>   r   r   distinctr   r   r   r@   rA   r   r   r   )rJ   r#   rN   rb   rc   rV   self_accountrR   rS   rT   rU   s              r\   attendance_overtime_searchrf      sb   
 KKOOG$EKK))+M'477H!#DAH???NLgx2H!?H ,&H  "H'I()4,5OO,=Vjc59+AUcVNV c{u($Xugkkoof6MvV@ 7;;??6+BC $	

 
 Ws   E1)E1c           
      &   | j                   j                         }| j                   j                  d      }t        | j                         j                  }|j                  | j                        }t        | |d      }||z  }|j                         }d}t        | |d      }|dk7  r|t        ||| j                   j                  d      d      }|D cg c]  }|d	   	 }}g }|D ].  }	|	j                  D ]  }
|j                  |
j                          0 t        j                  t!        |            }d
}n\t#        || j                   j                  d            }t        j                  t#        |d      D 
cg c]  }
|
j                   c}
      }t%        |      }t'        t(        |       |j+                         D 	cg c]  \  }}	|	dgk(  s| }}}	|D ]  }|j-                  |        t/        | ||||||d      S c c}w c c}
w c c}	}w )z;
    This method is used to search attendance activity
    r#   r_   ra   z1attendance/attendance_activity/activity_list.htmlorderbyr   Nr&   r'   z,attendance/attendance_activity/group_by.htmlr%   )datar0   r#   r1   activity_ids)r4   r6   r7   r   r=   r<   r>   r   rd   r   r   rE   rF   rG   rH   rI   r'   r   r   r   r   r@   rA   r   )rJ   rN   r#   attendance_activitiesself_attendance_activitiesrV   rX   rY   rZ   rT   r[   rj   rR   rS   rU   s                  r\   attendance_activity_searchrm      s    KK))+MKKOOG$E4b  "7!=!=&-ll "> " /&(L 24NN1::<BH"7,A9M{u( 1!5'++//&*A6!
 3HHuV}HH  	,E!-- ,x{{+,	, zz$w-0A -!7;;??6#:!
 zz)67Ld)STXX[[T
 'I()4,5OO,=Vjc59+AUcVNV c)$(	

 
' I U Ws   H:HHHc           
      &   | j                   j                  d      }| j                   j                         }t        | j                         j                  }|j                  | j                        }t        | |d      }||z  }|j                          t        | |d      }d}|dk7  r|d}t        ||| j                   j                  d      d      }|D cg c]  }|d	   	 }}g }|D ].  }	|	j                  D ]  }
|j                  |
j                          0 t        j                  t!        |            }n\t#        || j                   j                  d            }t        j                  |j                  D 
cg c]  }
|
j                   c}
      }t%        |      }t'        t(        |       |j+                         D 	cg c]  \  }}	|	d
gk(  s| }}}	|D ]  }|j-                  |        t/        | ||||||d      S c c}w c c}
w c c}	}w )zp
    This method is used to search late come early out by employee.
    Also include filter and pagination.
    r#   r_   z*attendance.view_attendancelatecomeearlyoutr   z/attendance/late_come_early_out/report_list.htmlr   z,attendance/late_come_early_out/group_by.htmlr&   r'   r%   )ri   r0   r#   r1   late_in_early_out_ids)r4   r7   r6   r   r=   r<   r>   r   rd   r   r   rE   rF   rG   rH   rI   r'   r   r   r   r   r@   rA   r   )rJ   r#   rN   reportsself_reportsrV   rX   rY   rZ   rT   r[   ro   rR   rS   rU   s                  r\   late_come_early_out_searchrr     s    KKOOG$EKK))+M$b  >>>ML FG $GWgx0G@H{u(A#GUGKKOOF4KVT29:uV}::  	,E!-- ,x{{+,	, !%

4= 9)@A $

)0)<)<=XX[[=!
 'I0)<,5OO,=Vjc59+AUcVNV c $%:	

 
% ; >
 Ws   H:HHHc           
         g d}g }|t        | j                  j                               k(  r|D cg c]  }|dk7  r|dk7  r| }}t        j                  j                  | j                  j                        }t        | j                  |      j                  }| j                  j                         }t        |      }| j                  j                  d      }d}| j                  j                         }|j                         D 	
cg c]  \  }	}
|
dgk(  s|	|v r|	 }}	}
|D ]  }	|j                  |	        t        j                   t#        || j                  j                  d	            j$                  D cg c]  }|j&                   c}      }|d
k7  r-|+t)        ||| j                  j                  d	      d	      }d}g }t+        | |t#        || j                  j                  d	            ||||d      S c c}w c c}
}	w c c}w )z7
    This method is used to filter own attendances
    r   r!   r"   r   )querysetr#   *attendance/own_attendance/attendances.htmlr%   r&   r   z'attendance/own_attendance/group_by.html)r+   r1   r/   r0   r#   )r'   r4   r5   r   r8   r<   r>   employee_getr	   r=   r6   r   r7   r@   rA   rH   rI   r   rE   rG   r   r   )rJ   rK   rL   rM   r+   rN   rR   r#   rV   rS   rT   rU   r[   r/   s                 r\   filter_own_attendancerx   D  s   F Mgkk&&())  
..5<R3R 
 
 $$++8Q8Q+RK#GKK+FIIKKK))+M'IKKOOG$E;HKK))+M $//+CYK3-#7 	N 
  cjj *W[[__V4k		
 KK	
O {u(' 7
 =(gkkoof6MN$.	

 
E
	
s   G=
H;Hc           
         t         j                  j                  | j                  j                        }| j
                  j                         }t        | |d      }t        | dt        || j
                  j                  d            |d      S )z5
    This method is used to sort out attendances
    rt   rh   rv   r&   )r+   r0   )r   r8   r<   r>   rw   r4   r6   r   r   r   r7   )rJ   r+   rN   s      r\   own_attendance_sortrz     sy     $$++8Q8Q+RKKK))+M+y9K4(gkkoof6MN	
     c                 "   | j                   j                  d      }t        j                  j	                         }| j                   j                  d      rt        | |d      }|j                  dd      }t        | d|      }||j                  | j                  d      z  }t        | j                   |      j                  }t        | d|j	                               }||j                  | j                        z  }t        | j                   |      j                  }| j                   j                         }t        |      }t        t        |       |j                         D cg c]  \  }}|d	gk(  s| }	}}|	D ]  }|j                  |        d
}
t!        j"                  t%        || j                   j                  d            j&                  D cg c]  }|j(                   c}      }t!        j"                  t%        || j                   j                  d            j&                  D cg c]  }|j(                   c}      }|dk7  rS|Qt+        ||| j                   j                  d      d      }t+        ||| j                   j                  d      d      }d}
nJt%        || j                   j                  d            }t%        || j                   j                  d            }t-        | |
|||||||d      S c c}}w c c}w c c}w )Nr#   r   T)is_validate_requestemployee_id__is_activer   )rJ   permru   )r`   r}   r_   r%   z&requests/attendance/request_lines.htmlrpager&   r   z!requests/attendance/group_by.html)requestsr+   requests_idsr/   r0   r1   r#   )r4   r7   r   r8   r:   r   r<   r   r>   r	   r=   r6   r   r   r@   rA   rH   rI   r   rE   rG   r   r   )rJ   r#   all_attendancer   r+   rN   rR   rS   rT   rU   rV   r[   r   r/   s                 r\   search_attendance_requestsr     s    KKOOG$E''++-N{{x B$$  % H ")H
 .//&-ll  0  H !h7::H$)##%K
  5 5&-ll !6 ! K $GKK=@@KKK))+M'Ij),,5OO,=Vjc59+AUcVNV c 8H:: *'++//'2k		
 KK	
L jj *W[[__V4k		
 KK	
O {u($Xugkkoog6NPWX' 7
 7 7;;??7+CD#K1HI &(.$	
 = W	
	
s   &L5LL0Lc                     t        | j                        j                  j                  dd      }t	        dt        |      i      S )zD
    This method is used to return all the ids of the employees
    rG   T)flatids)r	   r4   r=   values_listr   r'   )rJ   r   s     r\   widget_filterr     s<    
 GKK
(
+
+
7
74
7
HCS	*++r{   ).__doc__rH   r   urllib.parser   django.httpr   django.shortcutsr   django.utils.translationr   rC   attendance.filtersr   r	   r
   r   attendance.formsr   attendance.modelsr   r   r   r   r   attendance.views.viewsr   r   base.methodsr   r   r   horilla.decoratorsr   r   r   horilla.group_byr   r]   rf   rm   rr   rx   rz   r   r    r{   r\   <module>r      s4     ! $ # 6  4  B F F U U . /0B 1  BJ % %P 4  4n 2  2j 8  8v   " J  JZ , ,r{   