schillerCFK commited on
Commit
c489fbd
·
verified ·
1 Parent(s): d1534f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -36
app.py CHANGED
@@ -442,7 +442,7 @@ def get_inv_df():
442
  p_map = {}
443
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
444
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
445
- p_map[f"{c}|{n}|{s}"] = row["sq"]
446
 
447
  menu_items = conn.execute("SELECT * FROM menu ORDER BY seq").fetchall()
448
 
@@ -461,7 +461,7 @@ def get_inv_df():
461
  i_s = str(inv) if inv < 900 else "無限制"
462
  av_s = i_s
463
  else:
464
- p = p_map.get(key, 0)
465
  av = inv - p
466
  i_s, av_s = (str(inv) if inv < 900 else "無限制"), (str(av) if inv < 900 else "無限制")
467
 
@@ -547,15 +547,17 @@ def get_menu_html():
547
  p_map = {}
548
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
549
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
550
- p_map[f"{c}|{n}|{s}"] = row["sq"]
551
 
552
  tabs = []
553
  sections_by_tab = {}
554
  items_by_cat = {}
555
  step1_items = []
556
 
557
- for m in enabled_menu:
558
- c = str(m["category"] or "")
 
 
559
  c_l = c.lower()
560
  is_s1 = "step 1" in c_l or "step1" in c_l or "魚種" in c_l
561
  is_s2 = "step 2" in c_l or "step2" in c_l or "金額" in c_l
@@ -572,7 +574,7 @@ def get_menu_html():
572
  if tab_name not in tabs: tabs.append(tab_name)
573
  if tab_name not in sections_by_tab: sections_by_tab[tab_name] = []
574
  if c not in sections_by_tab[tab_name]: sections_by_tab[tab_name].append(c)
575
- items_by_cat.setdefault(c, []).append(dict(m))
576
 
577
  tabs_html = "<div class='menu-category-tabs'>"
578
  tabs_html += "<button class='cat-tab-btn active' onclick='window.filterMenu(\"all\", this)'>🌟 全部顯示</button>"
@@ -660,7 +662,7 @@ def add_order(c_name, p_method, d_loc, d_date, d_time, nts, order_json):
660
  p_map = {}
661
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
662
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
663
- p_map[f"{c}|{n}|{s}"] = row["sq"]
664
 
665
  flat = {f"{str(m['category'] or '')}|{str(m['name'] or '')}|{str(m['spec'] or '')}": dict(m) for m in enabled}
666
 
@@ -720,7 +722,8 @@ def get_latest_orders_html():
720
 
721
  today_str = datetime.now(TAIPEI_TZ).strftime("%Y-%m-%d")
722
  df = pd.DataFrame(orders_data)
723
- df['date'] = df['time'].str[:10]
 
724
  today_df = df[df['date'] == today_str]
725
 
726
  total_rev = today_df['total'].sum() if not today_df.empty else 0
@@ -1294,37 +1297,43 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), title="鑫生魚片
1294
  for btn, n in zip(all_n, p_names): btn.click(lambda name=n: switch(name), None, all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1295
 
1296
  def do_login_proc(acc, pwd):
1297
- u = do_login(acc, pwd)
1298
- if u["acc"]=="guest_error": return [gr.update(visible=True), gr.update(visible=False), "<div style='color:red;text-align:center;'>❌ 帳號或密碼錯誤</div>", ""] + [gr.update(visible=False)]*7 + [gr.update(visible=False)]*7 + [gr.update()]*7
1299
- lvl, msg = u["level"], f"🍣 **歡迎回來,{u['name']}**"
1300
-
1301
- s_o = lvl in [0,1,2,9]
1302
- s_k = lvl in [8,9]
1303
- s_a = lvl == 9
1304
-
1305
- return [
1306
- gr.update(visible=False), gr.update(visible=True), "", msg,
1307
- gr.update(visible=s_o), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1308
- gr.update(visible=s_o, variant="primary" if s_o else "secondary"),
1309
- gr.update(visible=s_k, variant="primary" if s_k and not s_o else "secondary"),
1310
- gr.update(visible=s_a, variant="secondary"),
1311
- gr.update(visible=s_a, variant="secondary"),
1312
- gr.update(visible=s_a, variant="secondary"),
1313
- gr.update(visible=s_a, variant="secondary"),
1314
- gr.update(visible=s_a, variant="secondary"),
1315
- get_menu_html() if s_o else "", get_kitchen_html() if s_k else "", get_menu_df() if s_a else pd.DataFrame(), get_inv_df() if s_a else pd.DataFrame(), gr.update(choices=get_inv_choices()) if s_a else gr.update(), get_members_df() if s_a else pd.DataFrame(), get_latest_orders_html() if s_a else ""
1316
- ]
 
 
 
 
1317
 
1318
  def do_guest_login_proc():
1319
- s_o, s_k, s_a = True, False, False
1320
- return [
1321
- gr.update(visible=False), gr.update(visible=True), "", "🍣 **歡迎光臨!請盡情選購**",
1322
- gr.update(visible=s_o), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1323
- gr.update(visible=s_o, variant="primary"), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1324
- get_menu_html(), "", pd.DataFrame(), pd.DataFrame(), gr.update(), pd.DataFrame(), ""
1325
- ]
 
 
 
1326
 
1327
- # 🌟 修正:綁定 Enter 鍵送出,保證操作順暢
1328
  l_btn.click(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1329
  l_acc.submit(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1330
  l_pwd.submit(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
 
442
  p_map = {}
443
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
444
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
445
+ p_map[f"{c}|{n}|{s}"] = safe_int(row["sq"])
446
 
447
  menu_items = conn.execute("SELECT * FROM menu ORDER BY seq").fetchall()
448
 
 
461
  i_s = str(inv) if inv < 900 else "無限制"
462
  av_s = i_s
463
  else:
464
+ p = safe_int(p_map.get(key, 0))
465
  av = inv - p
466
  i_s, av_s = (str(inv) if inv < 900 else "無限制"), (str(av) if inv < 900 else "無限制")
467
 
 
547
  p_map = {}
548
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
549
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
550
+ p_map[f"{c}|{n}|{s}"] = safe_int(row["sq"])
551
 
552
  tabs = []
553
  sections_by_tab = {}
554
  items_by_cat = {}
555
  step1_items = []
556
 
557
+ for row in enabled_menu:
558
+ # 🌟 將 Row 物件安全轉為字典,防止 .get() 報錯崩潰!
559
+ m = dict(row)
560
+ c = str(m.get("category", ""))
561
  c_l = c.lower()
562
  is_s1 = "step 1" in c_l or "step1" in c_l or "魚種" in c_l
563
  is_s2 = "step 2" in c_l or "step2" in c_l or "金額" in c_l
 
574
  if tab_name not in tabs: tabs.append(tab_name)
575
  if tab_name not in sections_by_tab: sections_by_tab[tab_name] = []
576
  if c not in sections_by_tab[tab_name]: sections_by_tab[tab_name].append(c)
577
+ items_by_cat.setdefault(c, []).append(m)
578
 
579
  tabs_html = "<div class='menu-category-tabs'>"
580
  tabs_html += "<button class='cat-tab-btn active' onclick='window.filterMenu(\"all\", this)'>🌟 全部顯示</button>"
 
662
  p_map = {}
663
  for row in conn.execute("SELECT category, item, spec, SUM(qty) as sq FROM orders WHERE status='待出餐' GROUP BY category, item, spec").fetchall():
664
  c, n, s = str(row["category"] or ""), str(row["item"] or ""), str(row["spec"] or "")
665
+ p_map[f"{c}|{n}|{s}"] = safe_int(row["sq"])
666
 
667
  flat = {f"{str(m['category'] or '')}|{str(m['name'] or '')}|{str(m['spec'] or '')}": dict(m) for m in enabled}
668
 
 
722
 
723
  today_str = datetime.now(TAIPEI_TZ).strftime("%Y-%m-%d")
724
  df = pd.DataFrame(orders_data)
725
+ # 🌟 防呆:確保時間字串擷取絕對安全
726
+ df['date'] = df['time'].astype(str).str[:10]
727
  today_df = df[df['date'] == today_str]
728
 
729
  total_rev = today_df['total'].sum() if not today_df.empty else 0
 
1297
  for btn, n in zip(all_n, p_names): btn.click(lambda name=n: switch(name), None, all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1298
 
1299
  def do_login_proc(acc, pwd):
1300
+ # 🌟 超強除錯防護罩:即使遇到爛資料,也絕對不讓系統死當!
1301
+ try:
1302
+ u = do_login(acc, pwd)
1303
+ if u["acc"]=="guest_error": return [gr.update(visible=True), gr.update(visible=False), "<div style='color:red;text-align:center;'>❌ 帳號或密碼錯誤</div>", ""] + [gr.update(visible=False)]*7 + [gr.update(visible=False)]*7 + [gr.update()]*7
1304
+ lvl, msg = u["level"], f"🍣 **歡迎回來,{u['name']}**"
1305
+
1306
+ s_o = lvl in [0,1,2,9]
1307
+ s_k = lvl in [8,9]
1308
+ s_a = lvl == 9
1309
+
1310
+ return [
1311
+ gr.update(visible=False), gr.update(visible=True), "", msg,
1312
+ gr.update(visible=s_o), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1313
+ gr.update(visible=s_o, variant="primary" if s_o else "secondary"),
1314
+ gr.update(visible=s_k, variant="primary" if s_k and not s_o else "secondary"),
1315
+ gr.update(visible=s_a, variant="secondary"),
1316
+ gr.update(visible=s_a, variant="secondary"),
1317
+ gr.update(visible=s_a, variant="secondary"),
1318
+ gr.update(visible=s_a, variant="secondary"),
1319
+ gr.update(visible=s_a, variant="secondary"),
1320
+ get_menu_html() if s_o else "", get_kitchen_html() if s_k else "", get_menu_df() if s_a else pd.DataFrame(), get_inv_df() if s_a else pd.DataFrame(), gr.update(choices=get_inv_choices()) if s_a else gr.update(), get_members_df() if s_a else pd.DataFrame(), get_latest_orders_html() if s_a else ""
1321
+ ]
1322
+ except Exception as e:
1323
+ return [gr.update(visible=True), gr.update(visible=False), f"<div style='color:red;text-align:center;'>❌ 系統錯誤:{str(e)}<br>請點擊設定中的強制還原</div>", ""] + [gr.update(visible=False)]*7 + [gr.update(visible=False)]*7 + [gr.update()]*7
1324
 
1325
  def do_guest_login_proc():
1326
+ try:
1327
+ s_o, s_k, s_a = True, False, False
1328
+ return [
1329
+ gr.update(visible=False), gr.update(visible=True), "", "🍣 **歡迎光臨!請盡情選購**",
1330
+ gr.update(visible=s_o), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1331
+ gr.update(visible=s_o, variant="primary"), gr.update(visible=s_k), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a), gr.update(visible=s_a),
1332
+ get_menu_html(), "", pd.DataFrame(), pd.DataFrame(), gr.update(), pd.DataFrame(), ""
1333
+ ]
1334
+ except Exception as e:
1335
+ return [gr.update(visible=True), gr.update(visible=False), f"<div style='color:red;text-align:center;'>❌ 系統錯誤:{str(e)}</div>", ""] + [gr.update(visible=False)]*7 + [gr.update(visible=False)]*7 + [gr.update()]*7
1336
 
 
1337
  l_btn.click(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1338
  l_acc.submit(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])
1339
  l_pwd.submit(do_login_proc, [l_acc, l_pwd], [login_box, main_box, l_msg, greeting] + all_p + all_n + [order_menu_html, k_html, mdf, inv_df, inv_name, mm_df, r_html])