DeepLearning101 commited on
Commit
cee18dd
·
verified ·
1 Parent(s): cdd1e7c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -1
main.py CHANGED
@@ -312,4 +312,33 @@ def notify_boss(name, tel, date, time, pax, amount):
312
  headers = {"Authorization": f"Bearer {LINE_ACCESS_TOKEN}"}
313
  payload = {"to": BOSS_LINE_ID, "messages": [{"type": "text", "text": msg}]}
314
  try: requests.post("https://api.line.me/v2/bot/message/push", headers=headers, json=payload)
315
- except: pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  headers = {"Authorization": f"Bearer {LINE_ACCESS_TOKEN}"}
313
  payload = {"to": BOSS_LINE_ID, "messages": [{"type": "text", "text": msg}]}
314
  try: requests.post("https://api.line.me/v2/bot/message/push", headers=headers, json=payload)
315
+ except: pass
316
+
317
+ # ==========================================
318
+ # 🌟 新增:計算某日庫存與銷量的 API 🌟
319
+ # ==========================================
320
+ @app.get("/api/inventory/{query_date}")
321
+ async def get_inventory(query_date: str):
322
+ if not supabase: return {}
323
+ try:
324
+ # 找出這天所有的訂單
325
+ res = supabase.table("bookings").select("cart, status").eq("date", query_date).execute()
326
+ sold_counts = {}
327
+ if res.data:
328
+ for b in res.data:
329
+ # 排除已經取消或 No-Show 的訂單,把扣打還給庫存
330
+ if "取消" in b.get("status", "") or "No-Show" in b.get("status", ""):
331
+ continue
332
+
333
+ # 計算這筆訂單買了什麼
334
+ cart = b.get("cart", {})
335
+ for item_id, qty in cart.items():
336
+ # 防呆:確保 qty 是整數
337
+ try: qty = int(qty)
338
+ except: qty = 0
339
+ sold_counts[item_id] = sold_counts.get(item_id, 0) + qty
340
+
341
+ return sold_counts # 回傳格式如:{"steak": 2, "fries": 5}
342
+ except Exception as e:
343
+ print(f"Inventory Error: {e}")
344
+ return {}