o
    P+hP                     @   s  d 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mZ ddlZddlmZ eeZd+d	d
Zdd Zdd Zdd Zd+ddZd+ddZd+ddZd+ddZd+ddZd+ddZd+ddZd+dd Zd+d!d"Zd+d#d$Z d+d%d&Z!d+d'd(Z"d+d)d*Z#dS ),zh
Notification utilities for sending emails to users at different stages of leave and overtime requests.
    )	send_mailEmailMultiAlternatives)render_to_string)
strip_tags)settings)UserGroupNEmployeec              
   C   s   | r| j  d|   S zddlm} |j }tjrdnd}| d|j W S  t	yB } zt
d|  W Y d}~dS d}~ww )	z
    Get the site URL from the request or settings.
    
    Args:
        request: The HTTP request object (optional)
    
    Returns:
        str: The site URL
    z://r   )Sitehttpshttpz,Could not get site URL from Site framework: Nzhttps://smarthr.ictaz.org.zm)schemeget_hostdjango.contrib.sites.modelsr   objectsget_currentr   SECURE_SSL_REDIRECTdomain	Exceptionloggerwarning)requestr   siteprotocole r   */var/www/html/wtlms/notifications/utils.pyget_site_url   s   
r   c                  C   ^   t jjdd } | r| j }dd |D }|r|S ddlm} |jjdd}d	d |D S )
z'Get email addresses of all HR managers.z
HR Managernamec                 S      g | ]}|j r|j qS r   email.0userr   r   r   
<listcomp>1       z!get_hr_emails.<locals>.<listcomp>r   r	   HRrolec                 S      g | ]
}|j jr|j jqS r   r'   r$   r&   employeer   r   r   r(   8       r   r   filterfirstuser_setallaccounts.modelsr
   )hr_grouphr_usersemailsr
   hr_employeesr   r   r   get_hr_emails+      
r<   c                  C   s   t jjdd } | r)| j }dd |D }|r#td|  |S td ntd dd	l	m
} |jjd
d}dd |D }|rNtd|  |S td |S )z&Get email addresses of all Registrars.	Registrarr    c                 S   r"   r   r#   r%   r   r   r   r(   A   r)   z(get_registrar_emails.<locals>.<listcomp>z#Found Registrar emails from group: z"No emails found in Registrar groupzRegistrar group not foundr   r	   REGr+   c                 S   r-   r   r.   r/   r   r   r   r(   M   r1   z"Found Registrar emails from role: z*No Registrar emails found from role either)r   r   r3   r4   r5   r6   r   infor   r7   r
   )registrar_groupregistrar_usersr:   r
   registrar_employeesr   r   r   get_registrar_emails;   s"   


rD   c                  C   r   )
z4Get email addresses of all Finance department users.Financer    c                 S   r"   r   r#   r%   r   r   r   r(   ]   r)   z&get_finance_emails.<locals>.<listcomp>r   r	   FINr+   c                 S   r-   r   r.   r/   r   r   r   r(   d   r1   r2   )finance_groupfinance_usersr:   r
   finance_employeesr   r   r   get_finance_emailsW   r=   rJ   c           
   
   C   s   |s| d|   dS |stjpd}t|}ddl}|t}|d|  d|  t| |||d}|	|d z|
 }|d	|  |W S  tyc }	 z|d
|	  W Y d}	~	dS d}	~	ww )z6
    Send an HTML email with plain text fallback.
    z"No recipients provided for email: r   znoreply@ictaz.orgNzSending email: z to )subjectbody
from_emailtoz	text/htmlzEmail sent successfully: zFailed to send email: )r   r   DEFAULT_FROM_EMAILr   logging	getLogger__name__r@   r   attach_alternativesendr   error)
rK   html_contentrecipient_listrM   r   plain_messagerP   r$   resultr   r   r   r   send_html_emailg   s2   

rZ   c                 C   s   t |}d| j }td| | j|d}t||| jjjgd t }|r@d| jj  }td| | j|d}t|||d dS dS )z
    Send notification when a leave request is submitted.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    zLeave Request Submitted - z,notifications/leave/employee_submission.htmlleave_requestr0   site_urlrK   rV   rW   zNew Leave Request - z'notifications/leave/hr_new_request.htmlN	r   
leave_typer   r0   rZ   r'   r$   r<   get_full_namer\   r   r]   employee_subjectemployee_html	hr_emails
hr_subjecthr_htmlr   r   r   notify_leave_submission   4   	

rh   c                 C   s   t d| j  t|}d| j }td| | j|d}t||| jjj	gd t
 }|rId| jj  }td| | j|d}t|||d dS dS )	z
    Send notification when HR approves a leave request.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    z3Sending HR approval notification for leave request zLeave Request HR Approved - z-notifications/leave/employee_hr_approved.htmlr[   r^   z!Leave Request Pending Approval - z.notifications/leave/registrar_new_request.htmlN)r   r@   idr   r`   r   r0   rZ   r'   r$   rD   ra   )r\   r   r]   rc   rd   registrar_emailsregistrar_subjectregistrar_htmlr   r   r   notify_leave_hr_approval   6   

rn   c                 C   B   t |}d| j }td| | j|d}t||| jjjgd dS )z
    Send notification when HR rejects a leave request.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    zLeave Request Rejected by HR - z-notifications/leave/employee_hr_rejected.htmlr[   r^   Nr   r`   r   r0   rZ   r'   r$   r\   r   r]   rc   rd   r   r   r   notify_leave_hr_rejection      	

rs   c                 C   s   t d| j  t|}d| j }td| | j|d}t||| jjj	gd t
 }|rId| jj  }td| | j|d}t|||d dS dS )z
    Send notification when Registrar approves a leave request.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    z:Sending Registrar approval notification for leave request zLeave Request Approved - z0notifications/leave/employee_final_approved.htmlr[   r^   z*notifications/leave/hr_final_approved.htmlN)r   r@   rj   r   r`   r   r0   rZ   r'   r$   r<   ra   rb   r   r   r   notify_leave_registrar_approval   ro   ru   c                 C      t |}d| j }td| | j|d}t||| jjjgd t }|r@d| jj  }td| | j|d}t|||d dS dS )z
    Send notification when Registrar rejects a leave request.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    zLeave Request Rejected - z0notifications/leave/employee_final_rejected.htmlr[   r^   z*notifications/leave/hr_final_rejected.htmlNr_   rb   r   r   r    notify_leave_registrar_rejection#  ri   rw   c                 C   s@   t |}d| j }td| | jd}t||| jjjgd dS )z
    Send reminder notification before leave starts.
    
    Args:
        leave_request: The LeaveRequest instance
        request: The HTTP request object (optional)
    zUpcoming Leave Reminder - z*notifications/leave/employee_upcoming.html)r\   r0   r^   Nrq   rr   r   r   r   notify_leave_upcomingJ  s   	

rx   c                 C   s   t |}d| j }td| | j||d}t||| jjjgd t }|rAd| jj  }td| | j|d}t|||d dS dS )	z
    Send notification when an overtime request is submitted.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    zOvertime Request Submitted - z/notifications/overtime/employee_submission.htmlovertime_requestr0   r   r]   r^   zNew Overtime Request - z*notifications/overtime/hr_new_request.htmlrz   r0   r]   N	r   dater   r0   rZ   r'   r$   r<   ra   rz   r   r]   rc   rd   re   rf   rg   r   r   r   notify_overtime_submissionc  s6   	

r   c                 C   s   t d| j  t|}d| j }td| | j||d}t||| jjj	gd t
 }|rKd| jj  }td| | j||d}t|||d dS dS )	z
    Send notification when HR approves an overtime request.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    z6Sending HR approval notification for overtime request zOvertime Request HR Approved - z0notifications/overtime/employee_hr_approved.htmlry   r^   z$Overtime Request Pending Approval - z1notifications/overtime/registrar_new_request.htmlN)r   r@   rj   r   r}   r   r0   rZ   r'   r$   rD   ra   )rz   r   r]   rc   rd   rk   rl   rm   r   r   r   notify_overtime_hr_approval  s:   

r   c                 C   rp   )z
    Send notification when HR rejects an overtime request.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    z"Overtime Request Rejected by HR - z0notifications/overtime/employee_hr_rejected.htmlr{   r^   N)r   r}   r   r0   rZ   r'   r$   )rz   r   r]   rc   rd   r   r   r   notify_overtime_hr_rejection  rt   r   c                 C   s   t d| j  t|}d| j }td| | j|d}t||| jjj	gd t
 }|rGd| jj  }td| | j|d}t|||d t }|rhd| jj  }	td| | j|d}
t|	|
|d d	S d	S )
z
    Send notification when Registrar approves an overtime request.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    z=Sending Registrar approval notification for overtime request zOvertime Request Approved - z7notifications/overtime/employee_registrar_approved.htmlr{   r^   z1notifications/overtime/hr_registrar_approved.htmlzOvertime Payment Required - z+notifications/overtime/finance_payment.htmlN)r   r@   rj   r   r}   r   r0   rZ   r'   r$   r<   ra   rJ   )rz   r   r]   rc   rd   re   rf   rg   finance_emailsfinance_subjectfinance_htmlr   r   r   "notify_overtime_registrar_approval  sP   

r   c                 C   rv   )z
    Send notification when Registrar rejects an overtime request.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    zOvertime Request Rejected - z7notifications/overtime/employee_registrar_rejected.htmlr{   r^   z1notifications/overtime/hr_registrar_rejected.htmlNr|   r~   r   r   r   #notify_overtime_registrar_rejection  ri   r   c                 C   rv   )z
    Send notification when payment for overtime is processed.
    
    Args:
        overtime_request: The OvertimeRequest instance
        request: The HTTP request object (optional)
    zOvertime Payment Processed - z,notifications/overtime/employee_payment.htmlr{   r^   z&notifications/overtime/hr_payment.htmlNr|   r~   r   r   r   !notify_overtime_payment_processed.  ri   r   )N)$__doc__django.core.mailr   r   django.template.loaderr   django.utils.htmlr   django.confr   django.contrib.auth.modelsr   r   rP   r7   r
   rQ   rR   r   r   r<   rD   rJ   rZ   rh   rn   rs   ru   rw   rx   r   r   r   r   r   r   r   r   r   r   <module>   s4    



+
'
)

)
'

)
+

8'