o
    ۷h                     @   s   d dl Z d dl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 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mZmZmZmZmZm Z  d dl!m"Z"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- d dl.m/Z/ d dl0m1Z1 G dd de1Z2dS )    N)timezone)QSum)logger)$EmployeePaystructure_detailsResponse)EmployeePaystructureResponse)EmployeePaystructure_details)AudtiService)PayrollComponentServicePaycomponentFlagmasterServiceCompanyContributionServiceSegmentMasterServiceEmployeePFServiceComponentTypeService)ActivestatusAdvancetypeModifyStatusget_pf_typepayrolldeduction_valAllowanceTypeFlagRef_Type)ErrorDescriptionErrorMessage)NWisefinSuccessSuccessStatusSuccessMessage)NWisefinError)NWisefinList)NWisefinPaginator)ApplicationNamespace)NWisefinThreadc                       s   e Zd Z 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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 ))#EmployeePaystructure_detailsServicec                    s   t  | | tj d S N)super__init___set_namespacer   PAYROLL_SERVICE)selfscope	__class__ M/var/www/FinalPayroll/payrollservice/service/emppaystructuredetailsservice.pyr$      s   z,EmployeePaystructure_detailsService.__init__c                 C   s  t  }|tj | du s|dks| d kr| }	nt|t|  d }	|	 d urt
j|  j|	 dj|| | | | | | | |t |  |	| d}
t
j|  j|	 d}
t|  |
|
j|tjtj }|!t"j# n?t
j|  j$|| | | | | | | ||  |	| d}
t|  |
|
j|tjtj%}|!t"j& |
j|_|S )NT1d   id)
emp_pay_idpaycomponentpaycomponent_typepaycomponent_percentagecompany_contributiontype	from_dateto_date
updated_byupdated_date	entity_idamountsegment)r1   r2   r3   r4   r5   r6   r7   r8   
created_byr;   r<   r=   )'r   
set_statusr   SUCCESSget_company_contributionget_type
get_amountfloatget_paycomponent_percentageget_idr   objectsusing_current_app_schemafilterupdateget_paycomponentget_paycomponent_typeget_from_dateget_to_dater   now
_entity_idget_segmentgetr	   _scopeaudit_functionr0   r   paystructure_detailsr   UPDATEset_messager   UPDATE_MESSAGEcreateCREATECREATE_MESSAGE)r'   requestallowance_objuser_iddatastandard_ctc	gross_payactionsuccess_objr<   emp_detailsaudit_insert_datar+   r+   r,    create_employeepay_structdetails    sZ    
zDEmployeePaystructure_detailsService.create_employeepay_structdetailsc                 C   sP  t |  tjd}tj|  ||	 |
  }t }dd |D }dd |D }t|  |}t|  |}|D ]d}	t }
|
|	j |
|	j| |	jdkrht|  |}|
|	j| |
|	j |
|	j |
|	j |
 |	j! |
"|	j# |
$|	j% |
&|	j'| |(|
 t)||* d}|+| qA|S )N)r;   statusc                 S      g | ]}|j qS r+   r2   .0ir+   r+   r,   
<listcomp>R       zPEmployeePaystructure_detailsService.employeepaystrct_summary.<locals>.<listcomp>c                 S   ri   r+   r=   rk   r+   r+   r,   rn   S   ro      
   ),r   rQ   r   activer   rG   rH   rI   rJ   
get_offsetget_query_limitr   r   rT   get_multiple_segment_datar
   "get_multiple_payroll_component_valr   set_idr0   set_paycomponent_valr2   r5   r   get_multiple_contributionset_company_contribution_valset_paycomponent_typer3   set_paycomponent_percentager4   
set_amountr<   set_typer6   set_from_dater7   set_to_dater8   set_segment_datar=   appendr   	get_indexset_pagination)r'   vys_page	conditionempallowance_detailsempallowance_datar2   
segment_idsegment_dtapay_com_objempallowancerespcompany_con_objvpager+   r+   r,   employeepaystrct_summaryN   s2   &

z<EmployeePaystructure_detailsService.employeepaystrct_summaryc                 C   s  t j|  j|tjd}dd |D }dd |D }tdd |D }t| 	 
|}t| 	 |}tdd |D }tdd |D }	t|	 t| g }
|D ]}t }||j ||j ||j ||j| |jd	krt| 	 |}||j| || || ||j ||j |j|_ |!|j" |#| |$| |%|	 |&|	 |'| |(| |)|j* |+|j, |-|j.| |
/| qU|
S )
N)r1   rh   c                 S   ri   r+   rj   rk   r+   r+   r,   rn   q   ro   zMEmployeePaystructure_detailsService.employee_level_detail.<locals>.<listcomp>c                 S   ri   r+   rp   rk   r+   r+   r,   rn   r   ro   c                 s   s     | ]}|j d kr|jV  qd S r"   )r6   r<   rk   r+   r+   r,   	<genexpr>s   s    zLEmployeePaystructure_detailsService.employee_level_detail.<locals>.<genexpr>c                 S   s   g | ]
}|j d kr|jqS )rq   )r5   r<   rk   r+   r+   r,   rn   y   s    c                 S   s&   g | ]}|j d kr|jdkr|jqS )r   N)r5   r6   r<   rk   r+   r+   r,   rn   z   s   & rq   )0r   rG   rH   rI   rJ   r   rs   sumr
   rT   rw   r   rv   printr   rx   r0   r~   r<   set_yearly_amountry   r2   r5   r   rz   r{   set_cc_totalset_yearly_cc_totalr|   r3   r}   r4   companycontributionr   r6   set_Annual_bonus_totalset_Yearly_Annual_bonus_totalset_total_component_amountset_yearly_totalset_cc_total1set_yearly_cc_total1r   r7   r   r8   r   r=   r   )r'   	emppay_idemp_objr2   r   bonus_totalr   r   r5   without_company_contributionarrrm   	data_respr   r+   r+   r,   employee_level_detailo   sJ   








z9EmployeePaystructure_detailsService.employee_level_detailc                 C   sv   t j|  j|djtj|t	 d}|dkr*t
 }|tj |tj |S t }|tj |tj |S )Nr/   )rh   r9   r:   r   )r   rG   rH   rI   rJ   rK   r   inactiver   rP   r   r?   r   r@   rX   r   DELETE_MESSAGEr   set_coder   UNEXPECTED_ERRORset_descriptionr   INVALID_DATA)r'   r0   r_   detail_datard   r+   r+   r,   paystrct_detail_inactive   s   *z<EmployeePaystructure_detailsService.paystrct_detail_inactivec                 C   s   t j|  j|d}t|  |jg}t	 }|
|j ||j ||j ||j| ||j ||j ||j ||j ||j t|  |j}|| |S )Nr/   )r   rG   rH   rI   rS   r
   rT   rw   r2   r   rx   r0   r~   r<   set_emp_payr1   ry   r|   r3   r}   r4   r   r6   r   r7   r   r8   r   get_segmentmasterr=   set_segment)r'   	struct_idr   r   r   r   r+   r+   r,   get_paystruct_value   s   
z7EmployeePaystructure_detailsService.get_paystruct_valuec                 C   s   t  }|j}g }t|   }|D ]>}||d d  }	t||d d|	||  d}
|D ]}|j|d krJt||j|d d|	||  d}|	| q,|	|
 qtj
|  | t }|tj |tj |S )N
percentager.   r0   F)r1   r3   is_deductionr<   r>   r;   )r1   r2   r3   r   r<   r>   r;   )r   var_arrr
   rT   get_default_componentsr   rQ   allowance_typer0   r   rG   rH   rI   bulk_creater   r?   r   r@   rX   r   rY   )r'   r1   ctc_amtemp_idalw_typealw_arr
create_arrdefault_datarm   r<   emp_alwr`   alwr   r+   r+   r,   create_emp_default_allowance   s:   
	z@EmployeePaystructure_detailsService.create_emp_default_allowancec                 C   s  t j|  j|d}dd |D }t|}dd |D }t|  |}t	 }|D ][}|j
j}	t|j
j}t|	}
t }|j
jdkrat||
 d  }|_|j
j|_||kr]||_n%||_n!|j
jdkrt||
 d  }|_|j
j|_||kr||_n||_|| q,|S )N)emp_pay__employee_idc                 S   ri   r+   r<   rk   r+   r+   r,   rn      ro   z[EmployeePaystructure_detailsService.employee_sancturaies_pf_calculation.<locals>.<listcomp>c                 S   ri   r+   rj   rk   r+   r+   r,   rn      ro   zEmployee PFr.   zESI std)r   rG   rH   rI   rJ   r   r   rT   get_component_valuesr   paycomponent_datar   rD   r<   r   nameintpfstructurer   )r'   employee_idobjr<   
sum_amountr   pf_data_calculation	list_datarm   r   pay_struct_det_perr   r`   r+   r+   r,   #employee_sancturaies_pf_calculation   s6   

zGEmployeePaystructure_detailsService.employee_sancturaies_pf_calculationc                 C   s   t  }t|  |}tj|  j|d |dt	j
d}dd |D }t|}|d }t|d }	t||	 d }
||k rH|d	 d
krHd}n#||krQt|
}n|
dkrZt|
}n|
|d krgt|d }nt|
}|| |S )Nr2   F)paycomponent__inr   r5   rh   c                 S   ri   r+   r   rk   r+   r+   r,   rn     ro   zVEmployeePaystructure_detailsService.employee_deduction_calculation.<locals>.<listcomp>
sal_amountpfstructure_percentager.   structure_nameESI0.0r<   )r   r   rT   get_paycomponent_datar   rG   rH   rI   rJ   r   rs   r   rD   roundr   )r'   r   paycomponent_idr   r   re   r<   r   esi_cal_amtpf_data_calculation_valuesfinal_calculationr+   r+   r,   employee_deduction_calculation  s*   


zBEmployeePaystructure_detailsService.employee_deduction_calculationc                 C   s   t  }t|  |}tj|  j|d |t	j
dd}|d }dd |D }t|}|d }	t|d }
t||
 d	 }|	|k rL|d
 dkrLd}n||krUt|}nt|}|| |S )Nr2   F)r   r   rh   r5   company_contribution_amountc                 S   ri   r+   r   rk   r+   r+   r,   rn   %  ro   zXEmployeePaystructure_detailsService.company_contribution_calculation.<locals>.<listcomp>r   company_contribution_percentager.   contribution_structure_namer   r   )r   r   rT   get_companycontri_datar   rG   rH   rI   rJ   r   rs   r   rD   r   r   )r'   r   r   r   r   re   r   r<   r   r   company_contribution_pertotal_calculationround_of_amountr+   r+   r,    company_contribution_calculation  s    $

zDEmployeePaystructure_detailsService.company_contribution_calculationc                 C   s2   t j|  jtj|dddddddd}|S )	NFrh   emp_pay__employee_id__inr5   r   r2   r<   r6   r7   r8   r   rG   rH   rI   rJ   r   rs   valuesr'   r   r   r   r+   r+   r,   employee_paystucture_details4  s   .z@EmployeePaystructure_detailsService.employee_paystucture_detailsc              	   C   s4   t j|  jtj|ddddddddd	}|S )
NFr   r   emp_pay__take_homer2   r<   r6   r7   r8   r   r   r+   r+   r,   employee_paystucture_details18  s   0zAEmployeePaystructure_detailsService.employee_paystucture_details1c                 C   s0   t j|  jtj|ddddddd}|S )N)rh   r   r2   r5   r<   r6   r   emp_pay__gross_payr   )r'   emp_liststruct_queryr+   r+   r,   emp_structure_info_withcb<  s   ,z=EmployeePaystructure_detailsService.emp_structure_info_withcbc                 C   s   t  }|tj |d dks|d d kr|d }nt|t|d  d }t|d}|d dkr=t|  |d }nt	|  
|d }t|  |d	 }	tj|  j|||	|d |d |d ||  |d
	}
|tj |
j|_|S )NCOMPANY_CONTRIBUTIONrq   TYPEAMOUNTPAYCOMPONENT_PERCENTAGEr.      PAYCOMPONENTPAYCOMPONENT_TYPE)	r1   r2   r3   r4   r5   r6   r>   r;   r<   )r   r?   r   r@   rD   r   r   rT   company_contribution_valr
   get_payrollcomponent_valr   get_componenttype_namer   rG   rH   rI   rZ   rQ   rX   r   r\   r0   )r'   details_datar_   r`   rb   rd   r   r<   r2   r3   re   r+   r+   r,   struct_detail_excel_upload@  s.   

z>EmployeePaystructure_detailsService.struct_detail_excel_uploadc           
      C   sj  t  }|tj | d urktj|  j	| dj
|| | | | | | | |t |  | | | d}tj|  j| d}t|  ||j|tjtj }	|!t"j# nDtj|  j$|| | | | | | | ||  | | | d}t|  ||j|tjtj%}	|!t"j& |j|_|S )Nr/   )r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   segment_percentage)r1   r2   r3   r4   r5   r6   r7   r8   r>   r;   r<   r=   r   )'r   r?   r   r@   get_details_idr   rG   rH   rI   rJ   rK   rL   rM   rE   rA   rB   rN   rO   r   rP   rQ   rC   rR   get_segment_percentagerS   r	   rT   rU   r0   r   rV   r   rW   rX   r   rY   rZ   r[   r\   )
r'   r]   r^   r_   r   	take_homer   rd   re   rf   r+   r+   r,   !create_employeepay_structdetails1`  sX   zEEmployeePaystructure_detailsService.create_employeepay_structdetails1c                 C   s.  zrt  }|tj tdt|  t|  	|d }	t
|  |d }
t|  |d }tj|  j||
||d |d |d ||  |d |d	 |d
 |	|d d}tdt|j  |tj |j|_|W S  ty } zt }|tj |t| |W  Y d }~S d }~ww )Nz(upload EmployeePaystructure_details dataSEGMENTr   r   r   r   r   r   	FROM_DATETO_DATESEGMENT_PERCENTAGE)r1   r2   r3   r4   r5   r6   r>   r;   r<   r7   r8   r=   r   z&upload EmployeePaystructure_details id)r   r?   r   r@   r   infostrr   rT   get_segment_namer
   r   r   r   r   rG   rH   rI   rZ   rQ   r0   rX   r   r\   	Exceptionr   r   r   r   r   )r'   r   r_   r`   rb   r   pf_typeis_esird   r   r2   r3   re   excepr   r+   r+   r,   struct_detail_excel_upload1  s@   z?EmployeePaystructure_detailsService.struct_detail_excel_upload1c                 C   sz  zt dt|  ddlm} t }|tj ddg}|D ]}	t dt|  |dks7t	|d d	krddlm} t
|  |	}
t
|  |
d }t	|d d	ks]|dkrt|  |d tj}t| tj|  jtj||dd|d}t dt|  t|d t|d  d }|d d krt|d |d dksn|d |k rt|d }n(t|}n#|dkrd}n|d |d krt|}nd}nd}n	t|d }nd}tj|  j||
d|d d||  |d}q |t j! |j"|_"t dt|  t dt|  |W S  t#y< } zt$ }|%t&j' |(t| |W  Y d }~S d }~ww )NzWupload EmployeePaystructure_details company contribution calcultion start (employee_id)r   )r   PFr   zcondition for loop finish if condition start is_esi == (1 or 0) or get_pf_type(pf_type)['name'] == ('NORMAL_PF' or 'STANDARD_PF' or 'VIRTUAL_PF') (employee_id)rq   r   	NORMAL_PFSTANDARD_PF
VIRTUAL_PFr0   F)rh   r   r   r5   r<   z*------COMPANY CONTRIBUTION SUM_AMT--------amount__sumr   r.   iuhr   g        )r1   r2   r3   r4   r5   r>   r;   r<   zUupload EmployeePaystructure_details company contribution calcultion end (employee_id)zbupload EmployeePaystructure_details company contribution calcultion end (strcuture_id--emp_pay_id)))r   r  r  django.db.modelsr   r   r?   r   r@   r   r   rT   r   company_contribution_datar   cc_epf_based_paycomr   COMPANYCONTRIBUTIONr   r   rG   rH   rI   rJ   r   rs   	aggregaterD   r   rZ   rQ   rX   r   r\   r0   r	  r   r   r   r   r   )r'   r_   r`   r   r
  r  r   rd   data1rm   r2   pfflag_mastersum_amtsum_precr<   re   r  r   r+   r+   r,   cc_struct_detail_excel_upload  sj   *


	zAEmployeePaystructure_detailsService.cc_struct_detail_excel_uploadc                 C   sL   t j|  j|dd}dd |D }t|  |}| |||}|S )NT)emp_payr5   c                 S   ri   r+   rj   rk   r+   r+   r,   rn   G  ro   zQEmployeePaystructure_detailsService.company_contribution_data.<locals>.<listcomp>)	r   rG   rH   rI   rJ   r   rT   company_contribution_data_getset_company_contribution_values)r'   id_datar   r   paycomponent_dtar`   r+   r+   r,   r  D  s   z=EmployeePaystructure_detailsService.company_contribution_datac           	   	   C   s   g }|D ]?}|D ]:}|j |krB|d|j|j|j|jd}|| |D ]}|D ]}|j|d kr@|d|j  |dt|j q'q#qq|S )NT)r0   r5   r   
max_amountr   r   r0   
details_iddetails_amount)	r0   r   r<   r   r   r   r2   __setitem__r  )	r'   r5   r   r   arr_dtarm   jr`   kr+   r+   r,   r!  M  s$   

zCEmployeePaystructure_detailsService.set_company_contribution_valuesc              
   C   s  t j|  j|tjdd}dd |D }|r?t|	dd}|d 
t|d< |jddgdd}tt|d |d }ni }tt|}|  g }|D ]v}g }t j|  j||tjdd	}	t|   }
|	D ]G}t|  |j}g }|
D ]}|j}|D ]}||jkr|j}|| qq}|d
 |jt|jt|j|d t|jd |d}|| qmt|   |||}|| qO|S )NF)r1   rh   r5   c                 S   ri   r+   rp   rk   r+   r+   r,   rn   a  ro   zREmployeePaystructure_detailsService.without_cmpny_contribution.<locals>.<listcomp>r=   r   first)subsetkeep)r1   r=   rh   r5   r2   paycomponent_namer0   )r2   r%  r<   r4   r.  r6   paycom)!r   rG   rH   rI   rJ   r   rs   pd	DataFramer   astyper  drop_duplicatesdictziplistsetsortr   rT   get_employeepfstruct_detailsr
   paycomponent_details_datar2   r   r   r0   r<   r4   r   r6   r   segment_for_structure_get)r'   r"  r   r   pay_dfunique_segment_infor   rm   pf_arrobj_dataemp_pf_datar*  r   r(  emp_pfpf_componentpf_component_dtaemp_pf_namepay_com_datasegment_datar+   r+   r,   without_cmpny_contribution]  sT   


z>EmployeePaystructure_detailsService.without_cmpny_contribution)__name__
__module____qualname__r$   rg   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r!  rG  __classcell__r+   r+   r)   r,   r!      s,    .!,&# 0' 	r!   )3datetimepandasr0  django.utilsr   r  r   r   npayroll.settingsr   8payrollservice.data.response.emppaystructdetailsresponser   4payrollservice.data.response.emppaystructureresponser   payrollservice.modelsr   #payrollservice.service.auditservicer	   ,payrollservice.service.payrollmastersservicer
   r   r   r   r   r   payrollservice.util.payrollutilr   r   r   r   r   r   r   3utilityservice.data.response.nwisefinerrorconstantsr   r   ,utilityservice.data.response.nwisefinsuccessr   r   r   *utilityservice.data.response.nwisefinerrorr   )utilityservice.data.response.nwisefinlistr   .utilityservice.data.response.nwisefinpaginatorr   +utilityservice.service.applicationconstantsr   "utilityservice.service.threadlocalr    r!   r+   r+   r+   r,   <module>   s&     $