B
    b!0                 @   s  d dl Z d dlZd dlZd dlZ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 d d	lmZ d d
lmZ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$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/ G dd dej0Z1G dd dej0Z2G dd deZ3G dd dej0Z4G dd deZ5G dd deZ6dd Z7dS )     N)render)gettext_lazy)settings)ModelChoiceField)forms)ValidationError)fieldswidgets)db_const)r   ZKModelAction)AllowanceTypePurposeType)AdminRuntimeWarning)Reimbursement)SalaryStructure)PUNCH_STATESAPPLICATIONAUDIT_STATUSAUDIT_SUCCESSREFUSECANCEL_AUDIT_SUCCESS)Employee)get_current_company_idc                   s   e Zd ZejedddZejedddZe	e
j edddZe	ej edddZejed	ddZejed
ddZejeddeddZejedejddZejej edejddZejddZ fddZ  fddZ!dd Z"  Z#S )ReimbursementCreationFormZReimbursement_field_employeeF)labelrequiredZReimbursement_field_amountTZpayroll_model_allowancetype)querysetr   r   Zpayroll_model_purposetypeZReimbursement_field_start_dateZReimbursement_field_end_dateZReimbursement_field_file-reimbursement_receipt_file_format_constraints)r   r   	help_textZReimbursement_field_remark)r   
max_lengthr   Z!payroll_model_additional_employee)r   r   Zwidgetr   )r   c                s   t t| j|| t }tjj|d }|r>dd |D }ng }|| jd _	t
jj|d }|rtdd |D }ng }|| jd _	d S )N)Zcompanyc             S   s   g | ]}|d  |d fqS )idZallowance_name ).0ptr!   r!   AG:\easytimepro\master/mysite/payroll\forms\reimbursement_forms.py
<listcomp>4   s    z6ReimbursementCreationForm.__init__.<locals>.<listcomp>allowance_typec             S   s   g | ]}|d  |d fqS )r    Zpurpose_namer!   )r"   r#   r!   r!   r$   r%   :   s    purpose_type)superr   __init__r   r   objectsfiltervaluesr   choicesr   )selfargskwargsZ
company_idr&   r'   )	__class__r!   r$   r)   /   s    z"ReimbursementCreationForm.__init__c                s   t t|  }t| |S )N)r(   r   cleandata_pre_check)r.   Zcleand_data)r1   r!   r$   r2   @   s    zReimbursementCreationForm.cleanc       #   
      s  | j dd }ttj }|dd }tjj|d}|dd }t	jj|d}t
| j dd }yt
| j dd }W n   g }Y nX g }	|dd  d }
d	}d	}d
ddddddg}|r|
r |r |d d }tjd| }|ddttt d  }x|
D ]}|j}|dd	}||kr2n|d}|d }| }||krbttd|  || d d	 fddtdD }|d| }|	| | }tj||}tj |st!| t"|d}|#| W d Q R X qW t$|	dkrd|	}nttdy.x&|D ]}|%d |i |%d|d i |%d|d i |%d|i |%d!|i y
|d= W n   Y nX t&jj'f |}|r2d||j(}|| }tj |st!| t)|}xT|D ]L}tj*|s|+d",d#}tj||}tj||}t-.|| qW ||_/|0  q2W W n< t1k
r }  zt2| dd l3}!|!4  W d d } ~ X Y nLX |rtj |rt-5| |d$ }"tj |"rt)|"st-5|" d S )%Nemployeer&   )r    r'   additional_employeedelete_files   r    ZjpgZjpegZpngZbmpZpdfZdocxZxlsx
   z{0}/z{0}/{1}/rooti   .r   Z>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890c             3   s   | ]}t  V  qd S )N)randomZchoice)r"   x)charsr!   r$   	<genexpr>i   s    z1ReimbursementCreationForm.save.<locals>.<genexpr>wb/Zreceipt_file_validationemployee_idZ
apply_timezutf-8gbkzroot/)6datagetliststrdatetimenowgetr   r*   r+   r   jsondumpsloadsr   ZREIMBURSEMENT_FILE_ROOTformathexinttimenamereplacesplitlowerr   _popappendjoinrangereadospathexistsmakedirsopenwritelenupdater   creater    listdirisfileencodedecodeshutilcopyrmb_filesave	Exceptionr   	traceback	print_excrmtree)#r.   r/   r0   empsZrmb_dater&   r'   r5   Zdeleted_filesZ
files_name	rmb_filesZ	rpt_filessource_fileZfiles_formatZ	file_dateZ	file_rootrl   	file_nameZ	name_listZf_formatZ	randomstrZ	file_data	file_pathfempobjZ	path_infoZ	emp_filesZ	img_filesZimgZs_filenew_fileero   Z	root_pathr!   )r@   r$   rm   E   s    
 











zReimbursementCreationForm.save)$__name__
__module____qualname__r   ZEmployeeManyToManyFieldrW   rx   Z
FloatField
rmb_amountr   r   r*   allr&   r   r'   ZDateTimeFieldrmb_start_datermb_end_dateZ	FileFieldrl   	TextFieldcZMAX_LENTH_REMARKZ
rmb_remarkdjango_formsZModelMultipleChoiceFieldr   r	   ZEmployeeSelectMultipler5   r6   r)   r2   rm   __classcell__r!   r!   )r1   r$   r      s"   
r   c               @   s6   e Zd ZejedeedZej	edde
jdZdS )ApproveFormZmanualLog_field_approvalState)r   initialr-   ZmanualLog_field_approvalRemarkF)r   r   r   N)r|   r}   r~   r   ZChoiceFieldrW   r   r   audit_statusr   r
   ZMAX_APPLY_REASONaudit_reasonr!   r!   r!   r$   r      s   r   c               @   s4   e Zd ZdZedZedZedZeZ	dd Z
dS )ReimbursementApproveTZpayroll_action_approvec             O   s   | j jd| j jddr0t| j | jddiS xR| jD ]H}|d |_t|d |j	_
| j jj|_tj |_| j j|_|  q8W d S )Npopupr8   
form_titleZreimbursement_approver   r   )requestPOSTrK   GETr   action_templater*   r   rQ   workflow_abstractExceptionr   userusernameapproverrI   rJ   
audit_time_approve_userrm   )r.   r/   r0   ry   r!   r!   r$   action   s    

zReimbursementApprove.actionN)r|   r}   r~   batch_selectrW   verbose_namehelp_txtshort_descriptionr   action_formr   r!   r!   r!   r$   r      s   r   c               @   s   e Zd ZejedddZdS )
RevokeFormrevoke_reasonT)r   r   N)r|   r}   r~   r   r   rW   r   r!   r!   r!   r$   r      s   r   c               @   s4   e Zd ZdZedZedZedZeZ	dd Z
dS )ReimbursementRevokeTZpayroll_action_revokec             O   s   | j jd| j jddr0t| j | jddiS xf| jD ]\}|jjt	kr|d |j_
t|j_| j jj|_tj |_| j j|_|  q8ttdq8W d S )Nr   r8   r   Zreimbursement_revoker   Z$only_approved_records_can_be_revoked)r   r   rK   r   r   r   r*   r   r   r   r   r   r   r   r   rI   rJ   r   r   rm   r   rW   )r.   r/   r0   ry   r!   r!   r$   r      s    

zReimbursementRevoke.actionN)r|   r}   r~   r   rW   r   r   r   r   r   r   r!   r!   r!   r$   r      s   r   c               @   s0   e Zd ZedZedZedZeZdd Z	dS )AddReimbursementActionZpayroll_action_addRmbc       
      O   s   | j jdd }| j jd}d}|rd|kr8|d d|krJ|d xX|D ]P}tjj|d }|sPt	jj|d}|d j
d |d j }ttd	| qPW t| j j}	|	j| |f| ntd
}|rt|d S )Nrl   r4   r8   rx   )rD   )r    r   r;   Z2payroll_reimbursement_payroll_structure_validationZselect_employee)r   ZFILESrG   r   rX   r   r*   r+   r_   r   Z
first_nameZemp_coder   rW   r   rm   )
r.   r/   r0   rs   rr   messagerx   Zsalary_structure_dataZemp_nameZformr!   r!   r$   r      s&    


zAddReimbursementAction.actionN)
r|   r}   r~   rW   r   r   r   r   r   r   r!   r!   r!   r$   r      s
   r   c             C   s   |  dd}|  dd}|  dd}|rB|tj krBttd|r`|tj kr`ttd|r||r|||kr|ttd|r|dkrttd	d S )
Nr   r8   r   r   Z7reimbursement_startdate_cannot_greater_than_current_dayZ5reimbursement_enddate_cannot_greater_than_current_dayZ"leave_time_invalid_range_start_endr   Zthe_amount_need_lager_than_zero)rK   rI   Ztodayr   rW   )rF   r   r   r   r!   r!   r$   r3      s    r3   )8r]   rI   rj   rR   rL   r>   Zdjango.shortcutsr   Zdjango.utils.translationr   rW   Zdjango.confr   Zdjango.formsr   Zdjangor   r   r   Zmysite.personnelr   r	   Z
mysite.attr
   Zmysite.adminr   Zmysite.payroll.modelsr   r   Zmysite.payrollr   Zmysite.admin.exceptionsr   Z)mysite.payroll.models.model_reimbursementr   Z,mysite.payroll.models.model_salary_structurer   Zmysite.att.models_choicesr   r   r   r   r   r   Zmysite.personnel.modelsr   Zmysite.att.utilsr   ZZKActionFormr   r   r   r   r   r   r3   r!   r!   r!   r$   <module>   s:    ~