B
    CDb:                 @   s   d dl Z d dl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 G dd dejZG d	d
 d
ejZG dd dejZG dd dejZdS )    N)serializers)timezone)
Membership)OrderPriceStrategyCompanyFeature)get_price_with_currency_symbolc               @   s   e Zd Ze Ze Ze Ze Ze Z	e Z
e Ze Ze Ze Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZG dd dZdS )OrderSerializerc             C   s@   t jj|jjd}|j}|j}|tj|d d }|	d}|S )N)id   )daysz%Y-%m-%d)
r   objectsgetcompanyr   subscription_end_datesubscription_durationdatetime	timedeltastrftime)selfinstancer   current_subscription_end_dater   expire_dateexpire_date_format r   GG:\easytimepro\master/mysite/cloud\api\serializers\order_serializers.pyget_expire_date   s    
zOrderSerializer.get_expire_datec             C   s   d |j |jjj}|S )Nz{}/{})formatprice_strategyget_type_displaycountryname)r   r   price_strategy_typer   r   r   get_price_strategy_type#   s    z'OrderSerializer.get_price_strategy_typec             C   s   |j j}|S )N)r   r"   )r   r   company_namer   r   r   get_company_name)   s    z OrderSerializer.get_company_namec             C   s>   t jj|jdd }|r6t jj|jddd jj}nd}|S )N   )r   roler   z	No vendor)r   r   filterr   existsvendor_companyr"   )r   r   
node_exitsvendor_namer   r   r   get_vendor_name.   s
    zOrderSerializer.get_vendor_namec             C   s   |j  }dd |D }|S )Nc             S   s   g | ]
}|j qS r   )r"   ).0fr   r   r   
<listcomp>9   s    z4OrderSerializer.get_feature_name.<locals>.<listcomp>)featureall)r   r   r2   feature_name_listr   r   r   get_feature_name7   s    
z OrderSerializer.get_feature_namec             C   s   |j j}|S )N)create_userusername)r   r   create_user_namer   r   r   get_create_user_username=   s    z(OrderSerializer.get_create_user_usernamec             C   s   |  }|S )N)get_payment_status_display)r   r   payment_status_namer   r   r   get_payment_status_nameB   s    z'OrderSerializer.get_payment_status_namec             C   s   |j }|jj}t||}|S )N)original_pricer   currencyr	   )r   r   r=   currency_alpha_3#original_price_with_currency_symbolr   r   r   get_original_priceG   s    
z"OrderSerializer.get_original_pricec             C   s   |j }|jj}t||}|S )N)discount_pricer   r>   r	   )r   r   rB   r?   #discount_price_with_currency_symbolr   r   r   get_discount_priceN   s    
z"OrderSerializer.get_discount_pricec             C   s   |j }|jj}t||}|S )N)payment_pricer   r>   r	   )r   r   rE   r?   "payment_price_with_currency_symbolr   r   r   get_payment_priceU   s    
z!OrderSerializer.get_payment_pricec               @   s<   e Zd ZeZddddddddd	d
ddddddddddgZdS )zOrderSerializer.Metar   r#   numberr   r%   r-   feature_namer   employees_countdevices_counttransactions_countmobile_applications_countwhatsapp_push_countr=   rB   rE   payment_statusr;   create_user_usernamecreate_timeN)__name__
__module____qualname__r   modelfieldsr   r   r   r   Meta\   s   rW   N)rR   rS   rT   r   SerializerMethodFieldr   r#   r%   r-   rI   r;   rP   r=   rB   rE   r   r$   r&   r.   r5   r9   r<   rA   rD   rG   rW   r   r   r   r   r
      s*   	r
   c                   s   e Zd ZejddZejddZejddZejddZejddZ	ejddZ
dd ZG dd dZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z fddZ  ZS )OrderCreateSerializer   )Z	min_valuec             C   sB   | j d}|j}| jj}|jjd|jd}| r>t	
d|S )Nrequest   )rO   Z
company_idzFThere are unsuccessful orders, please go to the billing page to check.)contextr   userrW   rU   r   r)   current_companyr*   r   ValidationError)r   Zattrsr[   r^   Zmodel_classZunpaid_status_orderr   r   r   validateo   s    zOrderCreateSerializer.validatec               @   s(   e Zd ZeZdZdddddddd	gZd
S )zOrderCreateSerializer.Meta__all__rH   r   r=   rB   rE   rO   r6   rQ   N)rR   rS   rT   r   rU   rV   Zread_only_fieldsr   r   r   r   rW   {   s   
rW   c             C   s   t jj| jd d}|S )Nr   )r   )r   r   r   initial_data)r   r   r   r   r   get_price_strategy   s    z(OrderCreateSerializer.get_price_strategyc             C   sB   dd | j d D }tjj|d}d}x|D ]}|j| }q,W |S )Nc             S   s   g | ]}t |qS r   )int)r/   Z
feature_idr   r   r   r1      s    z<OrderCreateSerializer.get_features_price.<locals>.<listcomp>r2   )Zid__inr   )rc   r   r   r)   Zprice)r   Zfeatures_id_listZfeaturesZfeatures_pricer0   r   r   r   get_features_price   s    
z(OrderCreateSerializer.get_features_pricec             C   s$   t | jd }|  }||j }|S )Nr   )floatrc   rd   Z*subscription_duration_unit_price_pre_month)r   r   r   subscription_duration_pricer   r   r   get_subscription_duration_price   s    
z5OrderCreateSerializer.get_subscription_duration_pricec             C   s:   t | jd }|  }||jkr&d}n||j |j }|S )NrK   r   )re   rc   rd   Zdevice_thresholdZdevice_unit_price)r   rK   r   device_pricer   r   r   get_device_price   s    
z&OrderCreateSerializer.get_device_pricec             C   s:   t | jd }|  }||jkr&d}n||j |j }|S )NrJ   r   )re   rc   rd   Zemployee_thresholdZemployee_unit_price)r   rJ   r   employee_pricer   r   r   get_employee_price   s    
z(OrderCreateSerializer.get_employee_pricec             C   sF   t | jd }|  }|js>||jkr,d}qB||j |j }nd}|S )NrL   r   )re   rc   rd   Ztransaction_counts_limit_statusZtransaction_trail_pointsZtransaction_thresholdZtransaction_unit_price)r   rL   r   Ztransaction_pricer   r   r   get_transaction_price   s    
z+OrderCreateSerializer.get_transaction_pricec             C   sF   t | jd }|  }|js>||jkr,d}qB||j |j }nd}|S )NrM   r   )re   rc   rd   Zmobile_app_counts_limit_statusZmobile_applications_thresholdZmobile_app_thresholdZmobile_applications_unit_price)r   rM   r   mobile_app_pricer   r   r   get_mobile_app_price   s    
z*OrderCreateSerializer.get_mobile_app_pricec             C   sF   t | jd }|  }|js>||jkr,d}qB||j |j }nd}|S )NrN   r   )re   rc   rd   Z!whatsapp_push_counts_limit_statusZwhatsapp_push_thresholdZwhatsapp_push_unit_price)r   rN   r   whatsapp_push_pricer   r   r   get_whatsapp_push_price   s    
z-OrderCreateSerializer.get_whatsapp_push_pricec             C   sn   |   }|  }|  }|  }|  }|  }|  }|| | | | | }||jj }	||	 }
||	|
fS )N)	rd   rf   rk   rm   rp   rr   ri   ZdiscountZdiscount_ratio)r   r   Zfeature_pricerj   rl   ro   rq   rh   r=   rB   rE   r   r   r   	get_price   s    zOrderCreateSerializer.get_pricec                s   d}| j d}|r$t|dr$|j}tj}||d< |rLtjj|jd|d< nd|d< ||d< t	t
dt
t

 t	t

 d	d
dd  |d< d|d< |  \|d< |d< |d< tt| |S )z
        According to the control points entered by the user
        calculate the price through the price strategy
        then create an order
        Nr[   r^   r6   )r   r   rQ   z%Y%m%d%H%M%S. irH   r\   rO   r=   rB   rE   )r]   r   hasattrr^   r   Znowr   r   r_   strtimer   	localtimereplacers   superrY   create)r   Zvalidated_datar^   r[   rQ   )	__class__r   r   r|      s     zOrderCreateSerializer.create)rR   rS   rT   r   ZIntegerFieldr   rJ   rK   rL   rM   rN   ra   rW   rd   rf   ri   rk   rm   rn   rp   rr   rs   r|   __classcell__r   r   )r}   r   rY   g   s$   	

rY   c               @   s"   e Zd Zdd ZG dd dZdS )OrderEditSerializerc             C   s:   || j jkr6| j jdkr$tdn|dkr6td|S )Nr\   zPermission deniedr   )r   rO   r   r`   )r   rO   r   r   r   validate_payment_status   s    
z+OrderEditSerializer.validate_payment_statusc               @   s   e Zd ZeZddgZdS )zOrderEditSerializer.Metar   rO   N)rR   rS   rT   r   rU   rV   r   r   r   r   rW     s   rW   N)rR   rS   rT   r   rW   r   r   r   r   r      s   
r   c               @   s   e Zd Ze Ze Ze Ze Ze Z	e Z
e Ze Ze Ze Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZG dd dZdS )OrderExportSerializerc             C   s@   t jj|jjd}|j}|j}|tj|d d }|	d}|S )N)r   r   )r   z%Y-%m-%d)
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r     s    
z%OrderExportSerializer.get_expire_datec             C   s   d |j |jjj}|S )Nz{}/{})r   r   r    r!   r"   )r   r   r#   r   r   r   r$   $  s    z-OrderExportSerializer.get_price_strategy_typec             C   s   |j j}|S )N)r   r"   )r   r   r%   r   r   r   r&   *  s    z&OrderExportSerializer.get_company_namec             C   s>   t jj|jdd }|r6t jj|jddd jj}nd}|S )Nr'   )r   r(   r   z	No vendor)r   r   r)   r   r*   r+   r"   )r   r   r,   r-   r   r   r   r.   /  s
    z%OrderExportSerializer.get_vendor_namec             C   s   |j  }dd |D }|S )Nc             S   s   g | ]
}|j qS r   )r"   )r/   r0   r   r   r   r1   :  s    z:OrderExportSerializer.get_feature_name.<locals>.<listcomp>)r2   r3   )r   r   r2   r4   r   r   r   r5   8  s    
z&OrderExportSerializer.get_feature_namec             C   s   |j j}|S )N)r6   r7   )r   r   r8   r   r   r   get_create_username>  s    z)OrderExportSerializer.get_create_usernamec             C   s   |  }|S )N)r:   )r   r   r;   r   r   r   get_payment_statusC  s    z(OrderExportSerializer.get_payment_statusc             C   s   |j }|jj}t||}|S )N)r=   r   r>   r	   )r   r   r=   r?   r@   r   r   r   rA   H  s    
z(OrderExportSerializer.get_original_pricec             C   s   |j }|jj}t||}|S )N)rB   r   r>   r	   )r   r   rB   r?   rC   r   r   r   rD   O  s    
z(OrderExportSerializer.get_discount_pricec             C   s   |j }|jj}t||}|S )N)rE   r   r>   r	   )r   r   rE   r?   rF   r   r   r   rG   V  s    
z'OrderExportSerializer.get_payment_pricec               @   s:   e Zd ZeZddddddddd	d
dddddddddgZdS )zOrderExportSerializer.Metar   r#   rH   r   r%   r-   rI   r   rJ   rK   rL   rM   rN   r=   rB   rE   rO   create_usernamerQ   N)rR   rS   rT   r   rU   rV   r   r   r   r   rW   ]  s   rW   N)rR   rS   rT   r   rX   r   r#   r%   r-   rI   rO   r   r=   rB   rE   r   r$   r&   r.   r5   r   r   rA   rD   rG   rW   r   r   r   r   r     s*   	r   )rx   r   Zrest_frameworkr   Zdjango.utilsr   Zmysite.base.modelsr   Zmysite.cloud.modelsr   r   r   r   Zmysite.cloud.utilsr	   ZModelSerializerr
   rY   r   r   r   r   r   r   <module>   s   [ 