
    vg:                        d Z ddl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 ddlmZmZmZ dd	lmZmZmZmZ dd
lmZmZmZ ddlmZ ddlmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddZ*d Z+e'd        Z,e'e&d               Z-e'e&d               Z.e'e&d               Z/ddZ0d dZ1d dZ2d Z3e'd        Z4d Z5e'd        Z6y)!zX
dashboard.py

This module is used to register endpoints for dashboard-related requests
    N)datedatetime)apps)JsonResponse)render)gettext_lazy)AttendanceFiltersAttendanceOverTimeFilterLateComeEarlyOutFilter)get_month_start_end_datesget_week_start_end_datespending_hour_dataworked_hour_data)
AttendanceAttendanceLateComeEarlyOutAttendanceValidationCondition)strtime_seconds)filtersubordinatespaginator_qry)
Department)Employee)settings)hx_request_requiredlogin_required)get_horilla_model_classc                     d}t         j                  j                  |      }||j                  |      }t        j                  j                  |d      }t	        |      t	        |      z
  }|S )zC
    This method is used to find count for on time attendances
    r   )attendance_date).employee_id__employee_work_info__department_id	late_come)attendance_id__attendance_datetype)r   objectsfilterr   len)requesttodayweek_day
departmenton_timeattendancesr   s          ./var/www/horilla/attendance/views/dashboard.pyfind_on_timer,   (   s|    
 G$$++E+BK!((;E ) 
 +2299',; : I +Y/GN    c                     t         j                  j                  d      }t        j                  d      r*t        dd      }|j                  j                  d      }ng }t        |      t        |      z
  }|S )zT
    This method is used to find count of expected attendances for the week day
    T)	is_activeleaveleaverequest)	app_labelmodelApproved)status)r   r"   r#   r   is_installedr   r$   )r'   	employeesLeaveRequeston_leaveexpected_attendancess        r+   find_expected_attendancesr;   :   sn       ''$'7I!.W''..j.Ay>CM9r-   c                 P   t        j                         }|j                  d      j                         }t	        | ||      }t        |      }t        |      }||z   }t        |      }d}d}	d}
|dk7  r||z  dz  d}||z  dz  d}	||z  dz  d}
t        | d||||	|||
d	      S )
zR
    This method is used to render individual dashboard for attendance module
    z%A)r&   r'   )
start_date)r'   r   d   z.2fz#attendance/dashboard/dashboard.html)r)   on_time_ratior   late_come_ratior:   marked_attendancesmarked_attendances_ratio)	r   r&   strftimelowerr,   find_late_comer$   r;   r   )r%   r&   r'   r)   r   late_come_objrA   r:   r?   r@   rB   s              r+   	dashboardrG   H   s     NNE~~d#))+H7%(CG%0I	NM&04hGMO q #&::cA#F+.BBcI#N"%99S@E 	! -*&.$8"4(@	
 r-   c                     t        j                         }t        j                  j	                  d|      }d|i}t        | d|      S )a\  
    Fetches and displays employees who left early (early outs) for the current day.

    This view retrieves all records from the `AttendanceLateComeEarlyOut` model
    where the type is "early_out" and the associated attendance date matches the current date.
    The results are passed to the template `on_break_employees.html` for rendering
    	early_out)r!   r    on_breakz,attendance/dashboard/on_break_employees.html)r   r&   r   r"   r#   r   )r%   r&   
early_outscontexts       r+   on_break_employeesrM   q   sP     NNE+33:: ; J 	JG 'I7SSr-   c                 ~   d}| j                   j                  dd      }ddj                  |j                  d      dd       z   }|dk(  rd}| j                  j                  d      }t
        j                  j                         }t        d      }|!|j                  t        |j                        }t        j                  j                  |ddd	      }t        | d
|      }|D cg c]  }|j                   }}t        j                  t!        |            }	t#        ||      }||	|d}
t%        | d|
      S c c}w )a  
    Displays and validates employee overtime records for the dashboard.

    This view retrieves a paginated list of employee attendance records with
    overtime that meets or exceeds a specified minimum threshold, which is defined
    in the `AttendanceValidationCondition` model. Only records that are validated
    but not yet approved are included, and the results are filtered based on the
    user's subordinate permissions.
    NHTTP_REFERER/   Tpage00:00Fovertime_second__gteattendance_validatedemployee_id__is_activeattendance_overtime_approveattendance.change_overtimer%   permqueryset)overtime_attendancesot_attendances_idsmain_dashboardz(attendance/dashboard/overtime_table.html)METAgetjoinsplitGETr   r"   firstr   minimum_overtime_to_approver   r#   r   idjsondumpslistr   r   )r%   r_   refererpage_number	conditionmin_otot_attendancesotid_listr^   rL   s              r+   dashboard_approve_overtimesrr      s:    Nll~s3GCHHW]]3/344G#~++//&)K-55;;=IW%F!F!F!R !F!FG''..#!#$)	 / N ()N  ..ruu.G.DM2">;?N .0(G
 'EwOO /s   (D:c                    d}| j                   j                  dd      }ddj                  |j                  d      dd       z   }|dk(  rd}| j                  j                  d      }t
        j                  j                  dd      }t        | d	|
      }|D cg c]  }|j                   }}t        j                  t        |            }t        ||      }|||d}t        | d|      S c c}w )aG  
    Displays and validates employee attendance records for the dashboard.

    This view retrieves a paginated list of attendance records that have not yet
    been validated. Only records belonging to active employees and accessible
    subordinates, as determined by the user's permissions, are included in the results.
    NrO   rP   rQ   TrR   F)rV   rW   rY   rZ   )validate_attendancesvalidate_attendances_idsr_   z+attendance/dashboard/to_validate_table.html)r`   ra   rb   rc   rd   r   r"   r#   r   rg   rh   ri   rj   r   r   )	r%   r_   rk   rl   rt   valvalidate_id_listru   rL   s	            r+   dashboard_validate_attendancesrx      s     Nll~s3GCHHW]]3/344G#~++//&)K%--44"4 5  .)% +??3??#zz$/?*@A()={K 4$<(G
 'H'RR @s   C,c                 8    t        | ||d      j                  }|S )z6
    This method is used to find total attendance
    )attendance_date__gteattendance_date__lter(   )r	   qs)r=   r(   end_date
attendances       r+   total_attendancer      s.     #$.$,$	
 	b  r-   c                 t    |t        d|| |d      j                  }|S t        d| |d      j                  }|S )z1
    This method is used to find late comers
    r   r!   r(   rz   r{   r!   rz   r{   r   r|   )r=   r(   r}   rF   s       r+   rE   rE      c     .#((2(0	
 " 	   /#(2(0
 " 	 r-   c                 t    |t        d|| |d      j                  }|S t        d| |d      j                  }|S )zT
    This method is used to find early out attendances and it returns query set
    rI   r   r   r   )r=   r}   r(   early_out_objs       r+   find_early_outr      r   r-   c                 h   g }g }g }d}|dk(  r|}|}|dk(  rt        |      \  }}|dk(  rt        |      \  }}|dk(  r|}|}t        |||      }t        |||      }t	        |||      }t        |      t        |      z
  }i }	|s|s|r$|j                  |t        |      t        |      gd}	|	r|	S d	S )
z@
    This method is used to generate all the dashboard data
    r   dayweeklymonthly
date_ranger=   r(   r}   )r(   r=   r}   labeldataN)r   r   r   rE   r   r$   r(   )
r%   r=   r!   r}   deptr~   rF   r   r)   r   s
             r+   generate_data_setr     s     JMMGu}
x7
C
Hy8D
H|
 "$J #$M #JM *oM 22GD-=__c-0#m2DE

 4!T!r-   c           
         t        d      t        d      t        d      g}g }t        j                         }|}d}| j                  j	                  d      r| j                  j	                  d      }| j                  j	                  d      r| j                  j	                  d      }| j                  j	                  d      r| j                  j	                  d      }t
        j                  j                         }|D ]   }|j                  t        | ||||             " t        d      }t        t        d|            }t        |||d	      S )
z
    This method is used to render json response of dashboard data

    Returns:
        JsonResponse: returns data set as json
    zOn Timez	Late Comez	Early Outr   r!   r}   z#No records available at the moment.N)dataSetlabelsmessage)_r   r&   rd   ra   r   r"   allappendr   rj   r#   r   )	r%   r   data_setr=   r}   r!   departmentsr   r   s	            r+   dashboard_attendancer   F  s    	
)	+	+F HJHD {{v[[__V,
{{v{{v&{{z";;??:. $$((*K V)':tXtTUV56GF4*+HH7STTr-   c                     t        | j                        j                  }t        t        j
                  j                  dd            }|t        ||      t        ||      gd}t        d|i      S )z,
    pending hours chart dashboard view
    r(   T)flat)r   datasetsr   )
r
   rd   r|   rj   r   r"   values_listr   r   r   )r%   recordsr   r   s       r+   pending_hoursr   j  sl     'w{{366G*$$00D0IJFfg.VW-
D ''r-   c                 
   | j                   j                  d      r| j                   j                  d      nt        j                         }| j                   j                  d      r| j                   j                  d      nd}| j                   j                  d      r| j                   j                  d      n|}|dk(  r|}|}|dk(  rt	        |      \  }}|dk(  rt        |      \  }}|dk(  r|}|}t        |d |      }t        j                  j                         }t        d	      }|!|j                  t        |j                        }|j                  |d
d
d
      }g }g }	|D ]  }|j                  s|j                  j                  s'|j                  j                  j                  sH|j!                  |j                  j                  j                  j"                          t%        t'        |            }|D ]  }
|	j!                  |
dd        |D ]{  }|j                  j                  j                  s$|j                  j                  j                  j"                  }|j(                  }|dz  }|	D ]  }
|
d   |k(  s|
dxx   |z  cc<    } dg dg}t+        |	|      D ](  \  }}
|
|d   k(  s|d   d   j!                  |d          * |||	t-        d      dt.        j0                   dd}t3        |      S )Nr   r!   r   r}   r   r   r   r   rS   TrT   r   )r(   ot_hoursi  r(   r    r   r   z!No validated Overtimes were foundrP   zimages/ui/overtime-icon.png)datasetr   department_totalr   emptyImageSrc)rd   ra   r   r&   r   r   r   r   r"   re   r   rf   r#   employee_idemployee_work_infodepartment_idr   r(   rj   setapproved_overtime_secondzipr   r   
STATIC_URLr   )r%   r=   
chart_typer}   r~   rm   rn   r*   r   r   departr(   rp   ot_hrsr   depart_totalresponses                    r+   department_overtime_chartr   {  s   ,3KKOOF,C(J,3KKOOF,C(J'.{{z'B
#
  U
X7
C
HY8D
H\!
!$J .55;;=IW%F!F!F!R !F!FG###!#$(	 $ K K! 
""&&99&&99GG&&99GGRR s;'(K Gv1 EFG " 	1
!!44BB&&99GGRR  44B$YF* 1,':5:&&0&1	1 	
G !$$4k B @f\,//AJv%%l:&>?@
 ,89X0011LMH !!r-   )N)NN)7__doc__rh   r   r   django.appsr   django.httpr   django.shortcutsr   django.utils.translationr   r   attendance.filtersr	   r
   r   attendance.methods.utilsr   r   r   r   attendance.modelsr   r   r   attendance.views.viewsr   base.methodsr   r   base.modelsr   employee.modelsr   horillar   horilla.decoratorsr   r   horilla.methodsr   r,   r;   rG   rM   rr   rx   r   rE   r   r   r   r   r    r-   r+   <module>r      s    #  $ # 6 
  
 3 : " $  B 3$  % %P T  T$ )P  )PX !S  !SH00,"^  U  UF(" O" O"r-   