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 3 |
+
from datetime import datetime
|
| 4 |
+
from simple_salesforce import Salesforce
|
| 5 |
+
|
| 6 |
+
# Salesforce Connection
|
| 7 |
+
sf = Salesforce(
|
| 8 |
+
username='your_username',
|
| 9 |
+
password='your_password',
|
| 10 |
+
security_token='your_security_token'
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Sample Menu Data in JSON Format
|
| 14 |
+
sample_menu = [
|
| 15 |
+
{
|
| 16 |
+
"name": "Samosa",
|
| 17 |
+
"price": 9.0,
|
| 18 |
+
"description": "Crispy fried pastry filled with spiced potatoes and peas.",
|
| 19 |
+
"image": "https://via.placeholder.com/200x200", # Replace with your image URLs
|
| 20 |
+
"veg_nonveg": "Veg",
|
| 21 |
+
"section": "Starters",
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"name": "Chicken Biryani",
|
| 25 |
+
"price": 15.0,
|
| 26 |
+
"description": "Flavorful rice dish cooked with chicken and spices.",
|
| 27 |
+
"image": "https://via.placeholder.com/200x200",
|
| 28 |
+
"veg_nonveg": "Non-Veg",
|
| 29 |
+
"section": "Biryani",
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"name": "Paneer Butter Masala",
|
| 33 |
+
"price": 12.0,
|
| 34 |
+
"description": "Paneer cubes cooked in a creamy tomato-based gravy.",
|
| 35 |
+
"image": "https://via.placeholder.com/200x200",
|
| 36 |
+
"veg_nonveg": "Veg",
|
| 37 |
+
"section": "Curries",
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# Sample Add-Ons
|
| 42 |
+
sample_add_ons = [
|
| 43 |
+
{"name": "Extra Cheese", "price": 2.0},
|
| 44 |
+
{"name": "Extra Spicy", "price": 1.5},
|
| 45 |
+
{"name": "No Onions", "price": 0.5},
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Function to Save Cart Summary to Salesforce
|
| 49 |
+
def save_cart_to_salesforce(cart, total_cost):
|
| 50 |
+
try:
|
| 51 |
+
if not cart or total_cost <= 0:
|
| 52 |
+
return "Cart is empty or invalid total cost."
|
| 53 |
+
|
| 54 |
+
# Create Order Record
|
| 55 |
+
order_record = {
|
| 56 |
+
'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
| 57 |
+
'Total_Cost__c': total_cost,
|
| 58 |
+
'Order_Date__c': datetime.now().isoformat(),
|
| 59 |
+
}
|
| 60 |
+
order_result = sf.Order__c.create(order_record)
|
| 61 |
+
order_id = order_result['id']
|
| 62 |
+
|
| 63 |
+
# Create Order Items
|
| 64 |
+
for item in cart:
|
| 65 |
+
extras = ", ".join(extra['name'] for extra in item.get('extras', []))
|
| 66 |
+
order_item_record = {
|
| 67 |
+
'Name': item['name'],
|
| 68 |
+
'Order__c': order_id,
|
| 69 |
+
'Quantity__c': item['quantity'],
|
| 70 |
+
'Price__c': item['price'],
|
| 71 |
+
'Extras__c': extras,
|
| 72 |
+
'Instructions__c': item.get('instructions', ''),
|
| 73 |
+
'Total_Cost__c': item['totalCost'],
|
| 74 |
+
}
|
| 75 |
+
sf.Order_Item__c.create(order_item_record)
|
| 76 |
+
|
| 77 |
+
return "Order saved successfully!"
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"Error saving order: {str(e)}"
|
| 80 |
+
|
| 81 |
+
# Function to Load Menu Items
|
| 82 |
+
def load_menu(preference):
|
| 83 |
+
filtered_menu = [
|
| 84 |
+
item for item in sample_menu
|
| 85 |
+
if preference == "All" or item["veg_nonveg"] == preference
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
html = ""
|
| 89 |
+
sections = {item["section"] for item in filtered_menu}
|
| 90 |
+
for section in sections:
|
| 91 |
+
html += f"<h2>{section}</h2>"
|
| 92 |
+
for item in filtered_menu:
|
| 93 |
+
if item["section"] == section:
|
| 94 |
+
html += f"""
|
| 95 |
+
<div style="border:1px solid #ddd; padding:10px; margin:10px;">
|
| 96 |
+
<img src="{item['image']}" style="width:100px; height:100px;">
|
| 97 |
+
<h3>{item['name']}</h3>
|
| 98 |
+
<p>{item['description']}</p>
|
| 99 |
+
<p>Price: ${item['price']}</p>
|
| 100 |
+
<button onclick="addToCart('{item['name']}', {item['price']})">Add to Cart</button>
|
| 101 |
+
</div>
|
| 102 |
+
"""
|
| 103 |
+
return html
|
| 104 |
+
|
| 105 |
+
# Function to Add Items to Cart
|
| 106 |
+
def add_to_cart(name, price, quantity, extras, instructions):
|
| 107 |
+
total_cost = (price * quantity) + sum(extra["price"] for extra in extras)
|
| 108 |
+
cart_item = {
|
| 109 |
+
"name": name,
|
| 110 |
+
"price": price,
|
| 111 |
+
"quantity": quantity,
|
| 112 |
+
"extras": extras,
|
| 113 |
+
"instructions": instructions,
|
| 114 |
+
"totalCost": total_cost,
|
| 115 |
+
}
|
| 116 |
+
return cart_item, total_cost
|
| 117 |
+
|
| 118 |
+
# Gradio Interface
|
| 119 |
+
with gr.Blocks() as app:
|
| 120 |
+
with gr.Row():
|
| 121 |
+
gr.HTML("<h1>Welcome to Biryani Hub</h1>")
|
| 122 |
+
preference = gr.Radio(["All", "Veg", "Non-Veg"], value="All", label="Filter Preference")
|
| 123 |
+
menu_display = gr.HTML()
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
cart = gr.JSON(label="Cart Items")
|
| 127 |
+
total_cost = gr.Textbox(label="Total Cost")
|
| 128 |
+
add_item_button = gr.Button("Add to Cart")
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
checkout_button = gr.Button("Checkout")
|
| 132 |
+
checkout_status = gr.Textbox(label="Status")
|
| 133 |
+
|
| 134 |
+
# Load Menu on Filter Change
|
| 135 |
+
preference.change(
|
| 136 |
+
lambda pref: load_menu(pref),
|
| 137 |
+
inputs=preference,
|
| 138 |
+
outputs=menu_display
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Add Item to Cart
|
| 142 |
+
add_item_button.click(
|
| 143 |
+
lambda name, price, quantity, extras, instructions: add_to_cart(
|
| 144 |
+
name, price, quantity, extras, instructions
|
| 145 |
+
),
|
| 146 |
+
inputs=["name", "price", "quantity", "extras", "instructions"],
|
| 147 |
+
outputs=[cart, total_cost],
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Checkout and Save to Salesforce
|
| 151 |
+
checkout_button.click(
|
| 152 |
+
lambda cart_items, total: save_cart_to_salesforce(cart_items, total),
|
| 153 |
+
inputs=[cart, total_cost],
|
| 154 |
+
outputs=checkout_status,
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
app.launch()
|