B
    Ac_                 @   s   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mZmZ G d
d dejZe
ejeddd Ze
ejeddd Ze
ejeddd ZdS )    N)models)settings)cache)signals)receiver)gettext_lazy)MyUser)CompanyFeaturePriceStrategyc               @   s  e Zd ZdZejeejdZej	ddddZ
ejeejdZeeZejdddZejdddZejdddZejdddZejdddZejdddZejdddZejdddZejdddZejddded	ZejeejdZejdd
ZG dd dZ dd Z!dS )Order))r   zManually canceled)   zTimeout cancelled)   ZPaid)   ZUnpaid)Z	on_delete   F)Z
max_lengthnullblank)r   r   Tr   )defaultr   r   choices)Zauto_now_addc               @   s   e Zd ZedZdgZdZdS )z
Order.Metar   z-create_timeZcloudN)__name__
__module____qualname___Zverbose_nameZorderingZ	app_label r   r   8G:\easytimepro\master/mysite/cloud\models\model_order.pyMeta,   s   r   c             C   s   | j S )N)number)selfr   r   r   __str__1   s    zOrder.__str__N)"r   r   r   ZSTATUS_CHOICESr   Z
ForeignKeyr   ZCASCADEprice_strategyZ	CharFieldr   r	   companyZManyToManyFieldr
   featureZIntegerFieldsubscription_durationemployees_countdevices_counttransactions_countmobile_applications_countwhatsapp_push_countZ
FloatFieldZoriginal_priceZdiscount_priceZpayment_pricepayment_statusr   Zcreate_userZDateTimeFieldZcreate_timer   r   r   r   r   r   r      s&   
r   )senderc             K   s   |s|j }|dkrtjj|jjd}|j}|j}|tj	|d d }	|	|_|j
|_|j|_|j|_|j|_|j|_|jddddd	d
gd |j }
x|
D ]}|j| qW d S )Nr   )id   )Zdayssubscription_end_datemax_employees_limitmax_devices_limitmax_transactions_limitmax_mobile_applications_limitmax_whatsapp_push_limit)Zupdate_fields)r(   r	   Zobjectsgetr    r*   r,   r"   datetimeZ	timedeltar#   r-   r$   r.   r%   r/   r&   r0   r'   r1   Zsaver!   alladd)r)   instancecreatedrawkwargsZorder_statusr    Zcurrent_subscription_end_dater"   Znew_subscription_end_dateZnew_featurefr   r   r   post_save_receiver5   s(    

r;   c             K   s   t jrddlm} |r\d|j|jjdd |j D d|j	d}d}|||d d}|
 }	n|j}
d	|
}||d d d}| }	td
}|	d d }xN|D ]F}|d
 }||kr|t }d|ji}d|}|||d d}| }	qW d S )Nr   )!HttpRequestInterfaceForIoTServicer   c             S   s   g | ]
}|j qS r   )r*   ).0r:   r   r   r   
<listcomp>Z   s    z@save_or_update_order_to_subscription_service.<locals>.<listcomp>)typeZusers_countr   r!   Zsubscription_period_annuallyZsubscription_period_monthlyz<https://subscriptiondev.minervaiot.com/api/v1/subscriptions/)bodyparamszFhttps://subscriptiondev.minervaiot.com/api/v1/subscriptions/?number={}application_iddataresultsr(   z?https://subscriptiondev.minervaiot.com/api/v1/subscriptions/{}/)r   CLOUD_VERSION"mysite.cloud.minerva_iot_interfacer<   r#   r   r*   r!   r4   r"   Zpostr   formatr2   r   r(   Zput)r)   r6   r7   r8   r9   r<   r@   urlinterface_objectcontentsubscription_numbercurrent_application_idrD   subscriptionresult_application_idsubscription_idr   r   r   ,save_or_update_order_to_subscription_serviceP   s4    






rP   c             K   s   t jrddlm} |j}d|}||d d d}| }td}|d d }	xD|	D ]<}
|
d }||krT|
t }d|}||d d d}|	 }qTW d S )	Nr   )r<   zFhttps://subscriptiondev.minervaiot.com/api/v1/subscriptions/?number={})r@   rA   rB   rC   rD   z?https://subscriptiondev.minervaiot.com/api/v1/subscriptions/{}/)
r   rE   rF   r<   r   rG   r2   r   r*   delete)r)   r6   r9   r<   rK   rH   rI   rJ   rL   rD   rM   rN   rO   r   r   r   $delete_order_to_subscription_servicet   s    



rR   )r3   Z	django.dbr   Zdjango.confr   Zdjango.core.cacher   Zdjango.db.modelsr   Zdjango.dispatch.dispatcherr   Zdjango.utils.translationr   r   Zmysite.accounts.modelsr   Zmysite.cloud.modelsr	   r
   r   ZModelr   Z	post_saver;   rP   Zpost_deleterR   r   r   r   r   <module>   s   !$