
    si}                        d Z ddlZddlZddlZddlmZmZ ddlmZmZ ddl	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 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"  ejF                  e$      Z%ejL                  dk(  Z'dD  ci c]  } | g  c} Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d Z8d Z9d Z:d  Z;d! Z<d" Z=d# Z>d$ Z?yc c} w )%z
employee/methods.py
    N)datedatetime)chaingroupby)apps)User)
connectionmodelstransaction)gettext)get_initial_prefix)Company
DepartmentEmployeeShiftEmployeeTypeJobPositionJobRoleWorkType)EmployeeEmployeeWorkInformation
postgresql)Badge ID
First Name	Last NamePhoneEmailGenderr   Job PositionJob Role	Work TypeShiftEmployee TypeReporting Managerr   LocationDate JoiningContract End DateBasic SalarySalary HourEmail ErrorFirst Name ErrorName and Email ErrorPhone ErrorGender ErrorJoining Date ErrorContract Date ErrorBadge ID ErrorBasic Salary ErrorSalary Hour ErrorUser ID ErrorCompany Errorc              #   V   K   t        dt        |       |      D ]  }| |||z      y w)Nr   )rangelen)iterablesizeis      </var/www/python-projects/worksol/employee/methods/methods.pychunkedr<   G   s3     1c(mT* %q1t8$$%s   ')c                     t        |       j                         } | j                  d      rdt        j                  dd| dd        z   S t        j                  dd|       S )N+z\D    )strstrip
startswithresub)phones    r;   normalize_phonerG   L   sS    JERVVE2uQRy11166%U##    c                 n   t        j                  |       s| t        |       j                         dk(  ry t	        | t
              r| j                         S t        |       j                         }g d}|D ](  }	 t        j                  ||      j                         c S  | d||<   y # t        $ r Y ?w xY w)Nr?   )z%Y-%m-%dz%d/%m/%Yz%m/%d/%Yz> is not a valid date. Expected formats: YYYY-MM-DD, DD/MM/YYYY)	pdisnarA   rB   
isinstancer   r   strptime
ValueError)
date_valuefield_labelerrors_dict	error_keydate_strdate_formatsfmts          r;   import_valid_daterV   S   s    	wwzj0C
O4I4I4Kr4Q*h'  :$$&H7L 	$$Xs388:: -UV 	   		s   8#B((	B43B4c                     t        j                  |       ry	 t        |       }|j                         rt	        t        |            S t	        |      S # t        t        f$ r t	        |       j                         cY S w xY w)a  
    Cleans and converts a badge ID value from Excel import.

    - If the value is a whole number (e.g., 5480.0), returns it as an integer string ("5480").
    - If the value is a decimal (e.g., 567.67), returns it as a float string ("567.67").
    - If the value is a non-numeric string (e.g., "A101"), returns the stripped string.
    - If the value is NaN or None, returns an empty string.
    r?   )	rJ   rK   float
is_integerrA   intrN   	TypeErrorrB   )value	float_vals     r;   clean_badge_idr^   i   sl     
wwu~"%L	!s9~&&y>!	" "5z!!"s   .A 
A (A=<A=c                 `    |j                  |       }	 t        |       y# t        $ r |cY S w xY w)z4
    This method is returns None or field value
    N)getrX   rN   )fielddictsfield_values      r;   convert_nanrd      s7     ))E"Kk s    --c                 ^    t        d      d   }t        |      dk\  rt        |      nd}| d| S )z7
    Sorts items based on a dynamic prefix length.
    Nr      )r   r7   )itemprefixprefix_lengths      r;   dynamic_prefix_sortrj      s7    
  %&:;F#&v;!#3CKMrH   c                  (   t         j                  j                         } | j                  d      j	                  d      j                  dd      }|j                         st        d      d    dg}|D cg c]  }|j                         st        |        }}|D cg c]  }|j                         r| }}t        |t              }t        |t              D cg c]  \  }}t        |       }}}|D ]G  }|j                          |D cg c]  }t        d	 |D              s| }}|j                  d
        I |D cg c]  }|d   |d   g }	}|r|	j!                  d|d   |d   g       |	S c c}w c c}w c c}}w c c}w c c}w )z9
    This method is used to return ordered badge ids
    N)badge_idrl   Tflatr   0001)keyc              3   <   K   | ]  }|j                           y wN)isdigit).0chars     r;   	<genexpr>z(get_ordered_badge_ids.<locals>.<genexpr>   s     )JT$,,.)Js   c                 f    t        dj                  t        t        j                  |                   S Nr?   )rZ   joinfilterrA   rs   )xs    r;   <lambda>z'get_ordered_badge_ids.<locals>.<lambda>   s    #bggfS[[!6L.M*N rH   r   )r   objectsallexcludeorder_byvalues_listfirstr   rs   rZ   sortedrj   r   listsortanyinsert)
	employeesdatarg   pure_numberssorted_data_groupgrouped_datafiltered_groupresults
             r;   get_ordered_badge_idsr      s      $$&I4(	*		Zd	+ 	
 ::<!$'(<=>dC
 +/A$$,,.CIALA "8TD8D8 #67K %,K=P$Q 5UL 
  P

"
c)JT)J&JD
 
 	 NOP 2>>uQxr#>F> a,q/<+;<=M7 B 9
 ?s0   7E:E: E?6E?!FF
*F
	Fc                 v   g }| j                   j                         D ]  }t        |t        j                        r1|j
                  t        k(  r|j                  |j                  df       Nt        |t        j                        si|j
                  t        k(  s}|j                  |j                  df        |S )af  
    Checks the relationship of a given model with the Employee model.

    This function iterates through all the fields of the specified model
    and identifies fields that are either `ForeignKey` or `ManyToManyField`
    and are related to the `Employee` model. For each such field, it adds
    the field name and the type of relationship to a list.
    
ForeignKeyManyToManyField)
_meta
get_fieldsrL   r
   r   related_modelr   appendnamer   )modelrelated_fieldsra   s      r;   &check_relationship_with_employee_modelr      s     N'') CeV../E4G4G84S!!5::|"<=uf445##x/!!5::/@"ABC rH   c                     | j                   rt        d      }d|fS g d}|D cg c]  }|| j                  vs| }}|r!t        d      dj                  |      z   }d|fS yc c}w )Nz0The uploaded file is empty, Not contain records.F)r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r   r$   r%   r&   r'   r(   z9These required headers are missing in the uploaded file: , )Tr?   )emptyr   columnsry   )
data_framemessagerequired_keysrp   missing_keyss        r;   valid_import_file_headersr      s    FGg~M, $1RCCz?Q?Q4QCRLRG
IIl#$ g~ Ss
   A"A"c                 8   t        j                  d      }t        j                  d      }t        d t        j                  D              }t        t        j
                  j                  dd            }t        t        j
                  j                  dd            }t        d t        j
                  j                  d	d
d      D              }t        t        j
                  j                  dd            }g g }	}| j                  d      }
d}t        |      }t        |      }t        |      }t        j                         }|
D ]  }i }d}t        |j                  dd            j                         j!                         }|j                  dd      }t#        |      }t%        |j                  d            }t'        d|      }t'        d|      }t        |j                  d      xs d      j                         j!                         }t'        d|      }t'        d|      }t'        d|      }t)        |j                  d      d|d      }|r||kD  rd|d<   d}t)        |j                  d      d|d      }|r|r||k  rd |d<   d}|r|j+                  |      sd!|d"<   d}|sd#|d$<   d}|j+                  |      sd%|d&<   d}||v rd'|d(<   d}n||d<   |j-                  |       ||v rd)|d*<   d}n|j-                  |       |||f}||v rd+|d,<   d}n|j-                  |       |r||vrd-d.j/                  |       d/|d0<   d}|r||vrd1| d2|d3<   d}|d4vr	 t1        |      } | dk  rt2        	 |d4vr	 t1        |      }!|!dk  rt2        	 |r'||d<   ||d<   ||d<   |j7                  |       |d9z  }r|j9                  |       |	j7                  |        ||	|fS # t2        t4        f$ r
 d5|d6<   d}Y w xY w# t2        t4        f$ r
 d7|d8<   d}Y w xY w):Nz0^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$z^\+?\d{10,15}$c              3   &   K   | ]	  }|d      yw)r   N )rt   choices     r;   rv   z+process_employee_records.<locals>.<genexpr>  s     Ofq	Os   rl   Trm   usernamec              3   .   K   | ]  \  }}}|||f  y wrr   r   )rt   fnamelnameemails       r;   rv   z+process_employee_records.<locals>.<genexpr>  s%      %E5% 
u%s   employee_first_nameemployee_last_namer   companyrecordsr   r   r?   r   r   r   r   r   r   r'   r(   r%   zJoining Dater.   z%Joining date cannot be in the future.Fr&   r/   z0Contract end date cannot be before joining date.zInvalid email address.r)   zFirst name cannot be empty.r*   zInvalid phone number format.r,   z.An employee with this badge ID already exists.r0   z$User with this email already exists.r3   z+This employee already exists in the system.r+   z Invalid gender. Allowed values: r   .r-   z	Company 'z' does not exist.r4   rx   z'Basic salary must be a positive number.r1   z*Salary hour must be a non-negative number.r2   r@   )rD   compile	frozensetr   choice_genderr~   r   r   r   to_dictsetr   todayrA   r`   rB   lowerrG   r^   rd   rV   matchaddry   rX   rN   r[   r   update)"r   email_regexphone_regexallowed_gendersexisting_badge_idsexisting_usernamesexisting_name_emailsexisting_companiessuccess_list
error_listemployee_dictscreated_countseen_badge_idsseen_usernamesseen_name_emailsr   emperrorssaver   	raw_phonerF   rl   
first_name	last_namegenderr   basic_salarysalary_hourjoining_datecontract_end_datename_email_tuplebasic_salary_valsalary_hour_vals"                                     r;   process_employee_recordsr      s   **PQK**./KO8N8NOOO"8#3#3#?#?
QU#?#VW"4<<#;#;JT#;#RS$ %#+#3#3#?#?!#7$
%  #7??#>#>yt#>#TU!2*L''	2NM+,N+,N/0JJLE w#CGGGR()//1779GGGR(		*!#''*"56 s3
S1	SWWX&,"-335;;=i-">37!-5 )GGN#^V=Q
 e#/V+,-GG'(!	
 2Cl2RB () D K--e4$<F=!D )FF%&D   '$BF=!D ~%'WF#$D 'C
Ox( N"&LF?#Du% '	59//= )* D  !12 fO32499_3M2NaP >" D w&88(1':K&LF?#D z)#(#6 #q($$ ) j("'"4"Q&$$ '  CL".C'8C#$$QMJJvc"ow#r ]225 	* /X+, 	* @ *+ 	s$   O$:P $O=<O= PPc                 T   | D cg c]  }|d   	 }}t         r:t        t        j                  j	                  |      j                  dd            n.t        t        j                  d t        |d      D                    }| D cg c]8  }|d   |vr/t        |d   |d   t        |d         j                         d	
      : }}g }|rHt        j                         5  t        j                  j                  |t         rdnd      }ddd       |S |S c c}w c c}w # 1 sw Y   |S xY w)z
    Creates new User instances in bulk from a list of dictionaries containing user data.

    Returns:
        list: A list of created User instances. If no new users are created, returns an empty list.
    r   username__inr   Trm   c              3   ~   K   | ]5  }t         j                  j                  |       j                  dd       7 yw)r   r   Trm   N)r   r~   rz   r   rt   chunks     r;   rv   z*bulk_create_user_import.<locals>.<genexpr>  sB         ###7CCT D  s   ;=  r   F)r   r   passwordis_superuserN
batch_size)is_postgresr   r   r~   rz   r   r   from_iterabler<   rA   rB   r   atomicbulk_create)success_listsrowemailsr   users_to_createcreated_userss         r;   bulk_create_user_importr     s>    '44sc'l4F4  	DLLV4@@RV@WX   %VS1	  
 ( !	 w<11 	\g,W&,,.		
	O 	 M! 	 LL44KDS 5 M	 =? 5		 s   D=D*DD'c                    | D cg c]  }|d   	 }}t         j                  dk(  }|r0t        j                  j	                  |      j                  dd      n%t        j                  d t        |d      D              D ci c]  }|j                  | }}| D cg c]\  }|d   |v rSt        ||d      |d   t        d	|      t        d
|      |d   |d   |j                  dd      j                               ^ }}g }|rDt        j                         5  t        j                  j!                  ||rdnd      }ddd       |S |S c c}w c c}w c c}w # 1 sw Y   |S xY w)z
    Creates Employee instances in bulk based on imported data.
    Uses adaptive chunking for compatibility with SQLite, avoids chunking in PostgreSQL.
    r   r   r   idr   c              3   |   K   | ]4  }t         j                  j                  |       j                  dd       6 yw)r   r   r   N)r   r~   rz   onlyr   s     r;   rv   z.bulk_create_employee_import.<locals>.<genexpr>  s7      % ###7<<T:N%s   :<r   r   r   r   r   r   r?   )employee_user_idrl   r   r   r   rF   r   Nr   )r	   vendorr   r~   rz   r   r   r   r<   r   r   rd   r`   r   r   r   r   )r   r   r   r   userexisting_usersemployees_to_createcreated_employeess           r;   bulk_create_employee_importr     s   
 '44sc'l4F4##|3K  LLV499$
K$$ %$VS1% 
 	t
N 
, ! w<>) 	+CL9_ +L# >*;<g,g,778R(..0	
  ! 	 ( 0 0 < <# != !	
 I 5
 	
 s   E EA!E&EE c                 T   t         j                  d       | D ]K  }	 |j                  j                  t	        |j
                               |j                  j                          M t         j                  d       y# t        $ r"}t         j                  d|        Y d}~d}~ww xY w)z(
    method to set initial password
    zstarted to set initial passwordz#falied to set initial password for Nzinitial password configured)	loggerinfor   set_passwordrA   rF   r   	Exceptionerror)r   employeees      r;   set_initial_passwordr    s    
 KK12 K	K%%223x~~3FG%%**,K KK-.  	KLL>xjIJJ	Ks   AA<<	B'B""B'c                      t         j                  j                         } | D ci c]  }|j                   d|j                   |  }}|S c c}w )ak  
    Optimizes the lookup of reporting managers from a list of work information.

    This function identifies unique reporting manager names from the provided
    list of work information, queries all matching `Employee` objects in a
    single database query, and creates a dictionary for quick lookups based
    on the full name of the reporting managers.
     )r   r~   entirer   r   )r   r  employee_dicts      r;   !optimize_reporting_manager_lookupr	    sb       '')I " ''
((*E*E)FGQM  	s   #A
c                    | D ch c]  }t        d|      x}r| }}t        t        j                  j	                  dd            }||z
  D cg c]  }t        |       }}|rGt        j                         5  t        j                  j                  |t        rdnd       ddd       yyc c}w c c}w # 1 sw Y   yxY w)	zV
    Bulk creation of department instances based on the excel import of employees
    r   
departmentTrm   )r  Nr   r   )	rd   r   r   r~   r   r   r   r   r   )r   	work_infodeptdepartments_to_importexisting_departmentsnew_departmentss         r;   bulk_create_department_importr    s     'i88D8 	  z11==lQU=VW *,@@ 	d#O 
 ! 	**KDS + 	 	 	 	   B.B3:*B88Cc           
         | D ch c]2  }t        d|      r$t        d|      rt        d|      t        d|      f4 }}|syt        j                  j                  dd      }|D ci c]  }|j                  | }}|D cg c]  \  }}||v r|||   f }}}|syt        t        j                  j                  |D 	
ch c]  \  }	}
|
	 c}
}	      j                  dd            }|D 
cg c]  \  }}
||
f|vrt        ||
	       }}}
|rGt        j                         5  t        j                  j                  |t        rdnd
       ddd       yyc c}w c c}w c c}}w c c}
}	w c c}
}w # 1 sw Y   yxY w)zd
    Optimized: Bulk creation of job position instances based on the Excel import of employees.
    r   r   Nr   r  )department_id__injob_positiondepartment_id)r  r  r   r   )rd   r   r~   r   r  r   r   rz   r   r   r   r   r   )r   rg   job_positions_to_importdepartment_objsdepdepartment_lookupjpr  valid_pairsr   dept_idexisting_pairsnew_positionss                r;   bulk_create_job_position_importr     s    "~t,\41P 
^T	*Kd,KL  # !((--dLAO8GH,HH
 0B$$ 
t$%K   ""9DE:1gwE 	# 	

+no
6N 'B=. 	7;M  ! 	+++$3 , 	 	 M I F
	 	s)   7E%E E >E&,E,"*E22E;c           	      6   | D ch c]"  }t        d|      x}rt        d|      x}r||f$ }}t        j                  j                  dd      }t	        t
        j                  j                  dd            }|D cg c]6  \  }}||v r-|||   j                  f|vrt        |||   j                        8 }}}|rGt        j                         5  t
        j                  j                  |t        rdnd	
       ddd       yyc c}w c c}}w # 1 sw Y   yxY w)zT
    Bulk creation of job role instances based on the excel import of employees
    r   r   r   r  job_rolejob_position_id)r"  r#  Nr   r   )rd   r   r~   r   r   r   r   r   r   r   r   r   )r   r  roleposjob_roles_to_importjob_positionsexisting_job_rolesnew_job_roless           r;   bulk_create_job_role_importr*  R  s1    '
I66D6	::S: 
s   '',,T>BMW__88EVWX
 -D#-=%(()1CC 	}S/A/D/DEM  ! 	OO''+$3 ( 	 	 )	 	s   'D<;D	*DDc                    | D ch c]  }t        d|      x}s| }}t        t        j                  j	                  dd            }||z
  D cg c]  }t        |       }}|rGt        j                         5  t        j                  j                  |t        rdnd       ddd       yyc c}w c c}w # 1 sw Y   yxY w)	zU
    Bulk creation of work type instances based on the excel import of employees
    r    	work_typeTrm   )r,  Nr   r   )	rd   r   r   r~   r   r   r   r   r   )r   r  wtwork_types_to_importexisting_work_typesnew_work_typess         r;   bulk_create_work_typesr1  r  s     *KY4W.Wb.W 
 h..::;T:RS *>@S)S#%2N 
 ! 	((;4C ) 	 	 	 	s   B/B/B4;*B99Cc                    | D ch c]  }t        d|      x}r| }}t        t        j                  j	                  dd            }||z
  D cg c]  }t        |       }}|rGt        j                         5  t        j                  j                  |t        rdnd       ddd       yyc c}w c c}w # 1 sw Y   yxY w)	zQ
    Bulk creation of shift instances based on the excel import of employees
    r!   employee_shiftTrm   )r3  Nr   r   )	rd   r   r   r~   r   r   r   r   r   )r   r  shiftshifts_to_importexisting_shifts
new_shiftss         r;   bulk_create_shiftsr8    s     ' )44E4 	  ))*:)FO &7 	U+J  ! 	!!--{t . 	 	 %	 	r  c                    | D ch c]  }t        d|      x}r| }}t        t        j                  j	                  dd            }||z
  D cg c]  }t        |       }}|rGt        j                         5  t        j                  j                  |t        rdnd       ddd       yyc c}w c c}w # 1 sw Y   yxY w)	zY
    Bulk creation of employee type instances based on the excel import of employees
    r"   employee_typeTrm   )r:  Nr   r   )	rd   r   r   r~   r   r   r   r   r   )r   r  etemployee_types_to_importexisting_employee_typesnew_employee_typess         r;   bulk_create_employee_typesr?    s     ' oy99B9 	    "((t(D +-DD 	2&  ! 	  ,,"{t - 	 	 % 	 	r  c                    ddl m} d }| |z   D cg c]  }|j                  r ||j                   d|j                  |j                  r|j                  nt	        j
                          ||j                         ||j                         ||j                         ||j                         ||j                        |j                  xs d	       }}|j                  j                  |       yc c}w )zP
    Creates employee contracts in bulk based on provided work information.
    r   )Contractc                     | r| S d S rr   r   )r\   s    r;   get_or_nonez/create_contracts_in_thread.<locals>.get_or_none  s    u'4'rH   z's Contract)	contract_nameemployee_idcontract_start_dater  r  r"  r4  r,  wageN)payroll.models.modelsrA  rE  date_joiningr   r   r  r#  job_role_idshift_idwork_type_idr   r~   r   )new_work_info_listupdate_work_info_listrA  rC  r  contracts_lists         r;   create_contracts_in_threadrP    s     /(" ,.CC    	&223;?!--*3*@*@	&&hnnFV"9#:#:;$Y%>%>? !6!67i001!)"8"89'',1	
N $   0%s   CC2c                    g }g }| D cg c]  }|d   	 }}t        d | D              }t        d | D              }t        d | D              }t        d | D              }t        d | D              }	t        d | D              }
t        d | D              }t        rd	nd
}|r&t        j                  d t	        ||      D              n<t
        j                  j                         j                  |      j                  d      }|D ci c]  }|j                  | }}t        r=t        j                  j                  |j                               j                  d      n<t        j                  d t	        t        |j                               d      D              D ci c]  }|j                  | }}t        j                  j                  |      j                  d      D ci c]  }|j                   | }}t"        j                  j                  |      j%                  d      j                  dd      D ci c]  }|j&                  |j(                  f| }}t*        j                  j                  |      j%                  d      j                  dd      D ci c]  }|j,                  |j.                  f| }}t0        j                  j                  |      j                  d      D ci c]  }|j2                  | }}t4        j                  j                  |
      j                  d      D ci c]  }|j6                  | }}t8        j                  j                  |	      j                  d      D ci c]  }|j:                  | }}t<        j                  j                  |       j                  d!      D ci c]  }|j>                  | }}tA               }| D ]=  } | d   }!|jC                  |!      }"|"s| d"   }#|jC                  |"      }$|jC                  | jC                  d#            }%|jC                  | jC                  d#            | jC                  d$      f}&|jC                  |&      }'|'| jC                  d%      f}(|jC                  |(      })|jC                  | jC                  d&            }*|jC                  | jC                  d'            }+|jC                  | jC                  d(            },| jC                  d)      }-d	}.tE        |-tF              rd*|-v r	|-|v r||-   }.|jC                  | jC                  d+            }/| jC                  d,      }0tI        jJ                  | d-         s| d-   ntM        jN                         }1tI        jJ                  | d.         s| d.   nd	}2tQ        tS        d/|             tT        u rtS        d/|       nd0}3tQ        tS        d1|             tT        u rtS        d1|       nd0}4|$mt        |"|#|%|'|)|*|+|,|.|/|0tI        jJ                  |1      s|1ntM        jN                         tI        jJ                  |2      s|2nd	|3|42      }$|jW                  |$       |#|$_,        |%|$_        |'|$_        |)|$_-        |*|$_.        |+|$_/        |,|$_0        |.|$_1        |/|$_2        |0|$_3        tI        jJ                  |1      s|1ntM        jN                         |$_4        tI        jJ                  |2      s|2nd	|$_5        |3|$_6        |4|$_7        |jW                  |$       @ |r)t        j                  jq                  |t        rd	nd
3       |r,t        j                  js                  |g d4t        rd	nd
3       tu        jv                  d5      r.ty        jz                  t|        ||f6      }5|5j                          y	y	c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )7z^
    Bulk creation of employee work info instances based on the excel import of employees
    r   c              3   >   K   | ]  }|j                  d         yw)r   Nr`   rt   r   s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     Ecggl+E   c              3   >   K   | ]  }|j                  d         yw)r   NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     IC/IrU  c              3   >   K   | ]  }|j                  d         yw)r   NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     ACCGGJ'ArU  c              3   >   K   | ]  }|j                  d         yw)r    NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     CcSWW[)CrU  c              3   >   K   | ]  }|j                  d         yw)r"   NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     Kc1KrU  c              3   >   K   | ]  }|j                  d         yw)r!   NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     ;c!;rU  c              3   >   K   | ]  }|j                  d         yw)r   NrS  rT  s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s     @3CGGI&@rU  Nr   c              3      K   | ]A  }t         j                  j                         j                  |       j	                  d       C yw)badge_id__inrl   N)r   r~   r  rz   r   r   s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s@      
 ##%,,%,@EEjQ
s   AA	r]  rl   employee_id__inrE  c              3   z   K   | ]3  }t         j                  j                  |       j                  d       5 yw)r_  rE  N)r   r~   rz   r   r   s     r;   rv   z/bulk_create_work_info_import.<locals>.<genexpr>  s<      %  (//66u6MRR!%s   9;i  )department__inr  )job_position__inr  r  )job_role__inr#  r"  )work_type__inr,  )employee_shift__inr3  )employee_type__inr:  )company__inr   r   r   r   r   r    r"   r!   r#   r  r   r$   r%   r&   r'   r   r(   )rE  r   r  r#  rJ  rL  employee_type_idrK  reporting_manager_id
company_idlocationrI  r   r   r   r   )r   r  r#  rJ  rL  ri  rK  rj  rk  rl  rI  r   r   r   payroll)targetargs)@r   r   r   r   r<   r   r~   r  rz   r   rl   r   valuesr   rE  r   r  r   select_relatedr  r  r   r#  r"  r   r,  r   r3  r   r:  r   r   r	  r`   rL   rA   rJ   isnullr   r   typerd   rZ   r   r   rJ  rL  ri  rK  rj  rk  rl  rI  r   r   r   r   bulk_updater   is_installed	threadingThreadrP  start)6r   rM  rN  r   	badge_idsdepartmentsr'  	job_roles
work_typesemployee_typesshifts	companies
chunk_sizeemployee_qsr   existing_employeesexisting_employee_work_infosr  r  r  existing_job_positionsjrr(  r-  r/  esr6  r;  r=  compr   reporting_manager_dictr  rl   employee_objr   employee_work_infodepartment_objjob_position_keyjob_position_objjob_role_keyjob_role_objwork_type_objemployee_type_obj	shift_objreporting_managerreporting_manager_objcompany_objrl  rI  r   r   r   contract_creation_threads6                                                         r;   bulk_create_work_info_importr    s    ,9:SZ:I:E}EEKI=IIMA=AAIC]CCJK]KKN;];;F@-@@I$#J 	 	 
 J7
 	

 $$&--9-EJJ:V  8CC#,,+CC  $++22 2 9 9 ; 3 d=!$$ % %T*<*C*C*E%FL	% $ 	$  $$ %%,,K,HMM
 	  %%,,m,L		(	o~	.	 
		2??+R/  //((i(@	)	*		,	 
		R[[)2-  ""))
)CHHU 	b   ''..&.INN
 	2O  &&---OTT
 	"  OO**y*AFFyQ 	d  ?@" e=	Z()--h7'"9==lK-11)--2MN !$$Y]]<%@AMM.)
 2556FG MM*%
 *--l;+//	k0JK377	o8VW#''	g(>?	%MM*=> $'-#9J2J $::(>?P(Q%(,,Y]]9-EF==,
 99Y~67 n%! 	 99Y':;< )* 	 K	:;sB 	2 	 Ky9:cA y1 	 %!8(, 0(*!2"%:&!(*		,(?LX^^EU .0YY7H-I%t)''"* %%&89 (-$/=,1A.-9*.;+2C/*3'6K3,7)*2'$&IIl$;AQ + *,3D)E!4 0 /;+-8*!(();<Ke=L ''33;4C 	4 	
 ''33!   +t% 	4 	
( ##,#3#3-$&;<$
  	!&&( $Y ;& D$ s;   _	_ _'_> _! _"4_';_,_1	_6c                 d    | j                  dd      \  }}t        ||g      }t        ||      }|S )zI
    method to return the model class from string 'app.models.Model'
    r   r@   )fromlist)rsplit
__import__getattr)
model_pathmodule_name
class_namemodulemodel_classs        r;   get_model_classr    s;     )//Q7Kzl;F#FJ7KrH   )@__doc__loggingrD   rv  r   r   	itertoolsr   r   pandasrJ   django.appsr   django.contrib.auth.modelsr   	django.dbr	   r
   r   django.utils.translationr   r   base.context_processorsr   base.modelsr   r   r   r   r   r   r   employee.modelsr   r   	getLogger__name__r   r   r   error_data_templater<   rG   rV   r^   rd   rj   r   r   r   r   r   r   r  r	  r  r   r*  r1  r8  r?  rP  r  r  )ra   s   0r;   <module>r     s	    	  # $   + 5 5 1 6   >			8	$</ # 
2I# L%
$,",	 *Z,!HQ3h&R)X/$00f@2<<1<Y)xA#s   
C