o
    @g%                     @   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m	Z	m
Z
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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# ddl$m%Z% ddl&m'Z' G dd deZ(dd Z)dd Z*edd Z+edd Z,edd Z-edd  Z.e d!d" Z/ed#d$ Z0ed%d& Z1ed'd( Z2ed)d* Z3ed+d, Z4ed-d. Z5ed/d0 Z6ed1d2 Z7ed3d4 Z8d5S )6    )datetime)messages)login_required)	send_mail)CountCaseWhenIntegerField)renderredirect)APIView)Responsestatus)	CustomersCompanyUserCustomerDocument)settings)CategoryBatch   )get_customer_statistics)recently_created_batches)get_recently_updated_customers)Contact)CustomersSerializerc                   @   s   e Zd Zdd ZdddZdS )CustomersDetailViewc                 C   sd   zt jj|d}|jstdditjdW S t|}t|jW S  t j	y1   tdditj
d Y S w )Nqr_codemessagezPassword not set.r   errorCustomer not found)r   objectsgetpasswordr   r   HTTP_400_BAD_REQUESTr   dataDoesNotExistHTTP_404_NOT_FOUNDselfrequestr   customer
serializer r.   (/var/www/rescue_company/website/views.pyr#      s   zCustomersDetailView.getNc                 C   s   |r>z(t jj|d}t||jd}| r"|  t|jtj	dW S t|j
tjdW S  t jy=   tdditjd Y S w tdditjdS )Nr   )r&   r   r    r!   z QR code is required for updating)r   r"   r#   r   r&   is_validsaver   r   HTTP_200_OKerrorsr%   r'   r(   r)   r.   r.   r/   post!   s   zCustomersDetailView.post)N)__name__
__module____qualname__r#   r4   r.   r.   r.   r/   r      s    
r   c                 C   
   t | dS )Nz
login.htmlr
   r+   r.   r.   r/   login0   s   
r;   c                  C   s`   t  } g }tdddD ]!}| j| }| j}|dkr"|d7 }|d8 }|| d|d q|S )N   r      r   -02d)r   nowrangemonthyearappend)todaymonthsirC   rD   r.   r.   r/   get_last_six_months4   s   
rI   c                 C   sR   | j }|js%t|j}t|j}t }t|j}||||d}t| d|S tdS )N)dashboard_datarecent_activitieslast_six_months_listrecently_updated_customersz
index.htmlz/admin)	useris_superuserr   companyr   rI   r   r
   r   )r+   rN   rJ   rK   rL   rM   contextr.   r.   r/   	dashboardA   s   


rR   c                 C   s   t j }t| dd|iS )Nzcustomer_management.html	customers)r   r"   allr
   )r+   r,   r.   r.   r/   customer_managementV   s   
rU   c                 C   s.   t jj|d}tjj|d}t| d||dS )Nid)r,   zcustomer_details.html)rS   	documents)r   r"   r#   r   filterr
   )r+   customer_idr,   rX   r.   r.   r/   customer_details\   s   r[   c                 C   s   t jj| jjdjtdtttddddt	 dtttddd	tddd
t	 dd}g }|D ]"}|
|j|j|j|j|j|j|j|j|jd|jdd
 q1t| dd|iS )N)created_by__companyrS   F r   )customers__password__isnullcustomers__password__gtthen)output_fieldT)r^   r`   )customers__passwordr`   )customer_count
registeredunregisteredz%d/%m/%Yz%H:%M)
rW   batch_number
batch_namerd   re   category
created_byrc   created_datecreated_timezqr_code_manager.htmlbatch)r   r"   rY   rN   rP   annotater   r   r   r	   rE   rW   rf   rg   rd   re   rh   ri   rc   createdstrftimer
   )r+   batch_queryset
batch_datarl   r.   r.   r/   qr_code_managerc   s>   




rr   c                 C   s0   t jj|d }tjj|d}t| dd|iS )NrV   )rl   zbatch_history.htmlbatch_customers)r   r"   rY   firstr   r
   )r+   batch_idrl   rs   r.   r.   r/   batch_history   s   rv   c                 C   s&   t jj| jjddd}t| dd|iS )NTF)r\   	is_active
is_deletedzqr_generator.html
categoriesr   r"   rY   rN   rP   r
   )r+   rh   r.   r.   r/   qr_generator   s   r{   c                 C   r8   )Nzprofile.htmlr9   r:   r.   r.   r/   profile   s   
r|   c              
   C   s\   zt jj|| jjd}t| dd|iW S  ty- } zt| tdW  Y d }~S d }~ww )N)rW   rP   zstaff_profile.htmlstaff_profilez/manage_company_user)	r   r"   r#   rN   rP   r
   	Exceptionprintr   )r+   staff_idstaff_profile1er.   r.   r/   r}      s   r}   c                 C   r8   )Nmanage_profiles.htmlr9   r:   r.   r.   r/   manage_profiles      
r   c                 C   r8   )Nzcreate_profiles.htmlr9   r:   r.   r.   r/   create_profiles   r   r   c           
   
   C   sl  | j dkr| jd}| jd}| jd}| jd}zt||||d}|  zZd| d}z
|| jjj 7 }W n tyM } zW Y d }~nd }~ww d	| d
| d| d| d	}t	||t
jddgdd d| d| d| d| d	}	t	d|	t
j|gdd W n ty } z
t| W Y d }~nd }~ww tj| ddd tdW S    td Y S t| dS )NPOSTnameemailsubjectr   )r   r   r   r   zRescuscanner Contact Mail from z : z"
                            name:z&

                            subject:z$

                            email"z'


                            message:z
                            zinfo.rescuescanner@gmail.comzonkart4u@gmail.comF)fail_silentlyz
                                    Recieved your message.
                                    details are as follows:


                                    name:z.

                                    subject:z,

                                    email"z/


                                    message:z%
                                    zAcknowledgement Mailzmail sent succefullysuccess)
extra_tagsz/contactz
/dashboardzcontact.html)methodr   r#   r   r1   rN   rP   r   r~   r   r   DEFAULT_FROM_EMAILr   r   r   r   r
   )
r+   r   r   r   r   contactmodified_subjectr   modified_messagecustomer_messager.   r.   r/   r      sb   





r   c                 C   r8   )Nzreports.htmlr9   r:   r.   r.   r/   reports   r   r   c                 C   s,   t jj| jjdd}t| t| dd|iS )NF)rP   rx   r   company_user)r   r"   rY   rN   rP   r   r
   )r+   company_user_accountsr.   r.   r/   manage_company_user   s   r   c                 C   s:   t jj| jjdd}t jj| jjdd}t| d||dS )NF)r\   rx   Tzcategory.html)category_listdeleted_category_listrz   )r+   r   r   r.   r.   r/   rh      s
   rh   N)9r   django.contribr   django.contrib.auth.decoratorsr   django.core.mailr   django.db.modelsr   r   r   r	   django.shortcutsr
   r   rest_framework.viewsr   rest_framework.responser   rest_frameworkr   accounts.modelsr   r   r   mainr   qr_manager.modelsr   r   company_view.dashboard_viewsr   company_view.recent_activitiesr   'company_view.recently_updated_customersr   modelsr   serializersr   r   r;   rI   rR   rU   r[   rr   rv   r{   r|   r}   r   r   r   r   r   rh   r.   r.   r.   r/   <module>   s`    



#



	


/

