396 lines
12 KiB
Python
396 lines
12 KiB
Python
import datetime
|
|
import queue
|
|
import threading
|
|
import time
|
|
import tkinter as tk
|
|
from tkinter import messagebox
|
|
from datetime import datetime
|
|
import requests
|
|
|
|
|
|
class OrderSenderApp(tk.Tk):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.title("Odesílač zásilek na stánek")
|
|
self.geometry("960x640") # Rozšířeno pro pravý katalog
|
|
self.resizable(False, False)
|
|
self.configure(bg="#0f172a")
|
|
|
|
# Paměť pro sledování odeslaných zásilek
|
|
self.sent_orders = (
|
|
{}
|
|
) # { order_id: {'badge': widget, 'status': 'čaká na potvrzení'} }
|
|
|
|
# Hlavička
|
|
header = tk.Frame(self, bg="#1e293b")
|
|
header.pack(fill="x", ipady=12)
|
|
tk.Label(
|
|
header,
|
|
text="📦 ODESLÁNÍ ZÁSILKY & KATALOG ODESLANÝCH",
|
|
font=("Segoe UI", 14, "bold"),
|
|
fg="#38bdf8",
|
|
bg="#1e293b",
|
|
).pack()
|
|
|
|
# Hlavní rozdělený kontejner (Vlevo formulář, vpravo katalog)
|
|
main_container = tk.Frame(self, bg="#0f172a")
|
|
main_container.pack(fill="both", expand=True, padx=20, pady=15)
|
|
|
|
# ==========================================================================
|
|
# LEVÝ PANEL - FORMULÁŘ
|
|
# ==========================================================================
|
|
form = tk.Frame(main_container, bg="#0f172a", width=420)
|
|
form.pack(side="left", fill="both", padx=(0, 15))
|
|
|
|
tk.Label(
|
|
form,
|
|
text="NOVÁ OBJEDNÁVKA",
|
|
font=("Segoe UI", 11, "bold"),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
).pack(anchor="w", pady=(0, 5))
|
|
|
|
self.create_label(form, "Cloudflare URL Adresa serveru:")
|
|
self.url_entry = self.create_entry(form)
|
|
self.url_entry.insert(
|
|
0, "https://protein-floyd-trance-affiliates.trycloudflare.com"
|
|
)
|
|
|
|
self.create_label(form, "Jméno a příjmení zákazníka:")
|
|
self.customer_entry = self.create_entry(form)
|
|
self.customer_entry.insert(0, "Jan Novák")
|
|
|
|
self.create_label(form, "Název produktu / zboží:")
|
|
self.product_entry = self.create_entry(form)
|
|
self.product_entry.insert(0, "Párek v rohlíku + Kofola")
|
|
|
|
row_frame = tk.Frame(form, bg="#0f172a")
|
|
row_frame.pack(fill="x", pady=5)
|
|
|
|
f1 = tk.Frame(row_frame, bg="#0f172a")
|
|
f1.pack(side="left", fill="x", expand=True, padx=(0, 5))
|
|
tk.Label(
|
|
f1,
|
|
text="Počet kusů:",
|
|
font=("Segoe UI", 10, "bold"),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
anchor="w",
|
|
).pack(fill="x")
|
|
self.qty_entry = tk.Entry(
|
|
f1,
|
|
font=("Segoe UI", 11),
|
|
bg="#1e293b",
|
|
fg="#ffffff",
|
|
insertbackground="white",
|
|
bd=1,
|
|
relief="solid",
|
|
)
|
|
self.qty_entry.pack(fill="x", ipady=4)
|
|
self.qty_entry.insert(0, "2")
|
|
|
|
f2 = tk.Frame(row_frame, bg="#0f172a")
|
|
f2.pack(side="left", fill="x", expand=True, padx=(5, 0))
|
|
tk.Label(
|
|
f2,
|
|
text="Cena za ks (Kč):",
|
|
font=("Segoe UI", 10, "bold"),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
anchor="w",
|
|
).pack(fill="x")
|
|
self.price_entry = tk.Entry(
|
|
f2,
|
|
font=("Segoe UI", 11),
|
|
bg="#1e293b",
|
|
fg="#ffffff",
|
|
insertbackground="white",
|
|
bd=1,
|
|
relief="solid",
|
|
)
|
|
self.price_entry.pack(fill="x", ipady=4)
|
|
self.price_entry.insert(0, "45")
|
|
|
|
self.create_label(form, "Částka dobírky v Kč (0 = zaplaceno předem):")
|
|
self.dobirka_entry = self.create_entry(form)
|
|
self.dobirka_entry.insert(0, "0")
|
|
|
|
btn = tk.Button(
|
|
form,
|
|
text="🚀 ODESLAT ZÁSILKU NA STÁNEK",
|
|
font=("Segoe UI", 11, "bold"),
|
|
bg="#10b981",
|
|
fg="#ffffff",
|
|
activebackground="#059669",
|
|
activeforeground="#ffffff",
|
|
bd=0,
|
|
cursor="hand2",
|
|
command=self.send_order,
|
|
)
|
|
btn.pack(fill="x", pady=(15, 8), ipady=10)
|
|
|
|
self.status_lbl = tk.Label(
|
|
form,
|
|
text="Přípraveno k odeslání",
|
|
font=("Segoe UI", 9),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
)
|
|
self.status_lbl.pack()
|
|
|
|
# ==========================================================================
|
|
# PRAVÝ PANEL - KATALOG ODESLANÝCH ZÁSILEK
|
|
# ==========================================================================
|
|
catalog_panel = tk.Frame(
|
|
main_container,
|
|
bg="#1e293b",
|
|
highlightbackground="#334155",
|
|
highlightthickness=1,
|
|
)
|
|
catalog_panel.pack(side="right", fill="both", expand=True)
|
|
|
|
tk.Label(
|
|
catalog_panel,
|
|
text="📋 ODESLANÉ ZÁSILKY & STAV POTVRZENÍ",
|
|
font=("Segoe UI", 11, "bold"),
|
|
fg="#38bdf8",
|
|
bg="#1e293b",
|
|
).pack(anchor="w", padx=15, pady=(15, 10))
|
|
|
|
self.catalog_scroll = tk.Frame(catalog_panel, bg="#1e293b")
|
|
self.catalog_scroll.pack(fill="both", expand=True, padx=10, pady=5)
|
|
|
|
self.empty_catalog_lbl = tk.Label(
|
|
self.catalog_scroll,
|
|
text="Zatím jste neodeslali žádnou zásilku.",
|
|
font=("Segoe UI", 11, "italic"),
|
|
fg="#64748b",
|
|
bg="#1e293b",
|
|
)
|
|
self.empty_catalog_lbl.pack(pady=50)
|
|
|
|
# Spuštění automatické kontroly stavů na pozadí
|
|
self.poll_order_statuses()
|
|
|
|
def create_label(self, parent, text):
|
|
lbl = tk.Label(
|
|
parent,
|
|
text=text,
|
|
font=("Segoe UI", 10, "bold"),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
anchor="w",
|
|
)
|
|
lbl.pack(fill="x", pady=(6, 2))
|
|
return lbl
|
|
|
|
def create_entry(self, parent):
|
|
entry = tk.Entry(
|
|
parent,
|
|
font=("Segoe UI", 10),
|
|
bg="#1e293b",
|
|
fg="#ffffff",
|
|
insertbackground="white",
|
|
bd=1,
|
|
relief="solid",
|
|
)
|
|
entry.pack(fill="x", ipady=4)
|
|
return entry
|
|
|
|
# ==========================================================================
|
|
# LOGIKA ODESÍLÁNÍ
|
|
# ==========================================================================
|
|
def send_order(self):
|
|
base_url = self.url_entry.get().strip().rstrip("/")
|
|
if not base_url.startswith("http"):
|
|
base_url = "https://" + base_url
|
|
|
|
target_url = f"{base_url}/api/order"
|
|
|
|
try:
|
|
customer = self.customer_entry.get().strip()
|
|
product = self.product_entry.get().strip()
|
|
quantity = int(self.qty_entry.get().strip())
|
|
price = float(self.price_entry.get().strip())
|
|
dobirka = float(self.dobirka_entry.get().strip())
|
|
time_str = datetime.now().strftime("%H:%M:%S")
|
|
|
|
payload = {
|
|
"customer": customer,
|
|
"product": product,
|
|
"quantity": quantity,
|
|
"price": price,
|
|
"dobirka": dobirka,
|
|
"time": time_str,
|
|
}
|
|
|
|
self.status_lbl.config(text="Odesílám...", fg="#eab308")
|
|
self.update()
|
|
|
|
resp = requests.post(target_url, json=payload, timeout=8)
|
|
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
order_id = str(data.get("order_id", "???"))
|
|
|
|
self.status_lbl.config(
|
|
text="✅ Objednávka doručena na stánek!", fg="#4ade80"
|
|
)
|
|
|
|
# Přidání položky do pravého katalogu
|
|
self.add_to_catalog(
|
|
order_id,
|
|
customer,
|
|
product,
|
|
quantity,
|
|
quantity * price,
|
|
dobirka,
|
|
time_str,
|
|
)
|
|
else:
|
|
self.status_lbl.config(
|
|
text=f"❌ Server vrátil chybu {resp.status_code}", fg="#f87171"
|
|
)
|
|
messagebox.showerror(
|
|
"Chyba serveru", f"Chybová odpověď:\n{resp.text[:200]}"
|
|
)
|
|
|
|
except ValueError:
|
|
messagebox.showwarning(
|
|
"Chybná data", "Zkontrolujte zadaná čísla v polích Počet, Cena a Dobírka."
|
|
)
|
|
self.status_lbl.config(text="❌ Chyba v zadání údajů", fg="#f87171")
|
|
except Exception as e:
|
|
self.status_lbl.config(
|
|
text="❌ Nepodařilo se připojit k serveru", fg="#f87171"
|
|
)
|
|
messagebox.showerror(
|
|
"Chyba připojení",
|
|
"Ujistěte se, že běží server na stánku a Cloudflare"
|
|
f" tunel!\n\nDetail: {e}",
|
|
)
|
|
|
|
# ==========================================================================
|
|
# VKLÁDÁNÍ DO KATALOGU A KONTROLA STAVU
|
|
# ==========================================================================
|
|
def add_to_catalog(
|
|
self, order_id, customer, product, quantity, total_price, dobirka, time_str
|
|
):
|
|
if self.empty_catalog_lbl:
|
|
self.empty_catalog_lbl.destroy()
|
|
self.empty_catalog_lbl = None
|
|
|
|
row = tk.Frame(
|
|
self.catalog_scroll,
|
|
bg="#0f172a",
|
|
highlightbackground="#334155",
|
|
highlightthickness=1,
|
|
)
|
|
row.pack(fill="x", pady=4, ipady=5)
|
|
|
|
left_info = tk.Frame(row, bg="#0f172a")
|
|
left_info.pack(side="left", padx=10)
|
|
|
|
tk.Label(
|
|
left_info,
|
|
text=f"#{order_id}",
|
|
font=("Segoe UI", 11, "bold"),
|
|
fg="#00f2fe",
|
|
bg="#0f172a",
|
|
).pack(anchor="w")
|
|
tk.Label(
|
|
left_info,
|
|
text=time_str,
|
|
font=("Segoe UI", 9),
|
|
fg="#94a3b8",
|
|
bg="#0f172a",
|
|
).pack(anchor="w")
|
|
|
|
mid_info = tk.Frame(row, bg="#0f172a")
|
|
mid_info.pack(side="left", padx=10, fill="x", expand=True)
|
|
|
|
tk.Label(
|
|
mid_info,
|
|
text=f"{customer} ({quantity}x {product})",
|
|
font=("Segoe UI", 10, "bold"),
|
|
fg="#f8fafc",
|
|
bg="#0f172a",
|
|
anchor="w",
|
|
).pack(fill="x")
|
|
|
|
sub_text = f"{total_price:.0f} Kč"
|
|
if dobirka > 0:
|
|
sub_text += f" | Dobírka {dobirka:.0f} Kč"
|
|
tk.Label(
|
|
mid_info,
|
|
text=sub_text,
|
|
font=("Segoe UI", 9),
|
|
fg="#cbd5e1",
|
|
bg="#0f172a",
|
|
anchor="w",
|
|
).pack(fill="x")
|
|
|
|
# Odznak stavu (výchozí: ČEKÁ)
|
|
badge = tk.Label(
|
|
row,
|
|
text="⏳ ČEKÁ",
|
|
font=("Segoe UI", 9, "bold"),
|
|
fg="#ffffff",
|
|
bg="#ef4444",
|
|
padx=8,
|
|
pady=3,
|
|
)
|
|
badge.pack(side="right", padx=10)
|
|
|
|
self.sent_orders[str(order_id)] = {
|
|
"badge": badge,
|
|
"status": "čaká na potvrzení",
|
|
}
|
|
|
|
def poll_order_statuses(self):
|
|
"""Pravidelně na pozadí zjišťuje, zda stánkový server nepotvrdil zásilky."""
|
|
base_url = self.url_entry.get().strip().rstrip("/")
|
|
|
|
if base_url:
|
|
if not base_url.startswith("http"):
|
|
base_url = "https://" + base_url
|
|
|
|
# Vyfiltrování zásilek, které ještě nejsou potvrzené
|
|
unconfirmed = [
|
|
oid
|
|
for oid, item in self.sent_orders.items()
|
|
if item["status"] != "potvrzeno"
|
|
]
|
|
|
|
if unconfirmed:
|
|
|
|
def check_async():
|
|
for oid in unconfirmed:
|
|
try:
|
|
res = requests.get(f"{base_url}/api/order/{oid}/status", timeout=3)
|
|
if res.status_code == 200:
|
|
st = res.json().get("status")
|
|
if st == "potvrzeno":
|
|
|
|
def apply(target_oid=oid):
|
|
self.sent_orders[target_oid]["status"] = "potvrzeno"
|
|
badge = self.sent_orders[target_oid]["badge"]
|
|
badge.config(
|
|
text="✅ POTVRZENO", bg="#10b981", fg="#ffffff"
|
|
)
|
|
|
|
self.after(0, apply)
|
|
except Exception:
|
|
pass
|
|
|
|
threading.Thread(target=check_async, daemon=True).start()
|
|
|
|
# Kontrola každé 2 sekundy
|
|
self.after(2000, self.poll_order_statuses)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = OrderSenderApp()
|
|
app.mainloop() |