File size: 2,803 Bytes
b553d22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f6156f
b553d22
 
0f6156f
b553d22
 
 
 
 
 
 
 
 
 
0f6156f
b553d22
 
 
 
 
 
 
0f6156f
 
 
 
 
 
 
 
b553d22
 
 
 
 
0f6156f
 
 
 
 
 
 
 
 
 
b553d22
0f6156f
 
 
 
 
b553d22
 
 
 
 
 
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
import gradio as gr
import firebase_admin
from firebase_admin import credentials, firestore

# Firebase Admin SDK initialization
cred = credentials.Certificate("key.json")  # Replace with your Firebase service account key file
firebase_admin.initialize_app(cred)
db = firestore.client()

# Function to get orders from Firestore and calculate totals
def fetch_orders():
    orders = db.collection("orders").get()
    orders_display = []
    grand_total = 0

    for order in orders:
        order_data = order.to_dict()
        order_id = order.id  # Capture document ID for delete operations
        name = order_data.get("name")
        phone = order_data.get("phone", "N/A")
        hostel = order_data.get("hostel_name", "N/A")
        items = order_data.get("items", [])
        total_price = order_data.get("total_price", 0)
        grand_total += total_price

        for item in items:
            if isinstance(item, dict):
                category = item.get("category", "")
                item_name = item.get("item", "")
                quantity = item.get("quantity", 0)
                price = item.get("price", 0)
                orders_display.append([order_id, name, phone, hostel, category, item_name, quantity, price])
                name, phone, hostel = "", "", ""

        orders_display.append(["", "", "", "", "Subtotal", "", total_price])

    orders_display.append(["", "", "", "", "Grand Total", "", grand_total])
    return orders_display

# Function to delete an order
def delete_order(order_id):
    try:
        db.collection("orders").document(order_id).delete()
        return f"Order with ID {order_id} deleted successfully!"
    except Exception as e:
        return f"Failed to delete order: {e}"

# Gradio Interface
def gradio_display_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### ADMIN PANEL")

        # Display section for individual order totals with headers
        orders_display = gr.Dataframe(value=[], headers=["ID", "Name", "Phone", "Hostel", "Category", "Item", "Quantity", "Price"], interactive=False, wrap=True)
        
        # Input field for delete operation
        order_id = gr.Textbox(label="Order ID (for Delete)", placeholder="Enter Order ID here")
        
        # Textbox for displaying the result message
        result_message = gr.Textbox(label="Result", interactive=False)

        # Buttons for operations
        fetch_button = gr.Button("Fetch Orders")
        delete_button = gr.Button("Delete Order")

        # Button functionalities
        fetch_button.click(fetch_orders, inputs=[], outputs=[orders_display])
        delete_button.click(delete_order, inputs=[order_id], outputs=[result_message])

    return demo

# Run the Gradio app
if __name__ == "__main__":
    gradio_display_interface().launch()