Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, redirect, request, url_for
|
| 2 |
+
from simple_salesforce import Salesforce
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Get Salesforce credentials from environment variables
|
| 12 |
+
SF_USERNAME = os.getenv('SF_USERNAME')
|
| 13 |
+
SF_PASSWORD = os.getenv('SF_PASSWORD')
|
| 14 |
+
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
|
| 15 |
+
SF_DOMAIN = os.getenv('SF_DOMAIN')
|
| 16 |
+
|
| 17 |
+
# Salesforce connection
|
| 18 |
+
try:
|
| 19 |
+
sf = Salesforce(username=SF_USERNAME,
|
| 20 |
+
password=SF_PASSWORD,
|
| 21 |
+
security_token=SF_SECURITY_TOKEN,
|
| 22 |
+
domain=SF_DOMAIN)
|
| 23 |
+
print("Salesforce connection successful!")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Salesforce connection failed: {e}")
|
| 26 |
+
sf = None
|
| 27 |
+
|
| 28 |
+
# Route for login page
|
| 29 |
+
@app.route('/')
|
| 30 |
+
def login():
|
| 31 |
+
return render_template('login.html')
|
| 32 |
+
|
| 33 |
+
# Route to process login
|
| 34 |
+
@app.route('/auth', methods=['POST'])
|
| 35 |
+
def auth():
|
| 36 |
+
email = request.form['email']
|
| 37 |
+
password = request.form['password']
|
| 38 |
+
|
| 39 |
+
if not sf:
|
| 40 |
+
return "Salesforce connection failed. Please check credentials and try again."
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Query Salesforce for user authentication
|
| 44 |
+
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
|
| 45 |
+
result = sf.query(query)
|
| 46 |
+
|
| 47 |
+
if result['totalSize'] == 0:
|
| 48 |
+
return "Invalid Login Details"
|
| 49 |
+
|
| 50 |
+
customer = result['records'][0]
|
| 51 |
+
reward_points = customer['Reward_Points__c']
|
| 52 |
+
|
| 53 |
+
# Redirect to rewards page
|
| 54 |
+
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error during authentication: {e}"
|
| 57 |
+
|
| 58 |
+
# Route to display rewards page
|
| 59 |
+
@app.route('/rewards/<customer_id>')
|
| 60 |
+
def rewards(customer_id):
|
| 61 |
+
try:
|
| 62 |
+
customer = sf.Customer_Login__c.get(customer_id)
|
| 63 |
+
points = customer['Reward_Points__c']
|
| 64 |
+
|
| 65 |
+
# Render the rewards page
|
| 66 |
+
return render_template('rewards.html', points=points, customer_id=customer_id)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error fetching rewards: {e}"
|
| 69 |
+
|
| 70 |
+
# Route to apply rewards
|
| 71 |
+
@app.route('/apply_rewards', methods=['POST'])
|
| 72 |
+
def apply_rewards():
|
| 73 |
+
customer_id = request.form['customer_id']
|
| 74 |
+
bill_amount = float(request.form['bill_amount'])
|
| 75 |
+
apply_rewards = request.form.get('apply_rewards')
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
customer = sf.Customer_Login__c.get(customer_id)
|
| 79 |
+
points = customer['Reward_Points__c']
|
| 80 |
+
gst = 0.18 * bill_amount
|
| 81 |
+
|
| 82 |
+
if points >= 500 and apply_rewards:
|
| 83 |
+
discount = 0.1 * bill_amount
|
| 84 |
+
final_bill = bill_amount - discount + gst
|
| 85 |
+
updated_points = points - 500
|
| 86 |
+
|
| 87 |
+
# Update the customer's reward points in Salesforce
|
| 88 |
+
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 89 |
+
|
| 90 |
+
message = "You saved 10% on your total bill!"
|
| 91 |
+
else:
|
| 92 |
+
# Customers with below 500 points earn 10% of their bill amount as reward points
|
| 93 |
+
discount = 0
|
| 94 |
+
earned_points = 0.1 * bill_amount
|
| 95 |
+
final_bill = bill_amount + gst
|
| 96 |
+
updated_points = points + earned_points
|
| 97 |
+
|
| 98 |
+
# Update the customer's reward points in Salesforce
|
| 99 |
+
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 100 |
+
|
| 101 |
+
message = f"You earned 10% of your bill amount ({earned_points} points) as reward points!"
|
| 102 |
+
|
| 103 |
+
# Render the summary page
|
| 104 |
+
return render_template(
|
| 105 |
+
'apply_rewards.html',
|
| 106 |
+
original_bill=bill_amount,
|
| 107 |
+
discount=discount,
|
| 108 |
+
gst=gst,
|
| 109 |
+
final_bill=final_bill,
|
| 110 |
+
updated_points=updated_points,
|
| 111 |
+
message=message
|
| 112 |
+
)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return f"Error applying rewards: {e}"
|
| 115 |
+
|
| 116 |
+
if __name__ == '__main__':
|
| 117 |
+
app.run(debug=True)
|