
    vgl                     P    d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 d Zd Zy)	z
process_attendance_data.py

This module contains a function for processing attendance data
from Excel files and saving it to a database.
    )datetimeN)
Attendance)EmployeeShiftWorkType)Employeec                 ,    | r| j                  d      S d S )Nz%H:%M)strftime)time_objs    </var/www/horilla/attendance/views/process_attendance_data.pyformat_timer      s    )18W%;t;    c                 	   g }g }t        j                         j                         }| D cg c]  }|d   	 }}t        j                  j                  | D cg c]  }|d   	 c}d      D ci c]  }|j                  | }}t        j                  j                         D ci c]  }|j                  | }	}t        j                  j                         D 
ci c]  }
|
j                  |
 }}
t        j                  j                  |      j                  d      D ci c]%  }|j                  j                  |j                  f|' }}| D ]U  }d}	 |d   }|d   }|d   }|j!                  |      }|	j!                  |      }|j!                  |      }d}d}d}	 t#        j$                  |d	         j                         }||f|v rd
|d<   d}	 t#        j$                  |d         j                         }	 t#        j$                  |d         j                         }	 t#        j$                  |d   d      j)                         }	 t#        j$                  |d   d      j)                         }	 t#        j$                  |d   d      j)                         }	 t#        j$                  |d    d      j)                         }||j*                  s
d#| |d$<   d}|d%| d&|d'<   d}|d(| d&|d)<   d}||||k  rd*|d+<   d}||||k  rd,|d-<   d}|||k\  rd.|d/<   d}|||k\  rd0|d+<   d}|||k\  rd1|d-<   d}|rR|j-                  t        |||||t/              |t/              t/              t/              2
             ||f|||f<   n|j-                  |       X |rt        j                  j3                  |       |S c c}w c c}w c c}w c c}w c c}
w c c}w # t&        $ r}d|d<   d}Y d}~-d}~ww xY w# t&        $ r}d|d<   d}Y d}~$d}~ww xY w# t&        $ r}d|d<   d}Y d}~d}~ww xY w# t&        $ r}| d|d<   d}Y d}~d}~ww xY w# t&        $ r}| d|d<   d}Y d}~d}~ww xY w# t&        $ r}| d|d<   d}Y d}~d}~ww xY w# t&        $ r}| d!|d"<   d}Y d}~d}~ww xY w# t&        $ r+}t1        |       |d3<   |j-                  |       Y d}~d}~ww xY w)4ak  
    Process a list of attendance data dictionaries and save valid records to the database,
    while collecting error details for invalid records.

    Parameters:
        attendance_dicts (list of dict): A list of dictionaries containing attendance data.

    Returns:
        list: A list of dictionaries representing errors encountered during processing.
    zBadge IDT)badge_id__in	is_active)employee_id__badge_id__inemployee_idShiftz	Work typeNzAttendance datez8This employee's attendance for this date already exists.zAttendance ErrorFzGThe attendance date format is invalid. Please use the format YYYY-MM-DDzAttendance Date ErrorzCheck-in datezEThe Check-in date format is invalid. Please use the format YYYY-MM-DDzCheck-in Date ErrorzCheck-out datezFThe Check-out date format is invalid. Please use the format YYYY-MM-DDzCheck-out Date ErrorzCheck-inz%H:%M:%S)formatz of check-in timezCheck-in Errorz	Check-outz of check-out timezCheck-out ErrorzWorked hourz of worked hourszWorked Hours ErrorzMinimum hourz of minimum hourszMinimum Hour ErrorzInvalid Badge ID given zBadge ID ErrorzInvalid shift ''zShift ErrorzInvalid work type 'zWork Type Errorz?Attendance check-in date cannot be smaller than attendance datezCheck-in Validation ErrorzEAttendance check-out date never smaller than attendance check-in datezCheck-out Validation ErrorzAttendance date in futurez Attendance Date Validation Errorz"Attendance check in date in futurez#Attendance check out date in future)
r   shift_idwork_type_idattendance_dateattendance_clock_in_dateattendance_clock_inattendance_clock_out_dateattendance_clock_outattendance_worked_hourminimum_hourzOther Errors)r   todaydater   objectsfilterbadge_idr   allemployee_shiftr   	work_typer   select_relatedr   r   getpdto_datetime	Exceptiontimer   appendr   strbulk_create)attendance_dicts
error_listattendance_listr   d	badge_idsemp	employeesshiftshiftswt
work_typesattexisting_attendance_recordsattendance_datasaver#   r   r   employeer&   r   check_in_datecheck_out_date	exceptioncheck_in	check_outworked_hourr   s                                r   process_attendance_datarF      s    JONN!!#E )991:9I9 ##**1ABA!J-Bd + 
 	cI  8E7L7L7P7P7RSee""E)SFS-5-=-=-A-A-CDr",,"DJD %%,,&/ - 

.
'	# 
	!	!3#6#67<# # , Z/X	/&z2H&w/H*;7L !}}X.HJJx(E"|4I"O M!N"$..#$56#$&   o.2MMR $$67 !D "/O P U U W!##$45"$& >>#J/
$& NN#K0$&  nn#M2:$& !~~#N3J $&  x'9'96MhZ4X 01}3B8*A1N. ),q9   12 (_-H ?2Y $$?@ !D(^-G!M1_ $$@A !D*"e+3 $$FG !D( E)< $$?@ !D)!U*= $$@A !D&&$,!&%.(71>,7,A2@-8-C/:;/G%0%> #L+X,GH
 !!/2mZ/v &&7[ : C TD#@  ]   78 	  [   56 	  \   67 	  7@kAR4S 01  8A{BT5U 12  ;D+EU8V 45  ;D+EV8W 45V  	/14Y0@ON+o..	/s?  O
O-O#OO *O#AS3O(&P9&P$ (Q	(Q#2(R(R%C!S(	P1O>8S>PS	P!PSP!!S$	P?-P:4S:P??S	Q 
QSQ  S#	R,
Q<6S<RS	R"
RSR""S%	S.
R>8S>SS	S: S55S:)__doc__r   pandasr)   attendance.modelsr   base.modelsr   r   employee.modelsr   r   rF    r   r   <module>rM      s&      ( / $<}r   