dschandra commited on
Commit
c38ed3b
·
verified ·
1 Parent(s): cbf0cb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -56
app.py CHANGED
@@ -4,102 +4,104 @@ from simple_salesforce import Salesforce
4
  # Salesforce Connection
5
  sf = Salesforce(username='[email protected]', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
6
 
7
- # Initialize cart
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
- menu_items = sf.query(query)["records"]
14
- return menu_items
15
 
16
- # Generate menu HTML with add-to-cart functionality
17
  def generate_menu_html(menu_items):
18
- html = ""
 
19
  for item in menu_items:
20
  html += f"""
21
- <div style='border: 1px solid #ccc; padding: 10px; margin: 10px; display: flex; align-items: center;'>
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
- <div style='display: flex; align-items: center;'>
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
- # Update cart when items are added
38
- def update_cart(item_id, quantity):
39
  if item_id in cart:
40
- cart[item_id]["quantity"] = quantity
41
  else:
42
- menu_item = sf.Menu_Item__c.get(item_id)
43
  cart[item_id] = {
44
- "name": menu_item["Name"],
45
- "price": menu_item["Price__c"],
46
- "quantity": quantity
47
  }
48
- return view_cart()
49
 
50
- # View cart details dynamically
51
- def view_cart():
52
- cart_html = "<h3>Cart Details</h3><ul>"
 
 
 
53
  total_price = 0
54
  for item_id, details in cart.items():
55
- item_total = details["price"] * details["quantity"]
56
- total_price += item_total
57
- cart_html += f"<li>{details['name']} x {details['quantity']} - ₹{item_total}</li>"
58
- cart_html += f"</ul><p><strong>Total: {total_price}</strong></p>"
 
 
 
 
 
59
  return cart_html
60
 
61
- # Place order and sync with Salesforce
62
- def place_order():
63
- # Create Order in Salesforce
 
 
 
64
  order = sf.Order.create({
65
- "Status": "Draft",
66
- "TotalAmount": sum(details["price"] * details["quantity"] for details in cart.values())
 
67
  })
68
- order_id = order["id"]
69
 
70
  # Add Order Items
71
  for item_id, details in cart.items():
72
  sf.OrderItem.create({
73
- "OrderId": order_id,
74
- "Product2Id": item_id,
75
- "UnitPrice": details["price"],
76
- "Quantity": details["quantity"]
77
  })
78
 
79
- # Clear the cart
80
  cart.clear()
81
  return "Order placed successfully!"
82
 
83
- # Fetch menu and initialize UI components
84
  menu_items = fetch_menu_items()
85
  menu_html = generate_menu_html(menu_items)
86
 
87
  with gr.Blocks() as demo:
88
- with gr.Row():
89
- gr.Markdown("# Welcome to Biryani Hub")
90
-
91
  with gr.Row():
92
  menu_display = gr.HTML(value=menu_html)
93
-
 
94
  with gr.Row():
95
- cart_display = gr.HTML(value=view_cart())
96
- item_id_input = gr.Textbox(placeholder="Item ID")
97
- quantity_input = gr.Number(label="Quantity", value=1, interactive=True)
98
- add_to_cart_button = gr.Button("Add to Cart")
99
- place_order_button = gr.Button("Place Order")
100
-
101
- add_to_cart_button.click(update_cart, inputs=[item_id_input, quantity_input], outputs=cart_display)
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='[email protected]', 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()