Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -119,7 +119,6 @@ orders_db = load_orders()
|
|
| 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:
|
|
@@ -256,7 +255,6 @@ def get_frontend_menu_items(user_level="全部"):
|
|
| 256 |
cat = cat.strip() if cat.strip() else "未分類"
|
| 257 |
if cat not in category_order:
|
| 258 |
category_order.append(cat)
|
| 259 |
-
# 提取名稱並附上原本的字典資料
|
| 260 |
items_by_cat.setdefault(cat, []).append((details.get("name", "未命名"), details))
|
| 261 |
|
| 262 |
flat_list = []
|
|
@@ -359,24 +357,6 @@ def delete_single_member(acc):
|
|
| 359 |
return gr.update(value=f"🗑️ 會員 {acc} 已刪除!"), get_members_df()
|
| 360 |
return gr.update(value=f"❌ 找不到會員 {acc}"), gr.update()
|
| 361 |
|
| 362 |
-
def _internal_update_members(df):
|
| 363 |
-
global MEMBERS
|
| 364 |
-
if df is None: return
|
| 365 |
-
new_members = {}
|
| 366 |
-
for row in df.values.tolist():
|
| 367 |
-
acc = str(row[0]).strip() if pd.notna(row[0]) else ""
|
| 368 |
-
pwd = str(row[1]).strip() if pd.notna(row[1]) else ""
|
| 369 |
-
lvl = str(row[2]).strip() if pd.notna(row[2]) else "1"
|
| 370 |
-
name = str(row[3]).strip() if pd.notna(row[3]) else ""
|
| 371 |
-
loc = str(row[4]).strip() if pd.notna(row[4]) else ""
|
| 372 |
-
|
| 373 |
-
if acc:
|
| 374 |
-
new_members[acc] = {"password": pwd, "level": lvl, "name": name, "location": loc}
|
| 375 |
-
|
| 376 |
-
MEMBERS.clear()
|
| 377 |
-
MEMBERS.update(new_members)
|
| 378 |
-
save_members_to_file()
|
| 379 |
-
|
| 380 |
def update_members_list(df):
|
| 381 |
_internal_update_members(df)
|
| 382 |
return gr.update(value=f"✅ 會員資料已成功更新!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"), get_members_df()
|
|
@@ -1379,7 +1359,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1379 |
del_btn = gr.Button("🗑️ 刪除", variant="stop")
|
| 1380 |
|
| 1381 |
def make_save_fn(old_name, old_spec):
|
| 1382 |
-
def save_item(seq_val, en_val, n_val, c_val, p_val, co_val, s_val, l_val, note_val):
|
| 1383 |
global MENU
|
| 1384 |
n_val = str(n_val).strip() if n_val else ""
|
| 1385 |
if not n_val:
|
|
@@ -1405,11 +1385,19 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1405 |
c_choices = get_menu_cat_choices()
|
| 1406 |
l_choices = get_menu_lvl_choices()
|
| 1407 |
i_choices = get_menu_item_choices()
|
| 1408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1409 |
return save_item
|
| 1410 |
|
| 1411 |
def make_del_fn(old_name, old_spec):
|
| 1412 |
-
def delete_item():
|
|
|
|
|
|
|
| 1413 |
global MENU
|
| 1414 |
MENU = [item for item in MENU if not (item.get("name") == old_name and item.get("spec", "") == old_spec)]
|
| 1415 |
MENU.sort(key=lambda x: x.get("seq", 999))
|
|
@@ -1419,18 +1407,24 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1419 |
c_choices = get_menu_cat_choices()
|
| 1420 |
l_choices = get_menu_lvl_choices()
|
| 1421 |
i_choices = get_menu_item_choices()
|
| 1422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1423 |
return delete_item
|
| 1424 |
|
| 1425 |
save_btn.click(
|
| 1426 |
fn=make_save_fn(old_n, old_s),
|
| 1427 |
-
inputs=[e_seq, e_enable, e_name, e_cat, e_price, e_cost, e_spec, e_lvl, e_note],
|
| 1428 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
| 1429 |
)
|
| 1430 |
del_btn.click(
|
| 1431 |
fn=make_del_fn(old_n, old_s),
|
| 1432 |
-
inputs=[],
|
| 1433 |
-
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
|
|
|
| 1434 |
)
|
| 1435 |
|
| 1436 |
# 永遠常駐在最後面的「新增專用卡片」
|
|
@@ -1450,7 +1444,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1450 |
new_note = gr.Textbox(label="備註", placeholder="內部備註", min_width=80)
|
| 1451 |
add_btn = gr.Button("➕ 確認新增", variant="primary")
|
| 1452 |
|
| 1453 |
-
def add_item(seq_val, en_val, n_val, c_val, p_val, co_val, s_val, l_val, note_val):
|
| 1454 |
global MENU
|
| 1455 |
n_val = str(n_val).strip() if n_val else ""
|
| 1456 |
if not n_val: return "❌ 品項名稱不可為空!", gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
|
@@ -1471,11 +1465,16 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1471 |
c_choices = get_menu_cat_choices()
|
| 1472 |
l_choices = get_menu_lvl_choices()
|
| 1473 |
i_choices = get_menu_item_choices()
|
| 1474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1475 |
|
| 1476 |
add_btn.click(
|
| 1477 |
fn=add_item,
|
| 1478 |
-
inputs=[new_seq, new_enable, new_name, new_cat, new_price, new_cost, new_spec, new_lvl, new_note],
|
| 1479 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
| 1480 |
)
|
| 1481 |
|
|
|
|
| 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:
|
|
|
|
| 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 |
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()
|
|
|
|
| 1359 |
del_btn = gr.Button("🗑️ 刪除", variant="stop")
|
| 1360 |
|
| 1361 |
def make_save_fn(old_name, old_spec):
|
| 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 ""
|
| 1365 |
if not n_val:
|
|
|
|
| 1385 |
c_choices = get_menu_cat_choices()
|
| 1386 |
l_choices = get_menu_lvl_choices()
|
| 1387 |
i_choices = get_menu_item_choices()
|
| 1388 |
+
|
| 1389 |
+
# 保留原本的過濾狀態,避免UI選單變空白導致菜單消失
|
| 1390 |
+
cv = cur_cat if cur_cat in c_choices else "全部分類"
|
| 1391 |
+
lv = cur_lvl if cur_lvl in l_choices else "全部等級"
|
| 1392 |
+
iv = cur_item if cur_item in i_choices else "所有品項"
|
| 1393 |
+
|
| 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(old_name, old_spec):
|
| 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 not (item.get("name") == old_name and item.get("spec", "") == old_spec)]
|
| 1403 |
MENU.sort(key=lambda x: x.get("seq", 999))
|
|
|
|
| 1407 |
c_choices = get_menu_cat_choices()
|
| 1408 |
l_choices = get_menu_lvl_choices()
|
| 1409 |
i_choices = get_menu_item_choices()
|
| 1410 |
+
|
| 1411 |
+
cv = cur_cat if cur_cat in c_choices else "全部分類"
|
| 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"🗑️ {old_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)
|
| 1416 |
return delete_item
|
| 1417 |
|
| 1418 |
save_btn.click(
|
| 1419 |
fn=make_save_fn(old_n, old_s),
|
| 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(old_n, old_s),
|
| 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]"
|
| 1428 |
)
|
| 1429 |
|
| 1430 |
# 永遠常駐在最後面的「新增專用卡片」
|
|
|
|
| 1444 |
new_note = gr.Textbox(label="備註", placeholder="內部備註", min_width=80)
|
| 1445 |
add_btn = gr.Button("➕ 確認新增", variant="primary")
|
| 1446 |
|
| 1447 |
+
def add_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):
|
| 1448 |
global MENU
|
| 1449 |
n_val = str(n_val).strip() if n_val else ""
|
| 1450 |
if not n_val: return "❌ 品項名稱不可為空!", gr.skip(), gr.skip(), gr.skip(), gr.skip()
|
|
|
|
| 1465 |
c_choices = get_menu_cat_choices()
|
| 1466 |
l_choices = get_menu_lvl_choices()
|
| 1467 |
i_choices = get_menu_item_choices()
|
| 1468 |
+
|
| 1469 |
+
cv = cur_cat if cur_cat in c_choices else "全部分類"
|
| 1470 |
+
lv = cur_lvl if cur_lvl in l_choices else "全部等級"
|
| 1471 |
+
iv = cur_item if cur_item in i_choices else "所有品項"
|
| 1472 |
+
|
| 1473 |
+
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)
|
| 1474 |
|
| 1475 |
add_btn.click(
|
| 1476 |
fn=add_item,
|
| 1477 |
+
inputs=[new_seq, new_enable, new_name, new_cat, new_price, new_cost, new_spec, new_lvl, new_note, menu_cat_filter, menu_lvl_filter, menu_item_filter],
|
| 1478 |
outputs=[global_sys_msg, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter]
|
| 1479 |
)
|
| 1480 |
|