Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -993,21 +993,23 @@ def update_quantity():
|
|
| 993 |
print(f"Error updating quantity: {str(e)}")
|
| 994 |
return jsonify({"success": False, "error": str(e)}), 500
|
| 995 |
|
| 996 |
-
|
| 997 |
-
|
| 998 |
@app.route("/checkout", methods=["POST"])
|
| 999 |
def checkout():
|
| 1000 |
email = session.get('user_email')
|
| 1001 |
user_id = session.get('user_name')
|
| 1002 |
table_number = session.get('table_number') # Retrieve table number
|
| 1003 |
|
|
|
|
|
|
|
| 1004 |
if not email or not user_id:
|
|
|
|
| 1005 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 1006 |
|
| 1007 |
try:
|
| 1008 |
# Fetch the selected coupon (if any)
|
| 1009 |
data = request.json
|
| 1010 |
selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
|
|
|
|
| 1011 |
|
| 1012 |
# Fetch cart items for the current user
|
| 1013 |
result = sf.query(f"""
|
|
@@ -1021,50 +1023,68 @@ def checkout():
|
|
| 1021 |
print(f"Cart Items Retrieved: {cart_items}") # Debugging log
|
| 1022 |
|
| 1023 |
if not cart_items:
|
|
|
|
| 1024 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 1025 |
|
| 1026 |
total_price = sum(item['Price__c'] for item in cart_items)
|
|
|
|
|
|
|
| 1027 |
discount = 0
|
| 1028 |
|
| 1029 |
# Fetch the user's existing coupons
|
| 1030 |
coupon_query = sf.query(f"""
|
| 1031 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 1032 |
""")
|
|
|
|
|
|
|
| 1033 |
has_coupons = bool(coupon_query["records"])
|
|
|
|
| 1034 |
|
| 1035 |
if selected_coupon:
|
| 1036 |
# Apply 10% discount if a valid coupon is selected
|
| 1037 |
discount = total_price * 0.10 # Example: 10% discount
|
|
|
|
|
|
|
| 1038 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
|
|
|
|
|
|
| 1039 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
|
|
|
| 1040 |
|
| 1041 |
# Remove the selected coupon from the list of existing coupons
|
| 1042 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
| 1043 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
| 1044 |
|
|
|
|
|
|
|
| 1045 |
# If no coupons remain, set the field to None (not empty string)
|
| 1046 |
if not updated_coupons:
|
| 1047 |
updated_coupons_str = None # Set to None if no coupons are left
|
|
|
|
| 1048 |
|
| 1049 |
# Update the Referral_Coupon__c record
|
|
|
|
| 1050 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 1051 |
"Coupon_Code__c": updated_coupons_str
|
| 1052 |
})
|
| 1053 |
-
|
| 1054 |
else:
|
| 1055 |
# If no coupon is selected, add reward points
|
| 1056 |
reward_points_to_add = total_price * 0.10 # Example: 10% reward points
|
|
|
|
| 1057 |
|
| 1058 |
# Fetch current reward points
|
| 1059 |
customer_record = sf.query(f"""
|
| 1060 |
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
| 1061 |
WHERE Email__c = '{email}'
|
| 1062 |
""")
|
|
|
|
|
|
|
| 1063 |
customer = customer_record.get("records", [])[0] if customer_record else None
|
| 1064 |
-
|
| 1065 |
if customer:
|
| 1066 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
|
|
|
| 1067 |
new_reward_points = current_reward_points + reward_points_to_add
|
|
|
|
| 1068 |
|
| 1069 |
# Update reward points
|
| 1070 |
sf.Customer_Login__c.update(customer["Id"], {
|
|
@@ -1073,6 +1093,7 @@ def checkout():
|
|
| 1073 |
|
| 1074 |
# Final total bill calculation
|
| 1075 |
total_bill = total_price - discount
|
|
|
|
| 1076 |
|
| 1077 |
# Store all order details (before deleting cart items)
|
| 1078 |
order_details = "\n".join(
|
|
@@ -1081,6 +1102,7 @@ def checkout():
|
|
| 1081 |
f"Price: ${item['Price__c']} | Image: {item['Image1__c']}"
|
| 1082 |
for item in cart_items
|
| 1083 |
)
|
|
|
|
| 1084 |
|
| 1085 |
# Fetch Customer ID from Customer_Login__c
|
| 1086 |
customer_query = sf.query(f"""
|
|
@@ -1089,8 +1111,10 @@ def checkout():
|
|
| 1089 |
""")
|
| 1090 |
|
| 1091 |
customer_id = customer_query["records"][0]["Id"] if customer_query["records"] else None
|
| 1092 |
-
|
|
|
|
| 1093 |
if not customer_id:
|
|
|
|
| 1094 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
| 1095 |
|
| 1096 |
# Store order data
|
|
@@ -1105,23 +1129,27 @@ def checkout():
|
|
| 1105 |
"Order_Details__c": order_details,
|
| 1106 |
"Table_Number__c": table_number # Store table number
|
| 1107 |
}
|
|
|
|
| 1108 |
|
| 1109 |
# Create the order in Salesforce
|
| 1110 |
order_response = sf.Order__c.create(order_data)
|
|
|
|
| 1111 |
|
| 1112 |
# Ensure the order was created successfully before deleting cart items
|
| 1113 |
if order_response:
|
| 1114 |
# Only delete cart items after the order is created
|
| 1115 |
for item in cart_items:
|
|
|
|
| 1116 |
sf.Cart_Item__c.delete(item["Id"])
|
| 1117 |
|
| 1118 |
return jsonify({"success": True, "message": "Order placed successfully!", "discount": discount, "totalBill": total_bill})
|
| 1119 |
|
| 1120 |
except Exception as e:
|
| 1121 |
-
print(f"Error during checkout: {str(e)}")
|
| 1122 |
return jsonify({"success": False, "error": str(e)})
|
| 1123 |
|
| 1124 |
|
|
|
|
| 1125 |
@app.route("/order", methods=["GET"])
|
| 1126 |
def order_summary():
|
| 1127 |
email = session.get('user_email') # Fetch logged-in user's email
|
|
|
|
| 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')
|
| 999 |
user_id = session.get('user_name')
|
| 1000 |
table_number = session.get('table_number') # Retrieve table number
|
| 1001 |
|
| 1002 |
+
print(f"Session Email: {email}, User ID: {user_id}, Table Number: {table_number}") # Debugging session data
|
| 1003 |
+
|
| 1004 |
if not email or not user_id:
|
| 1005 |
+
print("User not logged in")
|
| 1006 |
return jsonify({"success": False, "message": "User not logged in"})
|
| 1007 |
|
| 1008 |
try:
|
| 1009 |
# Fetch the selected coupon (if any)
|
| 1010 |
data = request.json
|
| 1011 |
selected_coupon = data.get("selectedCoupon", "").strip() # Get selected coupon
|
| 1012 |
+
print(f"Selected Coupon: {selected_coupon}") # Debugging selected coupon
|
| 1013 |
|
| 1014 |
# Fetch cart items for the current user
|
| 1015 |
result = sf.query(f"""
|
|
|
|
| 1023 |
print(f"Cart Items Retrieved: {cart_items}") # Debugging log
|
| 1024 |
|
| 1025 |
if not cart_items:
|
| 1026 |
+
print("Cart is empty")
|
| 1027 |
return jsonify({"success": False, "message": "Cart is empty"})
|
| 1028 |
|
| 1029 |
total_price = sum(item['Price__c'] for item in cart_items)
|
| 1030 |
+
print(f"Total Price: {total_price}") # Debugging total price calculation
|
| 1031 |
+
|
| 1032 |
discount = 0
|
| 1033 |
|
| 1034 |
# Fetch the user's existing coupons
|
| 1035 |
coupon_query = sf.query(f"""
|
| 1036 |
SELECT Id, Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
|
| 1037 |
""")
|
| 1038 |
+
print(f"Coupon Query Results: {coupon_query}") # Debugging coupon query results
|
| 1039 |
+
|
| 1040 |
has_coupons = bool(coupon_query["records"])
|
| 1041 |
+
print(f"Has Coupons: {has_coupons}") # Debugging coupon presence check
|
| 1042 |
|
| 1043 |
if selected_coupon:
|
| 1044 |
# Apply 10% discount if a valid coupon is selected
|
| 1045 |
discount = total_price * 0.10 # Example: 10% discount
|
| 1046 |
+
print(f"Discount Applied: {discount}") # Debugging discount calculation
|
| 1047 |
+
|
| 1048 |
referral_coupon_id = coupon_query["records"][0]["Id"]
|
| 1049 |
+
print(f"Referral Coupon ID: {referral_coupon_id}") # Debugging referral coupon ID
|
| 1050 |
+
|
| 1051 |
existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
|
| 1052 |
+
print(f"Existing Coupons Before Removal: {existing_coupons}") # Debugging existing coupons
|
| 1053 |
|
| 1054 |
# Remove the selected coupon from the list of existing coupons
|
| 1055 |
updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
|
| 1056 |
updated_coupons_str = "\n".join(updated_coupons).strip()
|
| 1057 |
|
| 1058 |
+
print(f"Updated Coupons After Removal: {updated_coupons}") # Debugging updated coupons
|
| 1059 |
+
|
| 1060 |
# If no coupons remain, set the field to None (not empty string)
|
| 1061 |
if not updated_coupons:
|
| 1062 |
updated_coupons_str = None # Set to None if no coupons are left
|
| 1063 |
+
print("No Coupons Remaining. Setting to None") # Debugging no coupons left
|
| 1064 |
|
| 1065 |
# Update the Referral_Coupon__c record
|
| 1066 |
+
print(f"Updating Referral Coupon: {updated_coupons_str}") # Debugging update to Salesforce
|
| 1067 |
sf.Referral_Coupon__c.update(referral_coupon_id, {
|
| 1068 |
"Coupon_Code__c": updated_coupons_str
|
| 1069 |
})
|
|
|
|
| 1070 |
else:
|
| 1071 |
# If no coupon is selected, add reward points
|
| 1072 |
reward_points_to_add = total_price * 0.10 # Example: 10% reward points
|
| 1073 |
+
print(f"Reward Points to Add: {reward_points_to_add}") # Debugging reward points
|
| 1074 |
|
| 1075 |
# Fetch current reward points
|
| 1076 |
customer_record = sf.query(f"""
|
| 1077 |
SELECT Id, Reward_Points__c FROM Customer_Login__c
|
| 1078 |
WHERE Email__c = '{email}'
|
| 1079 |
""")
|
| 1080 |
+
print(f"Customer Reward Points Query: {customer_record}") # Debugging customer reward points query
|
| 1081 |
+
|
| 1082 |
customer = customer_record.get("records", [])[0] if customer_record else None
|
|
|
|
| 1083 |
if customer:
|
| 1084 |
current_reward_points = customer.get("Reward_Points__c") or 0
|
| 1085 |
+
print(f"Current Reward Points: {current_reward_points}") # Debugging current reward points
|
| 1086 |
new_reward_points = current_reward_points + reward_points_to_add
|
| 1087 |
+
print(f"New Reward Points: {new_reward_points}") # Debugging new reward points calculation
|
| 1088 |
|
| 1089 |
# Update reward points
|
| 1090 |
sf.Customer_Login__c.update(customer["Id"], {
|
|
|
|
| 1093 |
|
| 1094 |
# Final total bill calculation
|
| 1095 |
total_bill = total_price - discount
|
| 1096 |
+
print(f"Total Bill After Discount: {total_bill}") # Debugging final total bill
|
| 1097 |
|
| 1098 |
# Store all order details (before deleting cart items)
|
| 1099 |
order_details = "\n".join(
|
|
|
|
| 1102 |
f"Price: ${item['Price__c']} | Image: {item['Image1__c']}"
|
| 1103 |
for item in cart_items
|
| 1104 |
)
|
| 1105 |
+
print(f"Order Details: {order_details}") # Debugging order details
|
| 1106 |
|
| 1107 |
# Fetch Customer ID from Customer_Login__c
|
| 1108 |
customer_query = sf.query(f"""
|
|
|
|
| 1111 |
""")
|
| 1112 |
|
| 1113 |
customer_id = customer_query["records"][0]["Id"] if customer_query["records"] else None
|
| 1114 |
+
print(f"Customer ID: {customer_id}") # Debugging customer ID retrieval
|
| 1115 |
+
|
| 1116 |
if not customer_id:
|
| 1117 |
+
print("Customer record not found")
|
| 1118 |
return jsonify({"success": False, "message": "Customer record not found in Salesforce"})
|
| 1119 |
|
| 1120 |
# Store order data
|
|
|
|
| 1129 |
"Order_Details__c": order_details,
|
| 1130 |
"Table_Number__c": table_number # Store table number
|
| 1131 |
}
|
| 1132 |
+
print(f"Order Data: {order_data}") # Debugging order data
|
| 1133 |
|
| 1134 |
# Create the order in Salesforce
|
| 1135 |
order_response = sf.Order__c.create(order_data)
|
| 1136 |
+
print(f"Order Response: {order_response}") # Debugging order creation response
|
| 1137 |
|
| 1138 |
# Ensure the order was created successfully before deleting cart items
|
| 1139 |
if order_response:
|
| 1140 |
# Only delete cart items after the order is created
|
| 1141 |
for item in cart_items:
|
| 1142 |
+
print(f"Deleting Cart Item: {item['Id']}") # Debugging cart item deletion
|
| 1143 |
sf.Cart_Item__c.delete(item["Id"])
|
| 1144 |
|
| 1145 |
return jsonify({"success": True, "message": "Order placed successfully!", "discount": discount, "totalBill": total_bill})
|
| 1146 |
|
| 1147 |
except Exception as e:
|
| 1148 |
+
print(f"Error during checkout: {str(e)}") # Debugging error message
|
| 1149 |
return jsonify({"success": False, "error": str(e)})
|
| 1150 |
|
| 1151 |
|
| 1152 |
+
|
| 1153 |
@app.route("/order", methods=["GET"])
|
| 1154 |
def order_summary():
|
| 1155 |
email = session.get('user_email') # Fetch logged-in user's email
|