import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
# Local cart to manage food items
cart = {}
def fetch_menu_items():
"""Fetch menu items from Salesforce."""
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
result = sf.query(query)
return result['records']
def generate_menu_html(menu_items):
"""Generate HTML for menu items."""
html = "
"
for item in menu_items:
html += f"""
{item['Name']}
₹{item['Price__c']}
"""
html += "
"
return html
def add_to_cart(item_id, name, price):
"""Add an item to the cart."""
if item_id in cart:
cart[item_id]['quantity'] += 1
else:
cart[item_id] = {
'name': name,
'price': price,
'quantity': 1
}
return update_cart_display()
def update_cart_display():
"""Update the cart display."""
if not cart:
return "Cart is empty."
cart_html = "
"
total_price = 0
for item_id, details in cart.items():
total_price += details['price'] * details['quantity']
cart_html += f"""
"
return cart_html
def proceed_to_order():
"""Place the order in Salesforce."""
if not cart:
return "Cart is empty. Cannot place an order."
# Create an Order record
order = sf.Order.create({
'Status': 'Draft',
'EffectiveDate': '2023-01-01', # Replace with dynamic date if needed
'AccountId': 'your_account_id' # Replace with actual Account ID
})
# Add Order Items
for item_id, details in cart.items():
sf.OrderItem.create({
'OrderId': order['id'],
'Quantity': details['quantity'],
'UnitPrice': details['price'],
'PricebookEntryId': 'your_pricebook_entry_id' # Replace accordingly
})
# Clear the cart after placing the order
cart.clear()
return "Order placed successfully!"
# Fetch menu items and generate HTML
menu_items = fetch_menu_items()
menu_html = generate_menu_html(menu_items)
with gr.Blocks() as demo:
gr.Markdown("# Welcome to Biryani Hub")
with gr.Row():
menu_display = gr.HTML(value=menu_html)
cart_display = gr.HTML(value="Cart is empty.")
with gr.Row():
add_button = gr.Button("Add to Cart")
proceed_button = gr.Button("Proceed to Order")
add_button.click(add_to_cart, inputs=[], outputs=cart_display)
proceed_button.click(proceed_to_order, inputs=[], outputs=cart_display)
demo.launch()