nagasurendra commited on
Commit
b95cfbd
·
verified ·
1 Parent(s): 7bec215

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -149,7 +149,9 @@ def login():
149
  print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
150
 
151
  user_name = user.get("Name", "") # Get the user's name
152
- reward_points = user.get("Reward_Points__c", 0)
 
 
153
 
154
  if reward_points >= 500:
155
  print(f"User {email} has {reward_points} reward points. Generating coupon...")
@@ -536,7 +538,7 @@ def checkout():
536
 
537
  # Fetch cart items
538
  result = sf.query(f"""
539
- SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c
540
  FROM Cart_Item__c
541
  WHERE Customer_Email__c = '{email}'
542
  """)
@@ -556,15 +558,13 @@ def checkout():
556
  has_coupons = bool(coupon_query["records"]) # Check if user has any coupons
557
 
558
  if selected_coupon:
559
- # Case 3: User selected a valid coupon → Apply discount & remove coupon
560
  discount = total_price * 0.10 # 10% discount
561
  referral_coupon_id = coupon_query["records"][0]["Id"]
562
  existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
563
 
564
  # Remove only the selected coupon
565
  updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
566
-
567
- # Convert list back to a string with newlines
568
  updated_coupons_str = "\n".join(updated_coupons).strip()
569
 
570
  # Update the Referral_Coupon__c record with the remaining coupons
@@ -572,7 +572,7 @@ def checkout():
572
  "Coupon_Code__c": updated_coupons_str
573
  })
574
  else:
575
- # Case 1 & Case 2: User has no coupons or has coupons but didn’t select one → Add 10% to reward points
576
  reward_points_to_add = total_price * 0.10
577
 
578
  # Fetch current reward points
@@ -586,17 +586,17 @@ def checkout():
586
  current_reward_points = customer.get("Reward_Points__c") or 0
587
  new_reward_points = current_reward_points + reward_points_to_add
588
 
589
- print(f"Updating reward points: Current = {current_reward_points}, Adding = {reward_points_to_add}, New = {new_reward_points}")
590
-
591
  # Update reward points in Salesforce
592
  sf.Customer_Login__c.update(customer["Id"], {
593
  "Reward_Points__c": new_reward_points
594
  })
595
- print(f"Successfully updated reward points for {email}")
596
 
597
  total_bill = total_price - discount
598
 
599
- # Store the order details
 
 
 
600
  order_data = {
601
  "Customer_Name__c": user_id,
602
  "Customer_Email__c": email,
@@ -604,18 +604,14 @@ def checkout():
604
  "Discount__c": discount,
605
  "Total_Bill__c": total_bill,
606
  "Order_Status__c": "Pending",
607
- "Order_Details__c": "\n".join(f"{item['Name']} x{item['Quantity__c']}" for item in cart_items)
608
  }
609
 
610
  sf.Order__c.create(order_data)
611
 
612
- # ✅ Fix: Check if cart items exist before deleting
613
- if cart_items:
614
- for item in cart_items:
615
- try:
616
- sf.Cart_Item__c.delete(item["Id"])
617
- except Exception as e:
618
- print(f"Error deleting cart item {item['Id']}: {str(e)}")
619
 
620
  return jsonify({"success": True, "message": "Order placed successfully!"})
621
 
 
149
  print(f"Session variables set: user_id={session['user_id']}, user_email={session['user_email']}")
150
 
151
  user_name = user.get("Name", "") # Get the user's name
152
+ reward_points = user.get("Reward_Points__c") or 0 # Ensures reward_points is always an integer
153
+
154
+
155
 
156
  if reward_points >= 500:
157
  print(f"User {email} has {reward_points} reward points. Generating coupon...")
 
538
 
539
  # Fetch cart items
540
  result = sf.query(f"""
541
+ SELECT Id, Name, Price__c, Add_Ons_Price__c, Quantity__c, Add_Ons__c, Instructions__c, Image1__c
542
  FROM Cart_Item__c
543
  WHERE Customer_Email__c = '{email}'
544
  """)
 
558
  has_coupons = bool(coupon_query["records"]) # Check if user has any coupons
559
 
560
  if selected_coupon:
561
+ # Apply discount & remove coupon
562
  discount = total_price * 0.10 # 10% discount
563
  referral_coupon_id = coupon_query["records"][0]["Id"]
564
  existing_coupons = coupon_query["records"][0]["Coupon_Code__c"].split("\n")
565
 
566
  # Remove only the selected coupon
567
  updated_coupons = [coupon for coupon in existing_coupons if coupon.strip() != selected_coupon]
 
 
568
  updated_coupons_str = "\n".join(updated_coupons).strip()
569
 
570
  # Update the Referral_Coupon__c record with the remaining coupons
 
572
  "Coupon_Code__c": updated_coupons_str
573
  })
574
  else:
575
+ # Add 10% to reward points if no coupon is used
576
  reward_points_to_add = total_price * 0.10
577
 
578
  # Fetch current reward points
 
586
  current_reward_points = customer.get("Reward_Points__c") or 0
587
  new_reward_points = current_reward_points + reward_points_to_add
588
 
 
 
589
  # Update reward points in Salesforce
590
  sf.Customer_Login__c.update(customer["Id"], {
591
  "Reward_Points__c": new_reward_points
592
  })
 
593
 
594
  total_bill = total_price - discount
595
 
596
+ # Store Order Details (Including Images)
597
+ order_details = "\n".join(f"{item['Name']} x{item['Quantity__c']}|{item['Image1__c']}" for item in cart_items)
598
+
599
+ # Store the order details in Order__c
600
  order_data = {
601
  "Customer_Name__c": user_id,
602
  "Customer_Email__c": email,
 
604
  "Discount__c": discount,
605
  "Total_Bill__c": total_bill,
606
  "Order_Status__c": "Pending",
607
+ "Order_Details__c": order_details # Updated to include images
608
  }
609
 
610
  sf.Order__c.create(order_data)
611
 
612
+ # ✅ Delete cart items after order is placed
613
+ for item in cart_items:
614
+ sf.Cart_Item__c.delete(item["Id"])
 
 
 
 
615
 
616
  return jsonify({"success": True, "message": "Order placed successfully!"})
617