B
    :bJ"                 @   sJ  d dl Z d dl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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 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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)Count	Aggregate	TextField)Q)	TruncDate)gettext_lazyactivate)	FilterSet)mixinsserializers)STATUS_VALID)NoneSerializer)ReportUtilGenericViewSetReportGenericFilter)short_date_format)Transaction)CHOICE_GENDER)status)Response)api_settings)
C_ATT_RULE)r   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                sJ   |   d }d| _dj|j|jjd| _tt| j	||f|\}}||fS )zZ
        SQL Server 2017 Support STRING_AGG
        STRING_AGG(column, separator)
        r   ZSTUFFa\  %(function)s((
            SELECT ', ' + RIGHT(CONVERT(NVARCHAR(20), a.[{column}], 120), 8)
            FROM [{table}] as a
            WHERE a.[emp_id]  = [{table}].[emp_id] and CONVERT(char(10), a.[{column}], 101) =
            CONVERT(datetime, CONVERT(char(10), [{table}].[{column}], 101), 101)
            FOR xml path('')
        ), 1, 1, ''))tablecolumn)
Zget_source_expressionsfunctionformataliasZfieldZattnametemplatesuperr   as_sql)selfcompiler
connectionextra_contextZ
expressionsqlparams)	__class__ BG:\easytimepro\master/mysite/att\api\views\view_report_timecard.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listaggzu%(function)s(substr(to_char(%(expressions)s,'YYYY-MM-DD hh24:mi:ss'), 12, 8), ',')  within group(order by PUNCH_TIME)Z	WM_CONCATzM%(function)s(substr(to_char(%(expressions)s,'YYYY-MM-DD hh24:mi:ss'), 12, 8)))mysiter*   Zp_query_oner   r   r   r   r    )	r!   r"   r#   r$   r*   Zrowsversionr%   r&   )r'   r(   r)   	as_oracle0   s    
zGroupContact.as_oraclec                s.   d| _ d| _tt| j||f|\}}||fS )NZGROUP_CONCATz3%(function)s(SUBSTRING(%(expressions)s, 12, 8), ''))r   r   r   r   r    )r!   r"   r#   r$   r%   r&   )r'   r(   r)   as_mysql=   s    zGroupContact.as_mysqlc                s.   d| _ d| _tt| j||f|\}}||fS )NZARRAY_TO_STRINGzC%(function)s(ARRAY_AGG(to_char(%(expressions)s,'hh24:mi:ss')), ','))r   r   r   r   r    )r!   r"   r#   r$   r%   r&   )r'   r(   r)   as_postgresqlD   s    zGroupContact.as_postgresql)__name__
__module____qualname__r    r.   r/   r0   __classcell__r(   r(   )r'   r)   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j
eddZejeddddZej
eddddZdd Zdd  Zd!d" ZG d#d$ d$Zd%S )&TimeCardReportSerializerZreport_column_empCodeemp__emp_code)labelsourceZreport_column_firstNameemp__first_nameT)r7   r8   Z
allow_nullZreport_column_lastNameemp__last_nameZreport_column_nickNameemp__nicknameZreport_column_gender)r7   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_attendanceDateZreport_column_punchTimestimesr   )r7   r8   defaultZreport_column_punchTimetime_setc             C   s   t t|d |d S )Nemp__gender)dictr   get)r!   objr(   r(   r)   
get_gender]   s    z#TimeCardReportSerializer.get_genderc             C   s   | dd }|sdS t|S )N
punch_dater+   )rE   r   )r!   rF   att_dater(   r(   r)   get_att_date`   s    z%TimeCardReportSerializer.get_att_datec                sF   ddl m   ddlm |d d}|   fdd|D }|S )Nr   )datetime)short_time_formatrB   ,c                s    g | ]}  | d qS )z%H:%M:%S)strptimestrip).0Ztime_obj)rK   rL   r(   r)   
<listcomp>k   s    z:TimeCardReportSerializer.get_punch_set.<locals>.<listcomp>)rK   mysite.att.utilsrL   splitsort)r!   rF   Ztime_set_listr(   )rK   rL   r)   get_punch_setf   s    z&TimeCardReportSerializer.get_punch_setc               @   s   e Zd ZeZdZdS )zTimeCardReportSerializer.Meta)emp_code
first_name	last_name	nick_namegender	dept_code	dept_nameposition_codeposition_namerI   punch_times	punch_setN)r1   r2   r3   r   modelfieldsr(   r(   r(   r)   Metan   s   rc   N)r1   r2   r3   r   Z	CharField_rV   rW   rX   rY   ZSerializerMethodFieldrZ   r[   r\   r]   r^   rI   ZIntegerFieldr_   r`   rG   rJ   rU   rc   r(   r(   r(   r)   r5   K   s(   r5   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 )	TimeCardReportFilterrH   Zgte)Z
field_namelookup_exprZlte)namerf   c               @   s   e Zd ZeZdddddgZdS )zTimeCardReportFilter.MetaZ	employeesZdepartmentsZareas
start_dateend_dateN)r1   r2   r3   r   ra   rb   r(   r(   r(   r)   rc   ~   s   rc   N)
r1   r2   r3   sixZPY3django_filtersZ
DateFilterrh   ri   rc   r(   r(   r(   r)   re   v   s   re   c                   sR   e Zd ZeZej  dZ	e
ZeedZ fddZdd Zdd Z  ZS )	TimeCardReportViewSetz-punch_time)listZexportc                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 }|jtdd	}|d
dddddddddddd}|jtdtdt dd}|S )NZemp__company_id)Zemp__department__in)Zemp__area__in)Zemp__position__in
resign_emp0)Zemp__statusZ
punch_time)rH   Zemp_idr6   r9   r:   r;   r<   r=   r>   r?   rH   rC   )Zoutput_field)r@   rB   )r   rl   get_querysetZrequestuserZis_employeeZcurrent_companyfilterZis_superuserZget_auth_deptZget_auth_areaZget_auth_posexistsallZdistinctstrr   rE   r   r   r   Zannotater   valuesorder_byr   r   r   )r!   querysetZ
company_idZbase_filterZ	auth_deptZ	auth_areaZauth_posrn   )r'   r(   r)   rp      s>    





 
z"TimeCardReportViewSet.get_querysetc             C   s   | j | jtS )N)serializer_dictrE   actionr   )r!   r(   r(   r)   get_serializer_class   s    z*TimeCardReportViewSet.get_serializer_classc             C   s   t d tdS )NZenzTotal Punches Report)r   rd   )r!   r(   r(   r)   get_file_title   s    z$TimeCardReportViewSet.get_file_title)r1   r2   r3   r   ra   Zobjectsrp   Zselect_relatedrw   rx   re   Zfilter_classr5   ry   r{   r|   r4   r(   r(   )r'   r)   rl      s   #rl   )-rj   rk   Zdjango.db.modelsr   r   r   r   Zdjango.db.models.functionsr   Zdjango.utils.translationr   rd   r   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   rR   r   Zmysite.iclock.modelsr   Zmysite.personnel.db_constr   r   Zrest_framework.responser   Zrest_framework.settingsr   Zmysite.att.global_cacher   r   r   ZModelSerializerr5   re   ZListModelMixinrl   r(   r(   r(   r)   <module>   s.   .+