123 lines
4.4 KiB
Python
123 lines
4.4 KiB
Python
import tkinter as tk
|
|
from tkinter import messagebox
|
|
from PIL import Image, ImageTk
|
|
from database import connect
|
|
import datetime
|
|
|
|
class PembeliMenu:
|
|
def __init__(self, parent):
|
|
self.parent = parent
|
|
self.frame = tk.Frame(parent)
|
|
self.frame.pack(fill="both", expand=True)
|
|
|
|
self.cart = [] # List dictionary: {'nama':, 'harga':, 'jumlah':}
|
|
self.images = []
|
|
|
|
# Header
|
|
tk.Label(self.frame, text="MENU PELANGGAN", font=("Arial", 18, "bold")).pack(pady=10)
|
|
|
|
# --- INPUT NOMOR MEJA (Fitur Baru) ---
|
|
frame_meja = tk.Frame(self.frame)
|
|
frame_meja.pack(pady=5)
|
|
tk.Label(frame_meja, text="Nomor Meja: ").pack(side=tk.LEFT)
|
|
self.entry_meja = tk.Entry(frame_meja, width=5)
|
|
self.entry_meja.pack(side=tk.LEFT)
|
|
|
|
# Logout
|
|
tk.Button(self.frame, text="Logout", bg="#f9e79f", command=self.logout).pack(pady=5)
|
|
|
|
# Area Menu & Cart
|
|
self.menu_frame = tk.Frame(self.frame)
|
|
self.menu_frame.pack(pady=10, fill="both", expand=True)
|
|
|
|
# Load Menu
|
|
self.load_menu()
|
|
|
|
# Listbox Keranjang
|
|
tk.Label(self.frame, text="Keranjang Pesanan:").pack()
|
|
self.listbox = tk.Listbox(self.frame, width=50, height=6)
|
|
self.listbox.pack()
|
|
|
|
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="PESAN SEKARANG", bg="#d1e7dd", command=self.checkout).pack(pady=10)
|
|
|
|
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:
|
|
# Pastikan nama file gambar sesuai dengan yang ada di folder project/
|
|
img = Image.open(gambar).resize((100, 80))
|
|
photo = ImageTk.PhotoImage(img)
|
|
self.images.append(photo)
|
|
tk.Label(f, image=photo).pack()
|
|
except:
|
|
tk.Label(f, text="[No Image]").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", command=lambda n=nama, h=harga: self.add_to_cart(n, h)).pack()
|
|
|
|
def add_to_cart(self, nama, harga):
|
|
self.cart.append({'nama': nama, 'harga': harga})
|
|
self.refresh_cart()
|
|
|
|
def refresh_cart(self):
|
|
self.listbox.delete(0, tk.END)
|
|
total = 0
|
|
for item in self.cart:
|
|
self.listbox.insert(tk.END, f"{item['nama']} - Rp {item['harga']:,.0f}")
|
|
total += item['harga']
|
|
self.total_lbl.config(text=f"Total: Rp {total:,.0f}")
|
|
|
|
def checkout(self):
|
|
meja = self.entry_meja.get()
|
|
if not meja:
|
|
messagebox.showwarning("Peringatan", "Isi nomor meja dulu!")
|
|
return
|
|
if not self.cart:
|
|
messagebox.showwarning("Kosong", "Pilih menu dulu!")
|
|
return
|
|
|
|
total_belanja = sum(item['harga'] for item in self.cart)
|
|
tanggal = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
db = connect()
|
|
cur = db.cursor()
|
|
|
|
# 1. Buat Header Transaksi (Status = Pending)
|
|
cur.execute("INSERT INTO transaksi (tanggal, total, meja_id, status) VALUES (?, ?, ?, ?)",
|
|
(tanggal, total_belanja, meja, "Pending"))
|
|
transaksi_id = cur.lastrowid # Ambil ID transaksi yang baru dibuat
|
|
|
|
# 2. Masukkan Detail Item
|
|
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'], 1, item['harga']))
|
|
|
|
db.commit()
|
|
db.close()
|
|
|
|
messagebox.showinfo("Berhasil", "Pesanan terkirim ke Dapur/Waiter!")
|
|
self.cart.clear()
|
|
self.refresh_cart()
|
|
self.entry_meja.delete(0, tk.END)
|
|
|
|
def logout(self):
|
|
self.frame.destroy()
|
|
from main import LoginScreen
|
|
LoginScreen(self.parent) |