|  | from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for | 
					
						
						|  | import os | 
					
						
						|  | from salesforce import get_salesforce_connection | 
					
						
						|  |  | 
					
						
						|  | menu_blueprint = Blueprint('menu', __name__) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | sf = get_salesforce_connection() | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static') | 
					
						
						|  | PLACEHOLDER_VIDEO = 'placeholder.mp4' | 
					
						
						|  | PLACEHOLDER_PATH = os.path.join(STATIC_DIR, PLACEHOLDER_VIDEO) | 
					
						
						|  | SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "Customized dish", "Apetizer", "Desserts", "Soft Drinks"] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if not os.path.exists(PLACEHOLDER_PATH): | 
					
						
						|  | with open(PLACEHOLDER_PATH, 'wb') as f: | 
					
						
						|  | f.close() | 
					
						
						|  | print(f"Created placeholder video at {PLACEHOLDER_PATH}") | 
					
						
						|  |  | 
					
						
						|  | def get_valid_video_path(item_name, video_url=None): | 
					
						
						|  | """Get valid video path for item with placeholder fallback.""" | 
					
						
						|  | if video_url: | 
					
						
						|  | if video_url.startswith(('http://', 'https://')): | 
					
						
						|  | return video_url | 
					
						
						|  | elif video_url.startswith('/'): | 
					
						
						|  | return video_url | 
					
						
						|  | elif video_url.startswith('069'): | 
					
						
						|  | return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}" | 
					
						
						|  | if not os.path.exists(PLACEHOLDER_PATH): | 
					
						
						|  | with open(PLACEHOLDER_PATH, 'wb') as f: | 
					
						
						|  | f.close() | 
					
						
						|  | print(f"Created missing placeholder video at {PLACEHOLDER_PATH}") | 
					
						
						|  | return f"/static/{PLACEHOLDER_VIDEO}" | 
					
						
						|  |  | 
					
						
						|  | def fetch_menu_data(selected_category, user_email): | 
					
						
						|  | """Common function to fetch and process menu data.""" | 
					
						
						|  |  | 
					
						
						|  | 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.get('records'): | 
					
						
						|  | return None, None, None, None | 
					
						
						|  |  | 
					
						
						|  | referral_code = user_result['records'][0].get('Referral__c', 'N/A') | 
					
						
						|  | reward_points = user_result['records'][0].get('Reward_Points__c', 0) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'" | 
					
						
						|  | cart_count_result = sf.query(cart_query) | 
					
						
						|  | cart_item_count = cart_count_result.get('totalSize', 0) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | menu_query = """ | 
					
						
						|  | SELECT Name, Price__c, Description__c, Image1__c, Image2__c, | 
					
						
						|  | Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c, | 
					
						
						|  | IngredientsInfo__c, NutritionalInfo__c, Allergens__c | 
					
						
						|  | FROM Menu_Item__c | 
					
						
						|  | """ | 
					
						
						|  | menu_result = sf.query_all(menu_query) | 
					
						
						|  | food_items = menu_result.get('records', []) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for item in food_items: | 
					
						
						|  | item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0 | 
					
						
						|  | item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c')) | 
					
						
						|  | item['Section__c'] = item.get('Section__c', "Others") | 
					
						
						|  | item['Description__c'] = item.get('Description__c', "No description available") | 
					
						
						|  | item['IngredientsInfo__c'] = item.get('IngredientsInfo__c', "Not specified") | 
					
						
						|  | item['NutritionalInfo__c'] = item.get('NutritionalInfo__c', "Not available") | 
					
						
						|  | item['Allergens__c'] = item.get('Allergens__c', "None listed") | 
					
						
						|  | item['is_menu_item'] = True | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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_all(custom_dish_query) | 
					
						
						|  | custom_dishes = custom_dish_result.get('records', []) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for item in custom_dishes: | 
					
						
						|  | item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0 | 
					
						
						|  | item['Video1__c'] = get_valid_video_path(item['Name']) | 
					
						
						|  | item['Section__c'] = item.get('Section__c', "Customized dish") | 
					
						
						|  | item['Description__c'] = item.get('Description__c', "No description available") | 
					
						
						|  | item['is_menu_item'] = False | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | all_items = food_items + custom_dishes | 
					
						
						|  | ordered_menu = {section: [] for section in SECTION_ORDER} | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], 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"]] | 
					
						
						|  | ordered_menu["Best Sellers"] = best_sellers[:4] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | added_item_names = set() | 
					
						
						|  | for item in all_items: | 
					
						
						|  | section = item['Section__c'] | 
					
						
						|  | if section not in ordered_menu: | 
					
						
						|  | ordered_menu[section] = [] | 
					
						
						|  |  | 
					
						
						|  | if item['Name'] in added_item_names: | 
					
						
						|  | continue | 
					
						
						|  |  | 
					
						
						|  | veg_nonveg = item.get("Veg_NonVeg__c", "both") | 
					
						
						|  | if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]: | 
					
						
						|  | continue | 
					
						
						|  | if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]: | 
					
						
						|  | continue | 
					
						
						|  |  | 
					
						
						|  | ordered_menu[section].append(item) | 
					
						
						|  | added_item_names.add(item['Name']) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | ordered_menu = {section: items for section, items in ordered_menu.items() if items} | 
					
						
						|  | return ordered_menu, referral_code, reward_points, cart_item_count | 
					
						
						|  |  | 
					
						
						|  | @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 and user_name: | 
					
						
						|  | 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" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email) | 
					
						
						|  | if ordered_menu is None: | 
					
						
						|  | return redirect(url_for('login')) | 
					
						
						|  |  | 
					
						
						|  | categories = ["All", "Veg", "Non veg"] | 
					
						
						|  |  | 
					
						
						|  | 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, | 
					
						
						|  | cart_item_count=cart_item_count | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | @menu_blueprint.route("/search", methods=["GET"]) | 
					
						
						|  | def search(): | 
					
						
						|  | selected_category = request.args.get("category", "All") | 
					
						
						|  | user_email = session.get('user_email') | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | if not user_email: | 
					
						
						|  | return redirect(url_for("login")) | 
					
						
						|  | user_name = session.get('user_name') | 
					
						
						|  | first_letter = user_name[0].upper() if user_name else "A" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | ordered_menu, referral_code, reward_points, cart_item_count = fetch_menu_data(selected_category, user_email) | 
					
						
						|  | if ordered_menu is None: | 
					
						
						|  | return redirect(url_for('login')) | 
					
						
						|  |  | 
					
						
						|  | return render_template( | 
					
						
						|  | "search.html", | 
					
						
						|  | ordered_menu=ordered_menu, | 
					
						
						|  | user_name=user_name, | 
					
						
						|  | first_letter=first_letter, | 
					
						
						|  | cart_item_count=cart_item_count | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | @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_all(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", "") | 
					
						
						|  | options = options.split(", ") if options else [] | 
					
						
						|  | formatted_addons.append({ | 
					
						
						|  | "name": addon.get("Name", ""), | 
					
						
						|  | "type": addon.get("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: | 
					
						
						|  | print(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 = float(data.get('itemPrice', 0)) | 
					
						
						|  | item_image = data.get('itemImage', '') | 
					
						
						|  | addons = data.get('addons', []) | 
					
						
						|  | instructions = data.get('instructions', '') | 
					
						
						|  | category = data.get('category', '') | 
					
						
						|  | section = data.get('section', '') | 
					
						
						|  | quantity = int(data.get('quantity', 1)) | 
					
						
						|  | customer_email = session.get('user_email') | 
					
						
						|  |  | 
					
						
						|  | if not item_name or not item_price or not customer_email: | 
					
						
						|  | return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400 | 
					
						
						|  |  | 
					
						
						|  | query = f""" | 
					
						
						|  | SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__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(float(addon.get('price', 0)) for addon in addons) | 
					
						
						|  | new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None" | 
					
						
						|  |  | 
					
						
						|  | if cart_items: | 
					
						
						|  | cart_item = cart_items[0] | 
					
						
						|  | cart_item_id = cart_item['Id'] | 
					
						
						|  | existing_quantity = int(cart_item.get('Quantity__c', 0)) | 
					
						
						|  | existing_addons = cart_item.get('Add_Ons__c', "None") | 
					
						
						|  | existing_addons_price = float(cart_item.get('Add_Ons_Price__c', 0)) | 
					
						
						|  | existing_instructions = cart_item.get('Instructions__c', "") | 
					
						
						|  |  | 
					
						
						|  | combined_addons = existing_addons if existing_addons != "None" else "" | 
					
						
						|  | if new_addons != "None": | 
					
						
						|  | combined_addons = f"{combined_addons}; {new_addons}".strip("; ") | 
					
						
						|  |  | 
					
						
						|  | combined_instructions = existing_instructions | 
					
						
						|  | if instructions: | 
					
						
						|  | combined_instructions = f"{existing_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 | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | total_price = (existing_quantity + quantity) * item_price + combined_addons_price | 
					
						
						|  |  | 
					
						
						|  | 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": total_price, | 
					
						
						|  | "Category__c": category, | 
					
						
						|  | "Section__c": section | 
					
						
						|  | }) | 
					
						
						|  | else: | 
					
						
						|  | 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": new_addons, | 
					
						
						|  | "Image1__c": item_image, | 
					
						
						|  | "Customer_Email__c": customer_email, | 
					
						
						|  | "Instructions__c": instructions, | 
					
						
						|  | "Category__c": category, | 
					
						
						|  | "Section__c": section | 
					
						
						|  | }) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | cart_query = f"SELECT Name, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}'" | 
					
						
						|  | cart_result = sf.query_all(cart_query) | 
					
						
						|  | cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])] | 
					
						
						|  |  | 
					
						
						|  | return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart}) | 
					
						
						|  |  | 
					
						
						|  | except ValueError as e: | 
					
						
						|  | return jsonify({"success": False, "error": f"Invalid data format: {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 |