207 lines
7.3 KiB
Python
207 lines
7.3 KiB
Python
import tkinter as tk
|
||
from tkinter import messagebox, ttk
|
||
from PIL import Image, ImageTk
|
||
from database import connect
|
||
import datetime
|
||
import os
|
||
|
||
class PembeliMenu:
|
||
def __init__(self, parent):
|
||
self.parent = parent
|
||
self.frame = tk.Frame(parent)
|
||
self.frame.pack(fill="both", expand=True)
|
||
|
||
self.cart = []
|
||
self.images = []
|
||
|
||
# --- Header ---
|
||
tk.Label(self.frame, text="SELAMAT DATANG", font=("Arial", 18, "bold")).pack(pady=10)
|
||
|
||
# --- INPUT DATA PELANGGAN ---
|
||
info_frame = tk.Frame(self.frame, bd=2, relief="groove", padx=10, pady=10)
|
||
info_frame.pack(pady=5)
|
||
|
||
tk.Label(info_frame, text="Nama Anda:").grid(row=0, column=0, sticky="e")
|
||
self.entry_nama = tk.Entry(info_frame)
|
||
self.entry_nama.grid(row=0, column=1, padx=5)
|
||
|
||
# --- LOGIKA MEJA ---
|
||
tk.Label(info_frame, text="Pilih Meja:").grid(row=0, column=2, sticky="e")
|
||
|
||
self.meja_var = tk.StringVar()
|
||
self.combo_meja = ttk.Combobox(info_frame, textvariable=self.meja_var, state="readonly", width=5)
|
||
self.combo_meja.grid(row=0, column=3, padx=5)
|
||
|
||
tk.Button(info_frame, text="🔄", command=self.refresh_available_tables, bd=1).grid(row=0, column=4, padx=2)
|
||
|
||
self.refresh_available_tables()
|
||
|
||
# Tombol Kembali
|
||
tk.Button(self.frame, text="Kembali ke Utama", bg="#f9e79f", command=self.logout).pack(pady=5)
|
||
|
||
# --- Area Menu ---
|
||
self.menu_frame = tk.Frame(self.frame)
|
||
self.menu_frame.pack(pady=10, fill="both", expand=True)
|
||
self.load_menu()
|
||
|
||
# --- Keranjang Belanja ---
|
||
tk.Label(self.frame, text="Keranjang Pesanan:", font=("Arial", 10, "bold")).pack()
|
||
|
||
self.listbox = tk.Listbox(self.frame, width=60, height=8, font=("Courier", 10))
|
||
self.listbox.pack()
|
||
|
||
tk.Button(self.frame, text="➖ Kurangi / Hapus Item", fg="red", font=("Arial", 9), command=self.kurangi_item_cart).pack(pady=2)
|
||
|
||
self.total_lbl = tk.Label(self.frame, text="Total: Rp 0", font=("Arial", 12, "bold"))
|
||
self.total_lbl.pack(pady=5)
|
||
|
||
tk.Button(self.frame, text="✅ KIRIM PESANAN", bg="#d1e7dd", font=("Arial", 11, "bold"), command=self.checkout).pack(pady=10)
|
||
|
||
def refresh_available_tables(self):
|
||
total_meja = [str(i) for i in range(1, 11)]
|
||
db = connect()
|
||
cur = db.cursor()
|
||
cur.execute("SELECT DISTINCT meja_id FROM transaksi WHERE status != 'Selesai'")
|
||
terisi = [str(row[0]) for row in cur.fetchall()]
|
||
db.close()
|
||
|
||
tersedia = [m for m in total_meja if m not in terisi]
|
||
|
||
if not tersedia:
|
||
self.combo_meja['values'] = ["Penuh"]
|
||
self.combo_meja.set("Penuh")
|
||
else:
|
||
self.combo_meja['values'] = tersedia
|
||
self.combo_meja.current(0)
|
||
|
||
def load_menu(self):
|
||
db = connect()
|
||
cur = db.cursor()
|
||
cur.execute("SELECT nama, harga, gambar FROM menu")
|
||
data = cur.fetchall()
|
||
db.close()
|
||
|
||
row_frame = None
|
||
for i, (nama, harga, gambar) in enumerate(data):
|
||
if i % 3 == 0:
|
||
row_frame = tk.Frame(self.menu_frame)
|
||
row_frame.pack()
|
||
|
||
f = tk.Frame(row_frame, bd=2, relief="ridge", padx=5, pady=5)
|
||
f.pack(side=tk.LEFT, padx=5)
|
||
|
||
try:
|
||
if os.path.exists(gambar):
|
||
img = Image.open(gambar).resize((100, 80))
|
||
photo = ImageTk.PhotoImage(img)
|
||
self.images.append(photo)
|
||
tk.Label(f, image=photo).pack()
|
||
else:
|
||
tk.Label(f, text="[No Image]").pack()
|
||
except:
|
||
tk.Label(f, text="Error Img").pack()
|
||
|
||
tk.Label(f, text=nama, font=("Arial",10,"bold")).pack()
|
||
tk.Label(f, text=f"Rp {harga:,.0f}").pack()
|
||
tk.Button(f, text="Tambah", bg="#cfe2ff", command=lambda n=nama, h=harga: self.add_to_cart(n, h)).pack(pady=2)
|
||
|
||
def add_to_cart(self, nama, harga):
|
||
item_found = False
|
||
for item in self.cart:
|
||
if item['nama'] == nama:
|
||
item['qty'] += 1
|
||
item['subtotal'] = item['qty'] * harga
|
||
item_found = True
|
||
break
|
||
|
||
if not item_found:
|
||
self.cart.append({
|
||
'nama': nama,
|
||
'harga_satuan': harga,
|
||
'qty': 1,
|
||
'subtotal': harga
|
||
})
|
||
|
||
self.refresh_cart()
|
||
|
||
def kurangi_item_cart(self):
|
||
idx = self.listbox.curselection()
|
||
if not idx:
|
||
messagebox.showwarning("Peringatan", "Pilih menu yang mau dikurangi!")
|
||
return
|
||
|
||
index = idx[0]
|
||
item = self.cart[index]
|
||
|
||
# Kurangi jumlah
|
||
item['qty'] -= 1
|
||
|
||
if item['qty'] > 0:
|
||
item['subtotal'] = item['qty'] * item['harga_satuan']
|
||
self.refresh_cart()
|
||
# AUTO-SELECT: Pilih kembali item yang sama
|
||
self.listbox.selection_set(index)
|
||
self.listbox.activate(index)
|
||
else:
|
||
self.cart.pop(index)
|
||
self.refresh_cart()
|
||
# AUTO-SELECT (Opsional): Pilih item yang menggantikan posisinya (jika ada)
|
||
if index < self.listbox.size():
|
||
self.listbox.selection_set(index)
|
||
self.listbox.activate(index)
|
||
|
||
def refresh_cart(self):
|
||
self.listbox.delete(0, tk.END)
|
||
total_belanja = 0
|
||
|
||
for item in self.cart:
|
||
display_text = f"{item['nama']} (x{item['qty']}) - Rp {item['subtotal']:,.0f}"
|
||
self.listbox.insert(tk.END, display_text)
|
||
total_belanja += item['subtotal']
|
||
|
||
self.total_lbl.config(text=f"Total: Rp {total_belanja:,.0f}")
|
||
|
||
def checkout(self):
|
||
nama_pemesan = self.entry_nama.get()
|
||
meja = self.combo_meja.get()
|
||
|
||
if not nama_pemesan:
|
||
messagebox.showwarning("Data Kurang", "Mohon isi Nama Anda!")
|
||
return
|
||
if not meja or meja == "Penuh":
|
||
messagebox.showwarning("Penuh", "Mohon maaf, meja penuh atau belum dipilih.")
|
||
return
|
||
if not self.cart:
|
||
messagebox.showwarning("Kosong", "Pilih menu dulu!")
|
||
return
|
||
|
||
total_belanja = sum(item['subtotal'] for item in self.cart)
|
||
tanggal = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
|
||
db = connect()
|
||
cur = db.cursor()
|
||
|
||
tanggal_custom = f"{tanggal} ({nama_pemesan})"
|
||
|
||
cur.execute("INSERT INTO transaksi (tanggal, total, meja_id, status) VALUES (?, ?, ?, ?)",
|
||
(tanggal_custom, total_belanja, meja, "Pending"))
|
||
|
||
transaksi_id = cur.lastrowid
|
||
|
||
for item in self.cart:
|
||
cur.execute("INSERT INTO detail_transaksi (transaksi_id, menu_nama, harga, jumlah, subtotal) VALUES (?, ?, ?, ?, ?)",
|
||
(transaksi_id, item['nama'], item['harga_satuan'], item['qty'], item['subtotal']))
|
||
|
||
db.commit()
|
||
db.close()
|
||
|
||
messagebox.showinfo("Berhasil", f"Pesanan a.n {nama_pemesan} berhasil dikirim!")
|
||
self.cart.clear()
|
||
self.refresh_cart()
|
||
self.entry_nama.delete(0, tk.END)
|
||
self.refresh_available_tables()
|
||
|
||
def logout(self):
|
||
self.frame.destroy()
|
||
from main import LoginScreen
|
||
LoginScreen(self.parent) |