o
    Q?h                     @   s   d dl Z d dlmZ d dl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mZmZmZmZmZ d dlmZ d dlmZ d d	lmZ d
d ZdS )    N)datetime)SERVER_TIMESTAMP)getManualProductMapping)convertOrderconvertOrderItemconvertShippingAddress	saveOrder)getColorImagesgetManualProductByIdgetManualProductVariantgetProductByIdgetProductsMappinggetProductVariant)	API_Error)
getAddress)getShopByIdc           !         s  |  d}| d}| d}|  d}|  d}d }|d| kr%t|}t|}|  dg }|s5tdS tjj | d	d
}	|	rF|	j|d< | dsR| d	|d< |sXtdS t }
t	|

 }|tdd  ttdt| }g }d}d}d}|D ]K}| d| dt	| dd}}}|s|s|std  S t|}|st|}d}|stddt|d|st||nt||}|stddt||d|st||nt||\}}|s|stddt||d| ddr| dd| }nd}||7 }d }| dr| dd }d  | dr&tdd  | dD d  t|| d!| d"d#}|r:|d }d$d% | dD d&d% | dD  } |v r` fd'd%| dD }n
d(d% | dD }|rq||d< td9i d|d|d)dd*| d*d+|d,| | d|d|d|d-td.td|r|n|d d/|d0| d0d1|d2|d|d3|d4|}|| |d7 }d}qt|d|r|n|d |r| d0nd5||tt|||t d6}t||} td7| | d8S ):NcurrentUseruidenterpriseId	addressIdshopId0itemszMissing required address fieldscountry)namecountryCodez%Order can't be created with no items.r   d   F	productId	variantIdquantity   zMissing required item fields.TzProduct not foundi  )r   zProduct variant not found.)r   r   zGProduct mapping not found, map the product/variant to use this variant.priceimages
propertiesc                 s   s2    | ]}t |d d dkr|dV  qdS )r    colorvalueN)strgetlower).0v r,   X/var/www/html/riverr-enterprise-integrations-main/functions/ManualOrders/ManualOrders.py	<genexpr>A   s   0 zmanualOrder.<locals>.<genexpr>blankProductIdblankVariantId)r   r/   r0   c                 S      g | ]}| d qS r%   r(   r*   imgr,   r,   r-   
<listcomp>D       zmanualOrder.<locals>.<listcomp>c                 S   r1   )sizer3   r4   r,   r,   r-   r6   D   r7   c                    s<   g | ]} | d | dfv r| d s| dr|qS )r%   r8   r3   r*   imager2   r,   r-   r6   F   s    6c                 S   s(   g | ]}d | d| dfv r|qS )defaultr%   r8   r3   r9   r,   r,   r-   r6   J   s    "
platformIdplatformProductIdplatformOrderIdplatformOrderItemId	createdAt	updatedAtproductr   variantr:   productMappingvariantMappingManual)r   r<   r   shopNamer   shippingAddressr@   rA   
grandTotalr>   
totalPricemetadatazOrder successfully added. )messageorderIdr,   )r(   r   r   r   	pycountry	countriesalpha_2r   nowint	timestamprandomrandintlenranger   r
   dictr   r   r   r   nextr	   r   r   appendr   r   )!datar   r   r   r   r   shopaddressr   r   rQ   r@   r>   
orderItemstotalmanualProductcountitemr   r   r   rB   rC   rD   rE   thisItemPricer:   mockupsprintImageColorsprintImages	orderItemorderrM   r,   r2   r-   manualOrder   s   




&
(&(


	



ri   )rT   r   rN   configs.firebaser   functions.ManualProductsr   functions.Ordersr   r   r   r   functions.Productsr	   r
   r   r   r   r   functions.Responser   functions.Shipments.Convertr   functions.Shopsr   ri   r,   r,   r,   r-   <module>   s    