lokesh341 commited on
Commit
46562a1
·
verified ·
1 Parent(s): 56e8c68

Update menu.py

Browse files
Files changed (1) hide show
  1. menu.py +9 -133
menu.py CHANGED
@@ -72,6 +72,11 @@ def menu():
72
  referral_code = user_result['records'][0].get('Referral__c', 'N/A')
73
  reward_points = user_result['records'][0].get('Reward_Points__c', 0)
74
 
 
 
 
 
 
75
  # Query to fetch Menu_Item__c records including Video1__c
76
  menu_query = """
77
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
@@ -158,6 +163,7 @@ def menu():
158
  categories = ["All", "Veg", "Non veg"]
159
  referral_code = 'N/A'
160
  reward_points = 0
 
161
 
162
  return render_template(
163
  "menu.html",
@@ -167,138 +173,8 @@ def menu():
167
  referral_code=referral_code,
168
  reward_points=reward_points,
169
  user_name=user_name,
170
- first_letter=first_letter
 
171
  )
172
 
173
- @menu_blueprint.route('/api/addons', methods=['GET'])
174
- def get_addons():
175
- item_name = request.args.get('item_name')
176
- item_section = request.args.get('item_section')
177
-
178
- if not item_name or not item_section:
179
- return jsonify({"success": False, "error": "Item name and section are required."}), 400
180
-
181
- try:
182
- query = f"""
183
- SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c
184
- FROM Customization_Options__c
185
- WHERE Section__c = '{item_section}'
186
- """
187
- result = sf.query(query)
188
- addons = result.get('records', [])
189
-
190
- if not addons:
191
- return jsonify({"success": False, "error": "No customization options found for the given section."}), 404
192
-
193
- formatted_addons = []
194
- for addon in addons:
195
- options = addon.get("Options__c", "")
196
- if options:
197
- options = options.split(", ")
198
- else:
199
- options = []
200
-
201
- formatted_addons.append({
202
- "name": addon["Name"],
203
- "type": addon["Customization_Type__c"],
204
- "options": options,
205
- "max_selections": addon.get("Max_Selections__c", 1),
206
- "extra_charge": addon.get("Extra_Charge__c", False),
207
- "extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0)
208
- })
209
-
210
- return jsonify({"success": True, "addons": formatted_addons})
211
-
212
- except Exception as e:
213
- print(f"Error fetching addons: {str(e)}")
214
- return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500
215
-
216
- @menu_blueprint.route('/cart/add', methods=['POST'])
217
- def add_to_cart():
218
- try:
219
- data = request.json
220
- item_name = data.get('itemName', '').strip()
221
- item_price = data.get('itemPrice')
222
- item_image = data.get('itemImage')
223
- addons = data.get('addons', [])
224
- instructions = data.get('instructions', '')
225
- category = data.get('category')
226
- section = data.get('section')
227
- quantity = data.get('quantity', 1)
228
- customer_email = session.get('user_email')
229
-
230
- if not item_name or not item_price:
231
- return jsonify({"success": False, "error": "Item name and price are required."}), 400
232
-
233
- if not customer_email:
234
- return jsonify({"success": False, "error": "User email is required."}), 400
235
-
236
- query = f"""
237
- SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c
238
- FROM Cart_Item__c
239
- WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
240
- """
241
- result = sf.query(query)
242
- cart_items = result.get("records", [])
243
-
244
- addons_price = sum(addon['price'] for addon in addons)
245
- new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
246
-
247
- if cart_items:
248
- cart_item_id = cart_items[0]['Id']
249
- existing_quantity = cart_items[0]['Quantity__c']
250
- existing_addons = cart_items[0].get('Add_Ons__c', "None")
251
- existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0)
252
- existing_instructions = cart_items[0].get('Instructions__c', "")
253
-
254
- combined_addons = existing_addons if existing_addons != "None" else ""
255
- if new_addons:
256
- combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
257
-
258
- combined_instructions = existing_instructions
259
- if instructions:
260
- combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ")
261
-
262
- combined_addons_list = combined_addons.split("; ")
263
- combined_addons_price = sum(
264
- float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
265
- )
266
-
267
- sf.Cart_Item__c.update(cart_item_id, {
268
- "Quantity__c": existing_quantity + quantity,
269
- "Add_Ons__c": combined_addons,
270
- "Add_Ons_Price__c": combined_addons_price,
271
- "Instructions__c": combined_instructions,
272
- "Price__c": (existing_quantity + quantity) * item_price + combined_addons_price,
273
- "Category__c": category,
274
- "Section__c": section
275
- })
276
- else:
277
- addons_string = "None"
278
- if addons:
279
- addons_string = new_addons
280
-
281
- total_price = item_price * quantity + addons_price
282
-
283
- sf.Cart_Item__c.create({
284
- "Name": item_name,
285
- "Price__c": total_price,
286
- "Base_Price__c": item_price,
287
- "Quantity__c": quantity,
288
- "Add_Ons_Price__c": addons_price,
289
- "Add_Ons__c": addons_string,
290
- "Image1__c": item_image,
291
- "Customer_Email__c": customer_email,
292
- "Instructions__c": instructions,
293
- "Category__c": category,
294
- "Section__c": section
295
- })
296
-
297
- return jsonify({"success": True, "message": "Item added to cart successfully."})
298
-
299
- except KeyError as e:
300
- return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400
301
-
302
- except Exception as e:
303
- print(f"Error adding item to cart: {str(e)}")
304
- return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
 
72
  referral_code = user_result['records'][0].get('Referral__c', 'N/A')
73
  reward_points = user_result['records'][0].get('Reward_Points__c', 0)
74
 
75
+ # Get cart item count
76
+ cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
77
+ cart_count_result = sf.query(cart_query)
78
+ cart_item_count = cart_count_result['totalSize']
79
+
80
  # Query to fetch Menu_Item__c records including Video1__c
81
  menu_query = """
82
  SELECT Name, Price__c, Description__c, Image1__c, Image2__c,
 
163
  categories = ["All", "Veg", "Non veg"]
164
  referral_code = 'N/A'
165
  reward_points = 0
166
+ cart_item_count = 0
167
 
168
  return render_template(
169
  "menu.html",
 
173
  referral_code=referral_code,
174
  reward_points=reward_points,
175
  user_name=user_name,
176
+ first_letter=first_letter,
177
+ cart_item_count=cart_item_count
178
  )
179
 
180
+ # [Rest of your existing routes...]