File size: 3,626 Bytes
f65f970 8fb83ea b316f59 1ff1c4f 77b50a3 1ff1c4f b316f59 77b50a3 b316f59 ec96e6b b316f59 77b50a3 b316f59 77b50a3 b316f59 77b50a3 ec96e6b b316f59 c0caa98 b316f59 ec96e6b b316f59 1ff1c4f b316f59 cbf0cb6 b316f59 9b01074 1ff1c4f c0caa98 1ff1c4f b316f59 ec96e6b b316f59 cbf0cb6 b316f59 1ff1c4f cbf0cb6 b316f59 9b01074 1ff1c4f b316f59 |
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 |
import gradio as gr
from simple_salesforce import Salesforce
# Salesforce Connection
sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
# Initialize cart
cart = {}
# Fetch menu items from Salesforce
def fetch_menu_items():
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
menu_items = sf.query(query)["records"]
return menu_items
# Generate menu HTML with add-to-cart functionality
def generate_menu_html(menu_items):
html = ""
for item in menu_items:
html += f"""
<div style='border: 1px solid #ccc; padding: 10px; margin: 10px; display: flex; align-items: center;'>
<img src='{item.get('Image1__c', '')}' style='width: 100px; height: 100px; margin-right: 10px;' />
<div>
<h3>{item['Name']}</h3>
<p>{item.get('Description__c', '')}</p>
<p>₹{item['Price__c']}</p>
<div style='display: flex; align-items: center;'>
<button onclick="decreaseQuantity('{item['Id']}')">-</button>
<span id="quantity-{item['Id']}" style="margin: 0 10px;">0</span>
<button onclick="increaseQuantity('{item['Id']}')">+</button>
</div>
</div>
</div>
"""
return html
# Update cart when items are added
def update_cart(item_id, quantity):
if item_id in cart:
cart[item_id]["quantity"] = quantity
else:
menu_item = sf.Menu_Item__c.get(item_id)
cart[item_id] = {
"name": menu_item["Name"],
"price": menu_item["Price__c"],
"quantity": quantity
}
return view_cart()
# View cart details dynamically
def view_cart():
cart_html = "<h3>Cart Details</h3><ul>"
total_price = 0
for item_id, details in cart.items():
item_total = details["price"] * details["quantity"]
total_price += item_total
cart_html += f"<li>{details['name']} x {details['quantity']} - ₹{item_total}</li>"
cart_html += f"</ul><p><strong>Total: ₹{total_price}</strong></p>"
return cart_html
# Place order and sync with Salesforce
def place_order():
# Create Order in Salesforce
order = sf.Order.create({
"Status": "Draft",
"TotalAmount": sum(details["price"] * details["quantity"] for details in cart.values())
})
order_id = order["id"]
# Add Order Items
for item_id, details in cart.items():
sf.OrderItem.create({
"OrderId": order_id,
"Product2Id": item_id,
"UnitPrice": details["price"],
"Quantity": details["quantity"]
})
# Clear the cart
cart.clear()
return "Order placed successfully!"
# Fetch menu and initialize UI components
menu_items = fetch_menu_items()
menu_html = generate_menu_html(menu_items)
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Welcome to Biryani Hub")
with gr.Row():
menu_display = gr.HTML(value=menu_html)
with gr.Row():
cart_display = gr.HTML(value=view_cart())
item_id_input = gr.Textbox(placeholder="Item ID")
quantity_input = gr.Number(label="Quantity", value=1, interactive=True)
add_to_cart_button = gr.Button("Add to Cart")
place_order_button = gr.Button("Place Order")
add_to_cart_button.click(update_cart, inputs=[item_id_input, quantity_input], outputs=cart_display)
place_order_button.click(place_order, inputs=[], outputs=cart_display)
# Launch the app
demo.launch() |