Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -782,12 +782,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), title="鑫生魚片
|
|
| 782 |
mdf = gr.Dataframe(interactive=False)
|
| 783 |
|
| 784 |
with gr.Column(visible=False) as page_inventory:
|
| 785 |
-
gr.Markdown("### 📦 庫存精準控管\n> **提示**:
|
| 786 |
inv_out = gr.Textbox(label="系統回報", interactive=False)
|
| 787 |
with gr.Row():
|
| 788 |
with gr.Column(scale=1):
|
| 789 |
-
inv_name
|
| 790 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
inv_df = gr.Dataframe(interactive=False, scale=2)
|
| 792 |
|
| 793 |
with gr.Column(visible=False) as page_members:
|
|
@@ -829,15 +838,50 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), title="鑫生魚片
|
|
| 829 |
|
| 830 |
# 🌟 庫存更新使用連線池
|
| 831 |
def update_inv_and_refresh(n, q):
|
|
|
|
| 832 |
with db_lock:
|
| 833 |
with get_db_connection() as conn:
|
| 834 |
conn.execute("UPDATE menu SET inventory=? WHERE name=?", (int(q), n))
|
| 835 |
conn.commit()
|
| 836 |
trigger_background_backup()
|
| 837 |
-
return f"✅ {n} 已
|
| 838 |
-
|
| 839 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 840 |
inv_sv.click(update_inv_and_refresh, [inv_name, inv_qty], [inv_out, inv_df])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
|
| 842 |
hidden_serve_id.change(serve_k_order_by_id, [hidden_serve_id], [res_msg, k_html]).then(lambda: "", None, [hidden_serve_id])
|
| 843 |
hidden_delete_id.change(delete_order_by_id, [hidden_delete_id], [res_msg, r_html]).then(lambda: "", None, [hidden_delete_id])
|
|
|
|
| 782 |
mdf = gr.Dataframe(interactive=False)
|
| 783 |
|
| 784 |
with gr.Column(visible=False) as page_inventory:
|
| 785 |
+
gr.Markdown("### 📦 庫存精準控管\n> **提示**:點擊右側表格任一列,可快速選取品項帶入。設定 `999` 代表無數量限制(無限售)。")
|
| 786 |
inv_out = gr.Textbox(label="系統回報", interactive=False)
|
| 787 |
with gr.Row():
|
| 788 |
with gr.Column(scale=1):
|
| 789 |
+
inv_name = gr.Dropdown(label="🎯 選擇常規品項", interactive=True)
|
| 790 |
+
with gr.Group():
|
| 791 |
+
gr.Markdown("#### ➕ 快速進貨 (增加數量)")
|
| 792 |
+
with gr.Row():
|
| 793 |
+
inv_add_val = gr.Number(label="進貨數量", value=0, interactive=True, scale=2)
|
| 794 |
+
inv_add_btn = gr.Button("📥 入庫", variant="primary", scale=1)
|
| 795 |
+
with gr.Group():
|
| 796 |
+
gr.Markdown("#### ✏️ 盤點覆蓋 (重設總量)")
|
| 797 |
+
with gr.Row():
|
| 798 |
+
inv_qty = gr.Number(label="目前總實體庫存", interactive=True, scale=2)
|
| 799 |
+
inv_sv = gr.Button("💾 覆蓋", variant="stop", scale=1)
|
| 800 |
inv_df = gr.Dataframe(interactive=False, scale=2)
|
| 801 |
|
| 802 |
with gr.Column(visible=False) as page_members:
|
|
|
|
| 838 |
|
| 839 |
# 🌟 庫存更新使用連線池
|
| 840 |
def update_inv_and_refresh(n, q):
|
| 841 |
+
if not n: return "⚠️ 請先選擇品項", gr.update()
|
| 842 |
with db_lock:
|
| 843 |
with get_db_connection() as conn:
|
| 844 |
conn.execute("UPDATE menu SET inventory=? WHERE name=?", (int(q), n))
|
| 845 |
conn.commit()
|
| 846 |
trigger_background_backup()
|
| 847 |
+
return f"✅ 【{n}】 總庫存已強制覆蓋為 {int(q)}", get_inv_df()
|
| 848 |
+
|
| 849 |
+
def add_inv_and_refresh(n, add_q):
|
| 850 |
+
if not n: return "⚠️ 請先選擇品項", gr.update(), 0
|
| 851 |
+
if not add_q or int(add_q) == 0: return "⚠️ 請輸入有效的進貨數量", gr.update(), 0
|
| 852 |
+
with db_lock:
|
| 853 |
+
with get_db_connection() as conn:
|
| 854 |
+
r = conn.execute("SELECT inventory FROM menu WHERE name=?", (n,)).fetchone()
|
| 855 |
+
if r:
|
| 856 |
+
new_inv = int(r["inventory"]) + int(add_q)
|
| 857 |
+
conn.execute("UPDATE menu SET inventory=? WHERE name=?", (new_inv, n))
|
| 858 |
+
conn.commit()
|
| 859 |
+
else:
|
| 860 |
+
return "⚠️ 找不到該品項", gr.update(), 0
|
| 861 |
+
trigger_background_backup()
|
| 862 |
+
return f"✅ 【{n}】 成功進貨入庫 {int(add_q)} 個!目前總庫存為 {new_inv}", get_inv_df(), 0
|
| 863 |
+
|
| 864 |
+
def handle_inv_selection(evt: gr.SelectData):
|
| 865 |
+
try:
|
| 866 |
+
row_idx = evt.index[0]
|
| 867 |
+
with get_db_connection() as conn:
|
| 868 |
+
menu_items = conn.execute("SELECT * FROM menu ORDER BY seq").fetchall()
|
| 869 |
+
valid_items = [m for m in menu_items if not any(x in str(m["category"]).lower() for x in ["step 2", "step2", "金額"])]
|
| 870 |
+
if row_idx < len(valid_items):
|
| 871 |
+
target = valid_items[row_idx]
|
| 872 |
+
return target["name"], int(target["inventory"]), 0
|
| 873 |
+
except: pass
|
| 874 |
+
return None, 0, 0
|
| 875 |
+
|
| 876 |
+
inv_df.select(handle_inv_selection, None, [inv_name, inv_qty, inv_add_val])
|
| 877 |
+
inv_name.change(lambda n: (next((int(i["inventory"]) for i in get_db_connection().execute("SELECT inventory, name FROM menu").fetchall() if i["name"]==n), 0), 0), [inv_name], [inv_qty, inv_add_val])
|
| 878 |
+
|
| 879 |
inv_sv.click(update_inv_and_refresh, [inv_name, inv_qty], [inv_out, inv_df])
|
| 880 |
+
inv_add_btn.click(add_inv_and_refresh, [inv_name, inv_add_val], [inv_out, inv_df, inv_add_val]).then(
|
| 881 |
+
lambda n: next((int(i["inventory"]) for i in get_db_connection().execute("SELECT inventory, name FROM menu").fetchall() if i["name"]==n), 0),
|
| 882 |
+
[inv_name],
|
| 883 |
+
[inv_qty]
|
| 884 |
+
)
|
| 885 |
|
| 886 |
hidden_serve_id.change(serve_k_order_by_id, [hidden_serve_id], [res_msg, k_html]).then(lambda: "", None, [hidden_serve_id])
|
| 887 |
hidden_delete_id.change(delete_order_by_id, [hidden_delete_id], [res_msg, r_html]).then(lambda: "", None, [hidden_delete_id])
|