B
    gcA                 @   s6  d dl Z d dl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 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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' G dd dej(Z)G dd deZ*G dd dej+eZ,dS )    N)MaxMin)Q)	TruncDate)gettext_lazyactivate)Response)	FilterSet)mixinsserializers)STATUS_VALID)NoneSerializer)ReportUtilGenericViewSetReportGenericFilter)short_date_formatshort_time_formatvalue_formatweekday_format)Transaction)CHOICE_GENDER)get_system_setting)
C_ATT_RULE)r   STATUS_RESIGN_PENDINGc               @   s  e Zd ZejedddZejedddddZejed	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dZ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Zejeddd dd!d"Zejed#dd dd$d"Zejed%d&ddZejd'dd(Zejd)dd(Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z G d6d7 d7Z!d8S )9FirstLastReportSerializerZreport_column_empCodeemp__emp_code)labelsourceZreport_column_firstNameemp__first_name-T)r   r   default
allow_nullZreport_column_lastNameemp__last_nameZreport_column_nickNameemp__nickname)r   r   r    Z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_attendanceDate)r   r    Zreport_column_attendanceWeekdayZreport_column_firstPunchZreport_column_lastPunchZreport_column_totalDurationZreport_column_in_temp      cal_in_temp)r   Zdecimal_placesZ
max_digitsr    r   Zreport_column_out_tempcal_out_tempZreport_column_displayed_maskcal_displayed_maskcal_in_temp_color)r   r    cal_out_temp_colorc             C   s   t t|d |d S )Nemp__gender)dictr   get)selfobj r3   CG:\easytimepro\master/mysite/att\api\views\view_report_firstlast.py
get_gender;   s    z$FirstLastReportSerializer.get_genderc             C   s   | dd }|sdS t|S )Ndate )r0   r   )r1   r2   att_dater3   r3   r4   get_att_date>   s    z&FirstLastReportSerializer.get_att_datec             C   s    | dd }|sdS t| S )Nr6   r7   )r0   r   weekday)r1   r2   r8   r3   r3   r4   get_weekdayD   s    z%FirstLastReportSerializer.get_weekdayc             C   s   | dd }|sdS t|S )Nfirstr7   )r0   r   )r1   r2   first_punchr3   r3   r4   get_first_punchJ   s    z)FirstLastReportSerializer.get_first_punchc             C   s   | dd }|sdS t|S )Nlastr7   )r0   r   )r1   r2   
last_punchr3   r3   r4   get_last_punchP   s    z(FirstLastReportSerializer.get_last_punchc             C   s8   d}|d r4|d r4|d |d  j }t|dddS |S )Nr7   r<   r?   ZdurationiQ g      ?)Zsecondsr   )r1   r2   totalr3   r3   r4   get_total_timeV   s
    z(FirstLastReportSerializer.get_total_timec               @   s   e Zd ZeZdZdS )zFirstLastReportSerializer.Meta)emp_code
first_name	last_name	nick_namegender	dept_code	dept_nameposition_codeposition_namer8   r:   r=   r@   
total_timein_tempout_tempdisplayed_maskin_temp_colorout_temp_colorN)__name__
__module____qualname__r   modelfieldsr3   r3   r3   r4   Meta]   s   rX   N)"rS   rT   rU   r   Z	CharField_rD   rE   rF   rG   ZSerializerMethodFieldrH   rI   rJ   rK   rL   r8   r:   r=   r@   rM   ZDecimalFieldrN   rO   rP   rQ   rR   r5   r9   r;   r>   rA   rC   rX   r3   r3   r3   r4   r      sF   

r   c               @   sZ   e Zd Zejr,ejdddZejdddZnejdddZejdddZG dd dZ	dS )	FirstLastReportFilterr6   Zgte)Z
field_namelookup_exprZlte)namer[   c               @   s   e Zd ZeZdddddgZdS )zFirstLastReportFilter.MetaZ	employeesZdepartmentsZareas
start_dateend_dateN)rS   rT   rU   r   rV   rW   r3   r3   r3   r4   rX   n   s   rX   N)
rS   rT   rU   sixZPY3django_filtersZ
DateFilterr]   r^   rX   r3   r3   r3   r4   rZ   f   s   rZ   c                   s   e Zd ZeZej ZeZ	dgZ
eed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dZdddZ  ZS )FirstLastReportViewSetrD   )listZexport)rM   c       	         s6  | j jj}d|i}| j jj}| j jj}| j jj}tt|  j	f |}| j jj
si }| rh| |d< | r|| |d< | r| |d< |r|j	f |}| }tt|d}|dkr|	ttdtd dB ttdB }|jtdd	}|d
dddddddddd}|jtdtddd}|S )NZemp__company_idZemp__department__inZemp__area__inZemp__position__in
resign_emp0)Zemp__status
punch_time)r6   emp_idr   r   r!   r"   r.   r#   r$   r%   r&   r6   )r<   r?   )requestuserZcurrent_companyZget_auth_deptZget_auth_areaZget_auth_possuperra   get_querysetfilterZis_superuserexistsallZdistinctstrr   r0   r   r   r   Zannotater   valuesr   r   Zorder_by)	r1   
company_idZbase_filterZ	auth_deptZ	auth_areaZauth_posquerysetfiltersrc   )	__class__r3   r4   rj      s>    




 
z#FirstLastReportViewSet.get_querysetc             C   sH  ddl m}m}m} |dkr |S x |D ]}tjj|d |d d}| r|d }|j}|j	}	|j
}
||	|}||	|}||
|}|r|nd|d< ||d< ||d	< tjj|d |d
 d}| r(|d }|j}|j	}|j
}
|||}|||}||
|}|r|nd|d< ||d< |d	 dkr0d|d	< q(|dkr(||d	< q(W |S )z
        for mask and cal in temp, base on the data of first Transaction
        for cal out temp, base on the data of last Transaction
        r   )temperature_updateget_real_colorget_mask_valueNrf   r<   )rf   re   r)   r,   r+   r?   r*   r-   NoYes)mysite.att.utilsrt   ru   rv   r   objectsrk   rl   rp   Ztemperature	mask_flag)r1   rq   rt   ru   rv   dataZtrans_inZtran_inrp   Z
celsius_inr{   rN   Zin_time_colorrP   Z	trans_outZtran_outZcelsius_outrO   Zout_time_colorr3   r3   r4   cal_td_md_data   s@    






z%FirstLastReportViewSet.cal_td_md_datac             O   s\   |  |  }| |}| |}|d k	rD| j|dd}| |jS | j|dd}t|jS )NT)Zmany)filter_querysetrj   Zpaginate_querysetr}   Zget_serializerZget_paginated_responser|   r   )r1   rg   argskwargsrq   ZpageZ
serializerr3   r3   r4   rb      s    

zFirstLastReportViewSet.listc             C   s   |  |  }| |}|S )N)r~   rj   r}   )r1   rq   r3   r3   r4   get_export_queryset   s    
z*FirstLastReportViewSet.get_export_querysetc             C   s   | j | jtS )N)serializer_dictr0   actionr   )r1   r3   r3   r4   get_serializer_class   s    z+FirstLastReportViewSet.get_serializer_classc             C   s   t d tdS )NZenzFirst & Last Report)r   rY   )r1   r3   r3   r4   get_file_title   s    z%FirstLastReportViewSet.get_file_titlec             C   s   | dd |S )Nr   zNo.)insert)r1   headersr3   r3   r4   update_export_headers   s    z,FirstLastReportViewSet.update_export_headersc             C   s:   x4t |D ](\}}|d|d i |jddd q
W |S )NzNo.   F)r?   )	enumerateupdatemove_to_end)r1   datasir|   r3   r3   r4   update_datas_before_export   s    z1FirstLastReportViewSet.update_datas_before_exportc             C   s   |r| d | d |S )NrQ   rR   )append)r1   export_headersgroup_by_fieldr3   r3   r4   update_header_before_export   s    

z2FirstLastReportViewSet.update_header_before_exportc             C   s$   |dkr| dd |d= |d= |S )NrI   r   zNo.)r   )r1   r   r   r3   r3   r4   update_group_export_headers   s
    z2FirstLastReportViewSet.update_group_export_headersNc             C   s  xt |D ]\}}y| jd}W n   d}Y nX |rPd||  d ||< y| jd}W n   d}Y nX |r|| rd|| d  d t||  d ||< y| jd	}W n   d}Y nX |r|| rd|| d
  d t||  d ||< y| jd}	W n   d}	Y nX |	r\||	 r\||	 }
|
dkrJd||	< n|
dkr\d||	< t|trz|d|d  qt|}|dd |||< qW |S )z"only use for att report pdf reportr8   Nz<span class="att_date">z</span>rN   z<span style="color:rQ   z;">rO   rR   rP   rx   z(<span style="color: #00CD66;">Yes</span>rw   z'<span style="color: #ff0000;">No</span>r   r   r7   )r   r   indexrn   
isinstancerb   r   )r1   r   r   
extra_datar   r|   att_date_indexin_temp_indexout_temp_indexdisplayed_mask_indexstatusr3   r3   r4   update_data_list   sF    

(
(



z'FirstLastReportViewSet.update_data_listc             C   s0  x(|D ]}xt |D ]\}}y| jd}W n   d}Y nX |rht|trhd||  d ||< y| jd}| jd}	W n   d }}	Y nX |r|| rd||	  d t||  d ||< y| jd	}
| jd
}W n   d }
}Y nX |
r0||
 r0d||  d t||
  d ||
< y| jd}W n   d}Y nX |r|| r|| }|dkr~d||< n|dkrd||< y|d= |d= W n@ tk
r } z td |dd }|||< W dd}~X Y nX |dkrt|tr|d|d  qt|}|dd |||< qW qW |S )z"only use for att report pdf reportr8   Nz<span class="att_date">z</span>rN   rQ   z<span style="color:z;">rO   rR   rP   rx   z(<span style="color: #00CD66;">Yes</span>rw   z'<span style="color: #ff0000;">No</span>r   zit is sum field nowr   rI   r   r7   )	r   r   r   r   rb   rn   	TypeErrorprintr   )r1   r   r   r   r|   r   vr   r   Zin_temp_color_indexr   Zout_temp_color_indexr   r   er3   r3   r4   update_group_data_list-  s\    
$$




z-FirstLastReportViewSet.update_group_data_list)N)N)rS   rT   rU   r   rV   rz   rj   rq   rZ   Zfilter_classZorderingr   r   Zsummary_fieldsr}   rb   r   r   r   r   r   r   r   r   r   __classcell__r3   r3   )rs   r4   ra   s   s&   
#&	
1ra   )-r_   r`   Zdjango.db.modelsr   r   r   Zdjango.db.models.functionsr   Zdjango.utils.translationr   rY   r   Zrest_framework.responser   Zdjango_filters.rest_frameworkr	   Zrest_frameworkr
   r   Zmysite.admin.modelsr   Zmysite.att.api.serializersr   Zmysite.att.api.utils_classr   r   ry   r   r   r   r   Zmysite.iclock.modelsr   Zmysite.personnel.db_constr   Zmysite.utilsr   Zmysite.att.global_cacher   r   ZModelSerializerr   rZ   ZListModelMixinra   r3   r3   r3   r4   <module>   s*   J