nagasurendra commited on
Commit
afa2ae5
·
verified ·
1 Parent(s): 5339d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -60
app.py CHANGED
@@ -112,10 +112,35 @@ def filter_menu(preference):
112
 
113
  return html_content
114
 
115
- # Function to create the Modal window with Add-ons
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def create_modal_window():
117
  add_ons = load_add_ons_from_salesforce()
118
-
119
  add_ons_html = ""
120
  for add_on in add_ons:
121
  add_ons_html += f"""
@@ -145,7 +170,6 @@ def create_modal_window():
145
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
146
  </div>
147
  """
148
-
149
  return modal_html
150
 
151
  # JavaScript for Modal and Cart
@@ -154,7 +178,6 @@ def modal_js():
154
  <script>
155
  let cart = [];
156
  let totalCartCost = 0;
157
-
158
  function openModal(name, image2, description, price) {
159
  const modal = document.getElementById('modal');
160
  modal.style.display = 'block';
@@ -169,13 +192,11 @@ def modal_js():
169
  document.getElementById('modal-price').innerText = price;
170
  document.getElementById('quantity').value = 1;
171
  document.getElementById('special-instructions').value = '';
172
- loadAddOns(); // Dynamically load add-ons when opening the modal
173
  }
174
-
175
  function closeModal() {
176
  document.getElementById('modal').style.display = 'none';
177
  }
178
-
179
  function addToCart() {
180
  const name = document.getElementById('modal-name').innerText;
181
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
@@ -185,11 +206,10 @@ def modal_js():
185
  const extras = selectedAddOns.map(extra => ({
186
  name: extra.value,
187
  price: parseFloat(extra.getAttribute('data-price')),
188
- quantity: 1 // Default quantity for add-ons is 1
189
  }));
190
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
191
  const totalCost = (price * quantity) + extrasCost;
192
-
193
  // Add the item to the cart with its specific add-ons
194
  cart.push({ name, price, quantity, extras, instructions, totalCost });
195
  totalCartCost += totalCost; // Update the total cost of the cart
@@ -197,12 +217,10 @@ def modal_js():
197
  updateCartTotalCost(); // Update total cost displayed
198
  closeModal();
199
  }
200
-
201
  function updateCartButton() {
202
  const cartButton = document.getElementById('cart-button');
203
  cartButton.innerText = `View Cart (${cart.length} items)`;
204
  }
205
-
206
  function openCartModal() {
207
  const cartModal = document.getElementById('cart-modal');
208
  const cartItemsContainer = document.getElementById('cart-items');
@@ -222,11 +240,9 @@ def modal_js():
222
  });
223
  cartModal.style.display = 'block';
224
  }
225
-
226
  function closeCartModal() {
227
  document.getElementById('cart-modal').style.display = 'none';
228
  }
229
-
230
  function removeFromCart(index) {
231
  totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
232
  cart.splice(index, 1);
@@ -234,7 +250,6 @@ def modal_js():
234
  updateCartTotalCost(); // Update total cost displayed
235
  openCartModal();
236
  }
237
-
238
  function updateCartItem(index, type, value) {
239
  if (type === 'item') {
240
  cart[index].quantity = parseInt(value);
@@ -248,59 +263,16 @@ def modal_js():
248
  document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
249
  updateCartTotalCost(); // Update total cost displayed
250
  }
251
-
252
  function updateCartTotalCost() {
253
  const totalCostElement = document.getElementById('cart-total-cost');
254
  totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
255
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
256
  }
257
-
258
  function proceedToCheckout() {
259
- // Gather the cart details
260
- const orderDetails = cart.map(item => {
261
- return {
262
- itemName: item.name,
263
- itemPrice: item.price,
264
- quantity: item.quantity,
265
- totalCost: item.totalCost,
266
- specialInstructions: item.instructions,
267
- addOns: item.extras.map(extra => {
268
- return {
269
- addOnName: extra.name,
270
- addOnPrice: extra.price,
271
- addOnQuantity: extra.quantity
272
- };
273
- })
274
- };
275
- });
276
-
277
- // Log the order details to the console (for debugging purposes)
278
- console.log("Proceeding to checkout with the following order details:");
279
- console.log(orderDetails);
280
-
281
- // Example: Send the order details to your backend (Salesforce, server, etc.)
282
- fetch("/api/submit_order", {
283
- method: "POST",
284
- headers: {
285
- "Content-Type": "application/json"
286
- },
287
- body: JSON.stringify(orderDetails) // Sending order details as JSON
288
- })
289
- .then(response => response.json())
290
- .then(data => {
291
- alert("Order placed successfully!");
292
- // Optionally, clear the cart after successful submission
293
- cart = [];
294
- updateCartButton();
295
- updateCartTotalCost(); // Update the total cost of the cart
296
- closeCartModal();
297
- })
298
- .catch(error => {
299
- alert("There was an error processing your order. Please try again.");
300
- console.error("Error:", error);
301
- });
302
  }
303
-
304
  // Reset all selected add-ons when opening a new item modal
305
  function resetAddOns() {
306
  const checkboxes = document.querySelectorAll('input[name="biryani-extra"]');
 
112
 
113
  return html_content
114
 
115
+ # Function to save cart details to Salesforce
116
+ def save_cart_to_salesforce(cart):
117
+ try:
118
+ # Loop through each cart item and create a new Order__c record
119
+ for item in cart:
120
+ order_data = {
121
+ 'Name2__c': item['name'], # Custom name for the order (can be the item name or order ID)
122
+ 'Item_Name__c': item['name'], # Name of the item in the order
123
+ 'Quantity__c': item['quantity'], # Quantity of the item
124
+ 'Price__c': item['price'], # Price of the item
125
+ 'Total_Cost__c': item['totalCost'], # Total cost for the item
126
+ 'Special_Instructions__c': item['instructions'], # Special instructions (if any)
127
+ }
128
+
129
+ # Handle add-ons if available
130
+ if item['extras']:
131
+ add_ons = ", ".join([add_on['name'] for add_on in item['extras']])
132
+ order_data['Add_Ons__c'] = add_ons # Add-ons associated with the item
133
+
134
+ # Create the order in Salesforce
135
+ sf.Order__c.create(order_data)
136
+
137
+ return "Cart saved successfully to Salesforce!"
138
+ except Exception as e:
139
+ return f"Error while saving cart to Salesforce: {str(e)}"
140
+
141
+ # Function to create the modal window with add-ons
142
  def create_modal_window():
143
  add_ons = load_add_ons_from_salesforce()
 
144
  add_ons_html = ""
145
  for add_on in add_ons:
146
  add_ons_html += f"""
 
170
  <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
171
  </div>
172
  """
 
173
  return modal_html
174
 
175
  # JavaScript for Modal and Cart
 
178
  <script>
179
  let cart = [];
180
  let totalCartCost = 0;
 
181
  function openModal(name, image2, description, price) {
182
  const modal = document.getElementById('modal');
183
  modal.style.display = 'block';
 
192
  document.getElementById('modal-price').innerText = price;
193
  document.getElementById('quantity').value = 1;
194
  document.getElementById('special-instructions').value = '';
195
+ resetAddOns(); // Reset add-ons when opening the modal
196
  }
 
197
  function closeModal() {
198
  document.getElementById('modal').style.display = 'none';
199
  }
 
200
  function addToCart() {
201
  const name = document.getElementById('modal-name').innerText;
202
  const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
 
206
  const extras = selectedAddOns.map(extra => ({
207
  name: extra.value,
208
  price: parseFloat(extra.getAttribute('data-price')),
209
+
210
  }));
211
  const extrasCost = extras.reduce((total, extra) => total + (extra.price * extra.quantity), 0);
212
  const totalCost = (price * quantity) + extrasCost;
 
213
  // Add the item to the cart with its specific add-ons
214
  cart.push({ name, price, quantity, extras, instructions, totalCost });
215
  totalCartCost += totalCost; // Update the total cost of the cart
 
217
  updateCartTotalCost(); // Update total cost displayed
218
  closeModal();
219
  }
 
220
  function updateCartButton() {
221
  const cartButton = document.getElementById('cart-button');
222
  cartButton.innerText = `View Cart (${cart.length} items)`;
223
  }
 
224
  function openCartModal() {
225
  const cartModal = document.getElementById('cart-modal');
226
  const cartItemsContainer = document.getElementById('cart-items');
 
240
  });
241
  cartModal.style.display = 'block';
242
  }
 
243
  function closeCartModal() {
244
  document.getElementById('cart-modal').style.display = 'none';
245
  }
 
246
  function removeFromCart(index) {
247
  totalCartCost -= cart[index].totalCost; // Deduct the cost of the removed item from total cost
248
  cart.splice(index, 1);
 
250
  updateCartTotalCost(); // Update total cost displayed
251
  openCartModal();
252
  }
 
253
  function updateCartItem(index, type, value) {
254
  if (type === 'item') {
255
  cart[index].quantity = parseInt(value);
 
263
  document.getElementById(`item-${index}-total`).innerText = item.totalCost.toFixed(2);
264
  updateCartTotalCost(); // Update total cost displayed
265
  }
 
266
  function updateCartTotalCost() {
267
  const totalCostElement = document.getElementById('cart-total-cost');
268
  totalCartCost = cart.reduce((total, item) => total + item.totalCost, 0);
269
  totalCostElement.innerText = `Total Cart Cost: $${totalCartCost.toFixed(2)}`;
270
  }
 
271
  function proceedToCheckout() {
272
+ // Save the cart to Salesforce when the user checks out
273
+ save_cart_to_salesforce(cart);
274
+ alert("Proceeding to checkout...");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"]');