import bcrypt import gradio as gr from datetime import datetime from simple_salesforce import Salesforce # Salesforce Connection sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q') # Function to Hash Password def hash_password(password): return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') # Function to Verify Password def verify_password(plain_password, hashed_password): return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) # Signup function def signup(name, email, phone, password): try: email = email.strip() query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if len(result['records']) > 0: return "Email already exists! Please use a different email." hashed_password = hash_password(password) sf.Customer_Login__c.create({ 'Name': name.strip(), 'Email__c': email, 'Phone_Number__c': phone.strip(), 'Password__c': hashed_password }) return "Signup successful! You can now login." except Exception as e: return f"Error during signup: {str(e)}" # Login function def login(email, password): try: email = email.strip() query = f"SELECT Name, Password__c FROM Customer_Login__c WHERE Email__c = '{email}'" result = sf.query(query) if len(result['records']) == 0: return "Invalid email or password.", None user = result['records'][0] stored_password = user['Password__c'] if verify_password(password.strip(), stored_password): return "Login successful!", user['Name'] else: return "Invalid email or password.", None except Exception as e: return f"Error during login: {str(e)}", None # Function to load menu data def load_menu_from_salesforce(): try: query = "SELECT Name, Price__c, Description__c, Image1__c, Image2__c, Veg_NonVeg__c, Section__c FROM Menu_Item__c" result = sf.query(query) return result['records'] except Exception as e: return [] # Function to load add-ons data def load_add_ons_from_salesforce(): try: query = "SELECT Name, Price__c FROM Add_Ons__c" result = sf.query(query) return result['records'] except Exception as e: return [] # Function to save cart summary in Salesforce def save_cart_summary_to_salesforce(cart_data, total_cost): try: # Log input data for debugging print("Saving Cart Data:", cart_data) print("Total Cost:", total_cost) # Validate inputs if not cart_data or total_cost <= 0: return "Cart is empty or total cost is invalid." # Create the Order record order_record = { 'Name': f"Order {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", 'Total_Cost__c': total_cost, 'Order_Date__c': datetime.now().isoformat() } order_result = sf.Order__c.create(order_record) order_id = order_result['id'] # Create Order Item records for item in cart_data: extras = ", ".join(extra['name'] for extra in item.get('extras', [])) order_item_record = { 'Name': item['name'], 'Order__c': order_id, 'Quantity__c': item['quantity'], 'Price__c': item['price'], 'Extras__c': extras, 'Instructions__c': item.get('instructions', ''), 'Total_Cost__c': item['totalCost'] } sf.Order_Item__c.create(order_item_record) return "Order and items saved successfully in Salesforce!" except Exception as e: print(f"Error saving order in Salesforce: {str(e)}") return f"Error saving order in Salesforce: {str(e)}" # Function to filter menu items def filter_menu(preference): menu_data = load_menu_from_salesforce() filtered_data = {} for item in menu_data: if "Section__c" not in item or "Veg_NonVeg__c" not in item: continue if item["Section__c"] not in filtered_data: filtered_data[item["Section__c"]] = [] if preference == "All" or (preference == "Veg" and item["Veg_NonVeg__c"] in ["Veg", "Both"]) or (preference == "Non-Veg" and item["Veg_NonVeg__c"] in ["Non veg", "Both"]): filtered_data[item["Section__c"].strip()].append(item) html_content = '
' for section, items in filtered_data.items(): html_content += f"

{section}

" html_content += '
' for item in items: html_content += f"""

{item['Name']}

${item['Price__c']}

{item['Description__c']}

""" html_content += '
' html_content += '
' if not any(filtered_data.values()): return "

No items match your filter.

" return html_content # Create Modal Window HTML def create_modal_window(): add_ons = load_add_ons_from_salesforce() add_ons_html = "" for add_on in add_ons: add_ons_html += f"""
""" modal_html = f""" """ return modal_html # JavaScript for Modal and Cart def modal_js(): modal_script = """ """ return modal_script # Gradio App with gr.Blocks() as app: with gr.Row(): gr.HTML("

Welcome to Biryani Hub

") with gr.Row(visible=True) as login_page: with gr.Column(): login_email = gr.Textbox(label="Email") login_password = gr.Textbox(label="Password", type="password") login_button = gr.Button("Login") signup_button = gr.Button("Go to Signup") login_output = gr.Textbox(label="Status") with gr.Row(visible=False) as signup_page: with gr.Column(): signup_name = gr.Textbox(label="Name") signup_email = gr.Textbox(label="Email") signup_phone = gr.Textbox(label="Phone") signup_password = gr.Textbox(label="Password", type="password") submit_signup = gr.Button("Signup") login_redirect = gr.Button("Go to Login") signup_output = gr.Textbox(label="Status") with gr.Row(visible=False) as menu_page: with gr.Column(): preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All") menu_output = gr.HTML() gr.HTML("
View Cart
") gr.HTML("") gr.HTML(create_modal_window()) gr.HTML(modal_js()) login_button.click( lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!") if login(email, password)[0] == "Login successful!" else (gr.update(), gr.update(), gr.update(), "Invalid email or password."), [login_email, login_password], [login_page, menu_page, menu_output, login_output] ) submit_signup.click( lambda name, email, phone, password: signup(name, email, phone, password), inputs=[signup_name, signup_email, signup_phone, signup_password], outputs=signup_output ) signup_button.click( lambda: (gr.update(visible=False), gr.update(visible=True)), inputs=[], outputs=[login_page, signup_page] ) login_redirect.click( lambda: (gr.update(visible=True), gr.update(visible=False)), inputs=[], outputs=[login_page, signup_page] ) preference.change(lambda pref: filter_menu(pref), [preference], menu_output) app.launch()