Compare commits

..

No commits in common. "c4b6e1fba9a29425ce7cf2a913e7f4a9dfcbd724" and "92abbb95faee3f36ff2964ab22839079a482ef4c" have entirely different histories.

2 changed files with 122 additions and 103 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 308 KiB

View File

@ -1,32 +1,23 @@
import tkinter as tk import tkinter as tk
from tkinter import messagebox, ttk from tkinter import messagebox, ttk
from PIL import Image, ImageTk from PIL import Image, ImageTk
import sqlite3 from database import connect
import datetime import datetime
import os import os
# ================= DATABASE CONNECT =================
def connect():
return sqlite3.connect("cafe.db")
# ================= PEMBELI MENU =================
class PembeliMenu: class PembeliMenu:
def __init__(self, parent): def __init__(self, parent):
self.parent = parent self.parent = parent
self.frame = tk.Frame(parent) self.frame = tk.Frame(parent)
self.frame.pack(fill="both", expand=True) self.frame.pack(fill="both", expand=True)
self.cart = []
self.images = []
self.cart = [] # --- Header ---
self.images = [] tk.Label(self.frame, text="SELAMAT DATANG", font=("Arial", 18, "bold")).pack(pady=10)
# ================= HEADER ================= # --- INPUT DATA PELANGGAN ---
tk.Label(
self.frame,
text="SELAMAT DATANG",
font=("Arial", 18, "bold")
).pack(pady=10)
# ================= INPUT DATA =================
info_frame = tk.Frame(self.frame, bd=2, relief="groove", padx=10, pady=10) info_frame = tk.Frame(self.frame, bd=2, relief="groove", padx=10, pady=10)
info_frame.pack(pady=5) info_frame.pack(pady=5)
@ -34,56 +25,38 @@ class PembeliMenu:
self.entry_nama = tk.Entry(info_frame) self.entry_nama = tk.Entry(info_frame)
self.entry_nama.grid(row=0, column=1, padx=5) 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") tk.Label(info_frame, text="Pilih Meja:").grid(row=0, column=2, sticky="e")
self.meja_var = tk.StringVar() self.meja_var = tk.StringVar()
self.combo_meja = ttk.Combobox( self.combo_meja = ttk.Combobox(info_frame, textvariable=self.meja_var, state="readonly", width=5)
info_frame,
textvariable=self.meja_var,
state="readonly",
width=5
)
self.combo_meja.grid(row=0, column=3, padx=5) self.combo_meja.grid(row=0, column=3, padx=5)
tk.Button(info_frame, text="🔄", command=self.refresh_available_tables).grid(row=0, column=4, padx=2) tk.Button(info_frame, text="🔄", command=self.refresh_available_tables, bd=1).grid(row=0, column=4, padx=2)
self.refresh_available_tables() self.refresh_available_tables()
# ================= CONTENT 2 KOLOM ================= # Tombol Kembali
content = tk.Frame(self.frame) tk.Button(self.frame, text="Kembali ke Utama", bg="#f9e79f", command=self.logout).pack(pady=5)
content.pack(fill="both", expand=True, padx=20, pady=10)
content.grid_columnconfigure(0, weight=3) # --- Area Menu ---
content.grid_columnconfigure(1, weight=1) self.menu_frame = tk.Frame(self.frame)
self.menu_frame.pack(pady=10, fill="both", expand=True)
# ================= MENU (KIRI) =================
self.menu_frame = tk.Frame(content)
self.menu_frame.grid(row=0, column=0, sticky="nw")
self.load_menu() self.load_menu()
# ================= KERANJANG (KANAN) ================= # --- Keranjang Belanja ---
cart_frame = tk.LabelFrame( tk.Label(self.frame, text="Keranjang Pesanan:", font=("Arial", 10, "bold")).pack()
content,
text="Keranjang Pesanan", self.listbox = tk.Listbox(self.frame, width=60, height=8, font=("Courier", 10))
font=("Arial", 11, "bold"), self.listbox.pack()
padx=10,
pady=10
)
cart_frame.grid(row=0, column=1, sticky="ne")
self.listbox = tk.Listbox(cart_frame, width=35, height=10, font=("Courier", 10)) tk.Button(self.frame, text=" Kurangi / Hapus Item", fg="red", font=("Arial", 9), command=self.kurangi_item_cart).pack(pady=2)
self.listbox.pack(pady=5)
tk.Button(cart_frame, text=" Kurangi / Hapus Item", fg="red", self.total_lbl = tk.Label(self.frame, text="Total: Rp 0", font=("Arial", 12, "bold"))
command=self.kurangi_item_cart).pack(pady=5)
self.total_lbl = tk.Label(cart_frame, text="Total: Rp 0",
font=("Arial", 12, "bold"))
self.total_lbl.pack(pady=5) self.total_lbl.pack(pady=5)
tk.Button(cart_frame, text="✅ KIRIM PESANAN", bg="#d1e7dd", tk.Button(self.frame, text="✅ KIRIM PESANAN", bg="#d1e7dd", font=("Arial", 11, "bold"), command=self.checkout).pack(pady=10)
font=("Arial", 11, "bold"), command=self.checkout).pack(pady=10)
# ================= MEJA =================
def refresh_available_tables(self): def refresh_available_tables(self):
total_meja = [str(i) for i in range(1, 11)] total_meja = [str(i) for i in range(1, 11)]
db = connect() db = connect()
@ -93,10 +66,14 @@ class PembeliMenu:
db.close() db.close()
tersedia = [m for m in total_meja if m not in terisi] tersedia = [m for m in total_meja if m not in terisi]
self.combo_meja['values'] = tersedia if tersedia else ["Penuh"]
self.combo_meja.set(tersedia[0] if tersedia else "Penuh")
# ================= MENU ================= 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): def load_menu(self):
db = connect() db = connect()
cur = db.cursor() cur = db.cursor()
@ -104,85 +81,127 @@ class PembeliMenu:
data = cur.fetchall() data = cur.fetchall()
db.close() db.close()
columns = 4 row_frame = None
for i, (nama, harga, gambar) in enumerate(data): for i, (nama, harga, gambar) in enumerate(data):
row, col = divmod(i, columns) if i % 3 == 0:
card = tk.Frame(self.menu_frame, width=180, height=220, bd=2, relief="ridge") row_frame = tk.Frame(self.menu_frame)
card.grid(row=row, column=col, padx=10, pady=10) row_frame.pack()
card.grid_propagate(False)
f = tk.Frame(row_frame, bd=2, relief="ridge", padx=5, pady=5)
f.pack(side=tk.LEFT, padx=5)
try: try:
filename = os.path.basename(gambar) if gambar else None if os.path.exists(gambar):
img_path = os.path.join("aset", filename) if filename else None img = Image.open(gambar).resize((100, 80))
if img_path and os.path.exists(img_path):
img = Image.open(img_path).resize((140, 90))
photo = ImageTk.PhotoImage(img) photo = ImageTk.PhotoImage(img)
self.images.append(photo) self.images.append(photo)
tk.Label(card, image=photo).pack(pady=5) tk.Label(f, image=photo).pack()
else: else:
tk.Label(card, text="[No Image]", height=6).pack() tk.Label(f, text="[No Image]").pack()
except Exception: except:
tk.Label(card, text="Error Img", height=6).pack() tk.Label(f, text="Error Img").pack()
tk.Label(card, text=nama, font=("Arial", 10, "bold"), wraplength=160).pack() tk.Label(f, text=nama, font=("Arial",10,"bold")).pack()
tk.Label(card, text=f"Rp {harga:,.0f}").pack(pady=2) 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)
tk.Button(card, text="Tambah", bg="#cfe2ff",
command=lambda n=nama, h=harga: self.add_to_cart(n, h)).pack(pady=5)
# ================= CART =================
def add_to_cart(self, nama, harga): def add_to_cart(self, nama, harga):
item_found = False
for item in self.cart: for item in self.cart:
if item['nama'] == nama: if item['nama'] == nama:
item['qty'] += 1 item['qty'] += 1
item['subtotal'] = item['qty'] * harga item['subtotal'] = item['qty'] * harga
self.refresh_cart() item_found = True
return break
self.cart.append({'nama': nama, 'harga_satuan': harga, 'qty': 1, 'subtotal': harga})
if not item_found:
self.cart.append({
'nama': nama,
'harga_satuan': harga,
'qty': 1,
'subtotal': harga
})
self.refresh_cart() self.refresh_cart()
def kurangi_item_cart(self): def kurangi_item_cart(self):
if not self.listbox.curselection(): return idx = self.listbox.curselection()
idx = self.listbox.curselection()[0] if not idx:
item = self.cart[idx] messagebox.showwarning("Peringatan", "Pilih menu yang mau dikurangi!")
return
index = idx[0]
item = self.cart[index]
# Kurangi jumlah
item['qty'] -= 1 item['qty'] -= 1
if item['qty'] <= 0:
self.cart.pop(idx) if item['qty'] > 0:
else:
item['subtotal'] = item['qty'] * item['harga_satuan'] item['subtotal'] = item['qty'] * item['harga_satuan']
self.refresh_cart() 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): def refresh_cart(self):
self.listbox.delete(0, tk.END) self.listbox.delete(0, tk.END)
total = 0 total_belanja = 0
for item in self.cart: for item in self.cart:
self.listbox.insert(tk.END, f"{item['nama']} (x{item['qty']}) - Rp {item['subtotal']:,.0f}") display_text = f"{item['nama']} (x{item['qty']}) - Rp {item['subtotal']:,.0f}"
total += item['subtotal'] self.listbox.insert(tk.END, display_text)
self.total_lbl.config(text=f"Total: Rp {total:,.0f}") total_belanja += item['subtotal']
self.total_lbl.config(text=f"Total: Rp {total_belanja:,.0f}")
# ================= CHECKOUT =================
def checkout(self): def checkout(self):
if not self.entry_nama.get() or not self.cart: nama_pemesan = self.entry_nama.get()
messagebox.showwarning("Error", "Data belum lengkap") meja = self.combo_meja.get()
if not nama_pemesan:
messagebox.showwarning("Data Kurang", "Mohon isi Nama Anda!")
return return
total = sum(item['subtotal'] for item in self.cart) 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") tanggal = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
db = connect() db = connect()
cur = db.cursor() cur = db.cursor()
tanggal_custom = f"{tanggal} ({nama_pemesan})"
cur.execute("INSERT INTO transaksi (tanggal, total, meja_id, status) VALUES (?, ?, ?, ?)", cur.execute("INSERT INTO transaksi (tanggal, total, meja_id, status) VALUES (?, ?, ?, ?)",
(tanggal, total, self.combo_meja.get(), "Pending")) (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.commit()
db.close() db.close()
messagebox.showinfo("Berhasil", "Pesanan berhasil dikirim") messagebox.showinfo("Berhasil", f"Pesanan a.n {nama_pemesan} berhasil dikirim!")
self.cart.clear() self.cart.clear()
self.refresh_cart() self.refresh_cart()
self.entry_nama.delete(0, tk.END)
self.refresh_available_tables()
# ================= RUN ================= def logout(self):
if __name__ == '__main__': self.frame.destroy()
root = tk.Tk() from main import LoginScreen
root.title("Sistem Cafe Python") LoginScreen(self.parent)
root.geometry("1200x700")
PembeliMenu(root)
root.mainloop()