nagasurendra commited on
Commit
06acbd0
·
verified ·
1 Parent(s): 3f9649c

Update order.py

Browse files
Files changed (1) hide show
  1. order.py +19 -38
order.py CHANGED
@@ -5,6 +5,8 @@ order_blueprint = Blueprint('order', __name__)
5
 
6
  # Initialize Salesforce connection
7
  sf = get_salesforce_connection()
 
 
8
  @order_blueprint.route("/order", methods=["GET"])
9
  def order_summary():
10
  email = session.get('user_email') # Fetch logged-in user's email
@@ -12,48 +14,27 @@ def order_summary():
12
  return redirect(url_for("login"))
13
 
14
  try:
15
- # Fetch the most recent order for the user
16
  result = sf.query(f"""
17
- SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, Billing_Status__c
 
18
  FROM Order__c
19
- WHERE Customer_Email__c = '{email}'
20
  ORDER BY CreatedDate DESC
21
- LIMIT 2
22
  """)
23
-
24
  orders = result.get("records", [])
25
- current_order = orders[0] if orders else None
26
- previous_order = orders[1] if len(orders) > 1 else None
27
-
28
- # Check if previous order exists and if Billing_Status is Pending
29
- if previous_order and previous_order.get('Billing_Status__c') == 'Pending':
30
- # Combine details from previous order with the current order
31
- combined_order_details = f"{previous_order['Order_Details__c']}\n{current_order['Order_Details__c']}"
32
- combined_total = previous_order['Total_Bill__c'] + current_order['Total_Bill__c']
33
- combined_discount = previous_order['Discount__c'] + current_order['Discount__c']
34
- combined_total_bill = combined_total - combined_discount
35
-
36
- # Update the previous order's Billing Status to "Completed" after combining the details
37
- sf.update('Order__c', previous_order['Id'], {
38
- 'Billing_Status__c': 'Completed',
39
- })
40
-
41
- # Prepare the combined order object
42
- order = {
43
- 'Order_Details__c': combined_order_details,
44
- 'Total_Amount__c': current_order['Total_Amount__c'],
45
- 'Discount__c': combined_discount,
46
- 'Total_Bill__c': combined_total_bill,
47
- 'Order_Status__c': current_order['Order_Status__c'],
48
- 'Billing_Status__c': 'Completed', # Mark as completed
49
- }
50
-
51
- else:
52
- # No previous order with pending billing status
53
- order = current_order
54
-
55
- return render_template("order.html", order=order)
56
-
57
  except Exception as e:
58
  print(f"Error fetching order details: {str(e)}")
59
- return render_template("order.html", order=None, error=str(e))
 
 
5
 
6
  # Initialize Salesforce connection
7
  sf = get_salesforce_connection()
8
+
9
+
10
  @order_blueprint.route("/order", methods=["GET"])
11
  def order_summary():
12
  email = session.get('user_email') # Fetch logged-in user's email
 
14
  return redirect(url_for("login"))
15
 
16
  try:
17
+ # Fetch all pending orders for the user
18
  result = sf.query(f"""
19
+ SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
20
+ Order_Status__c, Discount__c, Total_Bill__c
21
  FROM Order__c
22
+ WHERE Customer_Email__c = '{email}' AND Billing_Status__c = 'pending'
23
  ORDER BY CreatedDate DESC
 
24
  """)
 
25
  orders = result.get("records", [])
26
+
27
+ if not orders:
28
+ return render_template("order.html", orders=None, total_subtotal=0, total_discount=0, total_bill=0)
29
+
30
+ # Calculate aggregate totals
31
+ total_subtotal = sum(float(order['Total_Amount__c']) for order in orders)
32
+ total_discount = sum(float(order['Discount__c']) for order in orders)
33
+ total_bill = sum(float(order['Total_Bill__c']) for order in orders)
34
+
35
+ return render_template("order.html", orders=orders, total_subtotal=total_subtotal,
36
+ total_discount=total_discount, total_bill=total_bill)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  except Exception as e:
38
  print(f"Error fetching order details: {str(e)}")
39
+ return render_template("order.html", orders=None, total_subtotal=0, total_discount=0,
40
+ total_bill=0, error=str(e))