nagasurendra commited on
Commit
328af5b
·
verified ·
1 Parent(s): 25adbca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -47
app.py CHANGED
@@ -218,14 +218,12 @@ def submit_customization_ingredients():
218
  ingredients = data.get('ingredients', [])
219
  instructions = data.get('instructions', '')
220
 
221
- # Get the session email (this should already be set during login)
222
  customer_email = session.get('user_email')
223
-
224
  if not customer_email:
225
  return jsonify({"error": "User email not found in session"}), 400
226
 
227
  try:
228
- if items: # Cart submission
229
  for item in items:
230
  ingredient_names = ', '.join(i['name'] for i in item.get('ingredients', [])) if item.get('ingredients') else ''
231
  base_price = item.get('price', 0.0)
@@ -233,26 +231,17 @@ def submit_customization_ingredients():
233
  addons_price = 0
234
  total_price = (base_price * quantity) + addons_price
235
 
236
- # Check if item already exists in the cart for the user based on Customer_Email__c and Item Name
237
- soql = f"SELECT Id, Quantity__c, Add_Ons__c, Instructions__c, Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}' AND Name = '{item['name']}' LIMIT 1"
238
  existing_item = sf.query(soql)
239
 
240
- if existing_item['records']: # If the item exists, update it
241
  item_id = existing_item['records'][0]['Id']
242
  existing_quantity = existing_item['records'][0]['Quantity__c']
243
- existing_addons = existing_item['records'][0]['Add_Ons__c']
244
- existing_instructions = existing_item['records'][0]['Instructions__c']
245
- existing_price = existing_item['records'][0]['Price__c']
246
-
247
- # Update quantity, addons, and instructions
248
  new_quantity = existing_quantity + quantity
249
- new_addons = (existing_addons or '') + (', ' + ingredient_names if ingredient_names else '')
250
- new_instructions = (existing_instructions or '') + ('; ' + instructions if instructions else '')
251
-
252
- # Calculate the new total price
253
  updated_price = (base_price * new_quantity) + addons_price
254
 
255
- # Update the existing item in Salesforce
256
  sf.Cart_Item__c.update(item_id, {
257
  'Quantity__c': new_quantity,
258
  'Add_Ons__c': new_addons,
@@ -260,7 +249,8 @@ def submit_customization_ingredients():
260
  'Price__c': updated_price
261
  })
262
  logger.debug(f"Updated {item['name']} in Cart_Item__c")
263
- else: # If the item doesn't exist, create a new one
 
264
  sf.Cart_Item__c.create({
265
  'Name': item['name'],
266
  'Base_Price__c': base_price,
@@ -272,47 +262,41 @@ def submit_customization_ingredients():
272
  'Instructions__c': item.get('instructions', ''),
273
  'Category__c': item.get('veg_nonveg', ''),
274
  'Section__c': item.get('section', ''),
275
- 'Customer_Email__c': customer_email # Store session email
276
  })
 
 
 
277
  sf.Custom_Dish__c.create({
278
  'Name': item['name'],
279
  'Price__c': total_price,
280
- 'Description__c': f"Add-Ons: {ingredient_names}; Instructions: {item.get('instructions', '')}",
281
- 'Image1__c': menu_item.get('image_url', ''),
282
- 'Veg_NonVeg__c': menu_item.get('veg_nonveg', ''),
283
- 'Section__c': menu_item.get('section', '')
284
  })
 
285
 
286
- logger.debug(f"Created new Cart_Item__c for {item['name']}")
287
  return jsonify({"success": True, "message": f"Processed {len(items)} items"})
288
 
289
- elif menu_item: # Single item customization
290
  ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
291
  base_price = menu_item.get('price', 0.0)
292
  quantity = 1
293
  addons_price = 0
294
  total_price = (base_price * quantity) + addons_price
295
 
296
- # Check if the menu item already exists in the cart for the user
297
- soql = f"SELECT Id, Quantity__c, Add_Ons__c, Instructions__c, Price__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}' AND Name = '{menu_item['name']}' LIMIT 1"
298
  existing_item = sf.query(soql)
299
 
300
- if existing_item['records']: # If the item exists, update it
301
  item_id = existing_item['records'][0]['Id']
302
  existing_quantity = existing_item['records'][0]['Quantity__c']
303
- existing_addons = existing_item['records'][0]['Add_Ons__c']
304
- existing_instructions = existing_item['records'][0]['Instructions__c']
305
- existing_price = existing_item['records'][0]['Price__c']
306
-
307
- # Update quantity, addons, and instructions
308
  new_quantity = existing_quantity + quantity
309
- new_addons = (existing_addons or '') + (', ' + ingredient_names if ingredient_names else '')
310
- new_instructions = (existing_instructions or '') + ('; ' + instructions if instructions else '')
311
-
312
- # Calculate the new total price
313
  updated_price = (base_price * new_quantity) + addons_price
314
 
315
- # Update the existing item in Salesforce
316
  sf.Cart_Item__c.update(item_id, {
317
  'Quantity__c': new_quantity,
318
  'Add_Ons__c': new_addons,
@@ -320,7 +304,8 @@ def submit_customization_ingredients():
320
  'Price__c': updated_price
321
  })
322
  logger.debug(f"Updated customization for {menu_item['name']} in Cart_Item__c")
323
- else: # If the item doesn't exist, create a new one
 
324
  sf.Cart_Item__c.create({
325
  'Name': menu_item['name'],
326
  'Base_Price__c': base_price,
@@ -332,17 +317,21 @@ def submit_customization_ingredients():
332
  'Instructions__c': instructions,
333
  'Category__c': menu_item.get('veg_nonveg', ''),
334
  'Section__c': menu_item.get('section', ''),
335
- 'Customer_Email__c': customer_email # Store session email
336
  })
337
- sf.Custom_Dish__c.create({
338
- 'Name': item['name'],
339
- 'Price__c': total_price,
340
- 'Description__c': f"Add-Ons: {ingredient_names}; Instructions: {item.get('instructions', '')}",
341
- 'Image1__c': item.get('image_url', ''),
342
- 'Veg_NonVeg__c': menu_item.get('veg_nonveg', ''),
343
- 'Section__c': menu_item.get('section', '')
344
- })
345
  logger.debug(f"Created new Cart_Item__c for {menu_item['name']}")
 
 
 
 
 
 
 
 
 
 
 
 
346
  return jsonify({"success": True, "message": "Customization submitted"})
347
 
348
  else:
@@ -351,6 +340,7 @@ def submit_customization_ingredients():
351
  except Exception as e:
352
  logger.error(f"Failed to submit: {str(e)}")
353
  return jsonify({"error": f"Failed to submit: {str(e)}"}), 500
 
354
  from flask import Flask, render_template, request, jsonify
355
  import os
356
  import base64
 
218
  ingredients = data.get('ingredients', [])
219
  instructions = data.get('instructions', '')
220
 
 
221
  customer_email = session.get('user_email')
 
222
  if not customer_email:
223
  return jsonify({"error": "User email not found in session"}), 400
224
 
225
  try:
226
+ if items:
227
  for item in items:
228
  ingredient_names = ', '.join(i['name'] for i in item.get('ingredients', [])) if item.get('ingredients') else ''
229
  base_price = item.get('price', 0.0)
 
231
  addons_price = 0
232
  total_price = (base_price * quantity) + addons_price
233
 
234
+ soql = f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}' AND Name = '{item['name']}' LIMIT 1"
 
235
  existing_item = sf.query(soql)
236
 
237
+ if existing_item['records']:
238
  item_id = existing_item['records'][0]['Id']
239
  existing_quantity = existing_item['records'][0]['Quantity__c']
 
 
 
 
 
240
  new_quantity = existing_quantity + quantity
241
+ new_addons = (existing_item['records'][0].get('Add_Ons__c') or '') + (', ' + ingredient_names if ingredient_names else '')
242
+ new_instructions = (existing_item['records'][0].get('Instructions__c') or '') + ('; ' + instructions if instructions else '')
 
 
243
  updated_price = (base_price * new_quantity) + addons_price
244
 
 
245
  sf.Cart_Item__c.update(item_id, {
246
  'Quantity__c': new_quantity,
247
  'Add_Ons__c': new_addons,
 
249
  'Price__c': updated_price
250
  })
251
  logger.debug(f"Updated {item['name']} in Cart_Item__c")
252
+
253
+ else:
254
  sf.Cart_Item__c.create({
255
  'Name': item['name'],
256
  'Base_Price__c': base_price,
 
262
  'Instructions__c': item.get('instructions', ''),
263
  'Category__c': item.get('veg_nonveg', ''),
264
  'Section__c': item.get('section', ''),
265
+ 'Customer_Email__c': customer_email
266
  })
267
+ logger.debug(f"Created new Cart_Item__c for {item['name']}")
268
+
269
+ # Create Custom_Dish__c record
270
  sf.Custom_Dish__c.create({
271
  'Name': item['name'],
272
  'Price__c': total_price,
273
+ 'Description__c': (ingredient_names + '; ' + instructions).strip('; '),
274
+ 'Image1__c': item.get('image_url', ''),
275
+ 'Veg_NonVeg__c': item.get('veg_nonveg', ''),
276
+ 'Section__c': item.get('section', '')
277
  })
278
+ logger.debug(f"Created Custom_Dish__c for {item['name']}")
279
 
 
280
  return jsonify({"success": True, "message": f"Processed {len(items)} items"})
281
 
282
+ elif menu_item:
283
  ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
284
  base_price = menu_item.get('price', 0.0)
285
  quantity = 1
286
  addons_price = 0
287
  total_price = (base_price * quantity) + addons_price
288
 
289
+ soql = f"SELECT Id, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}' AND Name = '{menu_item['name']}' LIMIT 1"
 
290
  existing_item = sf.query(soql)
291
 
292
+ if existing_item['records']:
293
  item_id = existing_item['records'][0]['Id']
294
  existing_quantity = existing_item['records'][0]['Quantity__c']
 
 
 
 
 
295
  new_quantity = existing_quantity + quantity
296
+ new_addons = (existing_item['records'][0].get('Add_Ons__c') or '') + (', ' + ingredient_names if ingredient_names else '')
297
+ new_instructions = (existing_item['records'][0].get('Instructions__c') or '') + ('; ' + instructions if instructions else '')
 
 
298
  updated_price = (base_price * new_quantity) + addons_price
299
 
 
300
  sf.Cart_Item__c.update(item_id, {
301
  'Quantity__c': new_quantity,
302
  'Add_Ons__c': new_addons,
 
304
  'Price__c': updated_price
305
  })
306
  logger.debug(f"Updated customization for {menu_item['name']} in Cart_Item__c")
307
+
308
+ else:
309
  sf.Cart_Item__c.create({
310
  'Name': menu_item['name'],
311
  'Base_Price__c': base_price,
 
317
  'Instructions__c': instructions,
318
  'Category__c': menu_item.get('veg_nonveg', ''),
319
  'Section__c': menu_item.get('section', ''),
320
+ 'Customer_Email__c': customer_email
321
  })
 
 
 
 
 
 
 
 
322
  logger.debug(f"Created new Cart_Item__c for {menu_item['name']}")
323
+
324
+ # Create Custom_Dish__c record
325
+ sf.Custom_Dish__c.create({
326
+ 'Name': menu_item['name'],
327
+ 'Price__c': total_price,
328
+ 'Description__c': (ingredient_names + '; ' + instructions).strip('; '),
329
+ 'Image1__c': menu_item.get('image_url', ''),
330
+ 'Veg_NonVeg__c': menu_item.get('veg_nonveg', ''),
331
+ 'Section__c': menu_item.get('section', '')
332
+ })
333
+ logger.debug(f"Created Custom_Dish__c for {menu_item['name']}")
334
+
335
  return jsonify({"success": True, "message": "Customization submitted"})
336
 
337
  else:
 
340
  except Exception as e:
341
  logger.error(f"Failed to submit: {str(e)}")
342
  return jsonify({"error": f"Failed to submit: {str(e)}"}), 500
343
+
344
  from flask import Flask, render_template, request, jsonify
345
  import os
346
  import base64