You are on page 1of 3

import tkinter as tk

from tkinter import messagebox


import mysql.connector

# Connect to MySQL database


db = mysql.connector.connect(
host="localhost",
user="niharika",
password="social",
database="cafe"
)

cursor = db.cursor()

# Function to display menu


def display_menu():
cursor.execute("SELECT * FROM menu_items")
menu_items = cursor.fetchall()

for item in menu_items:


menu_listbox.insert(tk.END, f"{item[0]} - ${item[1]}")

# Function to add item to order


def add_to_order():
selected_item = menu_listbox.get(tk.ACTIVE)
order_listbox.insert(tk.END, selected_item)
calculate_total()

# Function to calculate total


def calculate_total():
total = sum(float(item.split('- $')[1]) for item in order_listbox.get(0,
tk.END))
total_label.config(text=f"Total: ${total}")

# Function to place order


def place_order():
order_items = order_listbox.get(0, tk.END)
total = float(total_label.cget("text").split("$")[1])

# Save order to database (order_id, total)


cursor.execute("INSERT INTO orders (total) VALUES (%s)", (total,))
order_id = cursor.lastrowid

for item in order_items:


item_name = item.split(' - $')[0]
cursor.execute("INSERT INTO order_items (order_id, item_name) VALUES (%s,
%s)", (order_id, item_name))

db.commit()

messagebox.showinfo("Order Placed", f"Order ID: {order_id}\nTotal: ${total}")


order_listbox.delete(0, tk.END)
total_label.config(text="Total: $0.0")

# Function to display order history


def display_order_history():
order_history_window = tk.Toplevel(root)
order_history_window.title("Order History")
cursor.execute("SELECT * FROM orders")
orders = cursor.fetchall()

for order in orders:


cursor.execute("SELECT item_name FROM order_items WHERE order_id=%s",
(order[0],))
items = cursor.fetchall()
item_names = ', '.join(item[0] for item in items)
order_history_listbox.insert(tk.END, f"Order ID: {order[0]}, Items:
{item_names}, Total: ${order[1]}")

# Function to display inventory


def display_inventory():
inventory_window = tk.Toplevel(root)
inventory_window.title("Inventory Management")

cursor.execute("SELECT * FROM inventory")


inventory_items = cursor.fetchall()

for item in inventory_items:


inventory_listbox.insert(tk.END, f"{item[0]} - Quantity: {item[1]}")

# Function to apply discount


def apply_discount():
discount = float(discount_entry.get())
total = float(total_label.cget("text").split("$")[1])
discounted_total = total * (1 - discount / 100)
total_label.config(text=f"Total (After {discount}% Discount): $
{discounted_total}")

# Main GUI
root = tk.Tk()
root.title("Cafe Management System")

menu_listbox = tk.Listbox(root, selectmode=tk.SINGLE)


menu_listbox.pack()

order_listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)


order_listbox.pack()

add_button = tk.Button(root, text="Add to Order", command=add_to_order)


add_button.pack()

total_label = tk.Label(root, text="Total: $0.0")


total_label.pack()

calculate_button = tk.Button(root, text="Calculate Total", command=calculate_total)


calculate_button.pack()

place_order_button = tk.Button(root, text="Place Order", command=place_order)


place_order_button.pack()

order_history_button = tk.Button(root, text="Order History",


command=display_order_history)
order_history_button.pack()

inventory_button = tk.Button(root, text="Inventory Management",


command=display_inventory)
inventory_button.pack()
discount_label = tk.Label(root, text="Discount (%):")
discount_label.pack()

discount_entry = tk.Entry(root)
discount_entry.pack()

apply_discount_button = tk.Button(root, text="Apply Discount",


command=apply_discount)
apply_discount_button.pack()

inventory_listbox = tk.Listbox(root, selectmode=tk.SINGLE)


inventory_listbox.pack()

display_menu()

root.mainloop()

You might also like