File size: 3,284 Bytes
3363dcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import pyrebase
import json
import streamlit as st
import time
from io import StringIO
import sys
import hashlib

firebase_config = json.loads(st.secrets["FIREBASE_CREDENTIALS"])

with open("temp_credentials.json", "w") as f:
    json.dump(firebase_config, f)

config = {
    "apiKey": firebase_config.get("api_key", ""),
    "authDomain": f"{firebase_config['project_id']}.firebaseapp.com",
    "databaseURL": "https://egg-order-system-default-rtdb.asia-southeast1.firebasedatabase.app/",
    "storageBucket": f"{firebase_config['project_id']}.appspot.com",
    "serviceAccount": StringIO(json.dumps(firebase_config))
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()

def print_error_and_exit(e):
    print("Firebase 連線或寫入失敗,請檢查下列事項:")
    print("1. databaseURL 是否正確")
    print("2. FIREBASE_CREDENTIALS 格式是否正確")
    print("3. Firebase Database Rules 權限是否允許")
    print("4. pyrebase 版本是否相容")
    print("詳細錯誤訊息:", e)
    sys.exit(1)

# 新增訂單
def append_order(order_id, content, price, status, note):
    data = {
        "訂單編號": order_id,
        "品項內容": content,
        "金額": price,
        "狀態": status,
        "備註": note,
        "timestamp": time.time()
    }
    try:
        db.child("orders").child(order_id).set(data)
    except Exception as e:
        print_error_and_exit(e)

# 根據狀態抓取訂單
def fetch_orders(status="未完成"):
    try:
        all_data = db.child("orders").get().val()
        if not all_data:
            return []
        return [v for v in all_data.values() if v.get("狀態") == status]
    except Exception as e:
        print("[ERROR] 讀取 Firebase 訂單失敗:", e)
        return []

# 更新訂單品項內容與金額
def update_order_content(order_id, new_items, new_amount):
    try:
        db.child("orders").child(order_id).update({
            "品項內容": new_items,
            "金額": new_amount
        })
    except Exception as e:
        print_error_and_exit(e)

# 更新完成項目與金額(累加)
def update_completed_items(order_id, new_items, new_amount):
    try:
        order_ref = db.child("orders").child(order_id)
        existing = order_ref.get().val()

        old_completed = existing.get("completed_items", [])
        if not isinstance(old_completed, list):
            old_completed = [old_completed]

        updated_completed = old_completed + new_items

        old_amount = existing.get("金額", 0)
        updated_amount = old_amount + new_amount

        order_ref.update({
            "completed_items": updated_completed,
            "金額": updated_amount
        })
    except Exception as e:
        print_error_and_exit(e)

# 標記訂單為完成狀態
def mark_order_done(order_id):
    try:
        db.child("orders").child(order_id).update({
            "狀態": "完成"
        })
    except Exception as e:
        print_error_and_exit(e)

# 刪除訂單
def delete_order_by_id(order_id):
    try:
        db.child("orders").child(order_id).remove()
    except Exception as e:
        print_error_and_exit(e)