Update app.py
Browse files
app.py
CHANGED
|
@@ -44,17 +44,20 @@ def home():
|
|
| 44 |
# Fetch user details from URL parameters
|
| 45 |
user_email = request.args.get("email")
|
| 46 |
user_name = request.args.get("name")
|
|
|
|
| 47 |
|
| 48 |
if user_email and user_name:
|
| 49 |
session["user_email"] = user_email
|
| 50 |
session["user_name"] = user_name
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
# Ensure session is saved before redirecting
|
| 54 |
session.modified = True
|
| 55 |
return redirect(url_for("menu")) # Redirect to menu directly
|
| 56 |
|
| 57 |
-
return render_template("redirect_page.html")
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
from datetime import datetime
|
|
@@ -990,6 +993,7 @@ def update_quantity():
|
|
| 990 |
def checkout():
|
| 991 |
email = session.get('user_email')
|
| 992 |
user_id = session.get('user_name')
|
|
|
|
| 993 |
|
| 994 |
if not email or not user_id:
|
| 995 |
return jsonify({"success": False, "message": "User not logged in"})
|
|
@@ -1011,31 +1015,27 @@ def checkout():
|
|
| 1011 |
|
| 1012 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 1013 |
discount = 0
|
|
|
|
| 1014 |
# Fetch the user's existing coupons
|
| 1015 |
coupon_query = sf.query(f"""
|
| 1016 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 1017 |
""")
|
| 1018 |
|
| 1019 |
-
has_coupons = bool(coupon_query["records"])
|
| 1020 |
|
| 1021 |
if selected_coupon:
|
| 1022 |
-
# Case 3: User selected a valid coupon → Apply discount & remove coupon
|
| 1023 |
discount = total_price * 0.10 # 10% discount
|
| 1024 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
| 1025 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
| 1026 |
|
| 1027 |
# Remove only the selected coupon
|
| 1028 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
| 1029 |
-
|
| 1030 |
-
# Convert list back to a string with newlines
|
| 1031 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
| 1032 |
|
| 1033 |
-
# Update the Referral_Coupon__c record with the remaining coupons
|
| 1034 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 1035 |
"Coupon_Code__c": updated_coupons_str
|
| 1036 |
})
|
| 1037 |
else:
|
| 1038 |
-
# Case 1 & Case 2: User has no coupons or has coupons but didn’t select one → Add 10% to reward points
|
| 1039 |
reward_points_to_add = total_price * 0.10
|
| 1040 |
|
| 1041 |
# Fetch current reward points
|
|
@@ -1049,17 +1049,13 @@ def checkout():
|
|
| 1049 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
| 1050 |
new_reward_points = current_reward_points + reward_points_to_add
|
| 1051 |
|
| 1052 |
-
print(f"Updating reward points: Current = {current_reward_points}, Adding = {reward_points_to_add}, New = {new_reward_points}")
|
| 1053 |
-
|
| 1054 |
-
# Update reward points in Salesforce
|
| 1055 |
sf.Customer_Login__c.update(customer["Id"], {
|
| 1056 |
"Reward_Points__c": new_reward_points
|
| 1057 |
})
|
| 1058 |
-
print(f"Successfully updated reward points for {email}")
|
| 1059 |
|
| 1060 |
total_bill = total_price - discount
|
| 1061 |
|
| 1062 |
-
# ✅ Store
|
| 1063 |
order_details = "\n".join(
|
| 1064 |
f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
|
| 1065 |
f"Instructions: {item.get('Instructions__c', 'None')} | "
|
|
@@ -1067,19 +1063,18 @@ def checkout():
|
|
| 1067 |
for item in cart_items
|
| 1068 |
)
|
| 1069 |
|
| 1070 |
-
# Fetch Customer ID from Customer_Login__c
|
| 1071 |
customer_query = sf.query(f"""
|
| 1072 |
SELECT Id FROM Customer_Login__c
|
| 1073 |
WHERE Email__c = '{email}'
|
| 1074 |
""")
|
| 1075 |
|
| 1076 |
-
# Assuming the customer exists
|
| 1077 |
customer_id = customer_query["records"][0]["Id"] if customer_query["records"] else None
|
| 1078 |
|
| 1079 |
if not customer_id:
|
| 1080 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
| 1081 |
|
| 1082 |
-
# Store
|
| 1083 |
order_data = {
|
| 1084 |
"Customer_Name__c": user_id,
|
| 1085 |
"Customer_Email__c": email,
|
|
@@ -1087,24 +1082,23 @@ def checkout():
|
|
| 1087 |
"Discount__c": discount,
|
| 1088 |
"Total_Bill__c": total_bill,
|
| 1089 |
"Order_Status__c": "Pending",
|
| 1090 |
-
"Customer2__c": customer_id,
|
| 1091 |
-
"Order_Details__c": order_details
|
|
|
|
| 1092 |
}
|
| 1093 |
|
| 1094 |
sf.Order__c.create(order_data)
|
| 1095 |
-
|
| 1096 |
|
| 1097 |
# ✅ Delete cart items after order is placed
|
| 1098 |
for item in cart_items:
|
| 1099 |
sf.Cart_Item__c.delete(item["Id"])
|
| 1100 |
-
|
| 1101 |
-
|
| 1102 |
|
| 1103 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 1104 |
|
| 1105 |
except Exception as e:
|
| 1106 |
print(f"Error during checkout: {str(e)}")
|
| 1107 |
return jsonify({"success": False, "error": str(e)})
|
|
|
|
| 1108 |
@app.route("/order", methods=["GET"])
|
| 1109 |
def order_summary():
|
| 1110 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
|
| 44 |
# Fetch user details from URL parameters
|
| 45 |
user_email = request.args.get("email")
|
| 46 |
user_name = request.args.get("name")
|
| 47 |
+
table_number = request.args.get("table") # Capture table number
|
| 48 |
|
| 49 |
if user_email and user_name:
|
| 50 |
session["user_email"] = user_email
|
| 51 |
session["user_name"] = user_name
|
| 52 |
+
session["table_number"] = table_number # Store table number in session
|
| 53 |
+
print(f"User logged in: {user_email} - {user_name} - Table: {table_number}")
|
| 54 |
|
| 55 |
# Ensure session is saved before redirecting
|
| 56 |
session.modified = True
|
| 57 |
return redirect(url_for("menu")) # Redirect to menu directly
|
| 58 |
|
| 59 |
+
return render_template("redirect_page.html")
|
| 60 |
+
|
| 61 |
|
| 62 |
|
| 63 |
from datetime import datetime
|
|
|
|
| 993 |
def checkout():
|
| 994 |
email = session.get('user_email')
|
| 995 |
user_id = session.get('user_name')
|
| 996 |
+
table_number = session.get('table_number') # Retrieve table number
|
| 997 |
|
| 998 |
if not email or not user_id:
|
| 999 |
return jsonify({"success": False, "message": "User not logged in"})
|
|
|
|
| 1015 |
|
| 1016 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 1017 |
discount = 0
|
| 1018 |
+
|
| 1019 |
# Fetch the user's existing coupons
|
| 1020 |
coupon_query = sf.query(f"""
|
| 1021 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 1022 |
""")
|
| 1023 |
|
| 1024 |
+
has_coupons = bool(coupon_query["records"])
|
| 1025 |
|
| 1026 |
if selected_coupon:
|
|
|
|
| 1027 |
discount = total_price * 0.10 # 10% discount
|
| 1028 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
| 1029 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
| 1030 |
|
| 1031 |
# Remove only the selected coupon
|
| 1032 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
|
|
|
|
|
|
| 1033 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
| 1034 |
|
|
|
|
| 1035 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 1036 |
"Coupon_Code__c": updated_coupons_str
|
| 1037 |
})
|
| 1038 |
else:
|
|
|
|
| 1039 |
reward_points_to_add = total_price * 0.10
|
| 1040 |
|
| 1041 |
# Fetch current reward points
|
|
|
|
| 1049 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
| 1050 |
new_reward_points = current_reward_points + reward_points_to_add
|
| 1051 |
|
|
|
|
|
|
|
|
|
|
| 1052 |
sf.Customer_Login__c.update(customer["Id"], {
|
| 1053 |
"Reward_Points__c": new_reward_points
|
| 1054 |
})
|
|
|
|
| 1055 |
|
| 1056 |
total_bill = total_price - discount
|
| 1057 |
|
| 1058 |
+
# ✅ Store all order details
|
| 1059 |
order_details = "\n".join(
|
| 1060 |
f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
|
| 1061 |
f"Instructions: {item.get('Instructions__c', 'None')} | "
|
|
|
|
| 1063 |
for item in cart_items
|
| 1064 |
)
|
| 1065 |
|
| 1066 |
+
# Fetch Customer ID from Customer_Login__c
|
| 1067 |
customer_query = sf.query(f"""
|
| 1068 |
SELECT Id FROM Customer_Login__c
|
| 1069 |
WHERE Email__c = '{email}'
|
| 1070 |
""")
|
| 1071 |
|
|
|
|
| 1072 |
customer_id = customer_query["records"][0]["Id"] if customer_query["records"] else None
|
| 1073 |
|
| 1074 |
if not customer_id:
|
| 1075 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
| 1076 |
|
| 1077 |
+
# ✅ Store table number in Order__c
|
| 1078 |
order_data = {
|
| 1079 |
"Customer_Name__c": user_id,
|
| 1080 |
"Customer_Email__c": email,
|
|
|
|
| 1082 |
"Discount__c": discount,
|
| 1083 |
"Total_Bill__c": total_bill,
|
| 1084 |
"Order_Status__c": "Pending",
|
| 1085 |
+
"Customer2__c": customer_id,
|
| 1086 |
+
"Order_Details__c": order_details,
|
| 1087 |
+
"Table_Number__c": table_number # ✅ Store table number
|
| 1088 |
}
|
| 1089 |
|
| 1090 |
sf.Order__c.create(order_data)
|
|
|
|
| 1091 |
|
| 1092 |
# ✅ Delete cart items after order is placed
|
| 1093 |
for item in cart_items:
|
| 1094 |
sf.Cart_Item__c.delete(item["Id"])
|
|
|
|
|
|
|
| 1095 |
|
| 1096 |
return jsonify({"success": True, "message": "Order placed successfully!"})
|
| 1097 |
|
| 1098 |
except Exception as e:
|
| 1099 |
print(f"Error during checkout: {str(e)}")
|
| 1100 |
return jsonify({"success": False, "error": str(e)})
|
| 1101 |
+
|
| 1102 |
@app.route("/order", methods=["GET"])
|
| 1103 |
def order_summary():
|
| 1104 |
email = session.get('user_email') # Fetch logged-in user's email
|