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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""簡易訂單系統
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1yB8xfV9Q47V313d2eTvReUCxZ9zMDMSX
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
|
| 14 |
+
# ----------------------------
|
| 15 |
+
# 系統資料與設定
|
| 16 |
+
# ----------------------------
|
| 17 |
+
# 模擬資料庫 (存在記憶體中,重新啟動程式會清空)
|
| 18 |
+
orders_db = []
|
| 19 |
+
|
| 20 |
+
# 商品菜單與價格字典
|
| 21 |
+
MENU = {
|
| 22 |
+
"招牌咖啡": 80,
|
| 23 |
+
"拿鐵咖啡": 100,
|
| 24 |
+
"珍珠奶茶": 60,
|
| 25 |
+
"高山青茶": 40,
|
| 26 |
+
"手工餅乾": 50
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# ----------------------------
|
| 30 |
+
# 處理邏輯函式
|
| 31 |
+
# ----------------------------
|
| 32 |
+
def get_orders_df():
|
| 33 |
+
"""將目前的訂單列表轉換為 Pandas DataFrame 以利在網頁中顯示"""
|
| 34 |
+
if not orders_db:
|
| 35 |
+
# 如果沒有訂單,回傳一個空的 DataFrame 加上欄位名稱
|
| 36 |
+
return pd.DataFrame(columns=["時間", "顧客姓名", "商品", "單價", "數量", "總價", "備註"])
|
| 37 |
+
return pd.DataFrame(orders_db)
|
| 38 |
+
|
| 39 |
+
def get_revenue():
|
| 40 |
+
"""計算目前所有訂單的總營收"""
|
| 41 |
+
if not orders_db:
|
| 42 |
+
return "### 💰 目前總營收:NT$ 0"
|
| 43 |
+
total = sum(order["總價"] for order in orders_db)
|
| 44 |
+
return f"### 💰 目前總營收:NT$ {total}"
|
| 45 |
+
|
| 46 |
+
def add_order(customer_name, notes, *quantities):
|
| 47 |
+
"""新增多筆訂單到資料庫中 (支援個別商品設定數量)"""
|
| 48 |
+
# 基本防呆機制
|
| 49 |
+
if not customer_name.strip():
|
| 50 |
+
return "❌ 錯誤:請輸入顧客姓名", get_orders_df(), get_revenue()
|
| 51 |
+
|
| 52 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 53 |
+
total_order_price = 0
|
| 54 |
+
items_desc = []
|
| 55 |
+
has_ordered_items = False
|
| 56 |
+
|
| 57 |
+
# 將動態傳入的數量與原本的 MENU 菜單做對應
|
| 58 |
+
menu_items = list(MENU.items())
|
| 59 |
+
|
| 60 |
+
for i, qty in enumerate(quantities):
|
| 61 |
+
if qty > 0:
|
| 62 |
+
has_ordered_items = True
|
| 63 |
+
item, price = menu_items[i]
|
| 64 |
+
total_price = price * qty
|
| 65 |
+
total_order_price += total_price
|
| 66 |
+
|
| 67 |
+
order = {
|
| 68 |
+
"時間": timestamp,
|
| 69 |
+
"顧客姓名": customer_name,
|
| 70 |
+
"商品": item,
|
| 71 |
+
"單價": price,
|
| 72 |
+
"數量": qty,
|
| 73 |
+
"總價": total_price,
|
| 74 |
+
"備註": notes
|
| 75 |
+
}
|
| 76 |
+
orders_db.append(order)
|
| 77 |
+
items_desc.append(f"{item}x{qty}")
|
| 78 |
+
|
| 79 |
+
if not has_ordered_items:
|
| 80 |
+
return "❌ 錯誤:請至少設定一項商品的數量(大於 0)", get_orders_df(), get_revenue()
|
| 81 |
+
|
| 82 |
+
# 回傳成功訊息,並同時更新表格與營收顯示
|
| 83 |
+
msg = f"✅ 成功新增訂單:{customer_name} 訂購了 {', '.join(items_desc)},總計 {total_order_price} 元。"
|
| 84 |
+
return msg, get_orders_df(), get_revenue()
|
| 85 |
+
|
| 86 |
+
def clear_orders():
|
| 87 |
+
"""清空所有訂單"""
|
| 88 |
+
orders_db.clear()
|
| 89 |
+
return "🗑️ 所有訂單已清空", get_orders_df(), get_revenue()
|
| 90 |
+
|
| 91 |
+
# ----------------------------
|
| 92 |
+
# 權限處理邏輯
|
| 93 |
+
# ----------------------------
|
| 94 |
+
def verify_password(pwd):
|
| 95 |
+
"""驗證管理員密碼"""
|
| 96 |
+
if pwd == "0000":
|
| 97 |
+
# 密碼正確:隱藏登入區,顯示總覽區,清空錯誤訊息
|
| 98 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
| 99 |
+
else:
|
| 100 |
+
# 密碼錯誤:保持現狀,顯示錯誤訊息
|
| 101 |
+
return gr.update(visible=True), gr.update(visible=False), "❌ **密碼錯誤,請重新輸入。**"
|
| 102 |
+
|
| 103 |
+
def logout():
|
| 104 |
+
"""登出並恢復到輸入密碼狀態,同時清空密碼欄位"""
|
| 105 |
+
return gr.update(visible=True), gr.update(visible=False), "", ""
|
| 106 |
+
|
| 107 |
+
# ----------------------------
|
| 108 |
+
# 建立 Gradio 網頁介面 (UI)
|
| 109 |
+
# ----------------------------
|
| 110 |
+
# 加入自訂 CSS 樣式來畫出底部分隔線
|
| 111 |
+
custom_css = """
|
| 112 |
+
.menu-row {
|
| 113 |
+
border-bottom: 1px solid #e5e7eb;
|
| 114 |
+
padding-bottom: 15px;
|
| 115 |
+
margin-bottom: 10px !important;
|
| 116 |
+
}
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=custom_css) as demo:
|
| 120 |
+
|
| 121 |
+
gr.Markdown(
|
| 122 |
+
"""
|
| 123 |
+
<h1 style='text-align: center;'>Sch簡易訂單管理系統</h1>
|
| 124 |
+
"""
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
with gr.Tabs():
|
| 128 |
+
# 第一個分頁:新增訂單 (公開)
|
| 129 |
+
with gr.TabItem("📝 新增訂單"):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
with gr.Column():
|
| 132 |
+
customer_name = gr.Textbox(label="顧客姓名", placeholder="請輸入顧客姓名")
|
| 133 |
+
|
| 134 |
+
gr.Markdown("#### 🛍️ 選擇商品與數量")
|
| 135 |
+
quantity_components = []
|
| 136 |
+
|
| 137 |
+
# 動態產生每個商品的專屬欄位 (左邊商品名稱,右邊數量)
|
| 138 |
+
for item, price in MENU.items():
|
| 139 |
+
# 套用自訂的 CSS 類別 "menu-row"
|
| 140 |
+
with gr.Row(elem_classes="menu-row"):
|
| 141 |
+
# 顯示商品名稱與單價
|
| 142 |
+
gr.Markdown(f"**{item}** <br><span style='color: gray;'>NT$ {price}</span>")
|
| 143 |
+
# 專屬的數量選單
|
| 144 |
+
qty = gr.Number(label="數量", value=0, precision=0, minimum=0)
|
| 145 |
+
quantity_components.append(qty)
|
| 146 |
+
|
| 147 |
+
notes = gr.Textbox(label="備註", placeholder="例如:少冰微糖、不要吸管...")
|
| 148 |
+
|
| 149 |
+
submit_btn = gr.Button("🚀 送出訂單", variant="primary")
|
| 150 |
+
status_msg = gr.Textbox(label="系統狀態", interactive=False)
|
| 151 |
+
|
| 152 |
+
# 第二個分頁:訂單總覽 (密碼保護)
|
| 153 |
+
with gr.TabItem("📊 訂單總覽"):
|
| 154 |
+
|
| 155 |
+
# 登入區塊 (預設顯示)
|
| 156 |
+
with gr.Column(visible=True) as login_pane:
|
| 157 |
+
gr.Markdown("### 🔒 系統管理員登入")
|
| 158 |
+
admin_pwd = gr.Textbox(label="請輸入管理員密碼", type="password", placeholder="請輸入密碼...")
|
| 159 |
+
login_btn = gr.Button("登入", variant="primary")
|
| 160 |
+
login_status = gr.Markdown()
|
| 161 |
+
|
| 162 |
+
# 總覽資料區塊 (預設隱藏,登入成功後顯示)
|
| 163 |
+
with gr.Column(visible=False) as overview_pane:
|
| 164 |
+
logout_btn = gr.Button("🚪 登出", size="sm")
|
| 165 |
+
gr.Markdown("### 📊 訂單詳細數據")
|
| 166 |
+
|
| 167 |
+
# 顯示總營收的 Markdown 區塊
|
| 168 |
+
revenue_display = gr.Markdown(get_revenue())
|
| 169 |
+
|
| 170 |
+
# 顯示訂單的資料表
|
| 171 |
+
orders_table = gr.Dataframe(
|
| 172 |
+
value=get_orders_df(),
|
| 173 |
+
headers=["時間", "顧客姓名", "商品", "單價", "數量", "總價", "備註"],
|
| 174 |
+
interactive=False,
|
| 175 |
+
wrap=True
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
clear_btn = gr.Button("🗑️ 清空所有訂單", variant="stop")
|
| 179 |
+
|
| 180 |
+
# ----------------------------
|
| 181 |
+
# 綁定按鈕點擊事件
|
| 182 |
+
# ----------------------------
|
| 183 |
+
# 點擊「送出訂單」時:
|
| 184 |
+
# 將姓名、備註以及所有動態產生的數量元件整合成一個清單傳遞給 add_order
|
| 185 |
+
inputs_list = [customer_name, notes] + quantity_components
|
| 186 |
+
|
| 187 |
+
submit_btn.click(
|
| 188 |
+
fn=add_order,
|
| 189 |
+
inputs=inputs_list,
|
| 190 |
+
outputs=[status_msg, orders_table, revenue_display]
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# 登入與登出事件:
|
| 194 |
+
login_btn.click(
|
| 195 |
+
fn=verify_password,
|
| 196 |
+
inputs=admin_pwd,
|
| 197 |
+
outputs=[login_pane, overview_pane, login_status]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
logout_btn.click(
|
| 201 |
+
fn=logout,
|
| 202 |
+
inputs=[],
|
| 203 |
+
outputs=[login_pane, overview_pane, login_status, admin_pwd]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# 點擊「清空訂單」時:
|
| 207 |
+
clear_btn.click(
|
| 208 |
+
fn=clear_orders,
|
| 209 |
+
inputs=[],
|
| 210 |
+
outputs=[status_msg, orders_table, revenue_display]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# ----------------------------
|
| 214 |
+
# 啟動伺服器
|
| 215 |
+
# ----------------------------
|
| 216 |
+
if __name__ == "__main__":
|
| 217 |
+
demo.launch(share=False, inbrowser=True)
|