Spaces:
Sleeping
Sleeping
File size: 4,221 Bytes
ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 4c3c59c ab4ea97 91ba13e ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 f979400 ab4ea97 91ba13e ab4ea97 f979400 ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 f979400 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
from flask import Flask, render_template, redirect, request, url_for
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
# Get Salesforce credentials from environment variables
SF_USERNAME = os.getenv('SF_USERNAME')
SF_PASSWORD = os.getenv('SF_PASSWORD')
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
SF_DOMAIN = os.getenv('SF_DOMAIN')
# Salesforce connection
try:
print("Attempting Salesforce connection...")
sf = Salesforce(username=SF_USERNAME,
password=SF_PASSWORD,
security_token=SF_SECURITY_TOKEN,
domain=SF_DOMAIN)
print("Salesforce connection successful!")
except Exception as e:
print(f"Salesforce connection failed: {e}")
sf = None
# Route for login page
@app.route('/auth', methods=['POST'])
def auth():
email = request.form['email']
password = request.form['password']
if not sf:
return render_template('login.html', error_message="Salesforce connection failed. Please try again.")
try:
# Query Salesforce for user authentication
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 render_template('login.html', error_message="Invalid login details. Please check your email and password.")
customer = result['records'][0]
reward_points = customer['Reward_Points__c']
# Redirect to rewards page or any other page
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
except Exception as e:
return render_template('login.html', error_message=f"Error during authentication: {e}")
# Route to display rewards page
@app.route('/rewards/<customer_id>')
def rewards(customer_id):
print(f"Rewards route accessed for customer {customer_id}") # Debug statement
try:
customer = sf.Customer_Login__c.get(customer_id)
points = customer['Reward_Points__c']
# Render the rewards page
return render_template('rewards.html', points=points, customer_id=customer_id)
except Exception as e:
return f"Error fetching rewards: {e}"
# Route to apply rewards
@app.route('/apply_rewards', methods=['POST'])
def apply_rewards():
print("Apply rewards route accessed") # Debug statement
customer_id = request.form['customer_id']
bill_amount = float(request.form['bill_amount'])
apply_rewards = request.form.get('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
# Update the customer's reward points in Salesforce
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
message = "You saved 10% on your total bill!"
else:
# Customers with below 500 points earn 10% of their bill amount as reward points
discount = 0
earned_points = 0.1 * bill_amount
final_bill = bill_amount + gst
updated_points = points + earned_points
# Update the customer's reward points in Salesforce
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
message = f"You earned 10% of your bill amount ({earned_points} points) as reward points!"
# Render the summary page with Dollar symbol
return render_template(
'apply_rewards.html',
original_bill=bill_amount,
discount=discount,
gst=gst,
final_bill=final_bill,
updated_points=updated_points,
message=message
)
except Exception as e:
return f"Error applying rewards: {e}"
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=7860)
|