Python-Menu/project/kasir.py

234 lines
5.6 KiB
Python

import tkinter as tk
from tkinter import messagebox
from database import connect
import qrcode
from PIL import ImageTk
class KasirPage:
def __init__(self, parent, controller=None):
self.parent = parent
self.frame = tk.Frame(parent)
self.frame.pack(fill="both", expand=True)
# =====================
# JUDUL
# =====================
tk.Label(
self.frame,
text="KASIR - PEMBAYARAN",
font=("Arial", 18, "bold")
).pack(pady=10)
tk.Button(
self.frame,
text="Logout",
bg="#f9e79f",
command=self.logout
).pack(pady=5)
tk.Button(
self.frame,
text="Refresh",
command=self.load_tagihan
).pack(pady=3)
# =====================
# LIST TAGIHAN
# =====================
tk.Label(
self.frame,
text="Daftar Tagihan (Status: Disajikan)",
font=("Arial", 12)
).pack(pady=5)
self.listbox = tk.Listbox(self.frame, width=65, height=10)
self.listbox.pack(pady=5)
# =====================
# METODE PEMBAYARAN
# =====================
metode_frame = tk.Frame(self.frame)
metode_frame.pack(pady=5)
tk.Label(metode_frame, text="Metode Pembayaran:").pack(side=tk.LEFT)
self.metode = tk.StringVar(value="Cash")
tk.OptionMenu(
metode_frame,
self.metode,
"Cash", "QRIS", "E-Wallet"
).pack(side=tk.LEFT, padx=5)
# =====================
# BUTTON BAYAR
# =====================
tk.Button(
self.frame,
text="Proses Pembayaran",
bg="#d1e7dd",
width=25,
command=self.bayar
).pack(pady=10)
self.transaksi_data = []
self.load_tagihan()
# =====================
# LOAD TAGIHAN
# =====================
def load_tagihan(self):
self.listbox.delete(0, tk.END)
self.transaksi_data = []
db = connect()
cur = db.cursor()
cur.execute("""
SELECT id, meja_id, total
FROM transaksi
WHERE status='Disajikan'
""")
rows = cur.fetchall()
db.close()
for row in rows:
t_id, meja, total = row
self.listbox.insert(
tk.END,
f"Meja {meja} | ID {t_id} | Total Rp {total:,.0f}"
)
self.transaksi_data.append(row)
# =====================
# BAYAR
# =====================
def bayar(self):
idx = self.listbox.curselection()
if not idx:
messagebox.showwarning("Pilih", "Pilih tagihan yang akan dibayar!")
return
t_id, meja, total = self.transaksi_data[idx[0]]
metode = self.metode.get()
if metode == "QRIS":
self.tampilkan_qr(t_id, meja, total)
elif metode == "E-Wallet":
self.konfirmasi_ewallet(t_id, meja, total)
else:
self.selesaikan_pembayaran(t_id, meja, total, metode)
# =====================
# QRIS (CLOSE WINDOW = LUNAS)
# =====================
def tampilkan_qr(self, t_id, meja, total):
win = tk.Toplevel(self.frame)
win.title("QRIS Payment")
win.geometry("320x420")
tk.Label(
win,
text="SCAN QR UNTUK BAYAR",
font=("Arial", 12, "bold")
).pack(pady=10)
qr_data = f"""
QRIS-DUMMY
Cafe Python
Transaksi: {t_id}
Meja: {meja}
Total: {total}
"""
qr = qrcode.make(qr_data)
qr_img = ImageTk.PhotoImage(qr)
lbl = tk.Label(win, image=qr_img)
lbl.image = qr_img
lbl.pack(pady=10)
tk.Label(
win,
text=f"Total: Rp {total:,.0f}",
font=("Arial", 11)
).pack(pady=5)
def selesai_qris():
self.selesaikan_pembayaran(t_id, meja, total, "QRIS")
win.destroy()
# ❌ WINDOW DITUTUP = TRANSAKSI BERHASIL
win.protocol("WM_DELETE_WINDOW", selesai_qris)
tk.Button(
win,
text="Konfirmasi Pembayaran",
bg="#d1e7dd",
width=25,
command=selesai_qris
).pack(pady=15)
# =====================
# E-WALLET
# =====================
def konfirmasi_ewallet(self, t_id, meja, total):
confirm = messagebox.askyesno(
"E-Wallet Payment",
f"""
Bayar menggunakan E-Wallet?
Meja : {meja}
Total : Rp {total:,.0f}
Lanjutkan pembayaran?
"""
)
if confirm:
self.selesaikan_pembayaran(t_id, meja, total, "E-Wallet")
# =====================
# FINAL PEMBAYARAN
# =====================
def selesaikan_pembayaran(self, t_id, meja, total, metode):
db = connect()
cur = db.cursor()
cur.execute(
"""
UPDATE transaksi
SET status='Lunas',
tanggal = DATETIME('now')
WHERE id=?
""",
(t_id,)
)
db.commit()
db.close()
messagebox.showinfo(
"Struk Pembayaran",
f"""
======= STRUK =======
ID Transaksi : {t_id}
Meja : {meja}
Total : Rp {total:,.0f}
Metode : {metode}
Status : Lunas
====================
Terima kasih 🙏
"""
)
self.load_tagihan()
# =====================
# LOGOUT
# =====================
def logout(self):
self.frame.destroy()
from main import LoginScreen
LoginScreen(self.parent)