o
    Q?h=%                     @   s   d dl m Z  d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZ dd	 Zi dfd
dZdddZd dlZdd Zdd Zd dlZd dlmZ g fddZdd Zdd ZdS )    )datetimeN)helpers)SERVER_TIMESTAMPdb)CREDS	getClientgetSupplierCredentials)getBlankProductgetBlanksUsedByEnterprises)getLocationsc                 C   s  |  d}t|}d }|  d}|r| d}| d}t|d\}}| d}t|}	t||}
i }|	D ]}tj|jj||
 d|
 d|d	| dd
dddd
t	d}| di }|rzP| di }|rl| dg ng }|D ];}| d}| |i ||< | di  dg }|D ]}t
| dd}|dkr| |i | dt|di qqpW q5 ty } ztd| W Y d }~q5d }~ww t||||
 q5t||| dS )NidcurrentUserenterpriseId
supplierIdpricingblankProductIdusernamepasswordUSDCustomerUSenBlank)
	wsVersionr   r   	productIdcurrencyfobId	priceTypelocalizationCountrylocalizationLanguageconfigurationType)
target_clsConfiguration	PartArrayPartpartIdPartPriceArray	PartPricepricer      zPricing updatesT)getr	   r   r   r   r   serialize_objectservicegetConfigurationAndPricingdictfloatupdateround	ExceptionprintsavePricing)paramsr   blankProductr   r   r   clientversionr   	locationscredspartsPricinglocationpricingResponsePartsr$   r%   r&   ParPricer(   e rA   P/var/www/html/riverr-enterprise-integrations-main/functions/Suppliers/Pricing.pyupdateBlankPricing   sd   





"rC   c           
      C   s   t d|  d}|rt d| d|  d}dd | D }|D ](}|| }tt|d}t||td}|	|j
|d	d
 ||v rJ||= q"|D ]	}	|	|	  qM| S )NblankProducts/z/inventoryPricingsenterprises//blankProducts/c                 S   s   i | ]}|j | qS rA   )r   to_dict).0docrA   rA   rB   
<dictcomp>J   s    zsavePricing.<locals>.<dictcomp>r)   r   r(   	updatedAtTmerge)r   
collectionstreamr*   valuesr1   maxr.   r   documentsetdelete)
r   pricingsr   refexisting_variantsblankVariantIdpricesr(   r   
variant_idrA   rA   rB   r4   E   s   r4   c                 C   sf   |rt d| d|  d| }| }|js$t d|  d|  }d}|jr1| dd}|S )NrE   rF   /inventoryPricings/rD   r   r(   )r   rS   r*   existsrG   )r   rY   r   rW   r(   rA   rA   rB   getBlankVariantPriceV   s     r^   c              
   C   sz   t j| d}z|j||ddd}t|d d d d d d	 }|W S  ty< } ztd
|  W Y d}~dS d}~ww )a1  
  Calculates the driving distance between two zip codes using the Google Maps Distance Matrix API.

  Args:
    api_key: Your Google Maps API key.
    origin_zip: The origin zip code.
    destination_zip: The destination zip code.

  Returns:
    The distance in meters, or None if there was an error.
  )keydrivingmetric)originsdestinationsmodeunitsrowsr   elementsdistancevalueError: N)
googlemapsClientdistance_matrixr3   r2   )api_key
origin_zipdestination_zipgmapsrm   distance_metersr@   rA   rA   rB   calculate_distancea   s    rs   c                 C   s>   d}t d}|D ]}t| ||}|r||k r|}|}q||fS )a  
  Finds the closest zip code from a list to a given origin zip code.

  Args:
    api_key: Your Google Maps API key.
    origin_zip: The origin zip code.
    zip_code_list: A list of zip codes to compare.

  Returns:
    The closest zip code and the distance to it in meters.
  Ninf)r/   rs   )rn   ro   zip_code_listclosest_zipclosest_distancezip_coderh   rA   rA   rB   find_closest_zip_code~   s   ry   )HTTPBasicAuthc              
   C   sr   dd | }ztj|t| |d}|  | W S  tjjy8 } ztd|  g W  Y d }~S d }~ww )Nz)https://api.ssactivewear.com/v2/products/,)authrj   )	joinrequestsr*   rz   raise_for_statusjson
exceptionsRequestExceptionr3   )api_usernameapi_passwordblankVariantIdsurlresponser@   rA   rA   rB   getBlankVariantsPricesFromSS   s   
r   c                 C   sR  t d|  d }dd |D }g }tdt|dD ]#}|||d  }ttddtdd	|}|r?|| qt  }d}t	|D ]@\}	}
|
d
}t
|
dd}|jt d|  d| t||tddd |d7 }|dkr|  td|	  t  }d}qJ|dkr|  td| d td|  dt|  d S )NrD   z/blankVariantsc                 S   s   g | ]}|j qS rA   r   )rH   blankVariantrA   rA   rB   
<listcomp>   s    z#updateSSPricing.<locals>.<listcomp>r   d   SSr   r   sku	salePricer\   rK   TrM      i  zCommitted 500 records at index zCommitted final z recordszPricing Updated => z	, Total: )r   rO   r*   rangelenr   r   extendbatch	enumerater/   rT   rS   r.   r   commitr3   )r   blankVariantsr   all_pricingsi	batch_idsrV   writerop_countidxr   rY   r(   rA   rA   rB   updateSSPricing   sD   

r   c                 C   s   t  }|D ]H}tddd| }|jrM| }|di }|rMt|}|d}|rM|t	t
jjd dk rMtt|d}td| dd i qd S )	NblankProductsr   z==updatesrL   )tzinfor   rD   )r
   r   rS   wherer*   r]   rG   r.   r   nowpytzutcreplacerC   r0   )r5   blanksInUser   r6   pricingUpdaterL   rA   rA   rB   updateBlankProductsPricings   s   
r   )N)r   r   zeepr   configs.firebaser   r   functions.Suppliers.Authr   r   r   !functions.Suppliers.BlankProductsr	   r
   functions.Suppliers.Locationsr   rC   r4   r^   rk   rs   ry   r~   requests.authrz   r   r   r   rA   rA   rA   rB   <module>   s$    8
'
.