Merge branch 'main' of https://git-eng.ukwms.ac.id/5803025001/Python-Menu
This commit is contained in:
commit
b1a5224a67
@ -6,35 +6,167 @@ class AdminMenu:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.frame = tk.Frame(root)
|
||||
self.frame.pack()
|
||||
self.frame.pack(fill="both", expand=True)
|
||||
|
||||
# =====================
|
||||
# JUDUL
|
||||
# =====================
|
||||
tk.Label(
|
||||
self.frame,
|
||||
text="ADMIN - KELOLA MENU",
|
||||
font=("Arial", 18, "bold")
|
||||
).pack(pady=10)
|
||||
).pack(pady=15)
|
||||
|
||||
self.nama = tk.Entry(self.frame)
|
||||
self.harga = tk.Entry(self.frame)
|
||||
self.gambar = tk.Entry(self.frame)
|
||||
# =====================
|
||||
# FORM INPUT (RAPI)
|
||||
# =====================
|
||||
form = tk.Frame(self.frame)
|
||||
form.pack(pady=10)
|
||||
|
||||
for label, entry in [
|
||||
("Nama Menu", self.nama),
|
||||
("Harga", self.harga),
|
||||
("Path Gambar", self.gambar)
|
||||
]:
|
||||
tk.Label(self.frame, text=label).pack()
|
||||
entry.pack()
|
||||
tk.Label(form, text="Nama Menu", width=15, anchor="w")\
|
||||
.grid(row=0, column=0, pady=5)
|
||||
self.nama = tk.Entry(form, width=30)
|
||||
self.nama.grid(row=0, column=1)
|
||||
|
||||
tk.Button(self.frame, text="Tambah Menu", command=self.tambah).pack(pady=5)
|
||||
tk.Label(form, text="Harga", width=15, anchor="w")\
|
||||
.grid(row=1, column=0, pady=5)
|
||||
self.harga = tk.Entry(form, width=30)
|
||||
self.harga.grid(row=1, column=1)
|
||||
|
||||
tk.Label(form, text="Path Gambar", width=15, anchor="w")\
|
||||
.grid(row=2, column=0, pady=5)
|
||||
self.gambar = tk.Entry(form, width=30)
|
||||
self.gambar.grid(row=2, column=1)
|
||||
|
||||
# =====================
|
||||
# BUTTON CRUD
|
||||
# =====================
|
||||
btn_frame = tk.Frame(self.frame)
|
||||
btn_frame.pack(pady=10)
|
||||
|
||||
tk.Button(btn_frame, text="Tambah", bg="#d1e7dd",
|
||||
width=10, command=self.tambah).grid(row=0, column=0, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Update", bg="#cfe2ff",
|
||||
width=10, command=self.update).grid(row=0, column=1, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Hapus", bg="#f8d7da",
|
||||
width=10, command=self.hapus).grid(row=0, column=2, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Refresh",
|
||||
width=10, command=self.load_menu).grid(row=0, column=3, padx=5)
|
||||
|
||||
tk.Button(btn_frame, text="Logout",
|
||||
width=10, command=self.logout).grid(row=0, column=4, padx=5)
|
||||
|
||||
# =====================
|
||||
# LIST MENU
|
||||
# =====================
|
||||
tk.Label(self.frame, text="Daftar Menu", font=("Arial", 12, "bold"))\
|
||||
.pack(pady=5)
|
||||
|
||||
self.listbox = tk.Listbox(self.frame, width=60, height=10)
|
||||
self.listbox.pack(pady=5)
|
||||
self.listbox.bind("<<ListboxSelect>>", self.pilih_menu)
|
||||
|
||||
self.menu_data = []
|
||||
self.load_menu()
|
||||
|
||||
# =====================
|
||||
# DATABASE FUNCTION
|
||||
# =====================
|
||||
def load_menu(self):
|
||||
self.listbox.delete(0, tk.END)
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute("SELECT * FROM menu")
|
||||
self.menu_data = cur.fetchall()
|
||||
db.close()
|
||||
|
||||
for menu in self.menu_data:
|
||||
self.listbox.insert(
|
||||
tk.END,
|
||||
f"{menu[0]} | {menu[1]} | Rp {menu[2]:,.0f}"
|
||||
)
|
||||
|
||||
def pilih_menu(self, event):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
return
|
||||
|
||||
menu = self.menu_data[idx[0]]
|
||||
|
||||
self.clear_form()
|
||||
self.nama.insert(0, menu[1])
|
||||
self.harga.insert(0, menu[2])
|
||||
self.gambar.insert(0, menu[3])
|
||||
|
||||
# =====================
|
||||
# CRUD
|
||||
# =====================
|
||||
def tambah(self):
|
||||
if not self.nama.get() or not self.harga.get():
|
||||
messagebox.showwarning("Error", "Nama dan Harga wajib diisi")
|
||||
return
|
||||
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
"INSERT INTO menu(nama,harga,gambar) VALUES (?,?,?)",
|
||||
(self.nama.get(), self.harga.get(), self.gambar.get())
|
||||
(self.nama.get(), float(self.harga.get()), self.gambar.get())
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
messagebox.showinfo("Sukses", "Menu berhasil ditambahkan")
|
||||
self.load_menu()
|
||||
self.clear_form()
|
||||
|
||||
def update(self):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
messagebox.showwarning("Pilih", "Pilih menu yang ingin diupdate")
|
||||
return
|
||||
|
||||
menu_id = self.menu_data[idx[0]][0]
|
||||
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
"UPDATE menu SET nama=?, harga=?, gambar=? WHERE id=?",
|
||||
(self.nama.get(), float(self.harga.get()), self.gambar.get(), menu_id)
|
||||
)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
messagebox.showinfo("Sukses", "Menu berhasil diupdate")
|
||||
self.load_menu()
|
||||
|
||||
def hapus(self):
|
||||
idx = self.listbox.curselection()
|
||||
if not idx:
|
||||
messagebox.showwarning("Pilih", "Pilih menu yang ingin dihapus")
|
||||
return
|
||||
|
||||
menu_id = self.menu_data[idx[0]][0]
|
||||
|
||||
if messagebox.askyesno("Konfirmasi", "Yakin hapus menu ini?"):
|
||||
db = connect()
|
||||
cur = db.cursor()
|
||||
cur.execute("DELETE FROM menu WHERE id=?", (menu_id,))
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
messagebox.showinfo("Sukses", "Menu berhasil dihapus")
|
||||
self.load_menu()
|
||||
self.clear_form()
|
||||
|
||||
def clear_form(self):
|
||||
self.nama.delete(0, tk.END)
|
||||
self.harga.delete(0, tk.END)
|
||||
self.gambar.delete(0, tk.END)
|
||||
|
||||
def logout(self):
|
||||
self.frame.destroy()
|
||||
from main import LoginScreen
|
||||
LoginScreen(self.root)
|
||||
|
||||
@ -27,6 +27,12 @@ def setup_database():
|
||||
)
|
||||
""")
|
||||
|
||||
# Tambah kolom kategori (aman)
|
||||
try:
|
||||
cur.execute("ALTER TABLE menu ADD COLUMN kategori TEXT")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
|
||||
# Tabel Transaksi
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS transaksi(
|
||||
@ -60,21 +66,27 @@ def setup_database():
|
||||
("waiter","waiter","waiter"),
|
||||
("pemilik","pemilik","pemilik")
|
||||
]
|
||||
cur.executemany("INSERT INTO users(username,password,role) VALUES (?,?,?)", users)
|
||||
cur.executemany(
|
||||
"INSERT INTO users(username,password,role) VALUES (?,?,?)",
|
||||
users
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# --- UPDATE DAFTAR MENU SESUAI FOLDER ASET ---
|
||||
# Insert Menu Default
|
||||
cur.execute("SELECT COUNT(*) FROM menu")
|
||||
if cur.fetchone()[0] == 0:
|
||||
menus = [
|
||||
('Ayam Goreng', 20000, 'aset/ayam_goreng.jpg'),
|
||||
('Bakso', 15000, 'aset/bakso.jpg'),
|
||||
('Es Teh', 5000, 'aset/es_teh.jpg'),
|
||||
('Es Teler', 15000, 'aset/es_teler.jpg'),
|
||||
('Jus Jeruk', 8000, 'aset/jus_jeruk.jpg'),
|
||||
('Mie Ayam', 12000, 'aset/mie_ayam.jpg')
|
||||
('Ayam Goreng', 'Makanan', 20000, 'aset/ayam_goreng.jpg'),
|
||||
('Bakso', 'Makanan', 15000, 'aset/bakso.jpg'),
|
||||
('Mie Ayam', 'Makanan', 12000, 'aset/mie_ayam.jpg'),
|
||||
('Es Teh', 'Minuman', 5000, 'aset/es_teh.jpg'),
|
||||
('Es Teler', 'Minuman', 15000, 'aset/es_teler.jpg'),
|
||||
('Jus Jeruk', 'Minuman', 8000, 'aset/jus_jeruk.jpg')
|
||||
]
|
||||
cur.executemany("INSERT INTO menu (nama, harga, gambar) VALUES (?,?,?)", menus)
|
||||
cur.executemany(
|
||||
"INSERT INTO menu (nama, kategori, harga, gambar) VALUES (?,?,?,?)",
|
||||
menus
|
||||
)
|
||||
db.commit()
|
||||
|
||||
db.close()
|
||||
Loading…
x
Reference in New Issue
Block a user