
    vgT                         d Z ddlZddlZddlmZ ddlm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mZmZmZmZmZmZ dd
lmZmZ  ej6                  e      Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-y)z
employee/methods.py
    N)datetime)groupby)apps)User)models)get_initial_prefix)Company
DepartmentEmployeeShiftEmployeeTypeJobPositionJobRoleWorkType)EmployeeEmployeeWorkInformationc                 `    |j                  |       }	 t        |       y# t        $ r |cY S w xY w)z4
    This method is returns None or field value
    N)getfloat
ValueError)fielddictsfield_values      ,/var/www/horilla/employee/methods/methods.pyconvert_nanr      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   len)itemprefixprefix_lengths      r   dynamic_prefix_sortr!   *   s7    
  %&:;F#&v;!#3CKM    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_idr$   T)flatr   0001)keyc              3   <   K   | ]  }|j                           y wN)isdigit).0chars     r   	<genexpr>z(get_ordered_badge_ids.<locals>.<genexpr>U   s     )JT$,,.)Js   c                 f    t        dj                  t        t        j                  |                   S )N )intjoinfilterstrr*   )xs    r   <lambda>z'get_ordered_badge_ids.<locals>.<lambda>W   s    #bggfS[[!6L.M*N r"   r   )r   objectsallexcludeorder_byvalues_listfirstr   r*   r0   sortedr!   r   listsortanyinsert)
	employeesdatar   pure_numberssorted_data_groupgrouped_datafiltered_groupresults
             r   get_ordered_badge_idsrK   5   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_fields
isinstancer   rM   related_modelr   appendnamerN   )modelrelated_fieldsr   s      r   &check_relationship_with_employee_modelrW   b   s     N'') CeV../E4G4G84S!!5::|"<=uf445##x/!!5::/@"ABC r"   c                    g }t         j                  j                  | D cg c]  }|d   	 c}      D ch c]  }|j                   }}| D ]H  }|d   }||v r|d   }t        ||t	        |      j                         d      }|j                  |       J g }	|rt         j                  j                  |      }	|	S c c}w c c}w )zP
    Bulk creation of user instances based on the excel import of employees
    Emailusername__inPhoneF)usernameemailpasswordis_superuser)r   r7   r2   r]   r3   striprS   bulk_create)
success_listsuser_obj_listrowuserexisting_usernames	work_infor^   phoneuser_objrJ   s
             r   bulk_create_user_importrk   x   s     M LL''2?@3#g,@ ( 
 	  # '	'"&&'"Z%%'	
 	X&' F))-8M) As
   B=Cc           
          g }t         j                  j                  | D cg c]  }|d   	 c}      D ci c]  }|j                  | }}| D ]  }|d   }|j	                  |      }|s|d   }t        d|      }t        d|      }	|d   }
|j	                  dd      j                         }t        ||||	||
|	      }|j                  |        g }|rt        j                  j                  |      }|S c c}w c c}w )
zT
    Bulk creation of employee instances based on the excel import of employees
    rY   rZ   Badge idz
First Namez	Last Namer\   Genderr/   )employee_user_idr$   employee_first_nameemployee_last_namer^   ri   gender)
r   r7   r2   r]   r   r   lowerr   rS   rb   )rc   employee_obj_listre   rf   existing_usersrh   r^   r$   
first_name	last_nameri   rr   employee_objrJ   s                 r   bulk_create_employee_importry      s.     LL''2?@3#g,@ ( 
 	tN  # /	'"!!%(Z( y9
Y7	'"x,224! *(
 	  .)/* F!!--.?@M; As
   C6C;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inforo   set_passwordr3   ri   save	Exceptionerror)rB   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               }| D ]5  }t        d|      }t        |t              s d|v s%|j	                  |       7 t        |      }t        j                  j                  |D cg c]  }|j                  d      d    c}|D cg c]  }|j                  d      d    c}      }|D ci c]  }|j                   d|j                   |  }}|S c c}w c c}w 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.
    Reporting Manager r      )employee_first_name__inemployee_last_name__in)setr   rQ   r3   addr>   r   r7   r2   splitrp   rq   )	rc   unique_managersrh   reporting_managermanager_namesrT   rB   r   employee_dicts	            r   !optimize_reporting_manager_lookupr      s     eO" 3	'(;YG'-#9J2J 123 )M  ''@M NC!3 N?LMt

3 2M ( I " ''
((*E*E)FGQM   !OMs   -CC!
5#C&c                 v   | D ch c]  }t        d|       }}t        j                  j                         D ch c]  }|j                   }}g }|D ]8  }|s||vst        |      }|j                  |       |j                  |       : |r t        j                  j                  |       yyc c}w c c}w )zV
    Bulk creation of department instances based on the excel import of employees
    r
   )
departmentN)r   r
   r7   r8   r   rS   r   rb   )rc   rh   departments_to_importdepexisting_departmentsdepartment_obj_listr   department_objs           r   bulk_create_department_importr      s    
 ?L1:L),  7A6H6H6L6L6NOsCNNOO+ 1
*,@@':>N&&~6 $$Z0	1 &&':;  Ps
   B1B6c                 j   | D ch c]  }t        d|      t        d|      f }}t        j                  j                         D ci c]  }|j                  | }}t
        j                  j                         D ci c]  }|j                  |j                  f| }}g }|D ]^  \  }}|r|s|j                  |      }	|	s||	j                  f|vs0t        |	|      }
|j                  |
       |
|||	j                  f<   ` |r t
        j                  j                  |       yyc c}w c c}w c c}w )zX
    Bulk creation of job position instances based on the excel import of employees
    Job Positionr
   )department_idjob_positionN)r   r
   r7   r8   r   r   r   r   r   idrS   rb   )rc   rh   job_positions_to_importr   departmentsr   existing_job_positionsjob_position_obj_listdepartment_namer   job_position_objs              r   bulk_create_job_position_importr     s]    ' 
^Y	/\91UV  3=2D2D2H2H2JK33>>3&KKK (//335 
	"	"L$>$>?M  )@ Y%o?$9 .++,4JJ*,<  "(()9:HX"L.2C2C#DEY  ''(=> 5 L   D&D+< D0c                 j   | D ch c]  }t        d|      t        d|      f }}t        j                  j                         D ci c]  }|j                  | }}t
        j                  j                         D ci c]  }|j                  |j                  f| }}g }|D ]^  \  }}	|r|	s|j                  |	      }
|
s||
j                  f|vs0t        |
|      }|j                  |       ||||
j                  f<   ` |r t
        j                  j                  |       yyc c}w c c}w c c}w )zT
    Bulk creation of job role instances based on the excel import of employees
    Job Roler   )job_position_idjob_roleN)r   r   r7   r8   r   r   r   r   r   r   rS   rb   )rc   rh   job_roles_to_importjpjob_positionsjrexisting_job_rolesjob_role_obj_listr   job_position_namer   job_role_objs               r   bulk_create_job_role_importr   #  sR    ' 
Z	+[-ST 
 4?3F3F3J3J3LMRR__b(MMM8?8K8K8M24b(()2-  ': O##0(,,->?&))*2DD"3ChWL$$\2BN*:*=*=>?O ##$56 5
 Nr   c                 `   | D ch c]  }t        d|       }}|j                  d       t        j                  j	                         D ci c]  }|j
                  | }}|D cg c]  }||vrt        |       }}|r t        j                  j                  |       yyc c}w c c}w c c}w )zU
    Bulk creation of work type instances based on the excel import of employees
    	Work TypeN)	work_type)r   discardr   r7   r8   r   rb   )rc   rh   work_types_to_importwtexisting_work_typesr   work_type_obj_lists          r   bulk_create_work_typesr   F  s     >K09K+    & 7?6F6F6J6J6LM2<<+MM
 .// 	9%  $$%78  N   B!B&%B+c                 `   | D ch c]  }t        d|       }}|j                  d       t        j                  j	                         D ci c]  }|j
                  | }}|D cg c]  }||vrt        |       }}|r t        j                  j                  |       yyc c}w c c}w c c}w )zQ
    Bulk creation of shift instances based on the excel import of employees
    ShiftN)employee_shift)r   r   r   r7   r8   r   rb   )rc   rh   shifts_to_importshiftexisting_shiftsshift_obj_lists         r   bulk_create_shiftsr   ^  s    
 JWWIGY7WWT" 2?1F1F1J1J1L(-e#O  &' 	U+N  )).9  X
r   c                 `   | D ch c]  }t        d|       }}|j                  d       t        j                  j	                         D ci c]  }|j
                  | }}|D cg c]  }||vrt        |       }}|r t        j                  j                  |       yyc c}w c c}w c c}w )zY
    Bulk creation of employee type instances based on the excel import of employees
    Employee TypeN)employee_type)r   r   r   r7   r8   r   rb   )rc   rh   employee_types_to_importetexisting_employee_typesr   employee_type_obj_lists          r   bulk_create_employee_typesr   v  s     BO 4=OY/    $$T* (4';';'?'?'A!#"  6 77 	=1  (()?@ # 
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 r)    )values    r   get_or_nonez/create_contracts_in_thread.<locals>.get_or_none  s    u'4'r"   z's Contract)	contract_nameemployee_idcontract_start_dater   r   r   r   r   wageN)payroll.models.modelsr   r   date_joiningr   todayr   r   job_role_idshift_idwork_type_idbasic_salaryr7   rb   )new_work_info_listupdate_work_info_listr   r   rh   contracts_lists         r   create_contracts_in_threadr     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        j                  j                  |	      j	                  d
      D ci c]  }|j
                  | }}t        j                  j                  |j                               j	                  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| }}t$        j                  j                  |      j	                  d      D ci c]  }|j&                  | }}t(        j                  j                  |
      j	                  d      D ci c]  }|j*                  | }}t,        j                  j                  |	      j	                  d      D ci c]  }|j.                  | }}t0        j                  j                  |      j	                  d      D ci c]  }|j2                  | }}t5        |       }| D ]6  }|d   }|d   } |j7                  |j7                  d            }!|j7                  |j7                  d            |j7                  d      f}"|j7                  |"      }#|j7                  |j7                  d             }$|j7                  |j7                  d!            }%|j7                  |j7                  d"            }&|j7                  |j7                  d#            }'|j7                  d$      }(d%})t9        |(t:              rd&|(v r	|(|v r||(   })|j7                  |j7                  d'            }*|j7                  d(      }+t=        j>                  |d)         s|d)   ntA        jB                         },t=        j>                  |d*         s|d*   nd%}-tE        tG        d+|            tH        u rtG        d+|      nd,}.tE        tG        d-|            tH        u rtG        d-|      nd,}/|j7                  |       }0|j7                  |0      }1|1mt        |0||!|#|$|%|&|'|)|*|+t=        j>                  |,      s|,ntA        jB                         t=        j>                  |-      s|-nd%|.|/.      }1|jK                  |1       ||1_&        |!|1_        |#|1_        |$|1_'        |%|1_(        |&|1_)        |'|1_*        |)|1_+        |*|1_,        |+|1_-        t=        j>                  |,      s|,ntA        jB                         |1_.        t=        j>                  |-      s|-nd%|1_/        |.|1_0        |/|1_1        |jK                  |1       9 |rt        j                  je                  |       |r"t        j                  jg                  |g d/       ti        jj                  d0      r.tm        jn                  tp        ||f1      }2|2js                          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 )2z^
    Bulk creation of employee work info instances based on the excel import of employees
    rm   c              3   >   K   | ]  }|j                  d         yw)r
   Nr   r+   re   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     Ecggl+E   c              3   >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     IC/Ir   c              3   >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     ACCGGJ'Ar   c              3   >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     CcSWW[)Cr   c              3   >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     Kc1Kr   c              3   >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     ;c!;r   c              3   >   K   | ]  }|j                  d         yw)r	   Nr   r   s     r   r-   z/bulk_create_work_info_import.<locals>.<genexpr>  s     @3CGGI&@r   )badge_id__inr$   )employee_id__inr   )department__inr   )job_position__inr   r   )job_role__inr   r   )work_type__inr   )employee_shift__inr   )employee_type__inr   )company__incompanyrY   r
   r   r   r   r   r   r   Nr   r	   LocationzDate joiningzContract End DatezBasic Salaryr   zSalary Hour)r   r^   r   r   r   r   employee_type_idr   reporting_manager_id
company_idlocationr   contract_end_dater   salary_hour)r^   r   r   r   r   r   r   r   r   r   r   r   r   r   payroll)targetargs):r   r   r7   r2   onlyr$   r   valuesr   r
   r   r   select_relatedr   r   r   r   r   r   r   r   r   r   r   r	   r   r   r   rQ   r3   pdisnullr   r   typer   r0   rS   r^   r   r   r   r   r   r   r   r   r   r   r   rb   bulk_updater   is_installed	threadingThreadr   start)3rc   r   r   re   	badge_idsr   r   	job_roles
work_typesemployee_typesshifts	companiesempexisting_employeesexisting_employee_work_infosr   r   r   r   r   r   r   r   esr   r   r   compexisting_companiesreporting_manager_dictrh   r^   r$   r   r'   r   r   work_type_objemployee_type_obj	shift_objr   reporting_manager_objcompany_objr   r   r   r   r   rx   employee_work_infocontract_creation_threads3                                                      r   bulk_create_work_info_importr!    s\     -::SZ:I:E}EEKI=IIMA=AAIC]CCJK]KKN;];;F@-@@I
 ##**	*BGG
S 	c  +2299.557 : 

$}
	$ 	$  $ %%,,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  ?}M" \=	'"Z(-11)--2MN $$Y]]<%@AMM.)
 255c:)--immJ.GH+//	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 	 *--h79==lK%!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*!(();<y\=| ''334FG''33!	
& ##,#3#3-$&;<$
  	!&&( $_ ;$s;   \*\/\4"\99 \> ]/]6]=]]).__doc__loggingr
  r   	itertoolsr   pandasr  django.appsr   django.contrib.auth.modelsr   	django.dbr   base.context_processorsr   base.modelsr	   r
   r   r   r   r   r   employee.modelsr   r   	getLogger__name__r{   r   r!   rK   rW   rk   ry   r   r   r   r   r   r   r   r   r   r!  r   r"   r   <module>r.     s          +  6   >			8	$	 *Z,>%P/><(?D 7F90:0A41<})r"   