Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -992,6 +992,7 @@ def update_quantity():
|
|
992 |
except Exception as e:
|
993 |
print(f"Error updating quantity: {str(e)}")
|
994 |
return jsonify({"success": False, "error": str(e)}), 500
|
|
|
995 |
@app.route("/checkout", methods=["POST"])
|
996 |
def checkout():
|
997 |
email = session.get('user_email')
|
@@ -1003,7 +1004,7 @@ def checkout():
|
|
1003 |
|
1004 |
try:
|
1005 |
data = request.json
|
1006 |
-
selected_coupon = data.get("selectedCoupon", "").strip()
|
1007 |
|
1008 |
# Fetch cart items
|
1009 |
result = sf.query(f"""
|
@@ -1023,25 +1024,30 @@ def checkout():
|
|
1023 |
coupon_query = sf.query(f"""
|
1024 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
1025 |
""")
|
1026 |
-
|
1027 |
has_coupons = bool(coupon_query["records"])
|
1028 |
|
1029 |
if selected_coupon:
|
1030 |
-
discount
|
|
|
1031 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
1032 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
1033 |
|
1034 |
-
# Remove
|
1035 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
1036 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
|
|
|
|
1037 |
if not updated_coupons:
|
1038 |
updated_coupons_str = None # Set to None if no coupons are left
|
1039 |
|
|
|
1040 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
1041 |
"Coupon_Code__c": updated_coupons_str
|
1042 |
})
|
|
|
1043 |
else:
|
1044 |
-
|
|
|
1045 |
|
1046 |
# Fetch current reward points
|
1047 |
customer_record = sf.query(f"""
|
@@ -1054,13 +1060,15 @@ def checkout():
|
|
1054 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
1055 |
new_reward_points = current_reward_points + reward_points_to_add
|
1056 |
|
|
|
1057 |
sf.Customer_Login__c.update(customer["Id"], {
|
1058 |
"Reward_Points__c": new_reward_points
|
1059 |
})
|
1060 |
|
|
|
1061 |
total_bill = total_price - discount
|
1062 |
|
1063 |
-
#
|
1064 |
order_details = "\n".join(
|
1065 |
f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
|
1066 |
f"Instructions: {item.get('Instructions__c', 'None')} | "
|
@@ -1079,7 +1087,7 @@ def checkout():
|
|
1079 |
if not customer_id:
|
1080 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
1081 |
|
1082 |
-
#
|
1083 |
order_data = {
|
1084 |
"Customer_Name__c": user_id,
|
1085 |
"Customer_Email__c": email,
|
@@ -1089,12 +1097,13 @@ def checkout():
|
|
1089 |
"Order_Status__c": "Pending",
|
1090 |
"Customer2__c": customer_id,
|
1091 |
"Order_Details__c": order_details,
|
1092 |
-
"Table_Number__c": table_number #
|
1093 |
}
|
1094 |
|
|
|
1095 |
sf.Order__c.create(order_data)
|
1096 |
|
1097 |
-
#
|
1098 |
for item in cart_items:
|
1099 |
sf.Cart_Item__c.delete(item["Id"])
|
1100 |
|
@@ -1104,6 +1113,7 @@ def checkout():
|
|
1104 |
print(f"Error during checkout: {str(e)}")
|
1105 |
return jsonify({"success": False, "error": str(e)})
|
1106 |
|
|
|
1107 |
@app.route("/order", methods=["GET"])
|
1108 |
def order_summary():
|
1109 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
992 |
except Exception as e:
|
993 |
print(f"Error updating quantity: {str(e)}")
|
994 |
return jsonify({"success": False, "error": str(e)}), 500
|
995 |
+
|
996 |
@app.route("/checkout", methods=["POST"])
|
997 |
def checkout():
|
998 |
email = session.get('user_email')
|
|
|
1004 |
|
1005 |
try:
|
1006 |
data = request.json
|
1007 |
+
selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
|
1008 |
|
1009 |
# Fetch cart items
|
1010 |
result = sf.query(f"""
|
|
|
1024 |
coupon_query = sf.query(f"""
|
1025 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
1026 |
""")
|
|
|
1027 |
has_coupons = bool(coupon_query["records"])
|
1028 |
|
1029 |
if selected_coupon:
|
1030 |
+
# Apply 10% discount if a valid coupon is selected
|
1031 |
+
discount = total_price * 0.10 # Example: 10% discount
|
1032 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
1033 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
1034 |
|
1035 |
+
# Remove the selected coupon from the list of existing coupons
|
1036 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
1037 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
1038 |
+
|
1039 |
+
# If no coupons remain, set the field to None (not empty string)
|
1040 |
if not updated_coupons:
|
1041 |
updated_coupons_str = None # Set to None if no coupons are left
|
1042 |
|
1043 |
+
# Update the Referral_Coupon__c record
|
1044 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
1045 |
"Coupon_Code__c": updated_coupons_str
|
1046 |
})
|
1047 |
+
|
1048 |
else:
|
1049 |
+
# If no coupon is selected, add reward points
|
1050 |
+
reward_points_to_add = total_price * 0.10 # Example: 10% reward points
|
1051 |
|
1052 |
# Fetch current reward points
|
1053 |
customer_record = sf.query(f"""
|
|
|
1060 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
1061 |
new_reward_points = current_reward_points + reward_points_to_add
|
1062 |
|
1063 |
+
# Update reward points
|
1064 |
sf.Customer_Login__c.update(customer["Id"], {
|
1065 |
"Reward_Points__c": new_reward_points
|
1066 |
})
|
1067 |
|
1068 |
+
# Final total bill calculation
|
1069 |
total_bill = total_price - discount
|
1070 |
|
1071 |
+
# Store all order details
|
1072 |
order_details = "\n".join(
|
1073 |
f"{item['Name']} x{item['Quantity__c']} | Add-Ons: {item.get('Add_Ons__c', 'None')} | "
|
1074 |
f"Instructions: {item.get('Instructions__c', 'None')} | "
|
|
|
1087 |
if not customer_id:
|
1088 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
1089 |
|
1090 |
+
# Store order data
|
1091 |
order_data = {
|
1092 |
"Customer_Name__c": user_id,
|
1093 |
"Customer_Email__c": email,
|
|
|
1097 |
"Order_Status__c": "Pending",
|
1098 |
"Customer2__c": customer_id,
|
1099 |
"Order_Details__c": order_details,
|
1100 |
+
"Table_Number__c": table_number # Store table number
|
1101 |
}
|
1102 |
|
1103 |
+
# Create the order in Salesforce
|
1104 |
sf.Order__c.create(order_data)
|
1105 |
|
1106 |
+
# Delete cart items after order is placed
|
1107 |
for item in cart_items:
|
1108 |
sf.Cart_Item__c.delete(item["Id"])
|
1109 |
|
|
|
1113 |
print(f"Error during checkout: {str(e)}")
|
1114 |
return jsonify({"success": False, "error": str(e)})
|
1115 |
|
1116 |
+
|
1117 |
@app.route("/order", methods=["GET"])
|
1118 |
def order_summary():
|
1119 |
email = session.get('user_email') # Fetch logged-in user's email
|