| 
							 | 
						from flask import Flask, render_template, request, jsonify, redirect, url_for, session | 
					
					
						
						| 
							 | 
						from flask_session import Session   | 
					
					
						
						| 
							 | 
						from flask.sessions import SecureCookieSessionInterface   | 
					
					
						
						| 
							 | 
						from salesforce import get_salesforce_connection | 
					
					
						
						| 
							 | 
						import os | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						print("Starting app...") | 
					
					
						
						| 
							 | 
						app = Flask(__name__) | 
					
					
						
						| 
							 | 
						print("Flask app initialized.") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						sf = get_salesforce_connection() | 
					
					
						
						| 
							 | 
						print("Salesforce connection established.") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q")   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						app.config["SESSION_TYPE"] = "filesystem"   | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						app.config["SESSION_COOKIE_SECURE"] = True   | 
					
					
						
						| 
							 | 
						app.config["SESSION_COOKIE_SAMESITE"] = "None"   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						Session(app)   | 
					
					
						
						| 
							 | 
						print("Session interface configured.") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						app.session_interface = SecureCookieSessionInterface() | 
					
					
						
						| 
							 | 
						print("Session interface configured.") | 
					
					
						
						| 
							 | 
						import random | 
					
					
						
						| 
							 | 
						import string | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						def generate_referral_code(length=8): | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    characters = string.ascii_letters + string.digits   | 
					
					
						
						| 
							 | 
						    referral_code = ''.join(random.choice(characters) for _ in range(length)) | 
					
					
						
						| 
							 | 
						    return referral_code | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/") | 
					
					
						
						| 
							 | 
						def home(): | 
					
					
						
						| 
							 | 
						    return render_template("index.html") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						from datetime import datetime | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						def generate_coupon_code(length=10): | 
					
					
						
						| 
							 | 
						    """Generates a random alphanumeric coupon code""" | 
					
					
						
						| 
							 | 
						    characters = string.ascii_uppercase + string.digits   | 
					
					
						
						| 
							 | 
						    return ''.join(random.choice(characters) for _ in range(length)) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/order-history", methods=["GET"]) | 
					
					
						
						| 
							 | 
						def order_history(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    if not email: | 
					
					
						
						| 
							 | 
						        return redirect(url_for("login")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c,  | 
					
					
						
						| 
							 | 
						                   Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, CreatedDate | 
					
					
						
						| 
							 | 
						            FROM Order__c | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						            ORDER BY CreatedDate DESC | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        orders = result.get("records", [])   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return render_template("order_history.html", orders=orders) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error fetching order history: {str(e)}") | 
					
					
						
						| 
							 | 
						        return render_template("order_history.html", orders=[], error=str(e)) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/signup", methods=["GET", "POST"]) | 
					
					
						
						| 
							 | 
						def signup(): | 
					
					
						
						| 
							 | 
						    if request.method == "POST": | 
					
					
						
						| 
							 | 
						        name = request.form.get("name") | 
					
					
						
						| 
							 | 
						        phone = request.form.get("phone") | 
					
					
						
						| 
							 | 
						        email = request.form.get("email").strip()   | 
					
					
						
						| 
							 | 
						        password = request.form.get("password") | 
					
					
						
						| 
							 | 
						        referral_code = request.form.get("referral")   | 
					
					
						
						| 
							 | 
						        generated_referral_code = generate_referral_code() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        try: | 
					
					
						
						| 
							 | 
						            ref = 0   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            email_query = "SELECT Id, Email__c FROM Customer_Login__c" | 
					
					
						
						| 
							 | 
						            email_result = sf.query(email_query) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            existing_emails = {record["Email__c"].lower() for record in email_result["records"]} | 
					
					
						
						| 
							 | 
						            if email.lower() in existing_emails: | 
					
					
						
						| 
							 | 
						                return render_template("signup.html", error="Email already in use! Please use a different email.") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            if referral_code: | 
					
					
						
						| 
							 | 
						                referral_query = f"SELECT Id, Email__c, Name FROM Customer_Login__c WHERE Referral__c = '{referral_code}'" | 
					
					
						
						| 
							 | 
						                referral_result = sf.query(referral_query) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                if not referral_result['records']: | 
					
					
						
						| 
							 | 
						                    return render_template("signup.html", error="Invalid referral code!") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                 | 
					
					
						
						| 
							 | 
						                referrer = referral_result['records'][0] | 
					
					
						
						| 
							 | 
						                referrer_email = referrer.get('Email__c') | 
					
					
						
						| 
							 | 
						                referrer_name = referrer.get('Name') | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                 | 
					
					
						
						| 
							 | 
						                new_coupon_code = generate_coupon_code() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                 | 
					
					
						
						| 
							 | 
						                existing_coupon_query = f"SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{referrer_email}'" | 
					
					
						
						| 
							 | 
						                existing_coupon_result = sf.query(existing_coupon_query) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                if existing_coupon_result['records']: | 
					
					
						
						| 
							 | 
						                    referral_record = existing_coupon_result['records'][0] | 
					
					
						
						| 
							 | 
						                    referral_id = referral_record['Id'] | 
					
					
						
						| 
							 | 
						                    existing_coupons = referral_record.get('Coupon_Code__c', '') | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                    updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                     | 
					
					
						
						| 
							 | 
						                    sf.Referral_Coupon__c.update(referral_id, { | 
					
					
						
						| 
							 | 
						                        "Coupon_Code__c": updated_coupons | 
					
					
						
						| 
							 | 
						                    }) | 
					
					
						
						| 
							 | 
						                else: | 
					
					
						
						| 
							 | 
						                     | 
					
					
						
						| 
							 | 
						                    sf.Referral_Coupon__c.create({ | 
					
					
						
						| 
							 | 
						                        "Name": referrer_name, | 
					
					
						
						| 
							 | 
						                        "Referral_Email__c": referrer_email, | 
					
					
						
						| 
							 | 
						                        "Coupon_Code__c": new_coupon_code | 
					
					
						
						| 
							 | 
						                    }) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            sf.Customer_Login__c.create({ | 
					
					
						
						| 
							 | 
						                "Name": name, | 
					
					
						
						| 
							 | 
						                "Phone_Number__c": phone, | 
					
					
						
						| 
							 | 
						                "Email__c": email, | 
					
					
						
						| 
							 | 
						                "Password__c": password, | 
					
					
						
						| 
							 | 
						                "Reward_Points__c": ref,   | 
					
					
						
						| 
							 | 
						                "Referral__c": generated_referral_code | 
					
					
						
						| 
							 | 
						            }) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						            return redirect(url_for("login")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        except Exception as e: | 
					
					
						
						| 
							 | 
						            return render_template("signup.html", error=f"Error: {str(e)}") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    return render_template("signup.html") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/login", methods=["GET", "POST"]) | 
					
					
						
						| 
							 | 
						def login(): | 
					
					
						
						| 
							 | 
						    if request.method == "POST": | 
					
					
						
						| 
							 | 
						        email = request.form.get("email") | 
					
					
						
						| 
							 | 
						        password = request.form.get("password") | 
					
					
						
						| 
							 | 
						        print(f"Login attempt with email: {email}")   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        try: | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            query = f"SELECT Id, Name, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c='{email}' AND Password__c='{password}'" | 
					
					
						
						| 
							 | 
						            result = sf.query(query) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						            if result["records"]: | 
					
					
						
						| 
							 | 
						                user = result["records"][0] | 
					
					
						
						| 
							 | 
						                session['user_id'] = user['Id'] | 
					
					
						
						| 
							 | 
						                session['user_email'] = email | 
					
					
						
						| 
							 | 
						                print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                user_name = user.get("Name", "")   | 
					
					
						
						| 
							 | 
						                reward_points = user.get("Reward_Points__c") or 0   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                if reward_points >= 500: | 
					
					
						
						| 
							 | 
						                    print(f"User {email} has {reward_points} reward points. Generating coupon...") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                     | 
					
					
						
						| 
							 | 
						                    new_coupon_code = generate_coupon_code() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                     | 
					
					
						
						| 
							 | 
						                    coupon_query = sf.query(f""" | 
					
					
						
						| 
							 | 
						                        SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						                    """) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                    if coupon_query["records"]: | 
					
					
						
						| 
							 | 
						                         | 
					
					
						
						| 
							 | 
						                        coupon_record = coupon_query["records"][0] | 
					
					
						
						| 
							 | 
						                        referral_coupon_id = coupon_record["Id"] | 
					
					
						
						| 
							 | 
						                        existing_coupons = coupon_record.get("Coupon_Code__c", "") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                         | 
					
					
						
						| 
							 | 
						                        updated_coupons = f"{existing_coupons}\n{new_coupon_code}".strip() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                         | 
					
					
						
						| 
							 | 
						                        sf.Referral_Coupon__c.update(referral_coupon_id, { | 
					
					
						
						| 
							 | 
						                            "Coupon_Code__c": updated_coupons | 
					
					
						
						| 
							 | 
						                        }) | 
					
					
						
						| 
							 | 
						                        print(f"Updated existing coupon record for {email}. New Coupon: {new_coupon_code}") | 
					
					
						
						| 
							 | 
						                    else: | 
					
					
						
						| 
							 | 
						                         | 
					
					
						
						| 
							 | 
						                        sf.Referral_Coupon__c.create({ | 
					
					
						
						| 
							 | 
						                            "Referral_Email__c": email, | 
					
					
						
						| 
							 | 
						                            "Name": user_name,   | 
					
					
						
						| 
							 | 
						                            "Coupon_Code__c": new_coupon_code | 
					
					
						
						| 
							 | 
						                        }) | 
					
					
						
						| 
							 | 
						                        print(f"Created new coupon record for {email} with name {user_name}. Coupon: {new_coupon_code}") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                     | 
					
					
						
						| 
							 | 
						                    new_reward_points = reward_points - 500 | 
					
					
						
						| 
							 | 
						                    sf.Customer_Login__c.update(user['Id'], { | 
					
					
						
						| 
							 | 
						                        "Reward_Points__c": new_reward_points | 
					
					
						
						| 
							 | 
						                    }) | 
					
					
						
						| 
							 | 
						                    print(f"Coupon {new_coupon_code} generated and 500 points deducted. New balance: {new_reward_points}") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						                return redirect(url_for("menu")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						            else: | 
					
					
						
						| 
							 | 
						                print("Invalid credentials!") | 
					
					
						
						| 
							 | 
						                return render_template("login.html", error="Invalid credentials!") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        except Exception as e: | 
					
					
						
						| 
							 | 
						            print(f"Error during login: {str(e)}") | 
					
					
						
						| 
							 | 
						            return render_template("login.html", error=f"Error: {str(e)}") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    return render_template("login.html") | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/menu", methods=["GET", "POST"]) | 
					
					
						
						| 
							 | 
						def menu(): | 
					
					
						
						| 
							 | 
						    selected_category = request.args.get("category", "All") | 
					
					
						
						| 
							 | 
						    user_id = session.get('user_id') | 
					
					
						
						| 
							 | 
						    user_email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    print(f"Session check in /menu: user_id={user_id}, user_email={user_email}") | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    if not user_id: | 
					
					
						
						| 
							 | 
						        print("Session missing, redirecting to login.") | 
					
					
						
						| 
							 | 
						        return redirect(url_for('login')) | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    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']: | 
					
					
						
						| 
							 | 
						            print("User not found!") | 
					
					
						
						| 
							 | 
						            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  | 
					
					
						
						| 
							 | 
						            FROM Menu_Item__c | 
					
					
						
						| 
							 | 
						        """ | 
					
					
						
						| 
							 | 
						        result = sf.query(menu_query) | 
					
					
						
						| 
							 | 
						        food_items = result['records'] if 'records' in result else [] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        categories = {item.get("Veg_NonVeg__c").capitalize() for item in food_items if item.get("Veg_NonVeg__c")} | 
					
					
						
						| 
							 | 
						        categories = {"Veg", "Non-Veg"}   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        if selected_category == "Veg": | 
					
					
						
						| 
							 | 
						            food_items = [item for item in food_items if item.get("Veg_NonVeg__c") in ["Veg", "both"]] | 
					
					
						
						| 
							 | 
						        elif selected_category == "Non-Veg": | 
					
					
						
						| 
							 | 
						            food_items = [item for item in food_items if item.get("Veg_NonVeg__c") in ["Non veg", "both"]] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error fetching menu data: {str(e)}") | 
					
					
						
						| 
							 | 
						        food_items = [] | 
					
					
						
						| 
							 | 
						        categories = {"All", "Veg", "Non-Veg"}   | 
					
					
						
						| 
							 | 
						        referral_code = 'N/A' | 
					
					
						
						| 
							 | 
						        reward_points = 0 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    return render_template( | 
					
					
						
						| 
							 | 
						        "menu.html", | 
					
					
						
						| 
							 | 
						        food_items=food_items, | 
					
					
						
						| 
							 | 
						        categories=sorted(categories),   | 
					
					
						
						| 
							 | 
						        selected_category=selected_category, | 
					
					
						
						| 
							 | 
						        referral_code=referral_code, | 
					
					
						
						| 
							 | 
						        reward_points=reward_points | 
					
					
						
						| 
							 | 
						    ) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/cart", methods=["GET"]) | 
					
					
						
						| 
							 | 
						def cart(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    if not email: | 
					
					
						
						| 
							 | 
						        return redirect(url_for("login")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Name, Price__c, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Image1__c, Instructions__c | 
					
					
						
						| 
							 | 
						            FROM Cart_Item__c | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						        cart_items = result.get("records", []) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        subtotal = sum(item['Price__c'] for item in cart_items) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        customer_result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Reward_Points__c  | 
					
					
						
						| 
							 | 
						            FROM Customer_Login__c | 
					
					
						
						| 
							 | 
						            WHERE Email__c = '{email}' | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						        reward_points = customer_result['records'][0].get('Reward_Points__c', 0) if customer_result['records'] else 0 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        coupon_result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        if coupon_result["records"]: | 
					
					
						
						| 
							 | 
						            raw_coupons = coupon_result["records"][0].get("Coupon_Code__c", "") | 
					
					
						
						| 
							 | 
						            coupons = raw_coupons.split("\n") if raw_coupons else [] | 
					
					
						
						| 
							 | 
						        else: | 
					
					
						
						| 
							 | 
						            coupons = [] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return render_template( | 
					
					
						
						| 
							 | 
						            "cart.html", | 
					
					
						
						| 
							 | 
						            cart_items=cart_items, | 
					
					
						
						| 
							 | 
						            subtotal=subtotal, | 
					
					
						
						| 
							 | 
						            reward_points=reward_points, | 
					
					
						
						| 
							 | 
						            customer_email=email, | 
					
					
						
						| 
							 | 
						            coupons=coupons   | 
					
					
						
						| 
							 | 
						        ) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error fetching cart items: {e}") | 
					
					
						
						| 
							 | 
						        return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[]) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route('/cart/add', methods=['POST']) | 
					
					
						
						| 
							 | 
						def add_to_cart(): | 
					
					
						
						| 
							 | 
						    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', '')   | 
					
					
						
						| 
							 | 
						    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."}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        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 + 1,   | 
					
					
						
						| 
							 | 
						                "Add_Ons__c": combined_addons,   | 
					
					
						
						| 
							 | 
						                "Add_Ons_Price__c": combined_addons_price,   | 
					
					
						
						| 
							 | 
						                "Instructions__c": combined_instructions,   | 
					
					
						
						| 
							 | 
						                "Price__c": (existing_quantity + 1) * item_price + combined_addons_price,   | 
					
					
						
						| 
							 | 
						            }) | 
					
					
						
						| 
							 | 
						        else: | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            addons_string = "None" | 
					
					
						
						| 
							 | 
						            if addons: | 
					
					
						
						| 
							 | 
						                addons_string = new_addons   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						            total_price = item_price + addons_price   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						             | 
					
					
						
						| 
							 | 
						            sf.Cart_Item__c.create({ | 
					
					
						
						| 
							 | 
						                "Name": item_name,   | 
					
					
						
						| 
							 | 
						                "Price__c": total_price,   | 
					
					
						
						| 
							 | 
						                "Base_Price__c": item_price,   | 
					
					
						
						| 
							 | 
						                "Quantity__c": 1,   | 
					
					
						
						| 
							 | 
						                "Add_Ons_Price__c": addons_price,   | 
					
					
						
						| 
							 | 
						                "Add_Ons__c": addons_string,   | 
					
					
						
						| 
							 | 
						                "Image1__c": item_image,   | 
					
					
						
						| 
							 | 
						                "Customer_Email__c": customer_email,   | 
					
					
						
						| 
							 | 
						                "Instructions__c": instructions   | 
					
					
						
						| 
							 | 
						            }) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "message": "Item added to cart successfully."}) | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error adding item to cart: {str(e)}") | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": str(e)}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/cart/add_item", methods=["POST"]) | 
					
					
						
						| 
							 | 
						def add_item_to_cart(): | 
					
					
						
						| 
							 | 
						    data = request.json   | 
					
					
						
						| 
							 | 
						    email = data.get('email')   | 
					
					
						
						| 
							 | 
						    item_name = data.get('item_name')   | 
					
					
						
						| 
							 | 
						    quantity = data.get('quantity', 1)   | 
					
					
						
						| 
							 | 
						    addons = data.get('addons', [])   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    if not email or not item_name: | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": "Email and item name are required."}), 400 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        sf.Cart_Item__c.create({ | 
					
					
						
						| 
							 | 
						            "Customer_Email__c": email,   | 
					
					
						
						| 
							 | 
						            "Item_Name__c": item_name,   | 
					
					
						
						| 
							 | 
						            "Quantity__c": quantity,   | 
					
					
						
						| 
							 | 
						            "Add_Ons__c": addons_string | 
					
					
						
						| 
							 | 
						        }) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "message": "Item added to cart successfully."}) | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error adding item to cart: {str(e)}")   | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": str(e)}), 500 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route('/cart/remove/<item_name>', methods=['POST']) | 
					
					
						
						| 
							 | 
						def remove_cart_item(item_name): | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						        customer_email = session.get('user_email') | 
					
					
						
						| 
							 | 
						        if not customer_email: | 
					
					
						
						| 
							 | 
						            return jsonify({'success': False, 'message': 'User email not found. Please log in again.'}), 400 | 
					
					
						
						| 
							 | 
						        query = f""" | 
					
					
						
						| 
							 | 
						            SELECT Id FROM Cart_Item__c  | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{customer_email}' AND Name = '{item_name}' | 
					
					
						
						| 
							 | 
						        """ | 
					
					
						
						| 
							 | 
						        result = sf.query(query) | 
					
					
						
						| 
							 | 
						        if result['totalSize'] == 0: | 
					
					
						
						| 
							 | 
						            return jsonify({'success': False, 'message': 'Item not found in cart.'}), 400 | 
					
					
						
						| 
							 | 
						        cart_item_id = result['records'][0]['Id'] | 
					
					
						
						| 
							 | 
						        sf.Cart_Item__c.delete(cart_item_id) | 
					
					
						
						| 
							 | 
						        return jsonify({'success': True, 'message': f"'{item_name}' removed successfully!"}), 200 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error: {str(e)}") | 
					
					
						
						| 
							 | 
						        return jsonify({'success': False, 'message': f"An error occurred: {str(e)}"}), 500 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route('/api/addons', methods=['GET']) | 
					
					
						
						| 
							 | 
						def get_addons(): | 
					
					
						
						| 
							 | 
						    item_name = request.args.get('item_name')   | 
					
					
						
						| 
							 | 
						    if not item_name: | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": "Item name is required."}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        query = f""" | 
					
					
						
						| 
							 | 
						            SELECT Name, Price__c  | 
					
					
						
						| 
							 | 
						            FROM Add_Ons__c  | 
					
					
						
						| 
							 | 
						        """ | 
					
					
						
						| 
							 | 
						        addons = sf.query(query)['records'] | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "addons": addons}) | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error fetching add-ons: {e}") | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": "Unable to fetch add-ons. Please try again later."}) | 
					
					
						
						| 
							 | 
						@app.route("/cart/update_quantity", methods=["POST"]) | 
					
					
						
						| 
							 | 
						def update_quantity(): | 
					
					
						
						| 
							 | 
						    data = request.json   | 
					
					
						
						| 
							 | 
						    email = data.get('email') | 
					
					
						
						| 
							 | 
						    item_name = data.get('item_name') | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        quantity = int(data.get('quantity')) | 
					
					
						
						| 
							 | 
						    except (ValueError, TypeError): | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": "Invalid quantity provided."}), 400 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						     | 
					
					
						
						| 
							 | 
						    if not email or not item_name or quantity is None: | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": "Email, item name, and quantity are required."}), 400 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        cart_items = sf.query( | 
					
					
						
						| 
							 | 
						            f"SELECT Id, Quantity__c, Price__c, Base_Price__c, Add_Ons_Price__c FROM Cart_Item__c " | 
					
					
						
						| 
							 | 
						            f"WHERE Customer_Email__c = '{email}' AND Name = '{item_name}'" | 
					
					
						
						| 
							 | 
						        )['records'] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        if not cart_items: | 
					
					
						
						| 
							 | 
						            return jsonify({"success": False, "error": "Cart item not found."}), 404 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        cart_item_id = cart_items[0]['Id'] | 
					
					
						
						| 
							 | 
						        base_price = cart_items[0]['Base_Price__c'] | 
					
					
						
						| 
							 | 
						        addons_price = cart_items[0].get('Add_Ons_Price__c', 0) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        new_item_price = (base_price * quantity) + addons_price | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        sf.Cart_Item__c.update(cart_item_id, { | 
					
					
						
						| 
							 | 
						            "Quantity__c": quantity, | 
					
					
						
						| 
							 | 
						            "Price__c": new_item_price,   | 
					
					
						
						| 
							 | 
						        }) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        cart_items = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Price__c, Add_Ons_Price__c  | 
					
					
						
						| 
							 | 
						            FROM Cart_Item__c  | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						        """)['records'] | 
					
					
						
						| 
							 | 
						        new_subtotal = sum(item['Price__c'] for item in cart_items)  | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal}) | 
					
					
						
						| 
							 | 
						        print(f"New item price: {new_item_price}, New subtotal: {new_subtotal}") | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "new_item_price": new_item_price, "subtotal": new_subtotal}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error updating quantity: {str(e)}") | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": str(e)}), 500 | 
					
					
						
						| 
							 | 
						@app.route("/checkout", methods=["POST"]) | 
					
					
						
						| 
							 | 
						def checkout(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email') | 
					
					
						
						| 
							 | 
						    user_id = session.get('user_id') | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    if not email or not user_id: | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "message": "User not logged in"}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						        data = request.json | 
					
					
						
						| 
							 | 
						        selected_coupon = data.get("selectedCoupon", "").strip() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c, Image1__c | 
					
					
						
						| 
							 | 
						            FROM Cart_Item__c | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						        cart_items = result.get("records", []) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        if not cart_items: | 
					
					
						
						| 
							 | 
						            return jsonify({"success": False, "message": "Cart is empty"}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        total_price = sum(item['Price__c'] for item in cart_items) | 
					
					
						
						| 
							 | 
						        discount = 0 | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        if selected_coupon: | 
					
					
						
						| 
							 | 
						            discount = total_price * 0.10   | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        total_bill = total_price - discount | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        order_details = "\n".join( | 
					
					
						
						| 
							 | 
						            f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | " | 
					
					
						
						| 
							 | 
						            f"Instructions: {item.get('Instructions__c', 'None')} | " | 
					
					
						
						| 
							 | 
						            f"Price: ${item['Price__c']} | Image: {item['Image1__c']}" | 
					
					
						
						| 
							 | 
						            for item in cart_items | 
					
					
						
						| 
							 | 
						        ) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        order_data = { | 
					
					
						
						| 
							 | 
						            "Customer_Name__c": user_id, | 
					
					
						
						| 
							 | 
						            "Customer_Email__c": email, | 
					
					
						
						| 
							 | 
						            "Total_Amount__c": total_price, | 
					
					
						
						| 
							 | 
						            "Discount__c": discount, | 
					
					
						
						| 
							 | 
						            "Total_Bill__c": total_bill, | 
					
					
						
						| 
							 | 
						            "Order_Status__c": "Pending", | 
					
					
						
						| 
							 | 
						            "Order_Details__c": order_details   | 
					
					
						
						| 
							 | 
						        } | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        sf.Order__c.create(order_data) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        for item in cart_items: | 
					
					
						
						| 
							 | 
						            sf.Cart_Item__c.delete(item["Id"]) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return jsonify({"success": True, "message": "Order placed successfully!"}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error during checkout: {str(e)}") | 
					
					
						
						| 
							 | 
						        return jsonify({"success": False, "error": str(e)}) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						@app.route("/order", methods=["GET"]) | 
					
					
						
						| 
							 | 
						def order_summary(): | 
					
					
						
						| 
							 | 
						    email = session.get('user_email')   | 
					
					
						
						| 
							 | 
						    if not email: | 
					
					
						
						| 
							 | 
						        return redirect(url_for("login")) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    try: | 
					
					
						
						| 
							 | 
						         | 
					
					
						
						| 
							 | 
						        result = sf.query(f""" | 
					
					
						
						| 
							 | 
						            SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c | 
					
					
						
						| 
							 | 
						            FROM Order__c | 
					
					
						
						| 
							 | 
						            WHERE Customer_Email__c = '{email}' | 
					
					
						
						| 
							 | 
						            ORDER BY CreatedDate DESC | 
					
					
						
						| 
							 | 
						            LIMIT 1 | 
					
					
						
						| 
							 | 
						        """) | 
					
					
						
						| 
							 | 
						        order = result.get("records", [])[0] if result.get("records") else None | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        if not order: | 
					
					
						
						| 
							 | 
						            return render_template("order.html", order=None) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						        return render_template("order.html", order=order) | 
					
					
						
						| 
							 | 
						    except Exception as e: | 
					
					
						
						| 
							 | 
						        print(f"Error fetching order details: {str(e)}") | 
					
					
						
						| 
							 | 
						        return render_template("order.html", order=None, error=str(e)) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						if __name__ == "__main__": | 
					
					
						
						| 
							 | 
						    app.run(debug=False, host="0.0.0.0", port=7860) |