o
    Nh4+                     @   s`   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
Z
e
eZG dd	 d	ZdS )
    )timezone)	timedelta   )NotificationService)NotificationTemplate)TicketNc                   @   sN   e Zd Zdd Zdd ZdddZdd	 Zd
d Zdd Zdd Z	dd Z
dS )TicketNotificationServicec                 C   s   t  | _d S )N)r   notification_service)self r   B/var/www/html/optinet_system/notifications/ticket_notifications.py__init__   s   z"TicketNotificationService.__init__c                 C   s"  d}d}|j r|j jr|j j}|j j}n|jr0t|jdr0|jjr0|jjjr0|jjj}|jjj}|s=td|j  dS |j|j	|j
| |jrO|jdnd|jpTdd	}ztjjd
dd}| j|j|}W n tjy   d|  d|j d|j	 d|j
 }Y nw | jj||d
|dS )z7Send notification to technician when ticket is assignedN employee_profilez8No phone number found for assigned technician on ticket F%Y-%m-%d %H:%MzNot scheduledzLocation TBD)	ticket_idtitlecustomer_namepriorityscheduled_datelocationsms_ticket_assignedTtemplate_type	is_activezNew z ticket z assigned to you: z. Customer: )phone_numbermessager   recipient_name)assigned_employeephone	full_nameassigned_tohasattrr   loggerwarningr   r   r   get_priority_displayr   strftimer   r   objectsgetr	   render_templatecontentDoesNotExistsend_sms)r
   ticketr   r   contexttemplater   r   r   r   !send_ticket_assigned_notification   sD   



	*z;TicketNotificationService.send_ticket_assigned_notification   c              
   C   s   |j r|j js	dS |j j|j|jd|j| d}ztj	j
ddd}| j|j|}W n tjyH   d|  d|d	  d
|d  }Y nw | jj|j j|d|j jdS )z%Send appointment reminder to customerF%H:%M)r   r   scheduled_timetechnician_nameservice_typesms_appointment_reminderTr   zReminder: Your z appointment is scheduled for r3   z today. Technician: r4   r   r   r   customer_id)customerr   r    r   r   r&   assigned_personget_category_displayr   r'   r(   r	   r)   r*   r+   r,   id)r
   r-   minutes_beforer.   r/   r   r   r   r   send_appointment_reminder:   s.   
&z3TicketNotificationService.send_appointment_reminderc              
   C   s   |j r|j js	dS |j j|j|jd}ztjjddd}| j	|j
|}W n tjyA   d|d  d|d	  d
|d  d}Y nw | jj|j j|d|j jdS )z-Send notification when technician is arrivingF)r   r4   r   sms_technician_arrivingTr   Hello r   z, our technician r4   z  is arriving shortly for ticket r   .r7   )r9   r   r    r:   r   r   r'   r(   r	   r)   r*   r+   r,   r<   r
   r-   r.   r/   r   r   r   r   %send_technician_arriving_notificationW   s*   (z?TicketNotificationService.send_technician_arriving_notificationc                 C   s   |j r|j js	dS |j j|j| t dd}ztj	j
ddd}| j|j|}W n tjyC   d|d  d	|d
  d}Y nw | jj|j j|d|j jdS )z+Send notification when service is completedFr2   )r   r   r5   completion_timesms_service_completedTr   zService completed! Ticket r    for r5   8 has been resolved. Thank you for choosing our services.r7   )r9   r   r    r   r;   r   nowr&   r   r'   r(   r	   r)   r*   r+   r,   r<   rB   r   r   r   #send_service_completed_notificationr   s,   z=TicketNotificationService.send_service_completed_notificationc              
   C   s   |j sdS |j j}|sdS |j|jt |j jd d}ztj	j
ddd}| j|j|}W n tjyK   d|d  d	|d
  d|d  d}Y nw | jj||ddS )z0Send overdue alert to technician and supervisorsF<   )r   r   overdue_timesms_ticket_overdueTr   zOVERDUE: Ticket r   rF   r   z is overdue by rK   z	 minutes.)r   r   r   )r   r   r   r   r   rH   r   secondsr   r'   r(   r	   r)   r*   r+   r,   )r
   r-   r   r.   r/   r   r   r   r   send_overdue_alert   s.   (z,TicketNotificationService.send_overdue_alertc           	      C   sb  |j r|j js	dS |j j|j|| |j|jdkr|jndd}d}d|d  d|d	  d
|d  d}|dkrXd}|jrW|jd|d< d|d  d|d	  d|d  d}n,|dkrqd}d|d  d|d  d|d	  d}n|dkrd}d|d  d|d	  d}zt	j
j|dd}| j|j|}W n t	jy   |}Y nw | jj|j j|||j jdS )z8Send notification to customer when ticket status changesF
Unassignedzour team)r   r   
old_status
new_statusr   r:   sms_ticket_status_changer@   r   z, your ticket r   z status has been updated to rQ   rA   	scheduledsms_appointment_confirmedr   r3   z, your appointment for ticket z is confirmed for in_progressr?   z, r:   z is now working on your ticket resolvedrE   rG   Tr   r7   )r9   r   r    r   get_status_displayr   r:   r   r&   r   r'   r(   r	   r)   r*   r+   r,   r<   )	r
   r-   rP   rQ   r.   r   default_messager/   r   r   r   r   &send_ticket_status_change_notification   sL   
$$&z@TicketNotificationService.send_ticket_status_change_notificationc                 C   s   |j r
|j jr
|jsdS |j j|j|jd|jd| |jdkr&|jnd|jp+dd}zt	j
jdd	d
}| j|j|}W n+ t	jyk   d|d  d|d  d|d  d|d  d|d  d|d  d}Y nw | jj|j j|d|j jdS )z)Send appointment confirmation to customerFz%Y-%m-%dr2   rO   zour technicianzyour location)r   r   r   r3   r5   r4   r   rT   Tr   r@   r   z, your r5   z appointment is confirmed for r   z at r3   z. r4   z will visit r   rA   r7   )r9   r   r   r    r   r&   r;   r:   r   r   r'   r(   r	   r)   r*   r+   r,   r<   rB   r   r   r   send_appointment_confirmation   s2   


Fz7TicketNotificationService.send_appointment_confirmationN)r1   )__name__
__module____qualname__r   r0   r>   rC   rI   rN   rY   rZ   r   r   r   r   r      s    
+.r   )django.utilsr   datetimer   servicesr   modelsr   tickets.modelsr   logging	getLoggerr[   r#   r   r   r   r   r   <module>   s   
