Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update menu.py
Browse files
    	
        menu.py
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 | 
             
            from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
         | 
| 2 | 
            -
            from salesforce import get_salesforce_connection
         | 
| 3 | 
             
            import os
         | 
|  | |
| 4 |  | 
| 5 | 
             
            menu_blueprint = Blueprint('menu', __name__)
         | 
| 6 |  | 
| @@ -15,31 +15,23 @@ SECTION_ORDER = ["Best Sellers", "Starters", "Biryanis", "Curries", "Breads", "C | |
| 15 |  | 
| 16 | 
             
            # Create placeholder video at startup if it doesn't exist
         | 
| 17 | 
             
            if not os.path.exists(PLACEHOLDER_PATH):
         | 
| 18 | 
            -
                open(PLACEHOLDER_PATH, 'wb') | 
|  | |
| 19 | 
             
                print(f"Created placeholder video at {PLACEHOLDER_PATH}")
         | 
| 20 |  | 
| 21 | 
             
            def get_valid_video_path(item_name, video_url=None):
         | 
| 22 | 
            -
                """
         | 
| 23 | 
            -
                Get valid video path for item with placeholder fallback
         | 
| 24 | 
            -
                Priority: 1. Video1__c from Salesforce 2. placeholder.mp4
         | 
| 25 | 
            -
                """
         | 
| 26 | 
            -
                # First try: Video1__c from Salesforce if provided
         | 
| 27 | 
             
                if video_url:
         | 
| 28 | 
            -
                    # If it's a complete URL (http/https)
         | 
| 29 | 
             
                    if video_url.startswith(('http://', 'https://')):
         | 
| 30 | 
             
                        return video_url
         | 
| 31 | 
            -
                    # If it's a relative path (/videos/xxx.mp4)
         | 
| 32 | 
             
                    elif video_url.startswith('/'):
         | 
| 33 | 
             
                        return video_url
         | 
| 34 | 
            -
                    # If it's a Salesforce File ID (starts with '069')
         | 
| 35 | 
             
                    elif video_url.startswith('069'):
         | 
| 36 | 
            -
                        return f"https:// | 
| 37 | 
            -
                
         | 
| 38 | 
            -
                # Final fallback: placeholder.mp4
         | 
| 39 | 
             
                if not os.path.exists(PLACEHOLDER_PATH):
         | 
| 40 | 
            -
                    open(PLACEHOLDER_PATH, 'wb') | 
|  | |
| 41 | 
             
                    print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
         | 
| 42 | 
            -
                
         | 
| 43 | 
             
                return f"/static/{PLACEHOLDER_VIDEO}"
         | 
| 44 |  | 
| 45 | 
             
            @menu_blueprint.route("/menu", methods=["GET", "POST"])
         | 
| @@ -47,11 +39,11 @@ def menu(): | |
| 47 | 
             
                selected_category = request.args.get("category", "All")
         | 
| 48 | 
             
                user_email = session.get('user_email')
         | 
| 49 |  | 
|  | |
| 50 | 
             
                if not user_email:
         | 
| 51 | 
             
                    user_email = request.args.get("email")
         | 
| 52 | 
             
                    user_name = request.args.get("name")
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                    if user_email:
         | 
| 55 | 
             
                        session['user_email'] = user_email
         | 
| 56 | 
             
                        session['user_name'] = user_name
         | 
| 57 | 
             
                    else:
         | 
| @@ -61,122 +53,95 @@ def menu(): | |
| 61 |  | 
| 62 | 
             
                first_letter = user_name[0].upper() if user_name else "A"
         | 
| 63 |  | 
| 64 | 
            -
                 | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
                     | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
                     | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 110 | 
            -
                     | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
                     | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
                     | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
                         | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
             | 
| 145 | 
            -
                         | 
| 146 | 
            -
             | 
| 147 | 
            -
             | 
| 148 | 
            -
             | 
| 149 | 
            -
             | 
| 150 | 
            -
             | 
| 151 | 
            -
             | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
                    # Remove empty sections
         | 
| 155 | 
            -
                    ordered_menu = {section: items for section, items in ordered_menu.items() if items}
         | 
| 156 | 
            -
                    categories = ["All", "Veg", "Non veg"]
         | 
| 157 | 
            -
             | 
| 158 | 
            -
                except Exception as e:
         | 
| 159 | 
            -
                    print(f"Error fetching menu data: {str(e)}")
         | 
| 160 | 
            -
                    # Fallback data
         | 
| 161 | 
            -
                    ordered_menu = {section: [] for section in SECTION_ORDER}
         | 
| 162 | 
            -
                    best_sellers = ["Chicken Biryani", "Paneer Butter Masala", "Veg Manchurian", "Prawn Fry"]
         | 
| 163 | 
            -
                    ordered_menu["Best Sellers"] = [{
         | 
| 164 | 
            -
                        "Name": name,
         | 
| 165 | 
            -
                        "Price__c": "12.99",
         | 
| 166 | 
            -
                        "Description__c": f"Popular {name}",
         | 
| 167 | 
            -
                        "Image1__c": "/static/placeholder.jpg",
         | 
| 168 | 
            -
                        "Video1__c": get_valid_video_path(name),
         | 
| 169 | 
            -
                        "Total_Ordered__c": 100,
         | 
| 170 | 
            -
                        "Veg_NonVeg__c": "Veg" if "Paneer" in name or "Veg" in name else "Non veg",
         | 
| 171 | 
            -
                        "IngredientsInfo__c": "Ingredients information not available",
         | 
| 172 | 
            -
                        "Nutritional_Info__c": "Nutritional information not available",
         | 
| 173 | 
            -
                        "Allergens__c": "Allergen information not available"
         | 
| 174 | 
            -
                    } for name in best_sellers]
         | 
| 175 | 
            -
                    
         | 
| 176 | 
            -
                    categories = ["All", "Veg", "Non veg"]
         | 
| 177 | 
            -
                    referral_code = 'N/A'
         | 
| 178 | 
            -
                    reward_points = 0
         | 
| 179 | 
            -
                    cart_item_count = 0
         | 
| 180 |  | 
| 181 | 
             
                return render_template(
         | 
| 182 | 
             
                    "menu.html",
         | 
| @@ -192,8 +157,8 @@ def menu(): | |
| 192 |  | 
| 193 | 
             
            @menu_blueprint.route('/api/addons', methods=['GET'])
         | 
| 194 | 
             
            def get_addons():
         | 
| 195 | 
            -
                item_name = request.args.get('item_name') | 
| 196 | 
            -
                item_section = request.args.get('item_section') | 
| 197 |  | 
| 198 | 
             
                if not item_name or not item_section:
         | 
| 199 | 
             
                    return jsonify({"success": False, "error": "Item name and section are required."}), 400
         | 
| @@ -204,7 +169,7 @@ def get_addons(): | |
| 204 | 
             
                        FROM Customization_Options__c
         | 
| 205 | 
             
                        WHERE Section__c = '{item_section}'
         | 
| 206 | 
             
                    """
         | 
| 207 | 
            -
                    result = sf. | 
| 208 | 
             
                    addons = result.get('records', [])
         | 
| 209 |  | 
| 210 | 
             
                    if not addons:
         | 
| @@ -213,14 +178,10 @@ def get_addons(): | |
| 213 | 
             
                    formatted_addons = []
         | 
| 214 | 
             
                    for addon in addons:
         | 
| 215 | 
             
                        options = addon.get("Options__c", "")
         | 
| 216 | 
            -
                        if options | 
| 217 | 
            -
                            options = options.split(", ")
         | 
| 218 | 
            -
                        else:
         | 
| 219 | 
            -
                            options = []
         | 
| 220 | 
            -
             | 
| 221 | 
             
                        formatted_addons.append({
         | 
| 222 | 
            -
                            "name": addon | 
| 223 | 
            -
                            "type": addon | 
| 224 | 
             
                            "options": options,
         | 
| 225 | 
             
                            "max_selections": addon.get("Max_Selections__c", 1),
         | 
| 226 | 
             
                            "extra_charge": addon.get("Extra_Charge__c", False),
         | 
| @@ -238,41 +199,39 @@ def add_to_cart(): | |
| 238 | 
             
                try:
         | 
| 239 | 
             
                    data = request.json
         | 
| 240 | 
             
                    item_name = data.get('itemName', '').strip()
         | 
| 241 | 
            -
                    item_price = data.get('itemPrice')
         | 
| 242 | 
            -
                    item_image = data.get('itemImage')
         | 
| 243 | 
             
                    addons = data.get('addons', [])
         | 
| 244 | 
             
                    instructions = data.get('instructions', '')
         | 
| 245 | 
            -
                    category = data.get('category')
         | 
| 246 | 
            -
                    section = data.get('section')
         | 
| 247 | 
            -
                    quantity = data.get('quantity', 1)
         | 
| 248 | 
             
                    customer_email = session.get('user_email')
         | 
| 249 |  | 
| 250 | 
            -
                    if not item_name or not item_price:
         | 
| 251 | 
            -
                        return jsonify({"success": False, "error": "Item name and  | 
| 252 | 
            -
             | 
| 253 | 
            -
                    if not customer_email:
         | 
| 254 | 
            -
                        return jsonify({"success": False, "error": "User email is required."}), 400
         | 
| 255 |  | 
| 256 | 
             
                    query = f"""
         | 
| 257 | 
            -
                        SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c 
         | 
| 258 | 
             
                        FROM Cart_Item__c
         | 
| 259 | 
             
                        WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
         | 
| 260 | 
             
                    """
         | 
| 261 | 
             
                    result = sf.query(query)
         | 
| 262 | 
             
                    cart_items = result.get("records", [])
         | 
| 263 |  | 
| 264 | 
            -
                    addons_price = sum(addon | 
| 265 | 
            -
                    new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons])
         | 
| 266 |  | 
| 267 | 
             
                    if cart_items:
         | 
| 268 | 
            -
                         | 
| 269 | 
            -
                         | 
| 270 | 
            -
                         | 
| 271 | 
            -
                         | 
| 272 | 
            -
                         | 
|  | |
| 273 |  | 
| 274 | 
             
                        combined_addons = existing_addons if existing_addons != "None" else ""
         | 
| 275 | 
            -
                        if new_addons:
         | 
| 276 | 
             
                            combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
         | 
| 277 |  | 
| 278 | 
             
                        combined_instructions = existing_instructions
         | 
| @@ -284,29 +243,26 @@ def add_to_cart(): | |
| 284 | 
             
                            float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
         | 
| 285 | 
             
                        )
         | 
| 286 |  | 
|  | |
|  | |
| 287 | 
             
                        sf.Cart_Item__c.update(cart_item_id, {
         | 
| 288 | 
             
                            "Quantity__c": existing_quantity + quantity,
         | 
| 289 | 
             
                            "Add_Ons__c": combined_addons,
         | 
| 290 | 
             
                            "Add_Ons_Price__c": combined_addons_price,
         | 
| 291 | 
             
                            "Instructions__c": combined_instructions,
         | 
| 292 | 
            -
                            "Price__c":  | 
| 293 | 
             
                            "Category__c": category,
         | 
| 294 | 
             
                            "Section__c": section
         | 
| 295 | 
             
                        })
         | 
| 296 | 
             
                    else:
         | 
| 297 | 
            -
                        addons_string = "None"
         | 
| 298 | 
            -
                        if addons:
         | 
| 299 | 
            -
                            addons_string = new_addons
         | 
| 300 | 
            -
             | 
| 301 | 
             
                        total_price = item_price * quantity + addons_price
         | 
| 302 | 
            -
             | 
| 303 | 
             
                        sf.Cart_Item__c.create({
         | 
| 304 | 
             
                            "Name": item_name,
         | 
| 305 | 
             
                            "Price__c": total_price,
         | 
| 306 | 
             
                            "Base_Price__c": item_price,
         | 
| 307 | 
             
                            "Quantity__c": quantity,
         | 
| 308 | 
             
                            "Add_Ons_Price__c": addons_price,
         | 
| 309 | 
            -
                            "Add_Ons__c":  | 
| 310 | 
             
                            "Image1__c": item_image,
         | 
| 311 | 
             
                            "Customer_Email__c": customer_email,
         | 
| 312 | 
             
                            "Instructions__c": instructions,
         | 
| @@ -314,11 +270,15 @@ def add_to_cart(): | |
| 314 | 
             
                            "Section__c": section
         | 
| 315 | 
             
                        })
         | 
| 316 |  | 
| 317 | 
            -
                     | 
| 318 | 
            -
             | 
| 319 | 
            -
             | 
| 320 | 
            -
                     | 
|  | |
|  | |
| 321 |  | 
|  | |
|  | |
| 322 | 
             
                except Exception as e:
         | 
| 323 | 
             
                    print(f"Error adding item to cart: {str(e)}")
         | 
| 324 | 
             
                    return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
         | 
|  | |
| 1 | 
             
            from flask import Blueprint, render_template, request, session, jsonify, redirect, url_for
         | 
|  | |
| 2 | 
             
            import os
         | 
| 3 | 
            +
            from salesforce import get_salesforce_connection
         | 
| 4 |  | 
| 5 | 
             
            menu_blueprint = Blueprint('menu', __name__)
         | 
| 6 |  | 
|  | |
| 15 |  | 
| 16 | 
             
            # Create placeholder video at startup if it doesn't exist
         | 
| 17 | 
             
            if not os.path.exists(PLACEHOLDER_PATH):
         | 
| 18 | 
            +
                with open(PLACEHOLDER_PATH, 'wb') as f:
         | 
| 19 | 
            +
                    f.close()
         | 
| 20 | 
             
                print(f"Created placeholder video at {PLACEHOLDER_PATH}")
         | 
| 21 |  | 
| 22 | 
             
            def get_valid_video_path(item_name, video_url=None):
         | 
| 23 | 
            +
                """Get valid video path for item with placeholder fallback."""
         | 
|  | |
|  | |
|  | |
|  | |
| 24 | 
             
                if video_url:
         | 
|  | |
| 25 | 
             
                    if video_url.startswith(('http://', 'https://')):
         | 
| 26 | 
             
                        return video_url
         | 
|  | |
| 27 | 
             
                    elif video_url.startswith('/'):
         | 
| 28 | 
             
                        return video_url
         | 
|  | |
| 29 | 
             
                    elif video_url.startswith('069'):
         | 
| 30 | 
            +
                        return f"https://biryanihub-dev-ed.develop.my.salesforce.com/sfc/servlet.shepherd/version/download/{video_url}"
         | 
|  | |
|  | |
| 31 | 
             
                if not os.path.exists(PLACEHOLDER_PATH):
         | 
| 32 | 
            +
                    with open(PLACEHOLDER_PATH, 'wb') as f:
         | 
| 33 | 
            +
                        f.close()
         | 
| 34 | 
             
                    print(f"Created missing placeholder video at {PLACEHOLDER_PATH}")
         | 
|  | |
| 35 | 
             
                return f"/static/{PLACEHOLDER_VIDEO}"
         | 
| 36 |  | 
| 37 | 
             
            @menu_blueprint.route("/menu", methods=["GET", "POST"])
         | 
|  | |
| 39 | 
             
                selected_category = request.args.get("category", "All")
         | 
| 40 | 
             
                user_email = session.get('user_email')
         | 
| 41 |  | 
| 42 | 
            +
                # Handle user authentication
         | 
| 43 | 
             
                if not user_email:
         | 
| 44 | 
             
                    user_email = request.args.get("email")
         | 
| 45 | 
             
                    user_name = request.args.get("name")
         | 
| 46 | 
            +
                    if user_email and user_name:
         | 
|  | |
| 47 | 
             
                        session['user_email'] = user_email
         | 
| 48 | 
             
                        session['user_name'] = user_name
         | 
| 49 | 
             
                    else:
         | 
|  | |
| 53 |  | 
| 54 | 
             
                first_letter = user_name[0].upper() if user_name else "A"
         | 
| 55 |  | 
| 56 | 
            +
                # Fetch user referral and reward points
         | 
| 57 | 
            +
                user_query = f"SELECT Referral__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{user_email}'"
         | 
| 58 | 
            +
                user_result = sf.query(user_query)
         | 
| 59 | 
            +
                if not user_result.get('records'):
         | 
| 60 | 
            +
                    return redirect(url_for('login'))
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                referral_code = user_result['records'][0].get('Referral__c', 'N/A')
         | 
| 63 | 
            +
                reward_points = user_result['records'][0].get('Reward_Points__c', 0)
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                # Get cart item count
         | 
| 66 | 
            +
                cart_query = f"SELECT COUNT() FROM Cart_Item__c WHERE Customer_Email__c = '{user_email}'"
         | 
| 67 | 
            +
                cart_count_result = sf.query(cart_query)
         | 
| 68 | 
            +
                cart_item_count = cart_count_result.get('totalSize', 0)
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                # Fetch all Menu_Item__c records with only existing fields
         | 
| 71 | 
            +
                menu_query = """
         | 
| 72 | 
            +
                    SELECT Name, Price__c, Description__c, Image1__c, Image2__c, 
         | 
| 73 | 
            +
                           Veg_NonVeg__c, Section__c, Total_Ordered__c, Video1__c
         | 
| 74 | 
            +
                    FROM Menu_Item__c
         | 
| 75 | 
            +
                """
         | 
| 76 | 
            +
                menu_result = sf.query_all(menu_query)
         | 
| 77 | 
            +
                food_items = menu_result.get('records', [])
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                # Process menu items
         | 
| 80 | 
            +
                for item in food_items:
         | 
| 81 | 
            +
                    item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
         | 
| 82 | 
            +
                    item['Video1__c'] = get_valid_video_path(item['Name'], item.get('Video1__c'))
         | 
| 83 | 
            +
                    item['Section__c'] = item.get('Section__c', "Others")
         | 
| 84 | 
            +
                    item['Description__c'] = item.get('Description__c', "No description available")
         | 
| 85 | 
            +
                    # Add default values for missing fields
         | 
| 86 | 
            +
                    item['IngredientsInfo__c'] = "Not specified"
         | 
| 87 | 
            +
                    item['NutritionalInfo__c'] = "Not available"
         | 
| 88 | 
            +
                    item['Allergens__c'] = "None listed"
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                # Fetch all Custom_Dish__c records with only existing fields
         | 
| 91 | 
            +
                custom_dish_query = """
         | 
| 92 | 
            +
                    SELECT Name, Price__c, Description__c, Image1__c, Image2__c, 
         | 
| 93 | 
            +
                           Veg_NonVeg__c, Section__c, Total_Ordered__c
         | 
| 94 | 
            +
                    FROM Custom_Dish__c
         | 
| 95 | 
            +
                    WHERE CreatedDate >= LAST_N_DAYS:7
         | 
| 96 | 
            +
                """
         | 
| 97 | 
            +
                custom_dish_result = sf.query_all(custom_dish_query)
         | 
| 98 | 
            +
                custom_dishes = custom_dish_result.get('records', [])
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                # Process custom dishes
         | 
| 101 | 
            +
                for item in custom_dishes:
         | 
| 102 | 
            +
                    item['Total_Ordered__c'] = item.get('Total_Ordered__c', 0) or 0
         | 
| 103 | 
            +
                    item['Video1__c'] = get_valid_video_path(item['Name'])
         | 
| 104 | 
            +
                    item['Section__c'] = item.get('Section__c', "Customized dish")
         | 
| 105 | 
            +
                    item['Description__c'] = item.get('Description__c', "No description available")
         | 
| 106 | 
            +
                    # Add default values for missing fields
         | 
| 107 | 
            +
                    item['IngredientsInfo__c'] = "Not specified"
         | 
| 108 | 
            +
                    item['NutritionalInfo__c'] = "Not available"
         | 
| 109 | 
            +
                    item['Allergens__c'] = "None listed"
         | 
| 110 | 
            +
             | 
| 111 | 
            +
                # Merge all items
         | 
| 112 | 
            +
                all_items = food_items + custom_dishes
         | 
| 113 | 
            +
                ordered_menu = {section: [] for section in SECTION_ORDER}
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                # Process best sellers
         | 
| 116 | 
            +
                best_sellers = sorted(all_items, key=lambda x: x['Total_Ordered__c'], reverse=True)
         | 
| 117 | 
            +
                if selected_category == "Veg":
         | 
| 118 | 
            +
                    best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Veg", "both"]]
         | 
| 119 | 
            +
                elif selected_category == "Non veg":
         | 
| 120 | 
            +
                    best_sellers = [item for item in best_sellers if item.get("Veg_NonVeg__c") in ["Non veg", "both"]]
         | 
| 121 | 
            +
                ordered_menu["Best Sellers"] = best_sellers[:4]
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                # Organize items into sections
         | 
| 124 | 
            +
                added_item_names = set()
         | 
| 125 | 
            +
                for item in all_items:
         | 
| 126 | 
            +
                    section = item['Section__c']
         | 
| 127 | 
            +
                    if section not in ordered_menu:
         | 
| 128 | 
            +
                        ordered_menu[section] = []
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                    if item['Name'] in added_item_names:
         | 
| 131 | 
            +
                        continue
         | 
| 132 | 
            +
             | 
| 133 | 
            +
                    veg_nonveg = item.get("Veg_NonVeg__c", "both")
         | 
| 134 | 
            +
                    if selected_category == "Veg" and veg_nonveg not in ["Veg", "both"]:
         | 
| 135 | 
            +
                        continue
         | 
| 136 | 
            +
                    if selected_category == "Non veg" and veg_nonveg not in ["Non veg", "both"]:
         | 
| 137 | 
            +
                        continue
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                    ordered_menu[section].append(item)
         | 
| 140 | 
            +
                    added_item_names.add(item['Name'])
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                # Remove empty sections
         | 
| 143 | 
            +
                ordered_menu = {section: items for section, items in ordered_menu.items() if items}
         | 
| 144 | 
            +
                categories = ["All", "Veg", "Non veg"]
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 145 |  | 
| 146 | 
             
                return render_template(
         | 
| 147 | 
             
                    "menu.html",
         | 
|  | |
| 157 |  | 
| 158 | 
             
            @menu_blueprint.route('/api/addons', methods=['GET'])
         | 
| 159 | 
             
            def get_addons():
         | 
| 160 | 
            +
                item_name = request.args.get('item_name')
         | 
| 161 | 
            +
                item_section = request.args.get('item_section')
         | 
| 162 |  | 
| 163 | 
             
                if not item_name or not item_section:
         | 
| 164 | 
             
                    return jsonify({"success": False, "error": "Item name and section are required."}), 400
         | 
|  | |
| 169 | 
             
                        FROM Customization_Options__c
         | 
| 170 | 
             
                        WHERE Section__c = '{item_section}'
         | 
| 171 | 
             
                    """
         | 
| 172 | 
            +
                    result = sf.query_all(query)
         | 
| 173 | 
             
                    addons = result.get('records', [])
         | 
| 174 |  | 
| 175 | 
             
                    if not addons:
         | 
|  | |
| 178 | 
             
                    formatted_addons = []
         | 
| 179 | 
             
                    for addon in addons:
         | 
| 180 | 
             
                        options = addon.get("Options__c", "")
         | 
| 181 | 
            +
                        options = options.split(", ") if options else []
         | 
|  | |
|  | |
|  | |
|  | |
| 182 | 
             
                        formatted_addons.append({
         | 
| 183 | 
            +
                            "name": addon.get("Name", ""),
         | 
| 184 | 
            +
                            "type": addon.get("Customization_Type__c", ""),
         | 
| 185 | 
             
                            "options": options,
         | 
| 186 | 
             
                            "max_selections": addon.get("Max_Selections__c", 1),
         | 
| 187 | 
             
                            "extra_charge": addon.get("Extra_Charge__c", False),
         | 
|  | |
| 199 | 
             
                try:
         | 
| 200 | 
             
                    data = request.json
         | 
| 201 | 
             
                    item_name = data.get('itemName', '').strip()
         | 
| 202 | 
            +
                    item_price = float(data.get('itemPrice', 0))
         | 
| 203 | 
            +
                    item_image = data.get('itemImage', '')
         | 
| 204 | 
             
                    addons = data.get('addons', [])
         | 
| 205 | 
             
                    instructions = data.get('instructions', '')
         | 
| 206 | 
            +
                    category = data.get('category', '')
         | 
| 207 | 
            +
                    section = data.get('section', '')
         | 
| 208 | 
            +
                    quantity = int(data.get('quantity', 1))
         | 
| 209 | 
             
                    customer_email = session.get('user_email')
         | 
| 210 |  | 
| 211 | 
            +
                    if not item_name or not item_price or not customer_email:
         | 
| 212 | 
            +
                        return jsonify({"success": False, "error": "Item name, price, and user email are required."}), 400
         | 
|  | |
|  | |
|  | |
| 213 |  | 
| 214 | 
             
                    query = f"""
         | 
| 215 | 
            +
                        SELECT Id, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Instructions__c, Price__c
         | 
| 216 | 
             
                        FROM Cart_Item__c
         | 
| 217 | 
             
                        WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}'
         | 
| 218 | 
             
                    """
         | 
| 219 | 
             
                    result = sf.query(query)
         | 
| 220 | 
             
                    cart_items = result.get("records", [])
         | 
| 221 |  | 
| 222 | 
            +
                    addons_price = sum(float(addon.get('price', 0)) for addon in addons)
         | 
| 223 | 
            +
                    new_addons = "; ".join([f"{addon['name']} (${addon['price']})" for addon in addons]) if addons else "None"
         | 
| 224 |  | 
| 225 | 
             
                    if cart_items:
         | 
| 226 | 
            +
                        cart_item = cart_items[0]
         | 
| 227 | 
            +
                        cart_item_id = cart_item['Id']
         | 
| 228 | 
            +
                        existing_quantity = int(cart_item.get('Quantity__c', 0))
         | 
| 229 | 
            +
                        existing_addons = cart_item.get('Add_Ons__c', "None")
         | 
| 230 | 
            +
                        existing_addons_price = float(cart_item.get('Add_Ons_Price__c', 0))
         | 
| 231 | 
            +
                        existing_instructions = cart_item.get('Instructions__c', "")
         | 
| 232 |  | 
| 233 | 
             
                        combined_addons = existing_addons if existing_addons != "None" else ""
         | 
| 234 | 
            +
                        if new_addons != "None":
         | 
| 235 | 
             
                            combined_addons = f"{combined_addons}; {new_addons}".strip("; ")
         | 
| 236 |  | 
| 237 | 
             
                        combined_instructions = existing_instructions
         | 
|  | |
| 243 | 
             
                            float(addon.split("($")[1][:-1]) for addon in combined_addons_list if "($" in addon
         | 
| 244 | 
             
                        )
         | 
| 245 |  | 
| 246 | 
            +
                        total_price = (existing_quantity + quantity) * item_price + combined_addons_price
         | 
| 247 | 
            +
             | 
| 248 | 
             
                        sf.Cart_Item__c.update(cart_item_id, {
         | 
| 249 | 
             
                            "Quantity__c": existing_quantity + quantity,
         | 
| 250 | 
             
                            "Add_Ons__c": combined_addons,
         | 
| 251 | 
             
                            "Add_Ons_Price__c": combined_addons_price,
         | 
| 252 | 
             
                            "Instructions__c": combined_instructions,
         | 
| 253 | 
            +
                            "Price__c": total_price,
         | 
| 254 | 
             
                            "Category__c": category,
         | 
| 255 | 
             
                            "Section__c": section
         | 
| 256 | 
             
                        })
         | 
| 257 | 
             
                    else:
         | 
|  | |
|  | |
|  | |
|  | |
| 258 | 
             
                        total_price = item_price * quantity + addons_price
         | 
|  | |
| 259 | 
             
                        sf.Cart_Item__c.create({
         | 
| 260 | 
             
                            "Name": item_name,
         | 
| 261 | 
             
                            "Price__c": total_price,
         | 
| 262 | 
             
                            "Base_Price__c": item_price,
         | 
| 263 | 
             
                            "Quantity__c": quantity,
         | 
| 264 | 
             
                            "Add_Ons_Price__c": addons_price,
         | 
| 265 | 
            +
                            "Add_Ons__c": new_addons,
         | 
| 266 | 
             
                            "Image1__c": item_image,
         | 
| 267 | 
             
                            "Customer_Email__c": customer_email,
         | 
| 268 | 
             
                            "Instructions__c": instructions,
         | 
|  | |
| 270 | 
             
                            "Section__c": section
         | 
| 271 | 
             
                        })
         | 
| 272 |  | 
| 273 | 
            +
                    # Fetch updated cart for UI update
         | 
| 274 | 
            +
                    cart_query = f"SELECT Name, Quantity__c FROM Cart_Item__c WHERE Customer_Email__c = '{customer_email}'"
         | 
| 275 | 
            +
                    cart_result = sf.query_all(cart_query)
         | 
| 276 | 
            +
                    cart = [{"itemName": item["Name"], "quantity": item["Quantity__c"]} for item in cart_result.get("records", [])]
         | 
| 277 | 
            +
                    
         | 
| 278 | 
            +
                    return jsonify({"success": True, "message": "Item added to cart successfully.", "cart": cart})
         | 
| 279 |  | 
| 280 | 
            +
                except ValueError as e:
         | 
| 281 | 
            +
                    return jsonify({"success": False, "error": f"Invalid data format: {str(e)}"}), 400
         | 
| 282 | 
             
                except Exception as e:
         | 
| 283 | 
             
                    print(f"Error adding item to cart: {str(e)}")
         | 
| 284 | 
             
                    return jsonify({"success": False, "error": "An error occurred while adding the item to the cart."}), 500
         | 
