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