File size: 4,371 Bytes
44dc6fd
a3f18b1
 
 
 
 
 
 
 
 
 
cb7c48a
 
 
a3f18b1
 
 
 
 
 
 
44dc6fd
 
a3f18b1
ea3b616
a3f18b1
 
 
 
 
 
ea3b616
a3f18b1
 
 
 
ea3b616
a3f18b1
ea3b616
a3f18b1
44dc6fd
 
a3f18b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc819c2
 
 
44dc6fd
a3f18b1
44dc6fd
 
 
 
 
cc819c2
44dc6fd
 
 
 
 
5076acb
 
 
ea3b616
 
 
6a32d77
44dc6fd
 
 
 
 
cc819c2
ea3b616
 
 
44dc6fd
 
5076acb
44dc6fd
 
 
 
 
 
 
5076acb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
import gradio as gr
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Salesforce connection
try:
    sf = Salesforce(
        username=os.getenv("SF_USERNAME"),
        password=os.getenv("SF_PASSWORD"),
        security_token=os.getenv("SF_SECURITY_TOKEN"),
        domain="login"  # Use "test" for sandbox
    )
    print("Salesforce connection successful!")
except Exception as e:
    print(f"Salesforce connection failed: {e}")
    sf = None

# Function to handle user authentication and Salesforce login
def authenticate_user(email, password):
    if not sf:
        return "Salesforce connection failed. Please check credentials and try again.", None, None, gr.update(visible=False), gr.update(visible=False)

    try:
        query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
        result = sf.query(query)

        if result['totalSize'] == 0:
            return "Invalid Login Details", None, None, gr.update(visible=False), gr.update(visible=False)

        customer = result['records'][0]
        reward_points = customer['Reward_Points__c']

        return f"Welcome, you have {reward_points} points. Proceed to rewards.", customer['Id'], reward_points, gr.update(visible=True), gr.update(visible=True)
    except Exception as e:
        return f"Error during authentication: {e}", None, None, gr.update(visible=False), gr.update(visible=False)

# Function to handle reward points logic
def handle_rewards(customer_id, bill_amount, apply_rewards):
    try:
        customer = sf.Customer_Login__c.get(customer_id)
        points = customer['Reward_Points__c']
        gst = 0.18 * bill_amount

        if points >= 500 and apply_rewards:
            discount = 0.1 * bill_amount
            final_bill = bill_amount - discount + gst
            updated_points = points - 500
            message = "You saved 10% on your total bill!"
        else:
            discount = 0
            earned_points = 0.1 * bill_amount
            final_bill = bill_amount + gst
            updated_points = points + earned_points
            message = f"You earned {earned_points:.2f} reward points!"

        # Update the customer's reward points in Salesforce
        sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})

        return message, final_bill, updated_points
    except Exception as e:
        return f"Error applying rewards: {e}", 0, 0

# Define the Gradio interface
def create_interface():
    with gr.Blocks() as demo:
        # Login section
        gr.Markdown("### Login to your account")
        email_input = gr.Textbox(label="Email", placeholder="Enter your email")
        password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
        login_button = gr.Button("Login")
        login_output = gr.Textbox(label="Status")
        customer_id_output = gr.Textbox(label="Customer ID", visible=False)  # Hidden
        reward_points_output = gr.Textbox(label="Available Reward Points", visible=False)  # Hidden
        
        # Reward points section (Initially hidden)
        reward_section = gr.Column(visible=False)
        gr.Markdown("### Reward Points Section", elem_id="reward_section")
        bill_amount_input = gr.Number(label="Enter Bill Amount", value=0)
        apply_rewards_checkbox = gr.Checkbox(label="Apply Reward Points", value=True)
        rewards_button = gr.Button("Calculate Bill")
        rewards_message = gr.Textbox(label="Message")
        final_bill_output = gr.Textbox(label="Final Bill Amount")
        remaining_points_output = gr.Textbox(label="Remaining Points")

        # Action for login button
        login_button.click(authenticate_user, inputs=[email_input, password_input], outputs=[login_output, customer_id_output, reward_points_output, reward_section, bill_amount_input])

        # Action for rewards calculation
        rewards_button.click(handle_rewards, 
                             inputs=[customer_id_output, bill_amount_input, apply_rewards_checkbox], 
                             outputs=[rewards_message, final_bill_output, remaining_points_output])
    
    return demo

# Run the Gradio interface
if __name__ == "__main__":
    demo = create_interface()
    demo.launch()