Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -119,6 +119,7 @@ orders_db = load_orders()
|
|
| 119 |
|
| 120 |
def load_menu():
|
| 121 |
loaded_menu = []
|
|
|
|
| 122 |
if os.path.exists(MENU_FILE):
|
| 123 |
try:
|
| 124 |
with open(MENU_FILE, "r", encoding="utf-8") as f:
|
|
@@ -184,10 +185,12 @@ def load_menu():
|
|
| 184 |
{"name": "手工餅乾", "price": 50, "cost": 20, "enable": True, "level": "全部", "seq": 5, "category": "🍪 美味點心", "spec": "一份", "note": ""}
|
| 185 |
]
|
| 186 |
else:
|
|
|
|
|
|
|
| 187 |
for i, v in enumerate(loaded_menu):
|
| 188 |
if "enable" not in v: v["enable"] = True
|
| 189 |
if "level" not in v: v["level"] = "全部"
|
| 190 |
-
|
| 191 |
if "category" not in v: v["category"] = "未分類"
|
| 192 |
if "spec" not in v: v["spec"] = ""
|
| 193 |
if "note" not in v: v["note"] = ""
|
|
@@ -255,6 +258,7 @@ def get_frontend_menu_items(user_level="全部"):
|
|
| 255 |
cat = cat.strip() if cat.strip() else "未分類"
|
| 256 |
if cat not in category_order:
|
| 257 |
category_order.append(cat)
|
|
|
|
| 258 |
items_by_cat.setdefault(cat, []).append((details.get("name", "未命名"), details))
|
| 259 |
|
| 260 |
flat_list = []
|
|
@@ -357,6 +361,24 @@ def delete_single_member(acc):
|
|
| 357 |
return gr.update(value=f"🗑️ 會員 {acc} 已刪除!"), get_members_df()
|
| 358 |
return gr.update(value=f"❌ 找不到會員 {acc}"), gr.update()
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
def update_members_list(df):
|
| 361 |
_internal_update_members(df)
|
| 362 |
return gr.update(value=f"✅ 會員資料已成功更新!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"), get_members_df()
|
|
@@ -1338,18 +1360,20 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1338 |
existing_cats = [c for c in get_menu_cat_choices() if c != "全部分類"]
|
| 1339 |
|
| 1340 |
for m in sorted_menu:
|
|
|
|
|
|
|
| 1341 |
old_n = m.get("name", "")
|
| 1342 |
-
|
| 1343 |
with gr.Column(elem_classes="menu-card-admin"):
|
| 1344 |
with gr.Row():
|
| 1345 |
e_enable = gr.Checkbox(label="上架啟用", value=m.get("enable", True), min_width=50)
|
| 1346 |
-
e_seq = gr.Number(label="排序(流水號)", value=
|
| 1347 |
e_name = gr.Textbox(label="品項名稱", value=old_n)
|
| 1348 |
e_cat = gr.Dropdown(choices=existing_cats, label="分類", value=m.get("category", "未分類"), allow_custom_value=True)
|
| 1349 |
with gr.Row():
|
| 1350 |
e_price = gr.Number(label="價格(NT$)", value=m.get("price", 0), min_width=80)
|
| 1351 |
e_cost = gr.Number(label="成本(NT$)", value=m.get("cost", 0), min_width=80)
|
| 1352 |
-
e_spec = gr.Textbox(label="規格", value=
|
| 1353 |
with gr.Row():
|
| 1354 |
e_lvl = gr.Textbox(label="適用等級", value=m.get("level", "全部"), min_width=80)
|
| 1355 |
e_note = gr.Textbox(label="備註", value=m.get("note", ""), min_width=80)
|
|
@@ -1358,7 +1382,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1358 |
save_btn = gr.Button("💾 儲存", variant="primary")
|
| 1359 |
del_btn = gr.Button("🗑️ 刪除", variant="stop")
|
| 1360 |
|
| 1361 |
-
def make_save_fn(
|
| 1362 |
def save_item(seq_val, en_val, n_val, c_val, p_val, co_val, s_val, l_val, note_val, cur_cat, cur_lvl, cur_item):
|
| 1363 |
global MENU
|
| 1364 |
n_val = str(n_val).strip() if n_val else ""
|
|
@@ -1366,7 +1390,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1366 |
return "❌ 品項名稱不可為空!", gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
| 1367 |
|
| 1368 |
for item in MENU:
|
| 1369 |
-
if item.get("
|
| 1370 |
item.update({
|
| 1371 |
"name": n_val, "spec": str(s_val).strip() if s_val else "",
|
| 1372 |
"seq": int(seq_val) if seq_val is not None else 999,
|
|
@@ -1394,12 +1418,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1394 |
return f"✅ {n_val} 已儲存,流水號已自動重排!", datetime.now(TAIPEI_TZ).timestamp(), gr.update(choices=c_choices, value=cv), gr.update(choices=l_choices, value=lv), gr.update(choices=i_choices, value=iv)
|
| 1395 |
return save_item
|
| 1396 |
|
| 1397 |
-
def make_del_fn(
|
| 1398 |
def delete_item(cur_cat, cur_lvl, cur_item):
|
| 1399 |
if cur_cat == "__CANCEL__":
|
| 1400 |
return gr.skip(), gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
|
|
|
| 1401 |
global MENU
|
| 1402 |
-
MENU = [item for item in MENU if
|
| 1403 |
MENU.sort(key=lambda x: x.get("seq", 999))
|
| 1404 |
for i, item in enumerate(MENU): item["seq"] = i + 1
|
| 1405 |
save_menu_to_file()
|
|
@@ -1412,16 +1437,16 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1412 |
lv = cur_lvl if cur_lvl in l_choices else "全部等級"
|
| 1413 |
iv = cur_item if cur_item in i_choices else "所有品項"
|
| 1414 |
|
| 1415 |
-
return f"🗑️ {
|
| 1416 |
return delete_item
|
| 1417 |
|
| 1418 |
save_btn.click(
|
| 1419 |
-
fn=make_save_fn(
|
| 1420 |
inputs=[e_seq, e_enable, e_name, e_cat, e_price, e_cost, e_spec, e_lvl, e_note, menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1421 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
| 1422 |
)
|
| 1423 |
del_btn.click(
|
| 1424 |
-
fn=make_del_fn(
|
| 1425 |
inputs=[menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1426 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1427 |
js="(c, l, i) => confirm('⚠️ 警告:確定要刪除這個品項嗎?動作無法復原!') ? [c, l, i] : ['__CANCEL__', l, i]"
|
|
|
|
| 119 |
|
| 120 |
def load_menu():
|
| 121 |
loaded_menu = []
|
| 122 |
+
# 支援讀取舊版的 Dictionary 格式,並自動轉換成可容許重複品項的 List 格式
|
| 123 |
if os.path.exists(MENU_FILE):
|
| 124 |
try:
|
| 125 |
with open(MENU_FILE, "r", encoding="utf-8") as f:
|
|
|
|
| 185 |
{"name": "手工餅乾", "price": 50, "cost": 20, "enable": True, "level": "全部", "seq": 5, "category": "🍪 美味點心", "spec": "一份", "note": ""}
|
| 186 |
]
|
| 187 |
else:
|
| 188 |
+
# 強制確保流水號正確排列
|
| 189 |
+
loaded_menu.sort(key=lambda x: x.get("seq", 999))
|
| 190 |
for i, v in enumerate(loaded_menu):
|
| 191 |
if "enable" not in v: v["enable"] = True
|
| 192 |
if "level" not in v: v["level"] = "全部"
|
| 193 |
+
v["seq"] = i + 1
|
| 194 |
if "category" not in v: v["category"] = "未分類"
|
| 195 |
if "spec" not in v: v["spec"] = ""
|
| 196 |
if "note" not in v: v["note"] = ""
|
|
|
|
| 258 |
cat = cat.strip() if cat.strip() else "未分類"
|
| 259 |
if cat not in category_order:
|
| 260 |
category_order.append(cat)
|
| 261 |
+
# 提取名稱並附上原本的字典資料
|
| 262 |
items_by_cat.setdefault(cat, []).append((details.get("name", "未命名"), details))
|
| 263 |
|
| 264 |
flat_list = []
|
|
|
|
| 361 |
return gr.update(value=f"🗑️ 會員 {acc} 已刪除!"), get_members_df()
|
| 362 |
return gr.update(value=f"❌ 找不到會員 {acc}"), gr.update()
|
| 363 |
|
| 364 |
+
def _internal_update_members(df):
|
| 365 |
+
global MEMBERS
|
| 366 |
+
if df is None: return
|
| 367 |
+
new_members = {}
|
| 368 |
+
for row in df.values.tolist():
|
| 369 |
+
acc = str(row[0]).strip() if pd.notna(row[0]) else ""
|
| 370 |
+
pwd = str(row[1]).strip() if pd.notna(row[1]) else ""
|
| 371 |
+
lvl = str(row[2]).strip() if pd.notna(row[2]) else "1"
|
| 372 |
+
name = str(row[3]).strip() if pd.notna(row[3]) else ""
|
| 373 |
+
loc = str(row[4]).strip() if pd.notna(row[4]) else ""
|
| 374 |
+
|
| 375 |
+
if acc:
|
| 376 |
+
new_members[acc] = {"password": pwd, "level": lvl, "name": name, "location": loc}
|
| 377 |
+
|
| 378 |
+
MEMBERS.clear()
|
| 379 |
+
MEMBERS.update(new_members)
|
| 380 |
+
save_members_to_file()
|
| 381 |
+
|
| 382 |
def update_members_list(df):
|
| 383 |
_internal_update_members(df)
|
| 384 |
return gr.update(value=f"✅ 會員資料已成功更新!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"), get_members_df()
|
|
|
|
| 1360 |
existing_cats = [c for c in get_menu_cat_choices() if c != "全部分類"]
|
| 1361 |
|
| 1362 |
for m in sorted_menu:
|
| 1363 |
+
# 改用隱藏的唯一 seq 來進行內部精準定位追蹤
|
| 1364 |
+
old_seq = m.get("seq", 999)
|
| 1365 |
old_n = m.get("name", "")
|
| 1366 |
+
|
| 1367 |
with gr.Column(elem_classes="menu-card-admin"):
|
| 1368 |
with gr.Row():
|
| 1369 |
e_enable = gr.Checkbox(label="上架啟用", value=m.get("enable", True), min_width=50)
|
| 1370 |
+
e_seq = gr.Number(label="排序(流水號)", value=old_seq, precision=0, min_width=80)
|
| 1371 |
e_name = gr.Textbox(label="品項名稱", value=old_n)
|
| 1372 |
e_cat = gr.Dropdown(choices=existing_cats, label="分類", value=m.get("category", "未分類"), allow_custom_value=True)
|
| 1373 |
with gr.Row():
|
| 1374 |
e_price = gr.Number(label="價格(NT$)", value=m.get("price", 0), min_width=80)
|
| 1375 |
e_cost = gr.Number(label="成本(NT$)", value=m.get("cost", 0), min_width=80)
|
| 1376 |
+
e_spec = gr.Textbox(label="規格", value=m.get("spec", ""), placeholder="例如: 中杯、大份")
|
| 1377 |
with gr.Row():
|
| 1378 |
e_lvl = gr.Textbox(label="適用等級", value=m.get("level", "全部"), min_width=80)
|
| 1379 |
e_note = gr.Textbox(label="備註", value=m.get("note", ""), min_width=80)
|
|
|
|
| 1382 |
save_btn = gr.Button("💾 儲存", variant="primary")
|
| 1383 |
del_btn = gr.Button("🗑️ 刪除", variant="stop")
|
| 1384 |
|
| 1385 |
+
def make_save_fn(o_seq):
|
| 1386 |
def save_item(seq_val, en_val, n_val, c_val, p_val, co_val, s_val, l_val, note_val, cur_cat, cur_lvl, cur_item):
|
| 1387 |
global MENU
|
| 1388 |
n_val = str(n_val).strip() if n_val else ""
|
|
|
|
| 1390 |
return "❌ 品項名稱不可為空!", gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
| 1391 |
|
| 1392 |
for item in MENU:
|
| 1393 |
+
if item.get("seq") == o_seq:
|
| 1394 |
item.update({
|
| 1395 |
"name": n_val, "spec": str(s_val).strip() if s_val else "",
|
| 1396 |
"seq": int(seq_val) if seq_val is not None else 999,
|
|
|
|
| 1418 |
return f"✅ {n_val} 已儲存,流水號已自動重排!", datetime.now(TAIPEI_TZ).timestamp(), gr.update(choices=c_choices, value=cv), gr.update(choices=l_choices, value=lv), gr.update(choices=i_choices, value=iv)
|
| 1419 |
return save_item
|
| 1420 |
|
| 1421 |
+
def make_del_fn(o_seq, o_name):
|
| 1422 |
def delete_item(cur_cat, cur_lvl, cur_item):
|
| 1423 |
if cur_cat == "__CANCEL__":
|
| 1424 |
return gr.skip(), gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
| 1425 |
+
|
| 1426 |
global MENU
|
| 1427 |
+
MENU = [item for item in MENU if item.get("seq") != o_seq]
|
| 1428 |
MENU.sort(key=lambda x: x.get("seq", 999))
|
| 1429 |
for i, item in enumerate(MENU): item["seq"] = i + 1
|
| 1430 |
save_menu_to_file()
|
|
|
|
| 1437 |
lv = cur_lvl if cur_lvl in l_choices else "全部等級"
|
| 1438 |
iv = cur_item if cur_item in i_choices else "所有品項"
|
| 1439 |
|
| 1440 |
+
return f"🗑️ {o_name} 已刪除,流水號已重排!", datetime.now(TAIPEI_TZ).timestamp(), gr.update(choices=c_choices, value=cv), gr.update(choices=l_choices, value=lv), gr.update(choices=i_choices, value=iv)
|
| 1441 |
return delete_item
|
| 1442 |
|
| 1443 |
save_btn.click(
|
| 1444 |
+
fn=make_save_fn(old_seq),
|
| 1445 |
inputs=[e_seq, e_enable, e_name, e_cat, e_price, e_cost, e_spec, e_lvl, e_note, menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1446 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
| 1447 |
)
|
| 1448 |
del_btn.click(
|
| 1449 |
+
fn=make_del_fn(old_seq, old_n),
|
| 1450 |
inputs=[menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1451 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1452 |
js="(c, l, i) => confirm('⚠️ 警告:確定要刪除這個品項嗎?動作無法復原!') ? [c, l, i] : ['__CANCEL__', l, i]"
|