o
    ۷hk                     @   sX  d dl Z d dlZd dlm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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 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( eedgee$geee&gdd Z)eedgee$geee&gdd Z*eedgee$geee&gdd Z+eedgee$geee&gdd Z,eedgee$geee&gdd Z-eedgee$geee&gdd Z.eedgee$geee&gd d! Z/eedgee$geee&gd"d# Z0dS )$    N)datetime)HttpResponse)render_to_string)csrf_exempt)api_viewpermission_classesauthentication_classes)IsAuthenticated)'EmployeePaystructure_deductionsResponse)!Employeemonthlypay_detailsService)NWisefinError)NWisefinList)NWisefinPage)NWisefinPaginator)NWisefinSuccess)
ModuleList)NWisefinAuthentication)NWisefinPermission)Payrollcommon_ApicallGETc           *      C   s  | j }| jdkr| jd}| jd}| jdd }|dv r|dkrRt }t|}||}t|||}||_	|d |_
|d d |_t| d	d
}	|	S |dkrqt|||}|ddt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| g }|jdddt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| gf jdd|d< t|}||}
i }|
d|d< |
d |d!< |
d"|d#< d$}t|d
}	d%tt  }d&t|d'  d(|	d)< tj|	d*d+}|j|d,d-d. |jd/ }|j}|d0|d  |d1|d!  |d2|d#  |d3d4i}|ddd| |d5dd!| |d6dd#| |d7d8i}|d9 |  |dd-d%| |d7d:i|d7d;ig}tttd<t t!j"dd  }ddg}t||D ])\}}t||j#$| }tt%|j&d= }|d> | | }|'|d?|d@ q|d7dAi}dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| dt| g}|D ]'}t||j#$| }tt%|j&d= }|d> | | }|'|d?|d@ q$|d4dBddC} t(|j#j)D ]\}!}"|d-|!|"|  q[|*  t|	S |dDkrt }t|}||}t|||}||_	|d |_
|dd |_dE}#t+,| }$t-|#|$}%dd l.}&|&j/|%d,dF}'tdGd
}	|	|' |	S |dHkrt|0||}t1|tst| d	S t|2|}(|(S d S t3 })dI|)_4t|) d	}	|	S d S )JNr   employee_idyearaction)0123r      application/jsoncontent_typer   ItemzJanuary z	February zMarch zApril zMay zJune zJuly zAugust z
September zOctober z	November z	December r   2      axiszGrand Total	full_nameEmployeeNamecodeEmployeeCode	pf_numberz	PF NUMBERapplication/vnd.ms-excelzYTD SUMMARYattachment; filename=".xlsx"Content-Disposition
xlsxwriterengineF   indexstartrowSheet1C2C3C4boldT      bg_colorFCBA03centerz#BDFCC9z#98F5FF   d   z7:	no_blankstypeformatz#D4D4D4z#ffcccc)r<   fg_colorborderr   zytdsummary.html)output_pathzapplication/pdfr   zinvalid inputs)5scopemethodr   getr
   r   emp_details_payrollr   "employeemonthlypay_detailinfo_get1employee_personal_infopaycomponentpay_info_monthr    employeemonthlyyear_excel_reportstrlocsumr   nowpdExcelWriterto_excelsheetsbookwrite
add_formatwrite_string	set_alignset_bolddictziprangeliststringascii_uppercasecolumnsget_loclenr6   conditional_format	enumeratevaluesclosejsonloadsr   pdfkitfrom_string!employeemonthlypay_detailinfo_get
isinstancepayroll_commonpdfr   message)*requestrK   emp_idr   r   arrapifunctionemp_dataemployeemonthpay_detailresponse	emp_data1dict1excelfilenamewriter	worksheetworkbookbold_formatheader_formatrG   dcolscoljexcel_headerlen_dfrngformat_1cols1col1len_df1rng1header_format2col_numvaluetemplate_pathdatastr_templaterq   output
common_pdf	error_obj r   Q/var/www/FinalPayroll/payrollservice/controller/empmonthlypaydetailscontroller.pyemployee_monthlydetails_year   s   







 |






r   c           #      C   sz  | j }| jdkr;t| }|jg dd}d|d< d|d< d|d< |jdd	d
dddddddd
d}|g d }d|d< | D ]H\}}|d }t|d }	t|d }
t|d }t|d }t|d }t|d }|| }||	 |
 | | }||j|df< ||j|df< qB|g d }d}t	|d}t
 }|d}|d}|d}|d}d| d | }d t|d!  d"|d#< tj|d$d%}|j|d&d'd( |jd) }|j}d}|j|}t|jd* }td+|  d,td+|  | } |d-d.i}!|| d/|!d0 |d1d2i}"|d3d't||" |d3d4td5| d | d | d |  |  t	|S d S )6Nr   )Lempmonthly_pay_idid_xcreated_date_xcreated_by_xupdated_date_xstatus_xupdated_by_x
pay_statuspay_modeis_tdspf_typeid_ycandidate_codeemployment_typeuser_id
first_namemiddle_name	last_namegradesrlnobranch
address_idemployeebank_idlastsync_datestatus_yr   r   r   r   r   created_by_ycreated_date_yupdated_by_yupdated_date_ygrade1grade2genderdepartment_idemployee_branch_idroleRMfunctional_headgrade1_dategrade2_datenoticeperioddobexpected_dojeffective_fromphone_noemployee_type
supervisor	hierarchy
contact_idmanagerorg_iddesignation_idworkmodeapprove_statusapproved_by
disabality
is_payrollr   
costcentrebusinesssegmentpayroll_dateidentity_id_x
created_bycreated_date
updated_byupdated_datepaycomponent_idrF   	from_dateto_dater   rh   0.0Dearness
House RentSpecialEmployee IDEmployee nonameDesignationE-mailBank A/C NoDAYS IN MONTHEMP EFFECTIVE WORKDAYS	Join DateTOTAL DEDUCTION)
employee_id_xcode_yr'   designationemail_idaccountnumberpayable_days	paid_daysdojamount)r   r   r   r   r   r   r   r   r   
Basic & DAr   r   r   r+   
esi_number	gross_payr   NET PAYr   
Total_days)r   r   r   r   r   r   r+   r   r   r   r   r   r   r   r   r   r   r   r   r,   r    z%bz%Yz%dz%Xz"Salary Statement For The Month Of  r-   r.   r/   r0   r1   r2   Fr=   r5   r8   r>   A   z2:r?   yellowrD   rE   r<   Tr   
   zCreated On: )rK   rL   r   salary_excel_downloadedroprenameiterrowsfloatrU   r   r   rW   strftimerT   rX   rY   rZ   r[   r\   rh   ri   rj   r6   chrr^   rk   r_   rn   )#rw   rK   payroll_serv	remove_dfdata_dffinal_dfr6   rowbasic_dar   
house_rentr   Total_deductionr   r   total_payable_daystotal_calculationdfr   r}   
month_yearmonthr   daymin_secr   r   r   r   r   column_indexr   r   rG   r   r   r   r   salarystatement_excel_downloade   sd   	"





".r  c                 C   s  | j }t }| j}| jdkrG| jdd}t|| dd|}z|rd|jvr,d|d< d|jvr5d|d< d	|jvr>d
|d	< d|jvrGd
|d< d|jvrPd
|d< d|jvrYd
|d< t	j
|d t	
|d  dd|d< t	j
|d t	
|d  dd|d< |d |d |d   |d< t	|d |d< |d }|jd|d< |jdddd}|d
 |jtjdidd |jddd t|d}t	j|d d!}	|j|	d"d# |	  |W S W n   |d$ t| d%d}
|
 Y S t }| jd&d}t|}t|d'}|jrtg | d'}|| n!|j d(d)}t!"||# |$  }||_%t|| d'}||_&t!j'|d*d+ d,d-}t|d%d}
|
S d S ).Nr   r   r   r$   r=   zAnnual Bonus Paid Monthlyg        zStd Annual Bonus Paid MonthlyzStd LTAr   LTAPFESIz	Gross payr  )downcastzGross EarningszGross pay_stdzStd Gross EarningszStd Emp deductions  ESIzStd Emp deductions  PFz
Net Amountr   %d %b %YEmp_name
Department)r'   
departmentr   T)inplacer    r1   r2   r8   
sheet_namezNO DATAr   pager  records)orientc                 S   s   | j S )N)__dict__)or   r   r   <lambda>/  s    z*salary_statement_summary.<locals>.<lambda>   )defaultindent)(rK   r   r   rL   r   rM   r   salarystatement_forallemp_cmrh   rX   
to_numericto_datetimedtr	  r  fillnareplacenpnan	set_indexr   rY   rZ   rn   
set_statusr   intr   emptyr   	get_indexset_paginationto_jsonro   rp   
get_offsetget_query_limitr   
paginationdumps)rw   rK   success_objr   r   r  datefin_dfrG   r   r}   	list_datar'  vys_pagevpageres
emp_fin_dfvalr   r   r   salary_statement_summary   sz   









5

rL  c                 C     | j }| jdkr| jd}| jd}| jdd}| jdd}| jdd	}| jd
d}| jd}| jd}	| jd}
t|}t|d}t|||||t|t|||	|
	}t|r|jr~t	|d}t
j|dd}|j|dd |  |S |d t|d< |d t|d< t
|d |d< |d }|jd|d< |jg ddddgdjdddjdd}t	|d}t
j|dd}|j|dd |  |S t	| dd}|S d S )Nr   r  r   r   r   r'  r$   queryr4   	paystatusemp_ccemp_bsemployeer  r    r1   r2   r8   r%  ztake_home w/o bonusbonusr   r   r   r)   r   r   r   headingsr6   rh   rm   rh   r&   levelr%   r   )rK   rL   r   rM   r:  r   r   payroll_review1r;  r   rX   rY   rZ   rn   astyper  r2  r3  r	  pivot_table
sort_index	swaplevelrw   rK   r  r   r   r'  rN  r   rP  rQ  rR  rG  r  rG   r   rD  r}   r   r   r   payroll_review4  sF   

&
*
r_  c                 C   "   | j }t| }t|dd}|S Nr   r    )rK   r   dropdown_ccr   )rw   rK   ccr}   r   r   r   rb  _     rb  c                 C   r`  ra  )rK   r   dropdown_bsr   )rw   rK   bsr}   r   r   r   re  k  rd  re  c                 C   rM  )Nr   r  r   r   r   r'  r$   rN  r4   rO  rP  rQ  rR  r  r    r1   r2   r8   r%  r   rS  r   r   rT  rU  rV  rh   rW  r%   r   )rK   rL   r   rM   r:  r   r   addictional_allowance_datar;  r   rX   rY   rZ   rn   rZ  r  r2  r3  r	  r[  r\  r]  r^  r   r   r   rg  v  sZ   



rg  c           
   
   C   s  | j }| j}| jdkr| jdd}t|}t|d}| jdd}t|| dd|||d d }|rvt	|t
r?t| dd	S |jr\t|d	}tj|d
d}|j|ddd |  |S t|d	}tj|d
d}|j|ddd |  |S t	|t
rt| dd	S t|dd	}	|	S d S )Nr   r'  r$   r  r   r   r=   r   r    r1   r2   r8   F)r&  r6   )rK   r   rL   r   rM   r:  r   r   #salarystatement_forallemp_cm_samplert   r   r   r;  rX   rY   rZ   rn   )
rw   rK   r   r'  rG  r   r  rG   r   r}   r   r   r   salary_statement_summary1  s6   





ri  )1ro   rf   r   numpyr6  pandasrX   django.httpr   django.template.loaderr   django.views.decorators.csrfr   rest_framework.decoratorsr   r   r   rest_framework.permissionsr	   >payrollservice.data.response.emppaystructuredeductionsresponser
   2payrollservice.service.empmonthlypaydetailsservicer   *utilityservice.data.response.nwisefinerrorr   )utilityservice.data.response.nwisefinlistr   )utilityservice.data.response.nwisefinpager   .utilityservice.data.response.nwisefinpaginatorr   ,utilityservice.data.response.nwisefinsuccessr   &utilityservice.permissions.util.dbutilr   $common_middleware.request_middlewarer   )utilityservice.service.nwisefinpermissionr   *utilityservice.service.payroll_api_servicer   r   r  rL  r_  rb  re  rg  ri  r   r   r   r   <module>   sz    
z
>
W
'


+
