B
    gc]                 @   s\  d dl Z d dlZd dlZd dlZd dlZd dl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 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" 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-m.Z. d dl/m0Z0 d dl1m2Z2 d\Z3Z4Z5dd Z6dd Z7dd Z8eddddd  Z9d!d" Z:ed#d$dd%d& Z;d'd( Z<d)d* Z=ej>d+d,d-d.d/ Z?ej>d+d0d-d1d2 Z@edd3dd4d5 ZAej>d+d6d-d7d8 ZBej>d+d9d-d:d; ZCd<d= ZDd>d? ZEej>d+d@d-dAdB ZFej>d+dCdDdE ZGeddFddGdH ZHdS )I    N)HTTPBasicAuth)tempDir)settings)Q)DatabaseError)periodic_task)crontab)gettext_lazy)cache)
celery_app)PayloadBase)tasks)get_system_setting)Employee)att_calculate)SystemSetting)tempFile	truncTimetrunc)Transaction)send_one_mail_with_attachments)         c             C   s   t tjjd|djddd}xHttt|d d D ],}||d |d d  }t|| ||d q8W t	dt
j
 d	 d
t
j
 ddf  d S )Nr   )Z
enable_att
company_ididT)flati  )r   z
job_%s.txtz%Y%mz%s %sz%Y%m%d%H%M%Sz$exception email calculation finished)listr   objectsfiltervalues_listrangeintlenr   r   datetimenowstrftime)
start_dateend_dater   empsiZeids r+   )G:\easytimepro\master/mysite/att\tasks.pyprocess_calculation&   s    r-   c          	   C   s.  t  | j| j| j}t  |j|j|jddd}t||| tjj||fdjtddtddB tddB |d	d	}i }xJ|D ]@}	|
|	jj}
|
r|
d
  |	jrdnd7  < |
d  |	jrdnd7  < |
d  |	jrdnd7  < qi }
|	jj|
d< |	jj|
d< |	jj|
d< |	jj|
d< |	jj|
d< |	j|
d< |	jr@dnd|
d
< |	jrTdnd|
d< |	jrhdnd|
d< |	jjr|	jjr|	jjnd|
d< |	jjr|	jjr|	jjnd|
d< |
||	jj< qW d}i }x|D ]}d}x*|D ]"}|| | || krd}P qW |rt|| d || d
 || d || d  |
|| d jj}
|
rr|
d ||  n8i }
|| d j|
d< || g|
d< |
||| d jj< || 
dd rtd|| d  tj|| d || |||| || 
dd r6|| d jdkr6tj || d || ||| || 
drp|d rpt!j"|| d || | || || 
dr|d rt!j#|| d || | || qW xx|D ]p}td|| d j$ || d }|j%& j|d}dd  |D }x(|D ] }t'||| d ||| q W qW d S )!N   ;   )Zatt_date__ranger   )Zlate__gt)Zearly_leave__gt)Z
absent__gt)Zemp__company_idemp
late_timesr   early_leave_timesabsent_timesemp_code
first_nameemailmobileZappZemp_objFZsmsZwhatsapp)r1   r2   r3   Tr)   deptzsend a email to 	sms_alertwhatsapp_alert)r   c             S   s   g | ]
}|j qS r+   )user).0mr+   r+   r,   
<listcomp>|   s    z"process_notify.<locals>.<listcomp>)(r$   yearmonthdayr-   r   r   r   r   select_relatedgetr0   r   lateearly_leaveabsentr4   r5   r6   r7   Z
app_status
enable_smsZsms_exceptionenable_whatsappZwhatsapp_exceptionprintZ
departmentappend
base_tasksZdelivery_exception_emaildelaymobile_tasksZdelivery_attendance_exceptionpersonnel_taskZdelivery_exception_smsZdelivery_exception_whatsappZ	dept_nameZmembership_setallZ+delivery_exception_email_to_depament_manage)r'   r(   paramsr   	frequencyZ_start_dateZ	_end_dateZdatasZexception_empdataZdetailkeysZexception_deptkZhitkeyr8   Zall_membershipZmanagersZmanagerr+   r+   r,   process_notify1   s    $
$$


&



rV   c              C   sX   t j dd} t j dd}td}t|ddt|ddt|ddd	}d S )
Nz
2019-11-01z%Y-%m-%dz
2019-11-15alert_settinglate_exceedr   early_leave_exceedabsent_exceed)r1   r2   r3   )r$   strptimer   r"   rC   )r'   r(   rW   rP   r+   r+   r,   test_att_exception_alert   s    r\   <   zatt.tasks.check_alert_setting)Z	run_everynamec              C   s@   dt jkrd S tjjdd} x| D ]}t|j}t| q"W d S )NattrW   )r^   )r   SALE_MODULEr   r   r   strr   att_exception_alert)alert_queryalertr   r+   r+   r,   check_alert_setting   s    


re   c          	   C   s  t d| }|sdS t|dd}|s*dS |tttfkr<dS |dd}|sPdS tj }|d|dd krtdS t|dd	}t|d
d}t|ddt|ddt|ddt	|ddt	|ddd}|tj
|d }d}	|tkr|}
t|
||| | d}	n|tkrZ| }||kr|tj
dd }
t|
||| | d}	nv|tkr|j}||kr|tj
d	d }|j|jkrt|j|jd}
nt|j|jd}
|}t|
||| | d}	|	rtjjd| d }|r|d|d< t||_|  dS )z
    Sending attendance exception alert email after attendance calculation,
    It is related with alert setting(System -> Configuration -> Alert Setting)
    :return:
    rW   NZemail_frequencyr   
email_timez%H:%M   Zsending_day	email_dayr   rX   rY   rZ   r9   Fr:   )r1   r2   r3   r9   r:   )daysTi)r^   r   z%Y-%m-%d %H:%MZlast_alert_time)r   r"   rC   DAILYWEEKLYMONTHLYr$   r%   r&   bool	timedeltarV   weekdayrA   r@   r?   r   r   r   firstjsondumpsvaluesave)r   rW   rQ   processing_timer%   day_intervalri   rP   r(   result_flagr'   week_dayrA   Z	last_dateobjr+   r+   r,   rb      sb    






rb   
   z$att.tasks.circle_check_att_data_filec              C   s6   t jjdd} x"| D ]}t|j}t|j| qW d S )NrW   )r^   )r   r   r   ra   r   check_att_data_filert   )rc   rd   r   r+   r+   r,   circle_check_att_data_file   s    

r}   c          
   C   s`  dd l }dd l}ddlm} | s$d S || } | dd}| dd}| dd}| dd}d }	}
|rv|d	|d
}	|r|d|d
}
|	s|
s|s|r\|jtj	d}|j
|sy|| W n tk
r   Y nX ||}|sd S xp|D ]h}|j||}|r|j|rt|d}| }W d Q R X || |r||}t||	|
|| qW d S )Nr   )r   r9   Fr:   temperature_alertmask_status sms_setting)r   whatsapp_settingz	att_data/r)osrr   mysite.utilsr   loadsrC   pathjoinr   ZADDITION_FILE_ROOTexistsmakedirsFileExistsErrorlistdirisfileopenreadremovesend_push_message)rW   r   r   rr   r   r9   r:   r~   r   r   r   Zatt_data_path	file_listfiler   ZrfZrdataatt_datar+   r+   r,   r|      sF    




r|   c          
   C   s  ddl m} ddlm} ddlm} tjjdd}|jj| 	dd}	|	r|	d }	|rr|	j
rr|	jrrt|	||  |r|	jr|	jrt|	||  |s|rdd	lm}
 dd
lm} |
jj|	jd}tjj| 	d|	jdd}g }x$|D ]}|jr|jr||j qW |rtd|	jd}x|D ]}|	j}| 	d}||j|}|jdkrx|rxd}d}|||	j|||	j|| nt |t!r$d|kr$d|kr$|d r$|d r$t ||r$t"|d |  krt"|d kr$n n(|r$d}d}|||	j|||	j||| q$W d S )Nr   )r   )-delivery_email_alert_for_temp_and_mask_status)DecimalZtemp_mask_setting)r^   Zemp_id)r   )MyUser)temperature_update)Zcurrent_company
punch_time)r   r   r0   )r   zNo mask alertZ
mask_alertZhigh_temp_minZhigh_temp_maxzAbnormal temperature alertZabnormal_temperature_alert)#Z&mysite.personnel.models.model_employeer   Zmysite.base.tasksr   Zdecimalr   r   r   r   rC   rG   Z	sms_punchsms_push_sendrL   rH   Zwhatsapp_punchwhatsapp_push_sendZmysite.accounts.modelsr   Zmysite.att.utilsr   r   r   rB   Zis_superuserr6   rJ   r   ZtemperatureZ	mask_flagr4   r5   
isinstancedictfloat)r   r   r   r~   r   r   r   r   Ztemperature_settingr0   r   r   Zget_admin_usersZtransaction_dataZ
email_listZeachrR   r   r   ZtempZsubjectZ
alert_namer+   r+   r,   r     sV    



$r   Tzatt.tasks.sms_push_send)bindr^   c             C   sh   ddl m} ddl m} |s d S ||jd|dd}|dd}|||d	 |d
 |j||d d d S )Nr   )format_punch_text)send_smspunchr   r   user_idr   Z
sms_apikeyZprovidersender)current_userapi_keyZsms_providernumbertextr   )(mysite.personnel.send_tripartite_messager   r   r5   rC   r7   )taskr0   r   r   r   r   
punch_textr   r+   r+   r,   r   :  s    r   zatt.tasks.whatsapp_push_sendc             C   sX   ddl m} ddl m} ||jd|dd}|rT|dd}|||d	 |j|d
 d S )Nr   )r   )sent_whatsappr   r   r   r   r   Zwhatsapp_apikey)r   r   r   r   )r   r   r   r5   rC   r7   )r   r0   r   r   r   r   r   r   r+   r+   r,   r   F  s    
r   zatt.tasks.auto_calculationc              C   s  ddl m } m} ddlm} ddlm} ddlm} ddlm	} ddl
m} |  }t||dd	 }t|}	| d
 |krd S |jjddd}
xz|
D ]r}td|}|sq|dd}|sq|dd}|d|gkr|jj|djddd}|sq||||	d |d qW d S )Nr   )r$   ro   )r   )Company)r   )EASYTIMEPRO)get_software_type_without_loadr   )rj   Zsoftware_typer   T)r   rW   autocalculation_alertFautocalculation_alert_timez23:00z%H:%M)r   )requestr   )r$   ro   mysite.att.calc.viewsr   Zmysite.cloud.modelsr   mysite.personnel.modelsr   Zmysite.admin.constr   Zmysite.admin.utilsr   r%   r   r   r   r    r   rC   r&   r   )r$   ro   r   r   r   r   r   ntd1d2Z	companiesr   rW   Zauto_cal_settingsr   r)   r+   r+   r,   auto_calculationR  s2    

   r   z+att.tasks.auto_calculation_approved_recordsc       	      C   s@   ddl m} td|}|r<|dd}|r<|||||| n d S )Nr   )r   rW   r   F)r   r   r   rC   )	r   r)   r   r   r   r   r   rW   rR   r+   r+   r,   !auto_calculation_approved_recordsq  s    
r   z"att.tasks.build_workflow_for_leavec          	   K   s  | dd }|rddlm} ddlm} ddlm}m}m}	 ddl	m
}
 ddlm}m} |j}|j|j}|rtd dd	lm} | |j||j |
j|d
|j||j|jd n$|
j|d
|j||j|jd t|dr|j}|j}|r|r||kr:||||j||j |
j|d
|j||j|j|d nl||krv|	|||j||j |
j|d
|||j|j|d n0||||j||j |
j|d
|||j|j|d |j||gkr|j}|j}tj|jg||d |j d dS d S )Ninstancer   )receiver)ContentType)approve_by_adminreject_by_adminrevoke_by_admin)prepare_mobile_notifications)AUDIT_SUCCESSCANCEL_AUDIT_SUCCESSg?)WorkflowInstanceBuilderZleave)	applicantapproval_status_approve_user)r   r   Znode_approver)r   r   T)!rC   Zdjango.dispatch.dispatcherr   Z"django.contrib.contenttypes.modelsr   Zmysite.workflow.viewsr   r   r   Zmysite.mobile.tasksr   Zmysite.att.models_choicesr   r   Z
apply_timer   Zget_for_modelr   timesleepZ'mysite.workflow.models.workflow_builderr   Zbuild_workflow_instance_forZemployeerL   Zaudit_statushasattrr   Z
start_timeZend_timer   Zemployee_idr   )signalr   Zcreatedkwargsr   r   r   r   r   r   r   r   r   Zcommit_timeZctr   Zapprover_auth_userZapprove_audit_statusr   r   r+   r+   r,   build_workflow_for_leave}  sX    





r   c          	   C   s\  ddl m} tj d}	tj d}
td|	 d|
 d|  tj| | t	| d }t
|}|d	}|ddd
 ddi}| d}t }|| |jd }|||d}|j|||d |j||d}td|	 d|
 d|j  |jdkrNt|d}||j |  x$|D ]}t| | |g|g|d q.W W d Q R X d S )Nr   )r   z%Y%mz%Y%m%d%H%M%SZjob_z.txtz task running with url=z.xlsz
Sheet NameZSAMPLEZAcceptzapplication/jsonz/login/	csrftoken)usernamepasswordZcsrfmiddlewaretoken)rR   headers)r   z url status=   wb)Zattachmentsr   )r   r   r$   r%   r&   r   r   r   r   ra   
xlsxwriterZWorkbookZadd_worksheetwriterequestsZSessionrC   ZcookiesZpostZstatus_coder   Zcontentcloser   )reportdateurlmailsr   r   r   	basic_urlr   Zcurrent_year_monthZcurrent_datetime	file_pathZworkbookZsheetZheaders_dataZ	login_urlZclientr   Z
login_dataZresponse_dataZfile_objZmail_idsr+   r+   r,   %creating_and_sending_reports_for_mail  s4    







r   c             C   sl   ddl m} d}t j | d} t j |d}||  }x2t|jd D ] }|| ||d d d }qDW |S )	Nr   )ro   r   z%Y-%m-%dr   )rj   z%m/%d,)r$   ro   r[   r!   rj   r&   )r'   r(   ro   column_datesZdeltar*   r+   r+   r,   get_columns_date  s     r   ZAutoCalculateAndSendReportsc             C   sL  ddl m} tjj|djddd}tj|d}tj|d}||||d |d ttj|	dd
 }tt|	d	t|	d
d
 }tt|	d	t|	d
ttt|	d	t|	d
d 
 }dj|	|
|d}xV|D ]L}|dkr$d}|d| d| d }n
|dkrLd}|d| d| d }n|dkrtd}|d| d| d }n|dkrd}|d| d| d }n|dkrd}|d| d| d }nj|dkrd }|d!| d| d }nB|d"krd#}|d$| d| d }n|d%kr<d&}|d'| d| d }n|d(krdd)}|d*| d| d }n|d+krd,}|d-| d| d }n|d.krd/}|d0| d| d }nz|d1krd2}|d3| d| d }nR|d4krd5}|d6| d| d }n*|d7kr,d8}|d9| d| d: }n|d;krRd<}|d=| d| d> }n|d?krd@}t||}|dA| dB| d| d }n|dCkrdD}tt|t|}|dE| dF| d| d }nh|dGkrdH}|dI| d| d }nB|dJkr*dK}tt|t|}|dL| dM| d| d }nd S t|||||||| qW d S )NNr   )r   )r   r   T)r   z%Y-%m-%d)r   r   z%Yz%mr   z{scheme}//{ip}:{port})schemeZipportZtransactionzTransaction Reportz/att/api/transactionReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,punch_time,punch_state,work_code,source,displayed_temp&start_date=z
&end_date=zC&page_size=999999&export_type=xls&page=1&export_style=&limit=999999Zmobile_transactionzMobile Transaction Reportz/att/api/mobiletransactionReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,punch_time,punch_state,work_code,source,gps_location&start_date=Ztotal_puncheszTotal Punches Reportz/att/api/timeCardReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,punch_times,punch_set&start_date=Z
first_lastzFirst&Last Reportz/att/api/firstLastReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,weekday,first_punch,last_punch,total_time,in_temp,out_temp&start_date=Zschedule_logzScheduled Log Reportz/att/api/scheduledLogReport/export/?export_headers=emp_code,first_name,dept_name,att_date,weekday,punch_time,punch_state,correct_state&start_date=Ztotal_timecardzTotal Time Card Reporta  /att/api/totalTimeCardReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,weekday,att_exception,timetable,duration,check_in,check_out,duty_duration,work_day,clock_in,clock_out,total_time,duty_wt,actual_wt,unscheduled,remaining,late,break_late,early_leave,break_early,absent,break_absent,leave,total_worked,normal_wt,break_duration,normal_ot,weekend_ot,holiday_ot,ot_lv1,ot_lv2,ot_lv3,attendance_status&start_date=Zmissed_in_outzMissed In & Out Punch Reportz/att/api/exceptionReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,timetable,att_date,description&start_date=rD   zLate Reportz/att/api/lateReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,weekday,timetable,check_in,check_out,clock_in,clock_out,total_time,late&start_date=rE   zEarly Leave Reportz/att/api/earlyLeaveReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,att_date,weekday,timetable,check_in,check_out,clock_in,clock_out,total_time,early_leave&start_date=ZbirthdayzBirthday Reportz/att/api/empBirthdayReport/export/?export_headers=emp_code,first_name,last_name,nick_name,birthday,dept_code,dept_name&start_date=rF   zAbsent Reportz/att/api/absentReport/export/?export_headers=emp_code,first_name,dept_name,att_date,weekday,timetable,check_in,check_out,clock_in,clock_out,total_time,late,early_leave,absent,status,Remarks&start_date=Zhalf_dayzHalf Day Reportz/att/api/halfDayReport/export/?export_headers=emp_code,first_name,dept_name,att_date,weekday,timetable,check_in,check_out,duty_duration,clock_in,clock_out,total_time,half_day&start_date=Zdaily_attendancezDaily Attendance Reportz/att/api/dailyAttendanceReport/export/?export_headers=emp_code,first_name,dept_name,att_date,timetable,duration,clock_in,clock_out,actual_wt,total_ot,normal_ot,weekend_ot,holiday_ot,total_worked,attendance_status,Remarks&start_date=Zdaily_detailszDaily Details Reportz/att/api/dailyDetailsReport/export/?export_headers=emp_code,first_name,dept_name,att_date,timetable,duration,check_in,check_out,clock_in_old,clock_out_old,actual_wt,total_ot,late,early_leave,total_worked,attendance_status,punch_set&start_date=zD&page_size=999999&export_type=xls&page=1&export_style=2&limit=999999Zdaily_summaryzDaily Summary Reportz/att/api/dailySummaryReport/export/?export_headers=emp_code,first_name,dept_name,att_date,timetable,clock_in,clock_out,total_worked,attendance_status,Remarks&start_date=zh&departments=-1&areas=-1&positions=-1&page_size=999999&export_type=xls&page=1&export_style=&limit=999999Zdaily_statuszDaily Status Reportz/att/api/dailyStatusReport/export/?export_headers=emp_code,first_name,last_name,nick_name,gender,dept_code,dept_name,position_code,position_name,z,total_late,total_early_leave,total_absent,total_worked,total_not,total_wot,total_hot,total_leave,leave_1,leave_2,leave_3,leave_4,leave_5,leave_6&start_date=Zbasic_statuszBasic Status ReportzW/att/api/monthlyBasicStatusReport/export/?export_headers=emp_code,first_name,dept_name,z,total_present_times,total_absent_times,total_leave_times,total_holiday_times,total_holiday_present_times,total_week_off_times,total_week_off_present_times&start_date=Zstatus_summaryzStatus Summary Reporta  /att/api/monthlyStatusSummaryReport/export/?export_headers=emp_code,first_name,dept_name,present,total_absent,total_holiday_times,total_holiday_present,total_week_off,total_week_off_present,leave_1,leave_2,leave_3,leave_4,leave_5,leave_6,total_leave,total_present&start_date=Zwork_detailedzWork Detailed Reportze/att/api/monthlyDetailedSummaryReport/export/?export_headers=emp_code,first_name,dept_name,data_type,z&start_date=)r   r   r   r   r   r    r$   r[   ra   r&   r   r"   calendarZ
monthrangeformatr   r   )r   reports_listr%   r'   r(   r   r   r   r   r   
ip_addressr   r   Zemp_idsZcalc_start_dateZcalc_end_dateZ
today_dateZfirst_day_of_monthZlast_day_of_monthr   r   Zreport_namer   r   r+   r+   r,   get_att_reports_urls  s    



















r   )r   c             C   s>  ddl m} |sd S t|dd}|s,d S |tttfkr>d S |dd }|sRd S tj }|dd }|dd }|dd }	|t	|	dd	
d}
|d
d }|dd }|dd }|d|krd S t|dd}|}|dd}|tj|d }d}|tkr|}d}n|tkrJ| }|t|kr|tjdd }d}nJ|tkr|j}|t|kr|jddtjdd j|jd}d}nd S |d}|d}|rtjjd|d }|r|d|d< t||_|  t|}|r:|d|gkr:|r:t|dkr:t||||||||
||| d S )Nr   )	zkDecryptZauto_email_frequencyrf   r   Zadmin_usernameadmin_passwordutf8Zbiotimer   r   r   z%H:%MZmail_sending_dayrh   Zauto_email_day1)rj   FTir   )rA   z%Y-%m-%dreport_setting)r^   r   z%Y-%m-%d %H:%MZlast_email_alert_time)Zmysite.zkauthr   r"   rC   rk   rl   rm   r$   r%   bytesdecoder&   ro   rp   rA   replacer   r   r   rq   rr   rs   rt   ru   r   r#   r   )r   r   r   r   rQ   rv   r%   r   r   r   r   r   r   r   rw   r'   ri   r(   rx   ry   rA   rz   r   r+   r+   r,   get_report_settings_data2  sj    



"

r   zatt.tasks.send_att_reportsc              C   s   t jr
d S dt jkrd S tjjdd} xn| D ]f}t|j}|	dd}t
d|j}t
d|j}|r,|r,|r,|	dd}|sd S t|t|j q,W d S )	Nr_   r   )r^   auto_mail_triggerFemail_settingrW   Zemail_alert)r   ZCLOUD_VERSIONr`   r   r   r   rr   r   rt   rC   r   r   r   rL   ra   )Zreport_queryr   r   r   r   rW   Z
mail_alertr+   r+   r,   send_att_reportsq  s    

r   )Ir$   r   rr   r   r   r   r   Zrequests.authr   r   r   Zdjango.confr   Zdjango.db.modelsr   Z	django.dbr   Zcelery.taskr   Zcelery.schedulesr   Zdjango.utils.translationr	   _Zdjango.core.cacher
   mysiter   Zmysite.att.modelsr   Zmysite.baser   rK   Zmysite.mobilerM   Zmysite.personnelrN   r   r   r   r   r   Zmysite.base.modelsr   r   r   r   Z&mysite.iclock.models.model_transactionr   Zmysite.admin.services.emailr   rk   rl   rm   r-   rV   r\   re   rb   r}   r|   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   <module>   s\   
R
A)/2Y?