Update app.py
Browse files
app.py
CHANGED
|
@@ -4,102 +4,104 @@ from simple_salesforce import Salesforce
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
-
#
|
| 8 |
cart = {}
|
| 9 |
|
| 10 |
-
# Fetch menu items from Salesforce
|
| 11 |
def fetch_menu_items():
|
|
|
|
| 12 |
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
|
| 13 |
-
|
| 14 |
-
return
|
| 15 |
|
| 16 |
-
# Generate menu HTML with add-to-cart functionality
|
| 17 |
def generate_menu_html(menu_items):
|
| 18 |
-
|
|
|
|
| 19 |
for item in menu_items:
|
| 20 |
html += f"""
|
| 21 |
-
<div style='
|
| 22 |
-
<img src='{item.get('Image1__c', '')}' style='width: 100px; height: 100px; margin-right: 10px;' />
|
| 23 |
<div>
|
|
|
|
| 24 |
<h3>{item['Name']}</h3>
|
| 25 |
-
<p>{item.get('Description__c', '')}</p>
|
| 26 |
<p>₹{item['Price__c']}</p>
|
| 27 |
-
<
|
| 28 |
-
<button onclick="decreaseQuantity('{item['Id']}')">-</button>
|
| 29 |
-
<span id="quantity-{item['Id']}" style="margin: 0 10px;">0</span>
|
| 30 |
-
<button onclick="increaseQuantity('{item['Id']}')">+</button>
|
| 31 |
-
</div>
|
| 32 |
</div>
|
| 33 |
</div>
|
| 34 |
"""
|
|
|
|
| 35 |
return html
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
if item_id in cart:
|
| 40 |
-
cart[item_id][
|
| 41 |
else:
|
| 42 |
-
menu_item = sf.Menu_Item__c.get(item_id)
|
| 43 |
cart[item_id] = {
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
}
|
| 48 |
-
return
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
total_price = 0
|
| 54 |
for item_id, details in cart.items():
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return cart_html
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
order = sf.Order.create({
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
})
|
| 68 |
-
order_id = order["id"]
|
| 69 |
|
| 70 |
# Add Order Items
|
| 71 |
for item_id, details in cart.items():
|
| 72 |
sf.OrderItem.create({
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
})
|
| 78 |
|
| 79 |
-
# Clear the cart
|
| 80 |
cart.clear()
|
| 81 |
return "Order placed successfully!"
|
| 82 |
|
| 83 |
-
# Fetch menu and
|
| 84 |
menu_items = fetch_menu_items()
|
| 85 |
menu_html = generate_menu_html(menu_items)
|
| 86 |
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
with gr.Row():
|
| 92 |
menu_display = gr.HTML(value=menu_html)
|
| 93 |
-
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
place_order_button.click(place_order, inputs=[], outputs=cart_display)
|
| 103 |
-
|
| 104 |
-
# Launch the app
|
| 105 |
-
demo.launch()
|
|
|
|
| 4 |
# Salesforce Connection
|
| 5 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 6 |
|
| 7 |
+
# Local cart to manage food items
|
| 8 |
cart = {}
|
| 9 |
|
|
|
|
| 10 |
def fetch_menu_items():
|
| 11 |
+
"""Fetch menu items from Salesforce."""
|
| 12 |
query = "SELECT Id, Name, Price__c, Description__c, Image1__c FROM Menu_Item__c"
|
| 13 |
+
result = sf.query(query)
|
| 14 |
+
return result['records']
|
| 15 |
|
|
|
|
| 16 |
def generate_menu_html(menu_items):
|
| 17 |
+
"""Generate HTML for menu items."""
|
| 18 |
+
html = "<div>"
|
| 19 |
for item in menu_items:
|
| 20 |
html += f"""
|
| 21 |
+
<div style='display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px;'>
|
|
|
|
| 22 |
<div>
|
| 23 |
+
<img src='{item['Image1__c']}' alt='{item['Name']}' style='width: 100px; height: 100px; object-fit: cover; margin-right: 10px;'>
|
| 24 |
<h3>{item['Name']}</h3>
|
|
|
|
| 25 |
<p>₹{item['Price__c']}</p>
|
| 26 |
+
<button onclick=\"addToCart('{item['Id']}', '{item['Name']}', {item['Price__c']})\">Add to Cart</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
</div>
|
| 28 |
</div>
|
| 29 |
"""
|
| 30 |
+
html += "</div>"
|
| 31 |
return html
|
| 32 |
|
| 33 |
+
def add_to_cart(item_id, name, price):
|
| 34 |
+
"""Add an item to the cart."""
|
| 35 |
if item_id in cart:
|
| 36 |
+
cart[item_id]['quantity'] += 1
|
| 37 |
else:
|
|
|
|
| 38 |
cart[item_id] = {
|
| 39 |
+
'name': name,
|
| 40 |
+
'price': price,
|
| 41 |
+
'quantity': 1
|
| 42 |
}
|
| 43 |
+
return update_cart_display()
|
| 44 |
|
| 45 |
+
def update_cart_display():
|
| 46 |
+
"""Update the cart display."""
|
| 47 |
+
if not cart:
|
| 48 |
+
return "Cart is empty."
|
| 49 |
+
|
| 50 |
+
cart_html = "<div>"
|
| 51 |
total_price = 0
|
| 52 |
for item_id, details in cart.items():
|
| 53 |
+
total_price += details['price'] * details['quantity']
|
| 54 |
+
cart_html += f"""
|
| 55 |
+
<div style='display: flex; justify-content: space-between; align-items: center;'>
|
| 56 |
+
<span>{details['name']} (x{details['quantity']})</span>
|
| 57 |
+
<span>₹{details['price'] * details['quantity']}</span>
|
| 58 |
+
</div>
|
| 59 |
+
"""
|
| 60 |
+
cart_html += f"<div style='margin-top: 10px;'>Total: ₹{total_price}</div>"
|
| 61 |
+
cart_html += "</div>"
|
| 62 |
return cart_html
|
| 63 |
|
| 64 |
+
def proceed_to_order():
|
| 65 |
+
"""Place the order in Salesforce."""
|
| 66 |
+
if not cart:
|
| 67 |
+
return "Cart is empty. Cannot place an order."
|
| 68 |
+
|
| 69 |
+
# Create an Order record
|
| 70 |
order = sf.Order.create({
|
| 71 |
+
'Status': 'Draft',
|
| 72 |
+
'EffectiveDate': '2023-01-01', # Replace with dynamic date if needed
|
| 73 |
+
'AccountId': 'your_account_id' # Replace with actual Account ID
|
| 74 |
})
|
|
|
|
| 75 |
|
| 76 |
# Add Order Items
|
| 77 |
for item_id, details in cart.items():
|
| 78 |
sf.OrderItem.create({
|
| 79 |
+
'OrderId': order['id'],
|
| 80 |
+
'Quantity': details['quantity'],
|
| 81 |
+
'UnitPrice': details['price'],
|
| 82 |
+
'PricebookEntryId': 'your_pricebook_entry_id' # Replace accordingly
|
| 83 |
})
|
| 84 |
|
| 85 |
+
# Clear the cart after placing the order
|
| 86 |
cart.clear()
|
| 87 |
return "Order placed successfully!"
|
| 88 |
|
| 89 |
+
# Fetch menu items and generate HTML
|
| 90 |
menu_items = fetch_menu_items()
|
| 91 |
menu_html = generate_menu_html(menu_items)
|
| 92 |
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
+
gr.Markdown("# Welcome to Biryani Hub")
|
| 95 |
+
|
|
|
|
| 96 |
with gr.Row():
|
| 97 |
menu_display = gr.HTML(value=menu_html)
|
| 98 |
+
cart_display = gr.HTML(value="Cart is empty.")
|
| 99 |
+
|
| 100 |
with gr.Row():
|
| 101 |
+
add_button = gr.Button("Add to Cart")
|
| 102 |
+
proceed_button = gr.Button("Proceed to Order")
|
| 103 |
+
|
| 104 |
+
add_button.click(add_to_cart, inputs=[], outputs=cart_display)
|
| 105 |
+
proceed_button.click(proceed_to_order, inputs=[], outputs=cart_display)
|
| 106 |
+
|
| 107 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|