Rammohan0504 commited on
Commit
b12635f
·
verified ·
1 Parent(s): b29184c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -2
app.py CHANGED
@@ -516,7 +516,8 @@ def apply_coupon():
516
  data = request.json
517
  coupon_code = data.get('coupon_code')
518
  subtotal = data.get('subtotal')
519
-
 
520
  if not coupon_code:
521
  return jsonify({'success': False, 'message': 'Coupon code is required.'})
522
 
@@ -530,9 +531,36 @@ def apply_coupon():
530
 
531
  # Apply a fixed 10% discount
532
  discount_percentage = 0.10 # 10% discount
 
533
  discount_amount = subtotal * discount_percentage
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534
 
535
- return jsonify({'success': True, 'discount': discount_amount})
536
 
537
  except Exception as e:
538
  return jsonify({'success': False, 'message': f'Error applying coupon: {str(e)}'})
 
516
  data = request.json
517
  coupon_code = data.get('coupon_code')
518
  subtotal = data.get('subtotal')
519
+ email = data.get('email')
520
+
521
  if not coupon_code:
522
  return jsonify({'success': False, 'message': 'Coupon code is required.'})
523
 
 
531
 
532
  # Apply a fixed 10% discount
533
  discount_percentage = 0.10 # 10% discount
534
+
535
  discount_amount = subtotal * discount_percentage
536
+
537
+ final_total = subtotal - discount_amount
538
+
539
+ order_query = f"""
540
+ SELECT Id, Total_Amount__c, Offers_Applied__c
541
+ FROM Order__c
542
+ WHERE Customer_Email__c = '{email}' AND Order_Status__c = 'Pending'
543
+ ORDER BY CreatedDate DESC
544
+ LIMIT 1
545
+ """
546
+ order_result = sf.query(order_query)
547
+
548
+ if order_result.get("records"):
549
+ order_id = order_result["records"][0]["Id"]
550
+
551
+ # Update the order with the new total amount and offer applied
552
+ sf.Order__c.update(order_id, {
553
+ "Total_Amount__c": final_total,
554
+ "Offers_Applied__c": f"Discount Applied: ${discount_amount:.2f}" # Save the discount applied in Offers_Applied
555
+ })
556
+
557
+ return jsonify({
558
+ 'success': True,
559
+ 'discount': discount_amount,
560
+ 'final_total': final_total,
561
+ 'message': 'Coupon applied successfully.'
562
+ })
563
 
 
564
 
565
  except Exception as e:
566
  return jsonify({'success': False, 'message': f'Error applying coupon: {str(e)}'})