o
    Yxi                     @   s   d dl Z d dlmZ dededefddZdedefd	d
ZdedefddZdedefddZ	dedefddZ
dedefddZdedefddZdedefddZdedefddZdedefddZdefdd Zdedefd!d"ZdS )#    N)current_appmethodparamsreturnc              
   C   s&  t j}|dp	d}|dpd}|dpd}|dpd}|s%td|r)|s-td| ||||d}z
tj||d	d
}W nA tjjyX }	 ztd| d|	 |	d}	~	w tjjyp }	 ztd| d|	 |	d}	~	w tjj	y   td| dw |
  | }
|
di d|
S )u.   Base JSON-RPC style call to the e-računi API.ERACUNI_URL ERACUNI_USERNAMEERACUNI_PASSWORDERACUNI_TOKENuV   ERACUNI_URL is not configured. Go to Settings → e-Računi and save your credentials.uo   ERACUNI_USERNAME or ERACUNI_PASSWORD is not configured. Go to Settings → e-Računi and save your credentials.)r   
parametersusernamemd5passtoken   )jsontimeoutu/   SSL certificate error connecting to e-računi (z7). The server may have an outdated CA bundle. Details: Nu   Cannot connect to e-računi at z;. Check the URL and that the server can reach it. Details: u,   e-računi request timed out after 30s (URL: )responseresult)r   configgetRuntimeErrorrequestspost
exceptionsSSLErrorConnectionErrorTimeoutraise_for_statusr   )r   r   cfgurlr   passwordr   payloadrespedata r&   7/var/www/warehouse.soldered.com/app/services/eracuni.py_call   sZ   r(   	date_fromc                 C   >   t dd| i}t|tr|S t|tr|d|dg S g S )z`Fetch a list of sales orders from date_from (YYYY-MM-DD).

    Returns list of order dicts.
    SalesOrderListdateFromsalesOrdersordersr(   
isinstancelistdictr   )r)   r   r&   r&   r'   
get_orders4   s   

r3   statusc                 C   r*   )zMFetch all sales orders with a given status (e.g. 'Draft', 'PartialDelivery').r+   r4   r-   r.   r/   )r4   r   r&   r&   r'   get_orders_by_statusB   s   

r5   numberc                 C   s*   t d| dd}t|tr|d|S i S )z5Fetch a single order by number (operator='Web Shop').SalesOrderGetzWeb Shop)r6   operator
salesOrderr(   r0   r2   r   r6   r   r&   r&   r'   	get_orderL   s   
r<   c                 K   s   d| i}| | td|S )zNUpdate an order. Supported kwargs: status, buyerTaxNumber, vatTransactionType.r6   SalesOrderUpdate)updater(   )r6   kwargsr   r&   r&   r'   update_orderW   s   

r@   c                 C   s8   t dd| i}t|trt|d|ddS t|S )zDCreate an invoice from a sales order. Returns the documentID string.SalesOrderCreateInvoicer6   
documentIDidr   )r(   r0   r2   strr   r;   r&   r&   r'   create_invoice^   s   
rE   document_idc                 C   s(   t dd| i}t|tr|d|S i S )z%Fetch a sales invoice by document ID.SalesInvoiceGetrB   salesInvoicer:   )rF   r   r&   r&   r'   get_invoicef   s   
rI   c                 C   V   ddl }tdd| i}t|tr||S t|tr)|d|dd}||S dS )	z'Fetch the PDF bytes of a sales invoice.r   NSalesInvoiceGetPDFrB   pdfcontentr       base64r(   r0   rD   	b64decoder2   r   rF   rP   r   pdf_b64r&   r&   r'   get_invoice_pdfn   s   



rT   c                 C   rJ   )	z'Fetch the PDF bytes of a delivery note.r   NDeliveryNoteGetPDFrB   rL   rM   r   rN   rO   rR   r&   r&   r'   get_delivery_note_pdf{   s   



rV   product_codec                 C   s   zt dd| i}t|tr|r|d|W S W n	 ty    Y nw zt }|D ]}|d| kr5|  W S q'W i S  tyB   Y i S w )zFetch a single product's full raw API response by productCode.

    Tries ProductGet first; falls back to scanning ProductList if the API
    doesn't support single-item fetches.
    
ProductGetproductCodeproduct)r(   r0   r2   r   	Exceptionget_products)rW   r   all_productspr&   r&   r'   get_product_raw   s(   
r_   c                  C   s:   t di } t| tr| S t| tr| d| dg S g S )uH   Fetch all active products from e-računi. Returns list of product dicts.ProductListproductsitemsr/   )r   r&   r&   r'   r\      s   


r\   c                 C   s6   t dd| i}t|tr|S t|tr|dg S g S )z7Return list of delivery notes for a sales order number.DeliveryNoteListr6   deliveryNotesr/   r;   r&   r&   r'   get_delivery_note_list   s   

re   )r   flaskr   rD   r2   r(   r1   r3   r5   r<   r@   rE   rI   bytesrT   rV   r_   r\   re   r&   r&   r&   r'   <module>   s    /

