U
    Rd$                     @   s*   d dl T d dlmZ G dd dejZdS )    )*)cmf_task_reportc                       sL   e Zd Zedeed fddZdd Zdd	d
Zde	dddZ
  ZS )CmfTaskReportNF)all_optionsmodels_filtersc           
         sN   |sg }|dkr2|o| dd }	|	r2|dd|	gg}t j|f|||d|S )Nreport_filter	parent_idz==)filtermodels_listobject_fields)getsuperfield_options_list)
clsZrelation_field_nameZ	object_idr   r
   r   r   r	   kwargsparent	__class__ "./common/models/cmf_task_report.pyr      s    z CmfTaskReport.field_options_listc                 C   s   |    d S N)Z_check_simple_perm)selfr   r   r   check_simple_perm   s    zCmfTaskReport.check_simple_perm   Уведомлениеc                 O   s   d S r   r   )r   textZnotify_nameargsr   r   r   r   all_place_notify   s    zCmfTaskReport.all_place_notify)returnc           4         s  ddl m}m}m}m}m}m}	 ddlm}
m	}m} ddl
m}m} dd l}ddddd	d
ddddddddddd	g}| dddddg | jj}| jj}| j}tj  dddd|gdd|gggf}t|jr|d d d d!|g d"t| jjj|g}tjj||d#}d"dd!|gdd|gdd|gd$d%d gg}t|jrL|d d!|g tjj|dd&d dd$gd#}t||rz| }t||r| }|st d' d S | j! d(| j" d)}t#j$ d*}d+| }|j%&|s|'| | }|(d,)d-dd. *d|d/d0 )d-d1d2 *d1|d3d0 )d-d4d5 *d4|d6d0 )d-d7d8 *d7|d9d0 )d-d:d; *d:|d<d0 )d-d=d> *d=|d?d0 d@}||||dAD ]4})d-||+dB *||d<d0 |d-7 }qt,dCdD |D }tj-jdEd!|gdFdGgd#}d} t. }!d}"|D ]j/j0jgdHdD j1jD  }# j0j|#k}$t2fdIdJ|}%dKdD |%D |$rtt2fdLdJ|}&ntt2 fdMdJ|}&t|&dkrАq8|"}')|'dj! )|'d:j" |"d-7 }"i }(|&D ]})|)j3|(|)j3j0j< q|(D ]
}*|"}+|(|* tt2fdNdJ|&},dOdD |,D tt2fdPdJ|}-)|+d1j! )|+dj! |"d-7 }"dQdR }.t4|-|.dSD ]~)|"d1j! )|"dj! j5jrj5jj6dTkr)|"d4j5j!j j6dTkr)|"d4j!j n)|"d7j! )|"d:j" tt2fdUdJ|,}/|/D ]})|)j7j }0|0| j8d@ }1fdVdW}2|2|"|1|)j9j |2|'|1|)j9j |2|+|1|)j9j |2|"d=|)j9j |2|'d=|)j9j |2|+d=|)j9j | |)j9jdX 7 } |0|!krd|!|0< |!|0  |)j9jdX 7  < qV|"d-7 }"qq q8d@})|"d-dY )|"d=|  ||||dAD ]8}*||dZd0 )|"||!:|  |d-7 }q`| d[| }3|;|3 |S )\Nr   )WorkbookColorStyleFontFillFormat)date	timedeltadatetime)rruleDAILYcodenameZcache_status_typepriorityZcmf_created_atZcmf_modified_atZ
alarm_dater   zparent.nameresponsiblezresponsible.nameZepicz	epic.namelogic_prefixzparent_task.namezparent_task.logic_prefix
date_startdate_endzreport_filter.bqlZreport_typeft_responsibleZtimetracker_historyZEXISTS
start_datez>=z<=   
cmf_authorINZAND)r	   fields
time_spentz!=zparent.parentu)   Нет задач для экспорта-z.xlsxz
/files/tmpz/files/tmp/u   Задачи   u   Проект   )size   u   Пользователь(      u   Эпик      u   Задача2      u   Код      u   Часы работы      )ZdtstartZuntilz%Y-%m-%dc                 S   s   g | ]}|j jqS r   r   value.0taskr   r   r   
<listcomp>u   s     z-CmfTaskReport.export_time.<locals>.<listcomp>id	cmf_ownercmf_owner_assistantsc                 S   s   g | ]}|j jqS r   rL   rG   )rI   Z	assistantr   r   r   rK   |   s     c                    s   | j j jjkS r   r   rG   rL   rJ   )projectr   r   <lambda>       z+CmfTaskReport.export_time.<locals>.<lambda>c                 S   s   g | ]}|j jqS r   rO   rH   r   r   r   rK      s     c                    s   | j j kS r   rF   record)project_tasks_idsr   r   rS      rT   c                    s   | j jko| jj jjkS r   )r   rG   Zcmf_author_idrL   rU   )current_personrW   r   r   rS      s    c                    s   | j jj jjkS r   )r3   rL   rG   rU   )userr   r   rS      rT   c                 S   s   g | ]}|j jqS r   rF   )rI   rV   r   r   r   rK      s     c                    s   | j j kS r   rO   rQ   )user_tasks_idsr   r   rS      rT   c                 S   s,   | j jr&| j jjdkr&| j jj| j S | jS )N	task.epic)parent_taskrG   r-   r*   rQ   r   r   r   
tasks_sort   s    z-CmfTaskReport.export_time.<locals>.tasks_sort)keyr[   c                    s   | j j jjkS r   rP   rU   rQ   r   r   rS      rT   c                    s8     | |}|s|d }n||d 7 } | || d S )N<   )Zget_cell_valueset_cell_value)xyZ	new_valuerG   )wsr   r   update_cell   s
    
z.CmfTaskReport.export_time.<locals>.update_cellr_   u   Всего:
   /)<Zpyexcelerater   r   r    r!   r"   r#   r&   r$   r%   Zdateutil.rruler'   r(   osZload_fieldsr.   rG   r/   r0   ZmodelsZ	CmfPersonZget_current_userlenappendZjsonloadsr   bqlZCmfTasklistZCmfTimeTrackerHistory
isinstanceZ	cmf_alertr*   r)   ZconfigZPROJECT_DIRpathexistsmkdirZ	new_sheetr`   Zset_col_stylestrftimesetZ
CmfProjectdictrM   rL   rN   r	   r3   sortedr\   r-   r1   Zdaysr6   r   Zsave)4r   rk   r5   Zformat_filer   r   r    r!   r"   r#   r$   r%   r&   r'   r(   rg   r1   Zend_dater,   Zhistory_restrictZtasks_filterZtasksZrecords_filterZrecords	file_nameZpath_to_tmprn   wbcoldZproject_idsZprojectsZitogs_total_time_spentZitogsZnumZ
owners_idsZis_ownerZproject_tasksZproject_recordsZproject_lineZproject_usersrV   r^   Z	user_lineZuser_recordsZ
user_tasksr]   Ztask_recordsZ	hist_dateZcol_numrd   Zpath_to_filer   )rX   rR   rW   rJ   rY   rZ   rc   r   export_time   s     








zCmfTaskReport.export_time)NNNFNN)r   )NNN)__name__
__module____qualname__classmethodboolrs   r   r   r   strry   __classcell__r   r   r   r   r      s              
r   N)Zcmf.includeZcommon.fieldsr   r   r   r   r   r   <module>   s   