B
    YcC                 @   sL  d dl Z d dlZd dl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 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mZ d d
lmZ d dlm Z 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-m.Z. d Z/G dd deZ0G dd dej1Z2G dd dej3e e"Z4dS )    N)APIException)CaseWhenIntegerFieldValue	TextField
FloatFieldQ)Sum	Aggregate)gettext_lazyactivate)mixinsserializers)Response)NoneSerializer)get_categoriesget_leave_summary_dict)ReportGenericFilter)ReportUtilGenericViewSetIntegerFormatFieldWorkDayRelatedFieldsUpdateUtil#IntegerFormatLeaveCountSummaryFieldIntegerFormatCountField)PayloadBase)get_item_symboldates_function)r	   )
C_ATT_RULE)STATUS_VALIDSTATUS_RESIGN_PENDINGc                   s@   e Zd Z fddZ fddZ fddZ fddZ  ZS )	GroupContactc                sH   d| _ d| _tt| j||f|\}}|dd}|jddd}||fS )zZ
        SQL Server 2017 Support STRING_AGG
        STRING_AGG(column, separator)
        ZSTUFFz%(function)s((
            SELECT ',' + %(expressions)s
            FROM [{table}] as {copy}
            WHERE {copy}.[emp_id] = [{table}].[emp_id]
            FOR xml path('')
        ), 1, 1, '')z[att_payloadbase]z{copy}Zatt_payloadbasea)tablecopy)functiontemplatesuperr    as_sqlreplaceformat)selfcompiler
connectionextra_contextsqlparams)	__class__ EG:\easytimepro\master/mysite/att\api\views\view_report_dailystatus.pyr'   !   s    zGroupContact.as_sqlc       	         sj   ddl m} |d}|r"|d p$d}|dkr<d| _d| _nd| _d	| _tt| j||f|\}}||fS )
Nr   )	sql_utilsz7select DISTINCT(VERSION) from product_component_version z11.0ZlistaggzB%(function)s(%(expressions)s, ',') within group(order by att_date)Z	WM_CONCATz%(function)s(%(expressions)s))mysiter3   Zp_query_oner$   r%   r&   r    r'   )	r*   r+   r,   r-   r3   Zrowsversionr.   r/   )r0   r1   r2   	as_oracle2   s    
zGroupContact.as_oraclec                s.   d| _ d| _tt| j||f|\}}||fS )NZGROUP_CONCATz!%(function)s(%(expressions)s, ''))r$   r%   r&   r    r'   )r*   r+   r,   r-   r.   r/   )r0   r1   r2   as_mysql?   s    zGroupContact.as_mysqlc                s.   d| _ d| _tt| j||f|\}}||fS )NZARRAY_TO_STRINGz-%(function)s(ARRAY_AGG(%(expressions)s), ','))r$   r%   r&   r    r'   )r*   r+   r,   r-   r.   r/   )r0   r1   r2   as_postgresqlE   s    zGroupContact.as_postgresql)__name__
__module____qualname__r'   r7   r8   r9   __classcell__r1   r1   )r0   r2   r       s   r    c                   s  e Zd ZejedddZejeddddZejedd	ddZejed
dddZ	ej
eddZejeddddZejeddddZejeddddZejeddddZeeddddZeeddddZeeddddZeed d!d"dZeed#d$d%dZeed&d'd%dZeed(d)d%dZeed*d+d,dZejdd-Ze Ze Z fd.d/Zd0d1 Z G d2d3 d3Z!  Z"S )4DailyStatusSerializerZreport_column_empCodeemp__emp_code)labelsourceZreport_column_firstNameemp__first_nameT)r@   rA   
allow_nullZreport_column_lastNameemp__last_nameZreport_column_nickNameemp__nicknameZreport_column_gender)r@   Zreport_column_departmentCodeemp__department__dept_codeZreport_column_departmentNameemp__department__dept_nameZreport_column_positionCodeemp__position__position_codeZreport_column_positionNameemp__position__position_nameZreport_column_lateDurationsum_latelate_in)r@   rA   Z
format_keyZ report_column_earlyLeaveDurationsum_early_leave	early_outZreport_column_absentDuration
sum_absentabsentZ report_column_actualWorkDuration
sum_workeddurationZreport_column_normalOvertimesum_notZovertimeZreport_column_weekendOvertimesum_wotZreport_column_holidayOvertimesum_hotZreport_column_leaveDuration	sum_leaveleave)rC   c       
         s   | d}tt| j|| t| }x>|jD ]4}dj|d d}t|d d|d |dd| j|< q,W x*|j	D ] \}}}	t
j|	|dd	| j|< qlW d S )
Nviewzleave_{index}id)indexZcategory_namer?   T)r@   rA   Zformat_indexleave_summary_dictrC   )r@   rA   rC   )popr&   r>   __init__r   
categoriesr)   r   fieldsdate_periodr   	CharField)
r*   argskwargsrW   rZ   cgZfieldrA   Zfiledr@   )r0   r1   r2   r\   j   s    
zDailyStatusSerializer.__init__c             C   s*   d}|d dkrd}n|d dkr&d}|S )Nr4   emp__genderMZMaleFZFemaler1   )r*   objgenderr1   r1   r2   
get_genderz   s    z DailyStatusSerializer.get_genderc               @   s   e Zd ZeZdZdS )zDailyStatusSerializer.Meta)emp_code
first_name	last_name	nick_namerh   	dept_name	dept_codeposition_codeposition_name
total_latetotal_early_leavetotal_absenttotal_worked	total_not	total_wot	total_hottotal_leavework_dayrQ   att_datewhole_day_durationN)r:   r;   r<   r   modelr^   r1   r1   r1   r2   Meta   s   r~   )#r:   r;   r<   r   r`   _rj   rk   rl   rm   ZSerializerMethodFieldrh   ro   rn   rp   rq   r   rr   rs   r   rt   ru   rv   rw   rx   ry   r   rz   r   rQ   r|   r\   ri   r~   r=   r1   r1   )r0   r2   r>   L   s:   

r>   c                   s   e Zd ZeZej  Ze	Z
ddddddddgZeed	Zd
Zd
Zi Z fddZdd Z fddZdd Zdd Zdd Z fddZdd Zdd Z  ZS )DailyStatusReportViewSetrr   rs   rt   ru   rv   rw   rx   ry   )listZexportNc                s  | j jj}tt|  }y| j jjs8d|i}|jf |}| j jjs| j jj	}| j jj
}| j jj}| rx|j| d}| r|j| d }| r|j| d}tt|d}|dkr|ttdtd dB ttdB }W n tk
r   tdY nX |S )	NZemp__company_id)Zemp__department__in)Zemp__area__in)Zemp__position__in
resign_emp0)Zemp__statuszPlease check with Admin)requestusercurrent_companyr&   r   get_querysetZis_employeefilterZis_superuserZget_auth_deptZget_auth_areaZget_auth_posexistsallZdistinctstrr   getr	   r   r   	Exceptionr   )r*   
company_idquerysetZbase_filterZ	auth_deptZ	auth_areaZauth_posr   )r0   r1   r2   r      s.    





$z%DailyStatusReportViewSet.get_querysetc             C   s  | tddptdddddddd	d
d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 dtdt dtdt dtdt dtdt dtttdddt dd}| 	 }|t
kr| |}t | _x\| jD ]R}dj|d d| jkr,| jdj|d d tdt d|dj|d d< qW | j}| jjd d }| jjd!d }|| _|| _g }|rh|rh| jjj}	td"}
tj|
d#|d d$ d%}tj|
d#|d d$ d%}x||krfttt|d&d'd'tt d(|	|d)t|d*d'd'tt d+|	|d)t|d,tt d-|	|d.t|d*tt d/|	|d.t|d&tt d0|	|d.t|dtt d1|	|d2t|dtt d|	|d3t|dtt d4|	|d5t|dtt d6|	|d7t|dtt d8|	|d9t|d:tt d;|	|d<t|d:tt d=|	|d>t! d||"d?< ||"d?|"d@|"dAf |tj#d,dB7 }qW || _$|j%f |}x|D ]}td"}
tj|
d#| jd d$ d%}tj|
d#| jd d$ d%}t&|d||| jjj|}|rx|' D ]\}}|||< qW qW |S )CNr   )duration__gt)Zwhole_day_duration__gtZemp_idr?   rB   rD   rE   rd   rF   rG   rH   rI   Zlate)Zoutput_fieldZearly_leaverO   ru   Zovertime__normal_otZovertime__weekend_otZovertime__holiday_otrV   rQ   Zduty_durationrz   )r   then)rJ   rL   rN   rP   rR   rS   rT   rU   Zsum_durationZsum_duty_durationZsum_work_dayzleave_{index}rX   )rY   zsum_leave{index}
start_dateend_datez[-:\s]r4      z%Y%m%d   FZ	holiday_p)r{   day_offclock_in__isnullclock_out__isnullr      Zweekdayoff_p   r   )r{   r   r   ZweekendZholidayhalf_day)r{   r   r   )r{   Z
absent__gtr   rK   )r{   Zlate__gtr   rM   )r{   Zearly_leave__gtr   Zpresent)r{   Zduty_worked__gtr   TZmiss_in)r{   r   r   Zmiss_out)r{   r   r   z%Y/%m/%dz%m/%dz%d)days)(r   r	   valuesZorder_byr
   r   r   r   r   Zget_work_day_display WORK_DAY_DISPLAY_BY_TOTAL_RECORDZupdate_fields_for_workdayr   r]   r)   summary_fieldsappendr   file_formatr   GETr   r   r   r   r   recompiledatetimestrptimesubr    r   r   strftimeZ	timedeltar_   Zannotater   items)r*   r   r   Zwork_day_displayrc   r   r   r   r_   r   patternZqueryZstart_date_newZend_date_newZ	data_dictkeyvalr1   r1   r2   annotate_queryset   s    
"





  z*DailyStatusReportViewSet.annotate_querysetc                s   | |d< t t| j||S )NrW   )r&   r   get_serializer)r*   ra   rb   )r0   r1   r2   r     s    z'DailyStatusReportViewSet.get_serializerc             C   s   | j | jtS )N)serializer_dictr   actionr   )r*   r1   r1   r2   get_serializer_class  s    z-DailyStatusReportViewSet.get_serializer_classc             C   s   t d tdS )NZenzDaily Status Report)r   r   )r*   r1   r1   r2   get_file_title   s    z'DailyStatusReportViewSet.get_file_titlec             C   s0   |   }| j| | |  }| |}|S )N)generate_time_mapextra_headers_translation_dictupdatefilter_querysetr   r   )r*   time_mapr   r1   r1   r2   get_export_queryset$  s
    
z,DailyStatusReportViewSet.get_export_querysetc                s   | |d< t t| j||S )NrW   )r&   r   r   )r*   ra   rb   )r0   r1   r2   r   ,  s    c             O   s`   |  |  }| |}| |}|d k	rH| j|dd}|j}| |S | j|dd}t|jS )NT)Zmany)r   r   r   Zpaginate_querysetr   dataZget_paginated_responser   )r*   r   ra   rb   r   ZpageZ
serializerresultr1   r1   r2   r   0  s    


zDailyStatusReportViewSet.listc             C   s   dddddddg}i }| j jdd }|d	\}}}tt|t|d
 }xbt|D ]V}tt|t||d
 }	|	dd t
|d
 	d }
d|d
 ||	 f ||
< qXW |S )Nre   TWZThrf   ZStSr   -r   r   /z%s %s)r   r   r   splitcalendarZ
monthrangeintrangeZweekdayzfillr   )r*   Zsym_weekr   r   ymr   r   iZwofkr1   r1   r2   r   <  s     z*DailyStatusReportViewSet.generate_time_map)r:   r;   r<   r   r}   Zobjectsr   Zselect_relatedr   r   Zfilter_classr   r>   r   r]   r_   r   r   r   r   r   r   r   r   r   r=   r1   r1   )r0   r2   r      s&   ]
r   )5r   r   r   Zrest_framework.exceptionsr   Zdjango.db.modelsr   r   r   r   r   r   r	   Zdjango.db.models.aggregatesr
   r   Zdjango.utils.translationr   r   r   Zrest_frameworkr   r   Zrest_framework.responser   Zmysite.att.api.serializersr   Zmysite.att.api.utilsr   r   Zmysite.att.api.utils_classr   r   r   r   r   r   Zmysite.att.modelsr   Zmysite.att.utilsr   r   Zmysite.att.global_cacher   Zmysite.admin.modelsr   r   r   r    ZModelSerializerr>   ZListModelMixinr   r1   r1   r1   r2   <module>   s*   $-?