|  | from flask import Blueprint, render_template, request, session, jsonify | 
					
						
						|  | from salesforce import get_salesforce_connection | 
					
						
						|  |  | 
					
						
						|  | menu_blueprint = Blueprint('menu', __name__) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | sf = get_salesforce_connection() | 
					
						
						|  |  | 
					
						
						|  | @menu_blueprint.route("/menu", methods=["GET", "POST"]) | 
					
						
						|  | def menu(): | 
					
						
						|  | selected_category = request.args.get("category", "All") | 
					
						
						|  | user_email = session.get('user_email') | 
					
						
						|  |  | 
					
						
						|  | if not user_email: | 
					
						
						|  | user_email = request.args.get("email") | 
					
						
						|  | user_name = request.args.get("name") | 
					
						
						|  |  | 
					
						
						|  | if user_email: | 
					
						
						|  | session['user_email'] = user_email | 
					
						
						|  | session['user_name'] = user_name | 
					
						
						|  | else: | 
					
						
						|  | return redirect(url_for("login")) | 
					
						
						|  | else: | 
					
						
						|  | user_name = session.get('user_name') | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | first_letter = user_name[0].upper() if user_name else "A" | 
					
						
						|  |  | 
					
						
						|  | try: | 
					
						
						|  |  | 
					
						
						|  | user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'" | 
					
						
						|  | user_result = sf.query(user_query) | 
					
						
						|  |  | 
					
						
						|  | if not user_result['records']: | 
					
						
						|  | return redirect(url_for('login')) | 
					
						
						|  |  | 
					
						
						|  | referral_code = user_result['records'][0].get('Referral__c', 'N/A') | 
					
						
						|  | reward_points = user_result['records'][0].get('Reward_Points__c', 0) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | menu_query = """ | 
					
						
						|  | SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c | 
					
						
						|  | FROM Menu_Item__c | 
					
						
						|  | """ | 
					
						
						|  | result = sf.query(menu_query) | 
					
						
						|  | food_items = result['records'] if 'records' in result else [] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for item in food_items: | 
					
						
						|  | if 'Total_Ordered__c' not in item or item['Total_Ordered__c'] is None: | 
					
						
						|  | item['Total_Ordered__c'] = 0 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | custom_dish_query = """ | 
					
						
						|  | SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c, Total_Ordered__c | 
					
						
						|  | FROM Custom_Dish__c | 
					
						
						|  | WHERE CreatedDate >= LAST_N_DAYS:7 | 
					
						
						|  | """ | 
					
						
						|  | custom_dish_result = sf.query(custom_dish_query) | 
					
						
						|  | custom_dishes = custom_dish_result['records'] if 'records' in custom_dish_result else [] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | all_items = food_items + custom_dishes | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | section_order = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"] | 
					
						
						|  | ordered_menu = {section: [] for section in section_order} | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | best_sellers = sorted(all_items, key=lambda x: x.get("Total_Ordered__c", 0), reverse=True) | 
					
						
						|  |  | 
					
						
						|  | if selected_category == "Veg": | 
					
						
						|  | best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]] | 
					
						
						|  | elif selected_category == "Non veg": | 
					
						
						|  | best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | best_sellers = best_sellers[:4] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if best_sellers: | 
					
						
						|  | ordered_menu["Best Sellers"] = best_sellers | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | added_item_names = set() | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for item in all_items: | 
					
						
						|  | section = item.get("Section__c", "Others") | 
					
						
						|  | if section not in ordered_menu: | 
					
						
						|  | ordered_menu[section] = [] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if item['Name'] in added_item_names: | 
					
						
						|  | continue | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if selected_category == "Veg" and item.get("Veg_NonVeg__c") not in ["Veg", "both"]: | 
					
						
						|  | continue | 
					
						
						|  | if selected_category == "Non veg" and item.get("Veg_NonVeg__c") not in ["Non veg", "both"]: | 
					
						
						|  | continue | 
					
						
						|  |  | 
					
						
						|  | ordered_menu[section].append(item) | 
					
						
						|  | added_item_names.add(item['Name']) | 
					
						
						|  | print(f"Added item to {section}: {item['Name']}") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | ordered_menu = {section: items for section, items in ordered_menu.items() if items} | 
					
						
						|  | print(f"Final ordered menu: {ordered_menu.keys()}") | 
					
						
						|  |  | 
					
						
						|  | categories = ["All", "Veg", "Non veg"] | 
					
						
						|  |  | 
					
						
						|  | except Exception as e: | 
					
						
						|  | print(f"Error fetching menu data: {str(e)}") | 
					
						
						|  | ordered_menu = {} | 
					
						
						|  | categories = ["All", "Veg", "Non veg"] | 
					
						
						|  | referral_code = 'N/A' | 
					
						
						|  | reward_points = 0 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | return render_template( | 
					
						
						|  | "menu.html", | 
					
						
						|  | ordered_menu=ordered_menu, | 
					
						
						|  | categories=categories, | 
					
						
						|  | selected_category=selected_category, | 
					
						
						|  | referral_code=referral_code, | 
					
						
						|  | reward_points=reward_points, | 
					
						
						|  | user_name=user_name, | 
					
						
						|  | first_letter=first_letter | 
					
						
						|  | ) | 
					
						
						|  | @menu_blueprint.route('/api/addons', methods=['GET']) | 
					
						
						|  | def get_addons(): | 
					
						
						|  | item_name = request.args.get('item_name') | 
					
						
						|  | item_section = request.args.get('item_section') | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if not item_name or not item_section: | 
					
						
						|  | return jsonify({"success": False, "error": "Item name and section are required."}), 400 | 
					
						
						|  |  | 
					
						
						|  | try: | 
					
						
						|  |  | 
					
						
						|  | query = f""" | 
					
						
						|  | SELECT Name, Customization_Type__c, Options__c, Max_Selections__c, Extra_Charge__c, Extra_Charge_Amount__c | 
					
						
						|  | FROM Customization_Options__c | 
					
						
						|  | WHERE Section__c = '{item_section}' | 
					
						
						|  | """ | 
					
						
						|  | result = sf.query(query) | 
					
						
						|  | addons = result.get('records', []) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if not addons: | 
					
						
						|  | return jsonify({"success": False, "error": "No customization options found for the given section."}), 404 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | formatted_addons = [] | 
					
						
						|  | for addon in addons: | 
					
						
						|  |  | 
					
						
						|  | options = addon.get("Options__c", "") | 
					
						
						|  | if options: | 
					
						
						|  | options = options.split(", ") | 
					
						
						|  | else: | 
					
						
						|  | options = [] | 
					
						
						|  |  | 
					
						
						|  | formatted_addons.append({ | 
					
						
						|  | "name": addon["Name"], | 
					
						
						|  | "type": addon["Customization_Type__c"], | 
					
						
						|  | "options": options, | 
					
						
						|  | "max_selections": addon.get("Max_Selections__c", 1), | 
					
						
						|  | "extra_charge": addon.get("Extra_Charge__c", False), | 
					
						
						|  | "extra_charge_amount": addon.get("Extra_Charge_Amount__c", 0) | 
					
						
						|  | }) | 
					
						
						|  |  | 
					
						
						|  | return jsonify({"success": True, "addons": formatted_addons}) | 
					
						
						|  |  | 
					
						
						|  | except Exception as e: | 
					
						
						|  |  | 
					
						
						|  | app.logger.error(f"Error fetching addons: {str(e)}") | 
					
						
						|  | return jsonify({"success": False, "error": "An error occurred while fetching customization options."}), 500 | 
					
						
						|  |  | 
					
						
						|  | @menu_blueprint.route('/cart/add', methods=['POST']) | 
					
						
						|  | def add_to_cart(): | 
					
						
						|  | try: | 
					
						
						|  |  | 
					
						
						|  | data = request.json | 
					
						
						|  | item_name = data.get('itemName', '').strip() | 
					
						
						|  | item_price = data.get('itemPrice') | 
					
						
						|  | item_image = data.get('itemImage') | 
					
						
						|  | addons = data.get('addons', []) | 
					
						
						|  | instructions = data.get('instructions', '') | 
					
						
						|  | category = data.get('category') | 
					
						
						|  | section = data.get('section') | 
					
						
						|  | quantity = data.get('quantity', 1) | 
					
						
						|  | customer_email = session.get('user_email') | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if not item_name or not item_price: | 
					
						
						|  | return jsonify({"success": False, "error": "Item name and price are required."}), 400 | 
					
						
						|  |  | 
					
						
						|  | if not customer_email: | 
					
						
						|  | return jsonify({"success": False, "error": "User email is required."}), 400 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | query = f""" | 
					
						
						|  | SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c | 
					
						
						|  | FROM Cart_Item__c | 
					
						
						|  | WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}' | 
					
						
						|  | """ | 
					
						
						|  | result = sf.query(query) | 
					
						
						|  | cart_items = result.get("records", []) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | addons_price = sum(addon['price'] for addon in addons) | 
					
						
						|  | new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if cart_items: | 
					
						
						|  | cart_item_id = cart_items[0]['Id'] | 
					
						
						|  | existing_quantity = cart_items[0]['Quantity__c'] | 
					
						
						|  | existing_addons = cart_items[0].get('Add_Ons__c', "None") | 
					
						
						|  | existing_addons_price = cart_items[0].get('Add_Ons_Price__c', 0) | 
					
						
						|  | existing_instructions = cart_items[0].get('Instructions__c', "") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | combined_addons = existing_addons if existing_addons != "None" else "" | 
					
						
						|  | if new_addons: | 
					
						
						|  | combined_addons = f"{combined_addons}; {new_addons}".strip("; ") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | combined_instructions = existing_instructions | 
					
						
						|  | if instructions: | 
					
						
						|  | combined_instructions = f"{combined_instructions} | {instructions}".strip(" | ") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | combined_addons_list = combined_addons.split("; ") | 
					
						
						|  | combined_addons_price = sum( | 
					
						
						|  | float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | sf.Cart_Item__c.update(cart_item_id, { | 
					
						
						|  | "Quantity__c": existing_quantity + quantity, | 
					
						
						|  | "Add_Ons__c": combined_addons, | 
					
						
						|  | "Add_Ons_Price__c": combined_addons_price, | 
					
						
						|  | "Instructions__c": combined_instructions, | 
					
						
						|  | "Price__c": (existing_quantity + quantity) * item_price + combined_addons_price, | 
					
						
						|  | "Category__c": category, | 
					
						
						|  | "Section__c": section | 
					
						
						|  | }) | 
					
						
						|  | else: | 
					
						
						|  |  | 
					
						
						|  | addons_string = "None" | 
					
						
						|  | if addons: | 
					
						
						|  | addons_string = new_addons | 
					
						
						|  |  | 
					
						
						|  | total_price = item_price * quantity + addons_price | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | sf.Cart_Item__c.create({ | 
					
						
						|  | "Name": item_name, | 
					
						
						|  | "Price__c": total_price, | 
					
						
						|  | "Base_Price__c": item_price, | 
					
						
						|  | "Quantity__c": quantity, | 
					
						
						|  | "Add_Ons_Price__c": addons_price, | 
					
						
						|  | "Add_Ons__c": addons_string, | 
					
						
						|  | "Image1__c": item_image, | 
					
						
						|  | "Customer_Email__c": customer_email, | 
					
						
						|  | "Instructions__c": instructions, | 
					
						
						|  | "Category__c": category, | 
					
						
						|  | "Section__c": section | 
					
						
						|  | }) | 
					
						
						|  |  | 
					
						
						|  | return jsonify({"success": True, "message": "Item added to cart successfully."}) | 
					
						
						|  |  | 
					
						
						|  | except KeyError as e: | 
					
						
						|  |  | 
					
						
						|  | return jsonify({"success": False, "error": f"Missing required field: {str(e)}"}), 400 | 
					
						
						|  |  | 
					
						
						|  | except Exception as e: | 
					
						
						|  |  | 
					
						
						|  | print(f"Error adding item to cart: {str(e)}") | 
					
						
						|  | return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500 | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  |