nagasurendra commited on
Commit
d6f7ea2
·
verified ·
1 Parent(s): 9140ee7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -7
app.py CHANGED
@@ -72,6 +72,37 @@ def load_add_ons_from_salesforce():
72
  return result['records']
73
  except Exception as e:
74
  return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  # Function to filter menu items
77
  def filter_menu(preference):
@@ -259,20 +290,36 @@ def modal_js():
259
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
260
  }
261
  function proceedToCheckout() {
262
- // Build the cart summary string with item details
263
- const cartSummary = cart.map(item => `
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  ${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}
265
  Extras: ${item.extras.map(extra => extra.name).join(', ') || 'None'}
266
  Instructions: ${item.instructions || 'None'}
267
  `).join('<br>');
268
-
269
- // Insert the summary into the modal
270
- document.getElementById('final-order-summary').innerHTML = cartSummary;
271
  document.getElementById('total-bill').innerText = totalCartCost.toFixed(2);
272
-
273
- // Show the cart modal with the summary
274
  openCartModal();
275
  }
 
276
  // Reset all selected add-ons when opening a new item modal
277
  function resetAddOns() {
278
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
@@ -314,6 +361,15 @@ with gr.Blocks() as app:
314
  gr.HTML(create_modal_window())
315
  gr.HTML(modal_js())
316
 
 
 
 
 
 
 
 
 
 
317
  login_button.click(
318
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
319
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
@@ -323,3 +379,4 @@ with gr.Blocks() as app:
323
 
324
  app.launch()
325
 
 
 
72
  return result['records']
73
  except Exception as e:
74
  return []
75
+ # Function to save cart summary in Salesforce
76
+ # Function to save cart summary in Salesforce
77
+ def save_cart_summary_to_salesforce(cart_data, total_cost):
78
+ try:
79
+ # Create the Order record
80
+ order_record = {
81
+ 'Name': 'Order', # You can dynamically set a name, e.g., "Order <timestamp>"
82
+ 'Total_Cost__c': total_cost,
83
+ 'Order_Date__c': datetime.now().isoformat()
84
+ }
85
+ order_result = sf.Order__c.create(order_record)
86
+ order_id = order_result['id'] # Get the created Order record ID
87
+
88
+ # Create Order Item records for each item in the cart
89
+ for item in cart_data:
90
+ extras = ", ".join(extra['name'] for extra in item['extras']) # Combine extras into a string
91
+ order_item_record = {
92
+ 'Name': item['name'],
93
+ 'Order__c': order_id, # Link to the parent Order
94
+ 'Quantity__c': item['quantity'],
95
+ 'Price__c': item['price'],
96
+ 'Extras__c': extras,
97
+ 'Instructions__c': item['instructions'],
98
+ 'Total_Cost__c': item['totalCost']
99
+ }
100
+ sf.Order_Item__c.create(order_item_record) # Create each order item in Salesforce
101
+
102
+ return "Order and items saved successfully in Salesforce!"
103
+ except Exception as e:
104
+ return f"Error saving order in Salesforce: {str(e)}"
105
+
106
 
107
  # Function to filter menu items
108
  def filter_menu(preference):
 
290
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
291
  }
292
  function proceedToCheckout() {
293
+ const cartSummary = cart.map(item => ({
294
+ name: item.name,
295
+ quantity: item.quantity,
296
+ price: item.price,
297
+ extras: item.extras.map(extra => ({ name: extra.name, price: extra.price, quantity: extra.quantity })),
298
+ instructions: item.instructions,
299
+ totalCost: item.totalCost
300
+ }));
301
+
302
+ // Use Gradio's Python function to send cart data to Salesforce
303
+ const gradioCall = async () => {
304
+ const response = await gradio.callPython({
305
+ function_name: "save_cart_summary_to_salesforce",
306
+ args: [cartSummary, totalCartCost],
307
+ });
308
+ alert(response.data); // Show success/error message
309
+ };
310
+ gradioCall();
311
+
312
+ // Display summary in the cart modal
313
+ const cartSummaryHtml = cart.map(item => `
314
  ${item.name} (x${item.quantity}) - $${item.totalCost.toFixed(2)}
315
  Extras: ${item.extras.map(extra => extra.name).join(', ') || 'None'}
316
  Instructions: ${item.instructions || 'None'}
317
  `).join('<br>');
318
+ document.getElementById('final-order-summary').innerHTML = cartSummaryHtml;
 
 
319
  document.getElementById('total-bill').innerText = totalCartCost.toFixed(2);
 
 
320
  openCartModal();
321
  }
322
+
323
  // Reset all selected add-ons when opening a new item modal
324
  function resetAddOns() {
325
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
 
361
  gr.HTML(create_modal_window())
362
  gr.HTML(modal_js())
363
 
364
+ # Register the save_cart_summary_to_salesforce function
365
+ proceed_to_checkout_button = gr.Button("Proceed to Checkout")
366
+ proceed_to_checkout_output = gr.Textbox(label="Order Status")
367
+ proceed_to_checkout_button.click(
368
+ save_cart_summary_to_salesforce,
369
+ inputs=[],
370
+ outputs=proceed_to_checkout_output
371
+ )
372
+
373
  login_button.click(
374
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
375
  if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."),
 
379
 
380
  app.launch()
381
 
382
+