File size: 4,930 Bytes
f65f970
 
 
 
 
 
11dc005
f65f970
48267cf
 
 
 
 
 
 
 
 
 
f65f970
48267cf
 
 
 
 
 
f65f970
48267cf
 
 
 
 
 
 
 
 
 
 
 
 
 
f65f970
 
 
48267cf
f65f970
 
 
 
48267cf
 
f65f970
 
 
 
 
 
 
 
48267cf
f65f970
 
 
48267cf
f65f970
48267cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f65f970
 
 
48267cf
 
 
 
 
 
 
f65f970
 
88a35fd
48267cf
88a35fd
48267cf
 
f65f970
 
48267cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f65f970
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import bcrypt
import gradio as gr
from datetime import datetime
from simple_salesforce import Salesforce

# Salesforce Connection
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')

# Function to Hash Password
def hash_password(password):
    return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')

# Function to Verify Password
def verify_password(plain_password, hashed_password):
    return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))

# Fetch menu items from Salesforce
def fetch_menu_items():
    try:
        query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c"
        result = sf.query(query)
        return result['records']
    except Exception as e:
        print(f"Error fetching menu items: {e}")
        return []

# Fetch add-ons from Salesforce
def fetch_add_ons():
    try:
        query = "SELECT Name, Price__c FROM Add_Ons__c"
        result = sf.query(query)
        return result['records']
    except Exception as e:
        print(f"Error fetching add-ons: {e}")
        return []

# Save cart details to Salesforce
def save_cart(cart_items, total_cost):
    try:
        # Create order record
        order_record = {
            'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
            'Total_Cost__c': total_cost,
            'Order_Date__c': datetime.now().isoformat()
        }
        order_result = sf.Order__c.create(order_record)
        order_id = order_result['id']

        # Save cart items as order items
        for item in cart_items:
            extras = ", ".join(extra['name'] for extra in item.get('extras', []))
            order_item_record = {
                'Name': item['name'],
                'Order__c': order_id,
                'Quantity__c': item['quantity'],
                'Price__c': item['price'],
                'Extras__c': extras,
                'Instructions__c': item.get('instructions', ''),
                'Total_Cost__c': item['total_cost']
            }
            sf.Order_Item__c.create(order_item_record)

        return f"Order {order_id} saved successfully!"
    except Exception as e:
        print(f"Error saving cart: {e}")
        return f"Error saving cart: {str(e)}"

# Function to filter menu based on preference
def filter_menu(preference):
    menu_items = fetch_menu_items()
    filtered_items = [item for item in menu_items if preference == "All" or item['Veg_NonVeg__c'] == preference]

    menu_output = {}
    for item in filtered_items:
        section = item.get('Section__c', 'Uncategorized')
        if section not in menu_output:
            menu_output[section] = []
        menu_output[section].append(item)
    return menu_output

# Gradio App
with gr.Blocks() as app:
    with gr.Row():
        gr.HTML("<h1>Welcome to Biryani Hub</h1>")

    # Menu and Cart Interface
    with gr.Row():
        preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], value="All", label="Filter Preference")
        menu_display = gr.JSON(label="Menu Items")
        cart_display = gr.JSON(label="Cart Items")
        total_cost_display = gr.Number(label="Total Cost", value=0)

    with gr.Row():
        item_name = gr.Textbox(label="Item Name")
        item_price = gr.Number(label="Price", value=0)
        item_quantity = gr.Number(label="Quantity", value=1)
        add_ons_display = gr.JSON(label="Add-Ons")
        special_instructions = gr.Textbox(label="Special Instructions")

    with gr.Row():
        add_to_cart_button = gr.Button("Add to Cart")
        save_order_button = gr.Button("Save Order")

    # State
    cart = gr.State([])  # Cart state to maintain items

    def update_menu(preference):
        menu_items = filter_menu(preference)
        return menu_items, fetch_add_ons()

    def add_to_cart(cart, name, price, quantity, add_ons, instructions):
        if not name or quantity <= 0:
            return cart, "Invalid item details!"
        total_cost = price * quantity
        cart.append({
            "name": name,
            "price": price,
            "quantity": quantity,
            "extras": add_ons,
            "instructions": instructions,
            "total_cost": total_cost
        })
        return cart, f"Added {name} to cart!"

    def save_order(cart):
        total_cost = sum(item['total_cost'] for item in cart)
        response = save_cart(cart, total_cost)
        return response

    # Interactivity
    preference.change(update_menu, inputs=[preference], outputs=[menu_display, add_ons_display])
    add_to_cart_button.click(add_to_cart, inputs=[cart, item_name, item_price, item_quantity, add_ons_display, special_instructions], outputs=[cart_display, total_cost_display])
    save_order_button.click(save_order, inputs=[cart], outputs=[total_cost_display])

app.launch()