Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -223,7 +223,12 @@ def submit_customization_ingredients():
|
|
223 |
return jsonify({"error": "User email not found in session"}), 400
|
224 |
|
225 |
try:
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
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,15 +236,21 @@ def submit_customization_ingredients():
|
|
231 |
addons_price = 0
|
232 |
total_price = (base_price * quantity) + addons_price
|
233 |
|
234 |
-
|
|
|
235 |
existing_item = sf.query(soql)
|
236 |
|
237 |
if existing_item['records']:
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
240 |
new_quantity = existing_quantity + quantity
|
241 |
-
new_addons =
|
242 |
-
new_instructions =
|
243 |
updated_price = (base_price * new_quantity) + addons_price
|
244 |
|
245 |
sf.Cart_Item__c.update(item_id, {
|
@@ -249,8 +260,8 @@ def submit_customization_ingredients():
|
|
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,
|
@@ -266,35 +277,41 @@ def submit_customization_ingredients():
|
|
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 |
-
'
|
|
|
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 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
295 |
new_quantity = existing_quantity + quantity
|
296 |
-
new_addons =
|
297 |
-
new_instructions =
|
298 |
updated_price = (base_price * new_quantity) + addons_price
|
299 |
|
300 |
sf.Cart_Item__c.update(item_id, {
|
@@ -304,8 +321,8 @@ def submit_customization_ingredients():
|
|
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,
|
@@ -321,14 +338,15 @@ def submit_customization_ingredients():
|
|
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 |
-
'
|
|
|
332 |
})
|
333 |
logger.debug(f"Created Custom_Dish__c for {menu_item['name']}")
|
334 |
|
@@ -341,6 +359,7 @@ def submit_customization_ingredients():
|
|
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
|
|
|
223 |
return jsonify({"error": "User email not found in session"}), 400
|
224 |
|
225 |
try:
|
226 |
+
# Fetch customer name from Customer_Login__c using email
|
227 |
+
soql_customer = f"SELECT Name, Email__c FROM Customer_Login__c WHERE Email__c = '{customer_email}' LIMIT 1"
|
228 |
+
customer_record = sf.query(soql_customer)
|
229 |
+
customer_name = customer_record['records'][0]['Name'] if customer_record['records'] else 'Unknown Customer'
|
230 |
+
|
231 |
+
if items: # Bulk items in cart submission
|
232 |
for item in items:
|
233 |
ingredient_names = ', '.join(i['name'] for i in item.get('ingredients', [])) if item.get('ingredients') else ''
|
234 |
base_price = item.get('price', 0.0)
|
|
|
236 |
addons_price = 0
|
237 |
total_price = (base_price * quantity) + addons_price
|
238 |
|
239 |
+
# Check if item exists in cart
|
240 |
+
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"
|
241 |
existing_item = sf.query(soql)
|
242 |
|
243 |
if existing_item['records']:
|
244 |
+
# Update existing cart item
|
245 |
+
record = existing_item['records'][0]
|
246 |
+
item_id = record['Id']
|
247 |
+
existing_quantity = record['Quantity__c'] or 0
|
248 |
+
existing_addons = record.get('Add_Ons__c') or ''
|
249 |
+
existing_instructions = record.get('Instructions__c') or ''
|
250 |
+
|
251 |
new_quantity = existing_quantity + quantity
|
252 |
+
new_addons = existing_addons + (', ' + ingredient_names if ingredient_names else '')
|
253 |
+
new_instructions = existing_instructions + ('; ' + instructions if instructions else '')
|
254 |
updated_price = (base_price * new_quantity) + addons_price
|
255 |
|
256 |
sf.Cart_Item__c.update(item_id, {
|
|
|
260 |
'Price__c': updated_price
|
261 |
})
|
262 |
logger.debug(f"Updated {item['name']} in Cart_Item__c")
|
|
|
263 |
else:
|
264 |
+
# Create new cart item
|
265 |
sf.Cart_Item__c.create({
|
266 |
'Name': item['name'],
|
267 |
'Base_Price__c': base_price,
|
|
|
277 |
})
|
278 |
logger.debug(f"Created new Cart_Item__c for {item['name']}")
|
279 |
|
280 |
+
# Create corresponding Custom_Dish__c record
|
281 |
sf.Custom_Dish__c.create({
|
282 |
+
'Name': f"{customer_name} - {item['name']}",
|
283 |
'Price__c': total_price,
|
284 |
'Description__c': (ingredient_names + '; ' + instructions).strip('; '),
|
285 |
'Image1__c': item.get('image_url', ''),
|
286 |
'Veg_NonVeg__c': item.get('veg_nonveg', ''),
|
287 |
+
'Total_Ordered__c': 1,
|
288 |
+
'Section__c': 'Customized dish' # Fixed value here
|
289 |
})
|
290 |
logger.debug(f"Created Custom_Dish__c for {item['name']}")
|
291 |
|
292 |
return jsonify({"success": True, "message": f"Processed {len(items)} items"})
|
293 |
|
294 |
+
elif menu_item: # Single item customization
|
295 |
ingredient_names = ', '.join(i['name'] for i in ingredients) if ingredients else ''
|
296 |
base_price = menu_item.get('price', 0.0)
|
297 |
quantity = 1
|
298 |
addons_price = 0
|
299 |
total_price = (base_price * quantity) + addons_price
|
300 |
|
301 |
+
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"
|
302 |
existing_item = sf.query(soql)
|
303 |
|
304 |
if existing_item['records']:
|
305 |
+
# Update existing cart item
|
306 |
+
record = existing_item['records'][0]
|
307 |
+
item_id = record['Id']
|
308 |
+
existing_quantity = record['Quantity__c'] or 0
|
309 |
+
existing_addons = record.get('Add_Ons__c') or ''
|
310 |
+
existing_instructions = record.get('Instructions__c') or ''
|
311 |
+
|
312 |
new_quantity = existing_quantity + quantity
|
313 |
+
new_addons = existing_addons + (', ' + ingredient_names if ingredient_names else '')
|
314 |
+
new_instructions = existing_instructions + ('; ' + instructions if instructions else '')
|
315 |
updated_price = (base_price * new_quantity) + addons_price
|
316 |
|
317 |
sf.Cart_Item__c.update(item_id, {
|
|
|
321 |
'Price__c': updated_price
|
322 |
})
|
323 |
logger.debug(f"Updated customization for {menu_item['name']} in Cart_Item__c")
|
|
|
324 |
else:
|
325 |
+
# Create new cart item
|
326 |
sf.Cart_Item__c.create({
|
327 |
'Name': menu_item['name'],
|
328 |
'Base_Price__c': base_price,
|
|
|
338 |
})
|
339 |
logger.debug(f"Created new Cart_Item__c for {menu_item['name']}")
|
340 |
|
341 |
+
# Create corresponding Custom_Dish__c record
|
342 |
sf.Custom_Dish__c.create({
|
343 |
+
'Name': f"{customer_name} - {menu_item['name']}",
|
344 |
'Price__c': total_price,
|
345 |
'Description__c': (ingredient_names + '; ' + instructions).strip('; '),
|
346 |
'Image1__c': menu_item.get('image_url', ''),
|
347 |
'Veg_NonVeg__c': menu_item.get('veg_nonveg', ''),
|
348 |
+
'Total_Ordered__c': 1,
|
349 |
+
'Section__c': 'Customized dish' # Fixed value here
|
350 |
})
|
351 |
logger.debug(f"Created Custom_Dish__c for {menu_item['name']}")
|
352 |
|
|
|
359 |
logger.error(f"Failed to submit: {str(e)}")
|
360 |
return jsonify({"error": f"Failed to submit: {str(e)}"}), 500
|
361 |
|
362 |
+
|
363 |
from flask import Flask, render_template, request, jsonify
|
364 |
import os
|
365 |
import base64
|