-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_orders.py
More file actions
243 lines (201 loc) · 9.68 KB
/
manage_orders.py
File metadata and controls
243 lines (201 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from tkinter import ttk, messagebox
import tkinter as tk
from common import get_db_connection
from payment_interface import payment_interface
# Funzione per recuperare i prodotti dal database in base alla categoria
def get_products_by_category(category):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("""
SELECT nome, prezzo FROM Pietanze
WHERE categoria = ?
""", (category,))
products = cursor.fetchall()
conn.close()
return products
# Funzione per recuperare i piatti in base alla categoria
def get_dishes_by_category(category):
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT nome FROM Pietanze WHERE categoria = ?", (category,))
dishes = [row[0] for row in cursor.fetchall()]
conn.close()
return dishes
# Funzione per la gestione degli ordini del cameriere
def manage_orders(clear_main_frame, main_frame, table_id, show_home):
"""Gestione degli ordini per un tavolo specifico."""
def update_dishes(event=None):
"""Aggiorna i piatti nel menu a tendina in base alla categoria selezionata."""
category = category_var.get()
if category:
# Recupera i prodotti per la categoria selezionata
products = get_products_by_category(category)
dish_names = [product[0] for product in products]
# Debug: per mostrare i piatti recuperati nel terminale
print(f"Categoria selezionata: {category}")
print(f"Piatti trovati: {dish_names}")
# Aggiorna il menu a tendina per i piatti
dish_menu["values"] = dish_names
if dish_names:
dish_menu.set(dish_names[0])
else:
dish_menu.set("") # Nessun piatto trovato
else:
# Se nessuna categoria è selezionata, svuota il menu
dish_menu["values"] = []
dish_menu.set("")
def update_payment_button():
"""Abilita il pulsante di pagamento solo se tutte le tabelle sono vuote."""
if not new_orders_list.get_children() and not ready_orders_list.get_children():
payment_button.config(state="normal")
else:
payment_button.config(state="disabled")
# Gestione degli ordini per un tavolo specifico
def update_order_lists():
"""Aggiorna le liste degli ordini."""
new_orders_list.delete(*new_orders_list.get_children())
ready_orders_list.delete(*ready_orders_list.get_children())
delivered_orders_list.delete(*delivered_orders_list.get_children())
# Recupera gli ordini da preparare
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("""
SELECT p.nome, d.quantita, d.prezzo_totale
FROM DettagliOrdine d
JOIN Ordini o ON d.ordine_id = o.id
JOIN Pietanze p ON d.pietanza_id = p.id
WHERE o.tavolo_id = ? AND o.stato = 'in preparazione'
""", (table_id,))
for row in cursor.fetchall():
new_orders_list.insert("", "end", values=row)
# Recupera gli ordini pronti
cursor.execute("""
SELECT p.nome, d.quantita, d.prezzo_totale
FROM DettagliOrdine d
JOIN Ordini o ON d.ordine_id = o.id
JOIN Pietanze p ON d.pietanza_id = p.id
WHERE o.tavolo_id = ? AND o.stato = 'pronto'
""", (table_id,))
for row in cursor.fetchall():
ready_orders_list.insert("", "end", values=row)
# Recupera gli ordini consegnati
cursor.execute("""
SELECT p.nome, d.quantita, d.prezzo_totale
FROM DettagliOrdine d
JOIN Ordini o ON d.ordine_id = o.id
JOIN Pietanze p ON d.pietanza_id = p.id
WHERE o.tavolo_id = ? AND o.stato = 'consegnato'
""", (table_id,))
for row in cursor.fetchall():
delivered_orders_list.insert("", "end", values=row)
# Calcola il totale
cursor.execute("""
SELECT SUM(d.prezzo_totale)
FROM DettagliOrdine d
JOIN Ordini o ON d.ordine_id = o.id
WHERE o.tavolo_id = ?
""", (table_id,))
total = cursor.fetchone()[0] or 0.0
total_label_var.set(f"Total: €{total:.2f}")
conn.close()
# Aggiorna lo stato del pulsante di pagamento
update_payment_button()
def add_order():
category = category_var.get()
dish = dish_var.get()
quantity = quantity_var.get()
if not category or not dish or not quantity.isdigit() or int(quantity) <= 0:
messagebox.showerror("Error", "Invalid input. Please fill all fields correctly.")
return
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT id, prezzo, tempo_preparazione FROM Pietanze WHERE nome = ?", (dish,))
dish_data = cursor.fetchone()
if not dish_data:
conn.close()
messagebox.showerror("Error", "Dish not found.")
return
dish_id, price, prep_time = dish_data
total_price = price * int(quantity)
# Tempo di preparazione totale
safety_margin = prep_time * 0.20 # 20% margine di sicurezza
ora_out = f"DATETIME('now', '+{prep_time + int(safety_margin)} minutes')"
stato = 'pronto' if category in ["bevanda", "dolce"] else 'in preparazione'
# Inserimento ordine
cursor.execute("""
INSERT INTO Ordini (tavolo_id, stato, totale)
VALUES (?, ?, ?)
""", (table_id, stato, total_price))
order_id = cursor.lastrowid
cursor.execute(f"""
INSERT INTO DettagliOrdine (ordine_id, pietanza_id, quantita, prezzo_totale, ora_out)
VALUES (?, ?, ?, ?, {ora_out})
""", (order_id, dish_id, quantity, total_price))
conn.commit()
conn.close()
update_order_lists()
messagebox.showinfo("Success", f"Order added: {dish} x{quantity} (€{total_price:.2f})")
def deliver_ready_orders():
"""Segna i piatti pronti come consegnati."""
conn = get_db_connection()
cursor = conn.cursor()
# Aggiorna lo stato degli ordini pronti a "consegnato"
cursor.execute("""
UPDATE Ordini
SET stato = 'consegnato'
WHERE tavolo_id = ? AND stato = 'pronto'
""", (table_id,))
conn.commit()
conn.close()
update_order_lists()
messagebox.showinfo("Success", "All ready orders delivered to the table.")
clear_main_frame()
header_frame = tk.Frame(main_frame)
header_frame.pack(fill="x", pady=10)
tk.Label(header_frame, text=f"Manage Orders for Table {table_id}", font=("Arial", 16, "bold")).pack(side="left", padx=5)
total_label_var = tk.StringVar(value="Total: €0.00")
tk.Label(header_frame, textvariable=total_label_var, font=("Arial", 14)).pack(side="right", padx=10)
# Contenitore per Category, Dish e Quantity
input_frame = tk.Frame(main_frame)
input_frame.pack(pady=10)
tk.Label(input_frame, text="Category").pack(side="left", padx=5)
category_var = tk.StringVar()
category_menu = ttk.Combobox(input_frame, textvariable=category_var, values=["antipasto", "primo", "secondo", "contorno", "dolce", "bevanda"])
category_menu.pack(side="left", padx=5)
category_menu.bind("<<ComboboxSelected>>", lambda e: update_dishes())
tk.Label(input_frame, text="Dish").pack(side="left", padx=5)
dish_var = tk.StringVar()
dish_menu = ttk.Combobox(input_frame, textvariable=dish_var)
dish_menu.pack(side="left", padx=5)
tk.Label(input_frame, text="Quantity").pack(side="left", padx=5)
quantity_var = tk.StringVar()
tk.Entry(input_frame, textvariable=quantity_var).pack(side="left", padx=5)
# Tabelle ordini
tk.Label(main_frame, text="New Orders").pack(pady=2)
new_orders_list = ttk.Treeview(main_frame, columns=("Dish", "Quantity", "Total Price"), show="headings", height=5)
new_orders_list.heading("Dish", text="Dish")
new_orders_list.heading("Quantity", text="Quantity")
new_orders_list.heading("Total Price", text="Total Price")
new_orders_list.pack(padx=5, pady=2, fill="both", expand=True)
tk.Label(main_frame, text="Ready Orders").pack(pady=2)
ready_orders_list = ttk.Treeview(main_frame, columns=("Dish", "Quantity", "Total Price"), show="headings", height=5)
ready_orders_list.heading("Dish", text="Dish")
ready_orders_list.heading("Quantity", text="Quantity")
ready_orders_list.heading("Total Price", text="Total Price")
ready_orders_list.pack(padx=5, pady=2, fill="both", expand=True)
tk.Label(main_frame, text="Delivered Orders").pack(pady=2)
delivered_orders_list = ttk.Treeview(main_frame, columns=("Dish", "Quantity", "Total Price"), show="headings", height=5)
delivered_orders_list.heading("Dish", text="Dish")
delivered_orders_list.heading("Quantity", text="Quantity")
delivered_orders_list.heading("Total Price", text="Total Price")
delivered_orders_list.pack(padx=5, pady=2, fill="both", expand=True)
# Contenitore per i pulsanti
button_frame = tk.Frame(main_frame)
button_frame.pack(pady=10)
tk.Button(button_frame, text="Add Order", command=add_order).pack(side="left", padx=5)
tk.Button(button_frame, text="Deliver Ready Orders", command=deliver_ready_orders).pack(side="left", padx=5)
tk.Button(button_frame, text="Back to Home", command=show_home).pack(side="left", padx=5)
# Definizione del pulsante di pagamento
payment_button = tk.Button(button_frame, text="Process Payment", command=lambda: payment_interface(clear_main_frame, main_frame, show_home, table_id), state="disabled")
payment_button.pack(side="left", padx=5)
update_order_lists()