U
    E¼³a¼7  ã                   @   sd  d dl mZmZmZmZmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZ d d	lZed
gƒdd„ ƒZed
gƒdd„ ƒZed
gƒdd„ ƒZed
gƒdd„ ƒZed
gƒdd„ ƒZ ed
gƒdd„ ƒZ!ed
gƒdd„ ƒZ"ed
gƒdd„ ƒZ#ed
gƒdd„ ƒZ$ed
gƒdd„ ƒZ%ed
gƒdd „ ƒZ&ed!gƒd"d#„ ƒZ'd	S )$é    )Ú	api_pathsÚcomponent_namesÚlabel_namesÚ	var_namesÚstatic_vars)ÚHttpResponseÚHttpResponseForbiddenÚJsonResponse)Úrequire_http_methods)Úsettings)Útest_data_organizationÚtest_data_users)Úlabel_translator)ÚhelpersÚlogging)Úrequest_validatorNÚPOSTc                 C   sà   | j dkrÜt | ¡}t | ¡rÄtjršd}tjD ]:}tj| }|tj	 t
jkr.|tj d |tj  |g}q.|dk	r€t|dddS tt tj|¡dddS qÜtƒ }t tj|| ¡\}}t||ddS ntt tj|¡dddS dS )	z
    Gets the username and email of the current account owner.
    :param request: Http request
    :return: JSON response
    r   Nú éÈ   F©ÚstatusÚsafei  é‘  )Úmethodr   Úget_user_languageÚuser_in_sessionr   Ú	TEST_MODEr   Zuser_detailsr   Z	user_roler   Z
owner_roleÚ
first_nameÚ	last_namer	   ÚltÚ	get_labelÚlnmZerr_unknown_resourceÚdictr   Úpost_api_requestr   ÚownerÚerr_unauthorized_access)ÚrequestÚlangÚoutputÚkeyÚitemÚbodyr   © r,   ú0/var/www/html/taskcallweb/views/account_views.pyÚget_account_owner   s     




r.   c                 C   s‚   | j dkr~t | ¡}t | ¡rft | j ¡ ¡}tj	rBt
ddddS t tj|| ¡\}}t
||ddS nt
t tj|¡dddS dS )z|
    Transfers the ownership of an account to another user.
    :param request: Http request
    :return: JSON response
    r   zOwnership has been transferredr   Fr   r   N)r   r   r   r   ÚjsonÚloadsr+   Údecoder   r   r	   r   r#   r   Zowner_transferr   r    r!   r%   ©r&   r'   r+   r   r(   r,   r,   r-   Útransfer_account_ownership,   s    


r3   c                 C   sº   | j dkr¶t | ¡}t | ¡ržtjr0d}tj}ntƒ }t	 
tj|| ¡\}}tƒ }tdt|ƒƒD ]$}|| tj ||< ||| tj< q^|| jtj< t||ddS tt tj|¡dddS dS )z}
    Get all the cards that are stored for the organization.
    :param request: Http request
    :return: JSON response
    r   r   r   Fr   r   N)r   r   r   r   r   r   r   Zaccount_cardsr"   r   r#   r   ZcardsÚrangeÚlenr   Úcard_idÚsessionÚcard_id_mapperr	   r   r    r!   r%   )r&   r'   r   r(   r+   r8   Úir,   r,   r-   Ú	get_cards@   s    


r:   c                 C   sÂ   | j dkr¾t | ¡}t | ¡r¦t | j ¡ ¡}tj	| j
krŒ| j
tj	 |tj	< | j
tj	= tjrhtdddS t tj|| ¡\}}t||ddS q¾tt tj|¡dddS ntt tj|¡dddS dS )za
    Add a new organization card
    :param request: Http request
    :return: JSON response
    r   zNew card has been addedF©r   r   r   N)r   r   r   r   r/   r0   r+   r1   r   Úcustomer_idr7   r   r   r	   r   r#   r   Z	cards_addr   r    r!   r%   r2   r,   r,   r-   Úadd_card\   s    



r=   c              
   C   sB  | j dkr>t | ¡}t | ¡r&z²t | j ¡ ¡}|tj	 }tj
| jkr®|| jtj
 kr®| jtj
 | }tj	|i}tjrˆtdddW S t tj|| ¡\}}t||ddW S n$t d¡ tt tj|¡dddW S W nN tk
r" } z.t t|ƒ¡ tt tj|¡ddd W Y ¢S d}~X Y nX ntt tj|¡d	ddS dS )
zl
    Deletes an existing organization card.
    :param request: Http request
    :return: JSON response
    r   zCard has been deletedFr;   r   z!Could not find card id in sessionéô  Nr   )r   r   r   r   r/   r0   r+   r1   r   r6   r8   r7   r   r   r	   r   r#   r   Zcards_deleter   Úerrorr   r    r!   Zerr_system_errorÚ	ExceptionÚ	exceptionÚstrr%   )r&   r'   r+   Z	mapped_idr6   r   r(   Úer,   r,   r-   Údelete_cardw   s*    

ÿ

0rD   c                 C   s€   | j dkr|t | ¡}t | ¡rdtjr0tdddS t | j	 
¡ ¡}t tj|| ¡\}}t||ddS ntt tj|¡dddS dS )zn
    Set a specific card as the default card.
    :param request: Http request
    :return: JSON response
    r   zCard has been set as defaultFr;   r   r   N)r   r   r   r   r   r   r	   r/   r0   r+   r1   r   r#   r   Zcards_set_defaultr   r    r!   r%   r2   r,   r,   r-   Úset_default_card™   s    


rE   c                 C   sŠ   | j dkr†t | ¡}t | ¡rntƒ }t tj|| ¡\}}|dkr`|d | j	t
j< |d |d g}t||ddS tt tj|¡dddS d	S )
zb
    Get the stripe vendor token.
    :param request: Http request
    :return: JSON response
    r   r   é   r   é   Fr   r   N)r   r   r   r   r"   r   r#   r   Zstripe_tokenr7   r   r<   r	   r   r    r!   r%   r2   r,   r,   r-   Úget_stripe_token­   s    


rH   c                 C   sx   | j dkrtt | ¡}t | ¡r\tjr2ttjddS t	ƒ }t
 tj|| ¡\}}t||ddS ntt tj|¡dddS dS )z^
    Get the
    :param request: Http request
    :return: JSON Response -> dict of lists
    r   Fr;   r   r   N)r   r   r   r   r   r   r	   r   Zorganization_subscriptionr"   r   r#   r   Zsubscription_currentr   r    r!   r%   r2   r,   r,   r-   Úget_current_subscriptionÃ   s    


rI   c                 C   s€   | j dkr|t | ¡}t | ¡rdtjr0tdddS t | j	 
¡ ¡}t tj|| ¡\}}t||ddS ntt tj|¡dddS dS )zg
    Add a subscription to an account.
    :param request: Http request
    :return: JSON response
    r   zNew subscription has been addedFr;   r   r   N)r   r   r   r   r   r   r	   r/   r0   r+   r1   r   r#   r   Zsubscription_updater   r    r!   r%   r2   r,   r,   r-   Úupdate_subscription×   s    


rJ   c                 C   s€   | j dkr|t | ¡}t | ¡rdtjr0tdddS t | j	 
¡ ¡}t tj|| ¡\}}t||ddS ntt tj|¡dddS dS )zq
    Updates the billing currency of an account.
    :param request: Http request
    :return: JSON response
    r   z!Billing currency has been updatedFr;   r   r   N)r   r   r   r   r   r   r	   r/   r0   r+   r1   r   r#   r   Zbilling_currency_updater   r    r!   r%   r2   r,   r,   r-   Úupdate_billing_currencyë   s    


rK   c                 C   sx   | j dkrtt | ¡}t | ¡r\tjr2ttjddS t	ƒ }t
 tj|| ¡\}}t||ddS ntt tj|¡dddS dS )zj
    Get all the bills of an organization.
    :param request: Http request
    :return: JsonResponse
    r   Fr;   r   r   N)r   r   r   r   r   r   r	   r   Zaccount_billsr"   r   r#   r   Zbillsr   r    r!   r%   r2   r,   r,   r-   Ú	get_billsÿ   s    


rL   ÚGETc              
   C   s  | j dkrt | ¡}t | ¡rêt | ¡}t | tj|¡\}}|sJtƒ S t	j
rbtjd }tj}nštj|i}t tj|| ¡\}	}|	dkrÒt tjtƒ | ¡\}
}|
dkrüt dt|
ƒ ¡ t |¡ t||
ddS n*t dt|	ƒ ¡ t |¡ t||	ddS |dk	rÐ|dk	rÐ||–}zft d	|¡}|rft|d
d}dt|tj ƒ d }d |¡}||d< |W S tt tj |¡dddW S  t!k
rÌ } z.t "t|ƒ¡ tt tj |¡ddd W Y ¢S d}~X Y nX ntt tj |¡dddS ntt tj#|¡dddS dS )z“
    Gets the pdf of a bill/invoice.
    :param request: Http request
    :param bill_id: the unique ID of the bill
    :return: Http response
    rM   r   r   z,Failed to get organization details - Status Fr   z$Failed to get bill details - Status Nztmpl_invoice_pdf.htmlzapplication/pdf)Úcontent_typeZInvoice_z.pdfzattachment; filename={0}zContent-Dispositionr>   r   )$r   r   r   r   Zget_nav_bar_componentsZget_session_permissionÚcnmZdis_com_organizationr   r   r   r   Zaccount_bill_detailsZorganization_detailsr   Úbill_idr   r#   r   Zbills_detailsÚorg_detailsr"   r   r?   rB   r	   Zrender_to_pdfr   Úformatr   r    r!   Zerr_invoice_downloadr@   rA   r%   )r&   rP   r'   Znav_bar_componentsZhas_view_permZhas_edit_permZbill_detailsrQ   Zrequest_bodyZbill_statusZ
org_statusÚcontextÚpdfÚresponseÚfilenameÚcontentrC   r,   r,   r-   Úget_bill_pdf  sZ    

  ÿ

  ÿ


0rX   )(Ú	constantsr   r   rO   r   r!   r   r   Údjango.httpr   r   r	   Zdjango.views.decorators.httpr
   Ztaskcallwebr   Zsystem_tests.test_datar   r   Ztranslatorsr   r   Úutilsr   r   Zvalidationsr   r/   r.   r3   r:   r=   rD   rE   rH   rI   rJ   rK   rL   rX   r,   r,   r,   r-   Ú<module>   s@   




!





