B
    gcA                 @   s   d dl 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 d dlmZ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 G dd deZeee G dd deZ dS )    N)cache)gettext_lazy)config)AdminRuntimeWarning)ZKModelAdmin)zk_site)actionsdb_constfieldswidgets)
ImportData)ResignEmployee)ResignChangeFrom)settings)activatec            
       s   e Zd Zddddddddd	d
g
ZdZejd Zej	ej
ejejgZejdejiiZeZdd Zdd Zede_dd Z fddZ  ZS )ResignAdminemp_codeZ
first_name	last_nameZ
departmentZpositionresign_typeresign_datereasonreport_generation_end_date
disableatt)employeer   r   r   )r   r   r   Zemployee__department__dept_nameZ!employee__position__position_namer   r   Zwidgetc             C   s   dS )NF )selfrequestr   r   <G:\easytimepro\master/mysite/personnel\admin\resign_admin.pyhas_add_permission)   s    zResignAdmin.has_add_permissionc             C   s   |j jS )N)r   r   )r   objr   r   r   r   ,   s    zResignAdmin.emp_codeZemp_field_employeeCodec             C   sH   t |d| jj| jjd}|  |j}|rDddlm} |d|d S )Nimport_data)req
input_name	app_label
model_namer   )ActionHandleError;)	ImportResignDataZoptsr$   r%   Zexe_import_data
error_infoZmysite.admin.actionr&   join)r   r   Z
obj_importZ	ret_errorr&   r   r   r   
dataimport1   s    zResignAdmin.dataimportc                s   t t| |j|jjd}|jjs|jj}|jj}|jj	}|
 rT|j| d}|
 rp|j| d }|
 r|j| d}|S )N)employee__company_id)Zemployee__department__in)Zemployee__area__in)Zemployee__position__in)superr   get_querysetfilterusercurrent_companyZis_superuserZget_auth_deptZget_auth_areaZget_auth_posexistsallZdistinct)r   r   ZqsZ	auth_deptZ	auth_areaZauth_pos)	__class__r   r   r.   :   s    zResignAdmin.get_queryset)__name__
__module____qualname__Zlist_displayZsort_fieldsr   ZEMPLOYEE_LIST_FILTERZlist_filterr   ZAddEmployeeResignZReinstatementZImportZDisableAttendanceFunctionr
   ZEmployeeOneToOner   ZEmployeeOneToOneWidgetZformfield_overridesr   Zformr   r   _Zshort_descriptionr+   r.   __classcell__r   r   )r4   r   r      s   

	r   c                   sr   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Z  ZS )r(   u   职位导入类r!   Nc                s   t t| |||| |j}tj}|d|}|r:t| dtd dtd dtd dtd g| _	dtd g| _
g | _d | _d | _d | _d | _d | _d | _d | _d S )NZlanguagez%sresign_field_employeeresign_field_resignDate$resign_field_reportGenerationEndDateresign_field_resignType)r-   r(   __init__r0   r   ZLANGUAGE_CODEZget_preferencesr   r8   Zmust_fieldsZcalculate_fields_verboseZexist_codesemployee_codeemployee_idr   r   r   resign_reasondisable_attendance_function)r   r"   r#   r$   r%   ZprofileZdefault_language_codeZlng)r4   r   r   r>   P   s&    


zImportResignData.__init__c             C   s   dS )NTr   )r   elemrecordsstorer   r   r   is_valid_recordk   s    z ImportResignData.is_valid_recordc             C   s  ddl m} ddlm}m} dd l}t| j}dd}dd|dddd	}t	
|||d
 xtt| jD ]}| j| }	|	dtd kr|| _|	dtd kr|| _|	dtd kr|| _|	dtd kr|| _|	dtd kr|| _|	dtd krr|| _qrW t|}
ttdd t|
D t|
}t|}ttdd t|D t|}x*t| jD ]\}}|d7 }d } } } } }}| jd k	r|| j }|| j s| jtdd|i  n~| jd k	rR|| j }y|d}W nX tk
rP   ytj |d}W n. tk
rJ   | jtd|  wnY nX Y nX || j sx| jtdd|i  n~| jd k	r|| j }y|d}W nX tk
r   ytj |d}W n. tk
r   | jtd|  wnY nX Y nX | jd k	r|| j ! }| jd k	r*|| j ! }| jd k	rvt|| j t"j#krh| jtd|t"j#d  || j ! }|s| jtdd|i  |rLt$j%j&|| j'j(j)d}| j*s|rt+tdn8|j%j&|| j'j(j)d, }|sL| jtd d|i  nF| j*rL|sL|j%j&|| j'j(j)d, }|sL| jtd d|i  |sj| jtd!d|i  |r|- }||. kr| jtd"|d#/|. d$  n| jtd%d|i  |r|0 }t1||kr| jtd&|d#/|. d$  ndtd'}| jsX||| j< ||| j< ||| j< ||| j< ||| j< ||| j< ||d(< t2|t2| |d)< t	
|||d
 qnW d*S )+Nr   )r   )	LEAVETYPEDISABLE_ATTzresign_import_processor_{0} z{0}zValidating...   )statustotalfinishtpfpi,  z%sr:   r;   r<   r=   Zresign_field_resignReasonZ&resign_field_disableAttendanceFunctionc             S   s   g | ]}d  |qS )z{0})format).0xr   r   r   
<listcomp>   s    z2ImportResignData.before_insert.<locals>.<listcomp>c             S   s   g | ]}d  |qS )z{0})rP   )rQ   rR   r   r   r   rS      s    z5error_data_on_row(%(index)s)_the_resign_date_is_emptyindexz%Y-%m-%dz5error_data_on_row(%s)_the_format_should_be_yyyy-mm-ddz5error_data_on_row(%(index)s)_the_report_date_is_emptyzNerror_data_on_row(%(index)s)_the_length_of_resign_reason_maximum_is_%(length)s)rT   lengthz7error_data_on_row(%(index)s)_the_employee_code_is_empty)Zemployee__emp_coder,   zEmployee code already Exists)r   
company_idzFerror_data_on_row(%(index)s)_the_employee_code_is_not_in_employee_listz4error_data_on_row(%(index)s)_the_Attendance_is_emptyzPerror_data_on_row(%(index)s)_the_format_of_resignation_type_should_be_%(expect)s,)rT   expectz:error_data_on_row(%(index)s)_the_resignation_type_is_emptyzZerror_data_on_row(%(index)s)_the_format_of_disable_attendance_function_should_be%(expect)sZboolean_option_disablerM   rO   T)3&mysite.personnel.models.model_employeer   Zmysite.personnel.db_constrG   rH   jsonlenrD   rP   r   setdumpsrangeheadr8   r?   r   r   r   rA   rB   dictzipsixZ
itervaluesZiterkeys	enumerater)   appendstrftime	Exceptiondatetimestrptimestripr	   ZMAX_RESIGN_REASONr   objectsr/   r   r0   r1   need_update_old_recordr   r2   titlekeysr*   
capitalizestrfloat)r   r   rG   rH   rZ   _lenprocessor_keyprocessor_datarT   eZ	type_dictZreserved_dictZatt_dictZreserved_attrC   r?   r   r   r   rB   rA   Z_resign_dateZ_report_generation_end_date
exist_dataZcheck_employeer   r   r   before_insertn   s    



$$









zImportResignData.before_insertc             C   s   d S )Nr   )r   insert_positionr   r   r   records_analysis   s    z!ImportResignData.records_analysisc                s  ddl m} ddl}t| j}dd}dd|dddd	}t|||d
 t| j	}g }t
jj| jjjdd}dd |D }	|	| _| j}
i }xLt| jD ]<\}}|jj|| j | jjjd}|r|d j}i }|| jks|
rxt|D ]}x0t| jD ] \}}||kr|| ||< qW || jkr||  | j|}| j| }|jr fdd|jD }|r|d  |j}|  | }|||< qW | !||}|d |d< t"|d t"| |d< t|||d
 qW | #| dS )u   任何数据库数据插入r   )r   Nzresign_import_processor_{0}rI   z{0}zProcessing...rJ   )rK   rL   rM   rN   rO   i,  )r,   r@   c             S   s   g | ]}|d  qS )r   r   )rQ   rt   r   r   r   rS     s    z0ImportResignData.data_insert.<locals>.<listcomp>)r   rV   c                s    g | ]}|d   kr|d qS )rJ   r   r   )rQ   rt   )	tmp_valuer   r   rS      s    rM   rO   )$rY   r   rZ   r[   rD   rP   r   r\   r]   r_   r   rj   r/   r   r0   r1   Zvalues_list
exist_pinsrk   rc   all_objectsr?   idr^   rb   Z	iteritemsZcalculate_fields_indexZvalid_head_indexsrT   Zvalid_model_fieldschoicesZattnameZget_db_valueprocess_rowrp   rx   )r   r   rZ   rq   rr   rs   Z
count_headrw   ru   rz   Z	overwritecalculate_dictrT   robjsr@   Zrow_fields_selectkvZf_indexZ	tmp_fieldZtvkeyvaluerow_datar   )ry   r   data_insert   sZ    






zImportResignData.data_insertc             C   s   |    dS )u   sqlserver 数据插入N)r   )r   r   r   r   sqlserver_insert.  s    z!ImportResignData.sqlserver_insertc             C   s   |    dS )u   mysql 数据插入N)r   )r   r   r   r   mysql_insert2  s    zImportResignData.mysql_insertc             C   s   |    dS )u   oracle 数据插入N)r   )r   r   r   r   oracle_insert6  s    zImportResignData.oracle_insertc             C   s   |    dS )u)   
        postgresql 数据插入
        N)r   )r   r   r   r   postgresql_insert:  s    z"ImportResignData.postgresql_insertc             C   s   d S )Nr   )r   r   r   r   after_insert@  s    zImportResignData.after_insertc             C   s   dt d }tjj|| | jjjd}|d j| jkrPt	j
j|d jdd }nt	 }|d |_y>|d |_|d |_|d |_|d	 |_|d
 |_|  W n$ tk
r   dd l}|  Y nX |S )Nz%sr:   )r   rV   r   )r@   r   r   r   r   r   )r8   r   r{   r/   r   r0   r1   r|   rz   r   rj   r   r   r   r   r   r   Zsaverf   	traceback	print_exc)r   r   r   ZePinKeyr   Z
resign_objr   r   r   r   r~   F  s$    





zImportResignData.process_row)r!   NN)r5   r6   r7   __doc__r>   rF   rv   rx   r   r   r   r   r   r   r~   r9   r   r   )r4   r   r(   M   s    4r(   )!rg   rb   Zdjango.core.cacher   Zdjango.utils.translationr   r8   mysiter   Zmysite.admin.exceptionsr   Zmysite.admin.kernelr   Zmysite.admin.sitesr   Zmysite.personnelr   r	   r
   r   Zmysite.personnel.import_datar   Zmysite.personnel.modelsr   r   Zmysite.personnel.formsr   Zdjango.confr   r   r   registerr(   r   r   r   r   <module>   s    2