B
    GDb                 @   s   d dl Z d dlmZ d dlm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 G dd	 d	eZG d
d dejZG dd deZG dd dejZdS )    N)serializers)Q)Transaction)PayloadBase)	FilterSet)STATUS_VALIDSTATUS_RESIGN_PENDINGc                   sr   e Zd ZejddZejddZejdddZejdddZ	dd	 Z
d
d Ze fddZG dd dZ  ZS )AttTeamSummaryFilteremployee_filter)methoddepartment_filterZ
punch_timegte)
field_namelookup_exprltec             C   sZ   |rt |dkr|S |d}ydd |D }W n tk
rH   g }Y nX |j|d}|S )Nz-1,c             S   s   g | ]}|rt |qS  )str).0ir   r   QG:\easytimepro\master/mysite/mobile\api_rest\serializers\attendance_serializer.py
<listcomp>   s    z8AttTeamSummaryFilter.employee_filter.<locals>.<listcomp>)Z
emp_id__in)r   split
ValueErrorfilter)selfquerysetnamevalueobjsr   r   r   r
      s    

z$AttTeamSummaryFilter.employee_filterc             C   s   |rt |dkr|S |d}ydd |D }W n tk
rH   g }Y nX |j|d}ddlm} | jjjsx| jjj	}n| jjj
j}|| t ||d}|d	kr|ttd
td d
B ttd
B }|S )Nz-1r   c             S   s   g | ]}|rt |qS r   )int)r   r   r   r   r   r   &   s    z:AttTeamSummaryFilter.department_filter.<locals>.<listcomp>)Zemp__department_id__inr   )
C_ATT_RULE
resign_emp0)Zemp__status)r   r   r   r   Zmysite.att.global_cacher!   requestuserZis_employeeZcurrent_companyZcompanyidZaction_initgetr   r   r   )r   r   r   r   r   r!   Z
company_idr"   r   r   r   r   !   s$    



 z&AttTeamSummaryFilter.department_filterc                sP   t t| j}| jdpd}| jdp*d}|dkrD|dkrD| S |jddS )N	employeesz-1departmentsT)Zemp__isnull)superr	   qsdatar'   ZnoneZexclude)r   r   r(   r)   )	__class__r   r   r+   6   s    zAttTeamSummaryFilter.qsc               @   s   e Zd ZeZddddgZdS )zAttTeamSummaryFilter.Metar(   r)   
start_dateend_dateN)__name__
__module____qualname__r   modelfieldsr   r   r   r   Meta?   s   r5   )r0   r1   r2   django_filtersZ
CharFilterr(   r)   
DateFilterr.   r/   r
   r   propertyr+   r5   __classcell__r   r   )r-   r   r	      s   	r	   c               @   s6   e Zd ZejddZe Zdd ZG dd dZ	dS )AttTeamSummarySerializerzemp.first_name)sourcec             C   s   |j jsdS dj|j jjdS )N z/files/{path})path)Zempphotoformatr   )r   objr   r   r   	get_photoH   s    z"AttTeamSummarySerializer.get_photoc               @   s   e Zd ZeZdZdS )zAttTeamSummarySerializer.Meta)Zemp_code
first_namer>   N)r0   r1   r2   r   r3   r4   r   r   r   r   r5   M   s   r5   N)
r0   r1   r2   r   Z	CharFieldrB   SerializerMethodFieldr>   rA   r5   r   r   r   r   r:   D   s   r:   c               @   s6   e Zd ZejdddZejdddZG dd dZdS )AttSummaryFilteratt_dater   )r   r   r   c               @   s   e Zd ZeZddgZdS )zAttSummaryFilter.Metar.   r/   N)r0   r1   r2   r   r3   r4   r   r   r   r   r5   V   s   r5   N)r0   r1   r2   r6   r7   r.   r/   r5   r   r   r   r   rD   R   s   rD   c                   sn   e Zd Ze Ze Ze Ze ZdZ	 fddZ
dd Zdd Zdd	 Zd
d ZG dd dZ  ZS )AttSummarySerializerNc                s,   t t| j|| | jd jdd | _d S )Nr$   ZHTTP_TIMEZONE)r*   rF   __init__contextZMETAr'   tz)r   argskwargs)r-   r   r   rG   b   s    zAttSummarySerializer.__init__c             C   s:   |j s
dS | jr&tjj|j | jd}ntj|j }|jS )Nr   )tzinfo)rE   rI   arrowArrowZfromdate	timestamp)r   r@   rE   r   r   r   get_att_datef   s    z!AttSummarySerializer.get_att_datec             C   s:   |j s
dS | jr&tjj|j | jd}ntj|j }|jS )Nr   )rL   )clock_inrI   rM   rN   fromdatetimerO   )r   r@   rQ   r   r   r   get_clock_ino   s    z!AttSummarySerializer.get_clock_inc             C   s:   |j s
dS | jr&tjj|j | jd}ntj|j }|jS )Nr   )rL   )	clock_outrI   rM   rN   rR   rO   )r   r@   rT   r   r   r   get_clock_outx   s    z"AttSummarySerializer.get_clock_outc             C   sH   t |jd }tt |d }tt |d }dj|d|ddS )N<   z{hrs}:{minutes}   )hrsminutes)r    Zdurationr   r?   zfill)r   r@   Ztotal_minutesrX   rY   r   r   r   get_duration_formatted   s    z+AttSummarySerializer.get_duration_formattedc               @   s   e Zd ZeZdZdS )zAttSummarySerializer.Meta)rE   ZweekdayrQ   rT   duration_formattedN)r0   r1   r2   r   r3   r4   r   r   r   r   r5      s   r5   )r0   r1   r2   r   rC   rE   rQ   rT   r\   rI   rG   rP   rS   rU   r[   r5   r9   r   r   )r-   r   rF   [   s   			rF   )rM   Zrest_frameworkr   Zdjango.db.modelsr   r6   Zmysite.iclock.modelsr   Zmysite.att.modelsr   Zdjango_filters.rest_frameworkr   Zmysite.admin.modelsr   r   r	   ZModelSerializerr:   rD   rF   r   r   r   r   <module>   s   4	