Cart / app.py
dschandra's picture
Update app.py
c38ed3b verified
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', 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 = "<div>"
for item in menu_items:
html += f"""
<div style='display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px;'>
<div>
<img src='{item['Image1__c']}' alt='{item['Name']}' style='width: 100px; height: 100px; object-fit: cover; margin-right: 10px;'>
<h3>{item['Name']}</h3>
<p>₹{item['Price__c']}</p>
<button onclick=\"addToCart('{item['Id']}', '{item['Name']}', {item['Price__c']})\">Add to Cart</button>
</div>
</div>
"""
html += "</div>"
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 = "<div>"
total_price = 0
for item_id, details in cart.items():
total_price += details['price'] * details['quantity']
cart_html += f"""
<div style='display: flex; justify-content: space-between; align-items: center;'>
<span>{details['name']} (x{details['quantity']})</span>
<span>₹{details['price'] * details['quantity']}</span>
</div>
"""
cart_html += f"<div style='margin-top: 10px;'>Total: ₹{total_price}</div>"
cart_html += "</div>"
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()