o
    Q?hL                     @   s   d dl Zd dlmZ d dlZd dlZd dlmZmZ d dlm	Z	m
Z
mZmZ d dlmZmZ d dlmZ d dlmZmZmZ d dlmZmZmZ d d	lmZ d
efddZddefddZdd ZdedefddZ dededefddZ!dS )    N)datetime)	Incrementdb)convertOrderconvertOrderItemconvertShippingAddress	saveOrder)
apiVersioncreateHeader)Address)Order	OrderItemfindOrderByPlatformOrderId)ShopgetShopsByPlatformIdgetUserShops)Paramsparamsc                 C   sp   t dd}g }| jd| jd}}|r|s| jr(t| jj}dd |D }|D ]}|t||| q*|S )N2Tstartendc                 S   s    g | ]}|j d kr|js|qS )r   )
platformIddisabled).0shop r   P/var/www/html/riverr-enterprise-integrations-main/V2/functions/Shopify/Orders.py
<listcomp>   s     z,verifyShopifyDailyOrders.<locals>.<listcomp>)r   argsgetcurrentUserr   uidappendverifyDailyOrders)r   shopsupdatesr   r   r   r   r   r   verifyShopifyDailyOrders   s   
r&   r   c              
      s,   j } j} j}t|}| dt d}tjtjd	 }t
tjtjd d 	 }	|rD|rDt
t|	 }	t
t|	 }ztj||t|	|ddddd	}
t|
j |
jd
krft|
j W d S g }d }|
jd
kr||
 dg  |
jd}|r|d}|r|r|dddd}tj||d}|jd
kr| }||dg  |jd}|d}nt|j W d S |r|sg }|rtjjdd}t|jdd  fdd|D dd}W d    n1 sw   Y  td j | |W S  ty } zt| W Y d }~d S d }~ww )Nz/admin/api/z/orders.json)tzi anypaid   )created_at_mincreated_at_maxstatusfinancial_statuslimit)headersr      ordersLinkz
rel="next"< z>; rel="next")r0      )max_workersc                 S   s   t |  S )N)addNotFoundOrder)r   r   r   r   <lambda>E   s    z#verifyDailyOrders.<locals>.<lambda>c                    s   g | ]} |fqS r   r   )r   orderr   r   r   r   E   s    z%verifyDailyOrders.<locals>.<listcomp>
   )	chunksizezAdded not found orders for shop)idaccessTokenurlr
   r	   r   nowpytzutc	isoformatfromtimestamp	timestampintrequestsr   dictprinttextstatus_codeextendjsonr0   endswithreplace
concurrentfuturesThreadPoolExecutorlistmap	Exception)r   r   r   shopIdr?   r@   r0   	ordersUrlnowTimestampyesterdayTimestampresr2   endpointr3   nextPagerequestdatanotFoundOrdersexecutorer   r;   r   r#      s\    






(r#   c                 C   s(   t | jt|d}|st|| S d S )Nr>   )r   r>   strr   addShopifyOrder)r   r:   foundr   r   r   r8   P   s   r8   r:   c                    s   di }g  di } d}g }|dv r$j d}|| |rGtd@i djdjdjdjd	t| d
| d| d| d| d| d| d d d| dd
dt	 dd ddkdt
 ddt
 dd dd  d!d" d#d$ d%i  d&i  d'd(d) d*d+ d#d, d-d.t d/d0jd1t|d2} d3g }d4d5 |D i i  jd6kr
|D ] }| d7d8kr| d! t| d9d:kr	|d; qr4 D ]"\}}	|	 d<}
|
r2td=|
   }|r2t|}q fd>d?|D }t||S d S )ANshipping_addresssource_name491917923293124555605	 - tiktokr!   r   rW   enterpriseIdshippingAddressnameaddress1address2cityprovincezipcountryemailphonecompany)
ro   rp   rq   rr   statert   ru   rv   rw   rx   	cancelledcancelled_atshippedfulfillment_status	fulfilled	createdAt
created_at	updatedAt
updated_atcurrencyCodecurrencyplatformOrderIdr>   
grandTotaltotal_priceshippingCosttotal_shipping_price_set
shop_moneyamountr   totalDiscounttotal_discounts
totalPricetotalTax	total_taxorderNumberorder_numbershopNamemetadatatags
line_itemsc                 S   s.   i | ]}t |d dd |dg D qS )r>   c                 S      i | ]}| d | dqS ro   valuer   )r   propr   r   r   
<dictcomp>       z.addShopifyOrder.<locals>.<dictcomp>.<dictcomp>
propertiesrc   r   r   itemr   r   r   r      s   . z#addShopifyOrder.<locals>.<dictcomp>lJ2eUFov5WyPlRm4geCrvendorMSP
product_id4683530141765zSkip the line_placementsDocumentIdshopifyCartPlacementsc                    s  g | ]}t d$i d jdjdjd|ddddjdtdd	td
dtdd|dd|dd|dd|dd|ddkd|dddd |dg D dt	|di dt	|di r t	|drt
dddnd d!t	|drȈ t	|drt
d$d" t	|dit	|di ni d#|dv qS )%r!   r   rm   platformOrderItemIdr>   r   rW   rz   r{   r   r   r   r   ro   platformProductIdr   pricequantityr|   r}   r~   	variantId
variant_idvariantPropertiesc                 S   r   r   r   r   pr   r   r   r      r   z.addShopifyOrder.<locals>.<listcomp>.<dictcomp>r   placementsDataproductMappingTF)printOnDemandignoredNvariantMappingblankVariantIdr   r   )r   r!   r   rm   r   r>   boolr   fromisoformatrc   rI   r   blankVariantsignoredItemsr:   propsr   variantMappingsr   r   r      sZ    

	





8^z#addShopifyOrder.<locals>.<listcomp>r   )r   ro   r"   r   r!   r   r>   rm   r   r   r   r   rc   rI   itemsr   
collectiondocumentto_dictconvertCustomizerItemsr   )r:   r   rf   rg   r   convertededOrderr   r   keyr   r   r_   convertedOrderItemsr   r   r   rd   T   s   







"
!
rd   r_   c                    s  t |d}tt| d }d||i d||i di }g }| D ]'\}}| D ]\}	}
|t d9|
d|	|
d|
dd	|i q4q,|d
}|d}||i dd| d}|dv rw|j d}g }| dg D ]}|dtfdd|D d  t fdd|D d fdd|D }t	|dkrfdd|D } dr dn d}t
d9i d|jddd|jd|dd| dd|jdt| d d!t| d"d#t| d$d|dd%|d&d'|d'd(|d(d)|d*d+kd,|dd-t d.d/|d0d1d2d3t |||d4d |D d5d |d6g D t |gd7d8}|| q|S ):NplacementDetailsr   colorvariantTypecanvasImagesr>   r@   ro   )r>   	placementr@   ro   variants
variationsrg   rh   rl   r   r   c                 3   s*    | ]}t |d t  kr|V  qdS )platformVariantIdNr   r   v)r   r   r   	<genexpr>A  s   ( z)convertCustomizerItems.<locals>.<genexpr>c                 3   s0    | ]}t |d t  d kr|V  qdS )varIdNr   r   )variantr   r   r   B  s   . c                    s$   g | ]}|    kr|qS r   r   r   i)r   	variationr   r   r   C  s   $ z*convertCustomizerItems.<locals>.<listcomp>c                    s   g | ]}|  d kr|qS )defaultr   r   )r   r   r   r   D  r   r   r!   r   r   rm   r   r   rW   rz   r{   r   r   r   r   r   r   r   r   r|   r}   r~   r   r   TFproductdecorationType)r   r   r   r   c                 S   s   g | ]}| d qS )r   r   r   r   r   r   r   a  s    c                 S   s$   g | ]}t |d |ddqS )ro   r   r   )rI   r   r   r   r   r   r   b  s    r   r   )blankProductIdr   images
placementsr   r   r   )rI   r   rc   rT   keysr   r"   ro   nextlenr   r!   rm   r>   r   r   r   )r:   r_   r   r   r   r   printImagesr   canvasImager   
printImager   r   rg   r   r   thisItemPrintImagesr   convertedOrderItemr   )r   r   r   r   r   r   %  s   





	






&r   )NN)"concurrent.futuresrQ   r   rB   rH   configs.firebaser   r   functions.Ordersr   r   r   r   functions.Shopify.Authr	   r
   V2.functions.Address.mainr   V2.functions.Orders.mainr   r   r   V2.functions.Shops.mainr   r   r   	V2.Paramsr   r&   r#   r8   rI   rd   r   r   r   r   r   <module>   s"   1 R