B
    )ecH'                 @   s:  d dl Z d dlmZ d dlmZmZmZmZ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 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m%Z% d dl&m'Z' d dl(Z(G dd dej)Z*G dd deZ+G dd dej,eZ-dS )    N)Q)SumCaseWhenMaxValue)gettext_lazyactivate)	FilterSet)mixinsserializers)action)Response)STATUS_VALID)NoneSerializer)ReportUtilGenericViewSetIntegerFormatFieldReportGenericFilter)PayloadMulPunchSet)short_date_format)CHOICE_GENDER)
C_ATT_RULE)r   STATUS_RESIGN_PENDING)TimeIntervalc                   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j
eddZejeddddZej
eddZ fddZdd Zdd Zd d! ZG d"d# d#Z  ZS )$MulTransactionSerializerZreport_column_empCodeemp__emp_code)labelsourceZreport_column_firstNameemp__first_nameT)r   r   
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_summaryTotalTimeDurationZreport_column_timetabletimetable__aliasZreport_column_attendanceDatec                s   ddl m} d|kr|d}tt| j|| | }xt|jD ]}tj	t
ddj|dd|d| jd	j|d< tj	t
d
dj|dd|d| jdj|d< tt
ddj|dddd| jdj|d< qBW d S )Nr   )get_short_time_formatviewreport_column_clockInTimez	in{index})indexT)r   r   r   formatzclock_in{index}report_column_clockOutTimez
out{index}zclock_out{index}report_column_totalTimeDurationztotal{index}Zduration)r   r   r   Z
format_keyztotal_time{index})mysite.att.utilsr'   popsuperr   __init__range	max_indexr   ZDateTimeField_r+   fieldsr   )selfargskwargsr'   r(   Zshort_time_formati)	__class__ HG:\easytimepro\master/mysite/att\api\views\view_report_multransaction.pyr1   /   s    
z!MulTransactionSerializer.__init__c             C   s   t t|d |d S )Nemp__gender)dictr   get)r6   objr;   r;   r<   
get_gender@   s    z#MulTransactionSerializer.get_genderc             C   s   |d sdS t |d S )Natt_date )r   )r6   r@   r;   r;   r<   get_att_dateC   s    z%MulTransactionSerializer.get_att_datec             C   sb   | j d jj}d}tjj||d dd}x0|D ](}tj|d d |d d d	d	}q2W |S )
NrequestrC   r&   )
company_idaliasZwork_time_durationr   <   )ZhourZminutez%H:%M)
contextusercurrent_companyr   objectsfilterZvalues_listdatetimetimestrftime)r6   r@   rF   Zsummary_timeZtimetable_dataZeachr;   r;   r<   get_total_timeH   s    
(z'MulTransactionSerializer.get_total_timec               @   s   e Zd ZeZdZdS )zMulTransactionSerializer.Meta)emp_code
first_name	last_name	nick_namegender	dept_name	dept_codeposition_codeposition_namerB   Zweekday
total_time	timetableN)__name__
__module____qualname__r   modelr5   r;   r;   r;   r<   MetaP   s   ra   )r]   r^   r_   r   Z	CharFieldr4   rR   rS   rT   rU   ZSerializerMethodFieldrV   rX   rW   rY   rZ   r[   r\   rB   r1   rA   rD   rQ   ra   __classcell__r;   r;   )r:   r<   r      s*   r   c               @   s   e Zd ZG dd dZdS )MulTransactionSummaryFilterc               @   s   e Zd ZeZdddddgZdS )z MulTransactionSummaryFilter.MetaZ	employeesZdepartmentsZareasZ
start_dateZend_dateN)r]   r^   r_   r   r`   r5   r;   r;   r;   r<   ra   Y   s   ra   N)r]   r^   r_   ra   r;   r;   r;   r<   rc   X   s   rc   c                   s   e Zd ZeZejjdd dZ	e
Zg ZeeedZdZdd Z fddZd	d
 Zdd Z fddZdd Z fddZdd Zedgdddd Z  ZS )MulTransactionViewSet   )Z	data_typerB   )listZexportperiod_countc       	      C   s&  | tddpd| _|dd}|jtdd}ddd	d
ddddddddg}|j| }i }xt| jD ]}dj	|d}dj	|d}dj	|d}tt
t|d dd||< tt
t|d dd||< tt
t|d ddtdd||< dj	|d}|| jkrl| j| qlW |jf |}|S )N
data_indexZdata_index__maxre   r   rB   r[   )	total_allr   r    r!   r=   r"   r#   r$   r%   ri   r&   z	in{index})r*   z
out{index}ztotal{index}clock_in)rh   Zthen	clock_outr   )defaultztotal_time{index})Z	aggregater   r?   r3   order_byZannotater   valuesr2   r+   r   r   r   summary_fieldsappend)	r6   querysetrn   Zparamsr9   Zin_keyZout_keyZ	total_keyZ
temp_fieldr;   r;   r<   annotate_querysetk   s(    

$
z'MulTransactionViewSet.annotate_querysetc                s   | |d< t t| j||S )Nr(   )r0   rd   get_serializer)r6   r7   r8   )r:   r;   r<   rs      s    z$MulTransactionViewSet.get_serializerc             C   s   | j | jtS )N)serializer_dictr?   r   r   )r6   r;   r;   r<   get_serializer_class   s    z*MulTransactionViewSet.get_serializer_classc             C   s   t d tdS )NZenzMultiple Transaction Report)r	   r4   )r6   r;   r;   r<   get_file_title   s    z$MulTransactionViewSet.get_file_titlec                s   t t| |}xnt|D ]b\}}|dr>ttd||< q|drZttd||< q|dr|dkrttd||< qW |S )Nrj   r)   rk   r,   r[   r-   )r0   rd   get_headers_translation	enumerate
startswithstrr4   )r6   Zexport_headersZresult_listr9   v)r:   r;   r<   rw      s    


z-MulTransactionViewSet.get_headers_translationc             C   s   |  |  }| |}|S )N)filter_querysetget_querysetrr   )r6   rq   r;   r;   r<   get_export_queryset   s    
z)MulTransactionViewSet.get_export_querysetc                s   t t|  }| jjjs| jjj}d|i}|jf |}| jjjs| jjj	}| jjj
}| jjj}| rv|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 }|S )NZemp__company_id)Zemp__department__in)Zemp__area__in)Zemp__position__in
resign_emp0)Zemp__status)r0   rd   r}   rE   rJ   Zis_employeerK   rM   Zis_superuserZget_auth_deptZget_auth_areaZget_auth_posexistsallZdistinctrz   r   r?   r   r   r   )r6   rq   rF   Zbase_filterZ	auth_deptZ	auth_areaZauth_posr   )r:   r;   r<   r}      s(    





 z"MulTransactionViewSet.get_querysetc             O   s\   |  |  }| |}| |}|d k	rD| j|dd}| |jS | j|dd}t|jS )NT)Zmany)r|   r}   rr   Zpaginate_querysetrs   Zget_paginated_responsedatar   )r6   rE   r7   r8   rq   ZpageZ
serializerr;   r;   r<   rf      s    

zMulTransactionViewSet.listr?   F)methodsZdetailc             C   sF   dd l }ddlm} | |  }| |}||d| jd}|S )Nr   )HttpResponse)coder   )jsonZdjango.httpr   r|   r}   rr   dumpsr3   )r6   rE   r   r   rq   Zresponser;   r;   r<   rg      s    
z"MulTransactionViewSet.period_count)r]   r^   r_   r   r`   rL   rM   Zselect_relatedrm   rq   rc   Zfilter_classro   r   rt   r3   rr   rs   ru   rv   rw   r~   r}   rf   r   rg   rb   r;   r;   )r:   r<   rd   ^   s"   rd   ).Zdjango_filtersZdjango.db.modelsr   r   r   r   r   r   Zdjango.utils.translationr   r4   r	   Zdjango_filters.rest_frameworkr
   Zrest_frameworkr   r   Zrest_framework.decoratorsr   Zrest_framework.responser   Zmysite.admin.modelsr   Zmysite.att.api.serializersr   Zmysite.att.api.utils_classr   r   r   Zmysite.att.modelsr   r.   r   Zmysite.personnel.db_constr   Zmysite.att.global_cacher   r   Z$mysite.att.models.model_timeintervalr   rN   ZModelSerializerr   rc   ZListModelMixinrd   r;   r;   r;   r<   <module>   s*   ;