3

\                 @   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
Zd dlZd dlT d dlZd dlmZ d dlmZ ejd	ZG d
d deeZeZdS )    N)AbstractShipping)Free)timezone)settings)*)ShippingApiError)	mark_safeshippingc               @   s^   e Zd Zd ZdZdZejZej	Z
dd Zdd Zdd Zdd
dZdd Zdd Zdd Zd	S )JaniojaniozExpress delivery with trackingc             C   s$   dj t|jjddtj jdS )Nzhook{}{}- z%Y%m%d)formatstrnumberreplacer   	localdatestrftime)selforder r   6/var/www/html/oscar2019/apps/shipping/methods/janio.py_create_waybill_number   s    zJanio._create_waybill_numberc          	   C   sF   g }x<|j j D ].}|j|jj d|jj|j|j|jj	d qW |S )NZOthers)Z	item_descZitem_categoryZitem_product_idZitem_quantityZitem_price_valueZitem_price_currency)
linesallappendproductZ	get_titlepkquantityline_price_excl_taxstockrecordZprice_currency)r   r   retliner   r   r   _create_ship_line_items   s    zJanio._create_ship_line_itemsc             C   s   | j d| j||j|jj |jjd |jj |jj|jj	|jj
j|jjt|jjdddt|j d tjdjtjtjtjtjtjd| j|dgd	S )
N          i  z{}, {} {}, {}Zprepaid)
service_idZtracking_noshipper_order_idZconsignee_nameZconsignee_addressZconsignee_postalZconsignee_emailZconsignee_countryZconsignee_stateZconsignee_numberZorder_lengthZorder_widthZorder_heightZorder_weightZpickup_countryZpickup_addressZpickup_postalZpayment_typeitems)
secret_keyorders)r+   r   r   userget_full_nameshipping_addressline1line2postcodeemailcountryZprintable_namestater   phone_numberfloatget_total_weightship_settingsZSENDER_COUNTRY_NAMEr   ZSENDER_COMPANY_NAMEZSENDER_UNIT_NUMBERZSENDER_STREET_1ZSENDER_POSTAL_CODEr#   )r   r   r   r   r   
_prep_json)   s0    
zJanio._prep_jsonNc       	      C   s   | j |jdd}tj| jjd|}|jdkr|j }y>|d d d }t|rntj|d j	}t
j|j S d S W n  tk
r } zd S d }~X nX d S )Nr$   )r+   r)   r(   zorder/order   resultsr   
print_urls)r+   r   requestsgetendpointr   status_codejsonlencontentioBytesIOgetvalue	Exception)	r   Zwaybillr   dataresdetailsr=   Zpdf_contenter   r   r   get_waybill_pdfJ   s    
zJanio.get_waybill_pdfc             C   s|   t j| jjdtj| j|ddid}tjdj|j	 |j	dkr\|j }|d d	 }|S tj
d
j|j t|jd S )Nzorder/orders/zContent-Typezapplication/json)rI   headerszJanio response status code : {}r;      Ztracking_nosr   zJanio returned an error : {} )r;   rO   )r>   postr@   r   rB   dumpsr:   loggerdebugrA   errortextr   )r   r   rJ   Zshipment_detailsZtracking_idr   r   r   process_shipmentY   s    

zJanio.process_shipmentc             C   s   d}t |S )NzJ<a href="https://tracker.janio.asia/" target="_blank">Track your order</a>)r   )r   urlr   r   r   tracking_urli   s    zJanio.tracking_urlc             C   s   dS )NTr   )r   r   r   r   tracking_availablem   s    zJanio.tracking_available)N)__name__
__module____qualname__namecodedescriptionr   JANIO_ENDPOINTr@   JANIO_API_KEYr+   r   r#   r:   rM   rV   rX   rY   r   r   r   r   r
      s   !
r
   )r>   Zapps.shipping.abstract_shippingr   Zoscar.apps.shipping.methodsr   django.utilsr   apps.shippingr   r9   
simplejsonrB   loggingZhookcoffee.pdf_settingsrE   apps.shipping.exceptionsr   django.utils.textr   	getLoggerrR   r
   __shipping_method_class__r   r   r   r   <module>   s   
a