lokesh341 commited on
Commit
d5c0d05
·
verified ·
1 Parent(s): 270feec

Update menu.py

Browse files
Files changed (1) hide show
  1. menu.py +132 -1
menu.py CHANGED
@@ -177,4 +177,135 @@ def menu():
177
  cart_item_count=cart_item_count
178
  )
179
 
180
- # [Rest of your existing routes...]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  cart_item_count=cart_item_count
178
  )
179
 
180
+ @menu_blueprint.route('/api/addons', methods=['GET'])
181
+ def get_addons():
182
+ item_name = request.args.get('item_name')
183
+ item_section = request.args.get('item_section')
184
+
185
+ if not item_name or not item_section:
186
+ return jsonify({"success": False, "error": "Item name and section are required."}), 400
187
+
188
+ try:
189
+ query = f"""
190
+ SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c
191
+ FROM Customization_Options__c
192
+ WHERE Section__c = '{item_section}'
193
+ """
194
+ result = sf.query(query)
195
+ addons = result.get('records', [])
196
+
197
+ if not addons:
198
+ return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
199
+
200
+ formatted_addons = []
201
+ for addon in addons:
202
+ options = addon.get("Options__c", "")
203
+ if options:
204
+ options = options.split(", ")
205
+ else:
206
+ options = []
207
+
208
+ formatted_addons.append({
209
+ "name": addon["Name"],
210
+ "type": addon["Customization_Type__c"],
211
+ "options": options,
212
+ "max_selections": addon.get("Max_Selections__c", 1),
213
+ "extra_charge": addon.get("Extra_Charge__c", False),
214
+ "extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
215
+ })
216
+
217
+ return jsonify({"success": True, "addons": formatted_addons})
218
+
219
+ except Exception as e:
220
+ print(f"Error fetching addons: {str(e)}")
221
+ return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
222
+
223
+ @menu_blueprint.route('/cart/add', methods=['POST'])
224
+ def add_to_cart():
225
+ try:
226
+ data = request.json
227
+ item_name = data.get('itemName', '').strip()
228
+ item_price = data.get('itemPrice')
229
+ item_image = data.get('itemImage')
230
+ addons = data.get('addons', [])
231
+ instructions = data.get('instructions', '')
232
+ category = data.get('category')
233
+ section = data.get('section')
234
+ quantity = data.get('quantity', 1)
235
+ customer_email = session.get('user_email')
236
+
237
+ if not item_name or not item_price:
238
+ return jsonify({"success": False, "error": "Item name and price are required."}), 400
239
+
240
+ if not customer_email:
241
+ return jsonify({"success": False, "error": "User email is required."}), 400
242
+
243
+ query = f"""
244
+ SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
245
+ FROM Cart_Item__c
246
+ WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
247
+ """
248
+ result = sf.query(query)
249
+ cart_items = result.get("records", [])
250
+
251
+ addons_price = sum(addon['price'] for addon in addons)
252
+ new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
253
+
254
+ if cart_items:
255
+ cart_item_id = cart_items[0]['Id']
256
+ existing_quantity = cart_items[0]['Quantity__c']
257
+ existing_addons = cart_items[0].get('Add_Ons__c', "None")
258
+ existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
259
+ existing_instructions = cart_items[0].get('Instructions__c', "")
260
+
261
+ combined_addons = existing_addons if existing_addons != "None" else ""
262
+ if new_addons:
263
+ combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
264
+
265
+ combined_instructions = existing_instructions
266
+ if instructions:
267
+ combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
268
+
269
+ combined_addons_list = combined_addons.split("; ")
270
+ combined_addons_price = sum(
271
+ float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
272
+ )
273
+
274
+ sf.Cart_Item__c.update(cart_item_id, {
275
+ "Quantity__c": existing_quantity + quantity,
276
+ "Add_Ons__c": combined_addons,
277
+ "Add_Ons_Price__c": combined_addons_price,
278
+ "Instructions__c": combined_instructions,
279
+ "Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
280
+ "Category__c": category,
281
+ "Section__c": section
282
+ })
283
+ else:
284
+ addons_string = "None"
285
+ if addons:
286
+ addons_string = new_addons
287
+
288
+ total_price = item_price * quantity + addons_price
289
+
290
+ sf.Cart_Item__c.create({
291
+ "Name": item_name,
292
+ "Price__c": total_price,
293
+ "Base_Price__c": item_price,
294
+ "Quantity__c": quantity,
295
+ "Add_Ons_Price__c": addons_price,
296
+ "Add_Ons__c": addons_string,
297
+ "Image1__c": item_image,
298
+ "Customer_Email__c": customer_email,
299
+ "Instructions__c": instructions,
300
+ "Category__c": category,
301
+ "Section__c": section
302
+ })
303
+
304
+ return jsonify({"success": True, "message": "Item added to cart successfully."})
305
+
306
+ except KeyError as e:
307
+ return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
308
+
309
+ except Exception as e:
310
+ print(f"Error adding item to cart: {str(e)}")
311
+ return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500