o
    i                     @   s   d dl Z d dlZd dlmZmZ d dlZd dlmZ edZdZ	de
fddZ		
	dde
dedede
de
dedefddZdS )    N)datetime	timedelta)current_appzEurope/Zagrebz https://express.api.mydhlapi.comreturnc                  C   sv   t t} | jddddd}| |k r|}n!| tdd jddddd}| dkr6|tdd7 }| dks)|dS )z=Calculate next DHL pickup slot in Zagreb time (14:00 cutoff).   r   )hourminutesecondmicrosecond   )days   z%Y-%m-%dT%H:%M:%S GMT+01:00)r   now	ZAGREB_TZreplacer   weekdaystrftime)r   cutoffpickup r   </var/www/warehouse.soldered.com/app/services/shipping/dhl.py_pickup_datetime   s   

r   EUR      ?Torder_number	recipientitemsinvoice_numbercurrency	weight_kgis_euc                 C   s  t j}| o|dddk}	t ddidd|d dgd	d
ddddgd|d |d |d |d d|d |d |d dd|d |d |d |d d|d |dd|d dddd!|d"d"d"d#d$g|	d%|  |rsd&nd'd(d)d*}
|	r|rd+d, t|D |t d-d.d/d0|
d1 d2< |d3  d4 }d5t	
|  }|d6d7}tjt d8|
|d9d:}|  | }|d; }|d< d= d1 }t	|}||fS )>u_  Create a DHL Express shipment.

    Args:
        order_number: e-računi order number
        recipient: dict with keys name, street, postal_code, city, country,
                   and optionally email, phone
        items: list of item dicts (productName, price, quantity)
        invoice_number: invoice number for customs (non-EU only)
        total_amount: total shipment value
        currency: currency code
        weight_kg: total package weight in kg
        is_eu: True if destination is within EU (no export declaration needed)

    Returns:
        (tracking_number: str, zpl_bytes: bytes)
    countryHRisRequestedFPshipperDHL_ACCOUNT_NUMBER)typeCodenumber   ZPL2labelECOM26_84_001T)r'   templateNamer#   )
printerDPIencodingFormatimageOptionsDHL_SHIPPER_STREETDHL_SHIPPER_CITYDHL_SHIPPER_POSTALDHL_SHIPPER_COUNTRY)addressLine1cityName
postalCodecountryCodeDHL_SHIPPER_NAMEDHL_SHIPPER_EMAILDHL_SHIPPER_PHONE)fullNameemailphone)postalAddresscontactInformationstreetcitypostal_codenamer=    r>   )shipperDetailsreceiverDetails
   )lengthwidthheight)weight
dimensionszOrder EXWDAPmetric)packagesisCustomsDeclarabledescriptionincotermunitOfMeasurement)plannedShippingDateAndTimer   productCodeaccountsoutputImagePropertiescustomerDetailscontentc                 S   sj   g | ]1\}}|d  | dp| dpdt| ddt| dd ddd	d
dgddddddqS )r   rS   productNameElectronicspricer   quantityPCS)valuerU   outbound85340090)r'   ra   	permanentr"   g?g?)netValue
grossValue)r(   rS   r^   r_   commodityCodesexportReasonTypemanufacturerCountryrL   )getfloatint).0iitemr   r   r   
<listcomp>k   s    z#create_shipment.<locals>.<listcomp>z%Y-%m-%d)r(   daterd   )	lineItemsinvoiceexportReasonr[   exportDeclarationDHL_API_KEY:zBasic zapplication/json)AuthorizationzContent-Typez
/shipments   )jsonheaderstimeoutshipmentTrackingNumber	documentsr   )r   configrj   r   	enumerater   r   r   encodebase64	b64encodedecoderequestspostDHL_API_BASEraise_for_statusrz   	b64decode)r   r   r   r   total_amountr   r   r    cfgis_internationalpayload
auth_bytesauth_headerr{   respdatatrackingzpl_b64	zpl_bytesr   r   r   create_shipment   s   



:
r   )NNr   r   T)r   r   r   r   pytzflaskr   timezoner   r   strr   dictlistrk   boolr   r   r   r   r   <module>   s(    
