unas

api
mail@pastecode.io avatar
unknown
python
a year ago
2.7 kB
28
Indexable
Never
def get_product_status(token, cikkszam):
    payload = f"""<?xml version="1.0" encoding="UTF-8"?>
    <Params>
     <Sku>{cikkszam}</Sku>
     <ContentType>minimal</ContentType>
    </Params>"""
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.post(f"{API_URL}getProduct", data=payload, headers=headers)
    print("getProduct Response:", response.text)  # Debugging statement
    root = ET.fromstring(response.content)
    
    status_element = root.find("Product/StatusBase")
    if status_element is not None:
        return int(status_element.text)
    else:
        return None

def set_product_active(token, cikkszam):
    payload = f"""<?xml version="1.0" encoding="UTF-8"?>
    <Products>
        <Product>
            <Sku>{cikkszam}</Sku>
            <Statuses>
                <Status>
                    <Value>1</Value>
                </Status>
            </Statuses>
        </Product>
    </Products>"""
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.post(f"{API_URL}setProduct", data=payload, headers=headers)
    print("setProduct Response:", response.text)  # Debugging statement
    return response.status_code == 200

def on_import():
    cikkszam = cikk_input.get()
    vonalkodok = kod_input.get("1.0", "end-1c").split('\n')

    connection = connect_to_database()
    uploaded_count = insert_into_table(connection, cikkszam, vonalkodok)

    # Authenticate and get stock from UNAS API
    token = authenticate()
    stock_in_unas = get_stock(token, cikkszam)

    # Calculate the new stock quantity by adding the number of successfully inserted barcodes
    new_stock = stock_in_unas + uploaded_count

    # Update stock in UNAS API with the new quantity
    success = False
    retries = 3
    while retries > 0 and not success:
        success = update_stock(token, cikkszam, new_stock)
        if not success:
            retries -= 1

    if success:
        print("Checking product status...")  # Debugging statement
        # Check product status and activate if Inaktív
        product_status = get_product_status(token, cikkszam)
        print(f"Product status: {product_status}")  # Debugging statement
        if product_status == 0:  # Inaktív
            print("Attempting to set product active...")  # Debugging statement
            set_product_active(token, cikkszam)
    else:
        messagebox.showerror('API Error', f'Failed to update stock for Cikkszám: {cikkszam}. Items to add: {uploaded_count}')

    cikk_input.delete(0, tk.END)
    kod_input.delete("1.0", tk.END)
    cikk_input.focus_set()