nagasurendra commited on
Commit
88fcb87
·
verified ·
1 Parent(s): 49038d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -31
app.py CHANGED
@@ -270,22 +270,50 @@ def submit_customization_ingredients():
270
  addons_price = 0
271
  total_price = (base_price * quantity) + addons_price
272
 
273
- # Create Cart_Item__c record and include Customer_Email__c
274
- sf.Cart_Item__c.create({
275
- 'Name': item['name'],
276
- 'Base_Price__c': base_price,
277
- 'Quantity__c': quantity,
278
- 'Add_Ons__c': ingredient_names,
279
- 'Add_Ons_Price__c': addons_price,
280
- 'Price__c': total_price,
281
- 'Image1__c': item.get('image_url', ''),
282
- 'Instructions__c': item.get('instructions', ''),
283
- 'Category__c': item.get('veg_nonveg', ''),
284
- 'Section__c': item.get('section', ''),
285
- 'Customer_Email__c': customer_email # Store session email
286
- })
287
- logger.debug(f"Submitted {len(items)} items to Cart_Item__c")
288
- return jsonify({"success": True, "message": f"Submitted {len(items)} items"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
  elif menu_item: # Single item customization
291
  ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
@@ -294,21 +322,48 @@ def submit_customization_ingredients():
294
  addons_price = 0
295
  total_price = (base_price * quantity) + addons_price
296
 
297
- # Create Cart_Item__c record and include Customer_Email__c
298
- sf.Cart_Item__c.create({
299
- 'Name': menu_item['name'],
300
- 'Base_Price__c': base_price,
301
- 'Quantity__c': quantity,
302
- 'Add_Ons__c': ingredient_names,
303
- 'Add_Ons_Price__c': addons_price,
304
- 'Price__c': total_price,
305
- 'Image1__c': menu_item.get('image_url', ''),
306
- 'Instructions__c': instructions,
307
- 'Category__c': menu_item.get('veg_nonveg', ''),
308
- 'Section__c': menu_item.get('section', ''),
309
- 'Customer_Email__c': customer_email # Store session email
310
- })
311
- logger.debug(f"Submitted customization for {menu_item['name']} to Cart_Item__c")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  return jsonify({"success": True, "message": "Customization submitted"})
313
 
314
  else:
 
270
  addons_price = 0
271
  total_price = (base_price * quantity) + addons_price
272
 
273
+ # Check if item already exists in the cart for the user based on Customer_Email__c and Item Name
274
+ existing_item = sf.Cart_Item__c.query(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")
275
+
276
+ if existing_item: # If the item exists, update it
277
+ item_id = existing_item[0]['Id']
278
+ existing_quantity = existing_item[0]['Quantity__c']
279
+ existing_addons = existing_item[0]['Add_Ons__c']
280
+ existing_instructions = existing_item[0]['Instructions__c']
281
+ existing_price = existing_item[0]['Price__c']
282
+
283
+ # Update quantity, addons, and instructions
284
+ new_quantity = existing_quantity + quantity
285
+ # Ensure existing_addons and existing_instructions are never None or empty before concatenation
286
+ new_addons = (existing_addons or '') + (', ' + ingredient_names if ingredient_names else '')
287
+ new_instructions = (existing_instructions or '') + ('; ' + instructions if instructions else '')
288
+
289
+
290
+ # Calculate the new total price
291
+ updated_price = (base_price * new_quantity) + addons_price
292
+
293
+ # Update the existing item in Salesforce
294
+ sf.Cart_Item__c.update(item_id, {
295
+ 'Quantity__c': new_quantity,
296
+ 'Add_Ons__c': new_addons,
297
+ 'Instructions__c': new_instructions,
298
+ 'Price__c': updated_price
299
+ })
300
+ logger.debug(f"Updated {item['name']} in Cart_Item__c")
301
+ else: # If the item doesn't exist, create a new one
302
+ sf.Cart_Item__c.create({
303
+ 'Name': item['name'],
304
+ 'Base_Price__c': base_price,
305
+ 'Quantity__c': quantity,
306
+ 'Add_Ons__c': ingredient_names,
307
+ 'Add_Ons_Price__c': addons_price,
308
+ 'Price__c': total_price,
309
+ 'Image1__c': item.get('image_url', ''),
310
+ 'Instructions__c': item.get('instructions', ''),
311
+ 'Category__c': item.get('veg_nonveg', ''),
312
+ 'Section__c': item.get('section', ''),
313
+ 'Customer_Email__c': customer_email # Store session email
314
+ })
315
+ logger.debug(f"Created new Cart_Item__c for {item['name']}")
316
+ return jsonify({"success": True, "message": f"Processed {len(items)} items"})
317
 
318
  elif menu_item: # Single item customization
319
  ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
 
322
  addons_price = 0
323
  total_price = (base_price * quantity) + addons_price
324
 
325
+ # Check if the menu item already exists in the cart for the user
326
+ existing_item = sf.Cart_Item__c.query(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")
327
+
328
+ if existing_item: # If the item exists, update it
329
+ item_id = existing_item[0]['Id']
330
+ existing_quantity = existing_item[0]['Quantity__c']
331
+ existing_addons = existing_item[0]['Add_Ons__c']
332
+ existing_instructions = existing_item[0]['Instructions__c']
333
+ existing_price = existing_item[0]['Price__c']
334
+
335
+ # Update quantity, addons, and instructions
336
+ new_quantity = existing_quantity + quantity
337
+ # Ensure existing_addons and existing_instructions are never None or empty before concatenation
338
+ new_addons = (existing_addons or '') + (', ' + ingredient_names if ingredient_names else '')
339
+ new_instructions = (existing_instructions or '') + ('; ' + instructions if instructions else '')
340
+
341
+ # Calculate the new total price
342
+ updated_price = (base_price * new_quantity) + addons_price
343
+
344
+ # Update the existing item in Salesforce
345
+ sf.Cart_Item__c.update(item_id, {
346
+ 'Quantity__c': new_quantity,
347
+ 'Add_Ons__c': new_addons,
348
+ 'Instructions__c': new_instructions,
349
+ 'Price__c': updated_price
350
+ })
351
+ logger.debug(f"Updated customization for {menu_item['name']} in Cart_Item__c")
352
+ else: # If the item doesn't exist, create a new one
353
+ sf.Cart_Item__c.create({
354
+ 'Name': menu_item['name'],
355
+ 'Base_Price__c': base_price,
356
+ 'Quantity__c': quantity,
357
+ 'Add_Ons__c': ingredient_names,
358
+ 'Add_Ons_Price__c': addons_price,
359
+ 'Price__c': total_price,
360
+ 'Image1__c': menu_item.get('image_url', ''),
361
+ 'Instructions__c': instructions,
362
+ 'Category__c': menu_item.get('veg_nonveg', ''),
363
+ 'Section__c': menu_item.get('section', ''),
364
+ 'Customer_Email__c': customer_email # Store session email
365
+ })
366
+ logger.debug(f"Created new Cart_Item__c for {menu_item['name']}")
367
  return jsonify({"success": True, "message": "Customization submitted"})
368
 
369
  else: