Rammohan0504 commited on
Commit
24f6002
·
verified ·
1 Parent(s): e90c877

Update orderhistory.py

Browse files
Files changed (1) hide show
  1. orderhistory.py +56 -12
orderhistory.py CHANGED
@@ -1,31 +1,75 @@
1
  from flask import Blueprint, render_template, request, session, jsonify # Added jsonify import
2
  from salesforce import get_salesforce_connection
 
 
3
 
4
  orderhistory_blueprint = Blueprint('orderhistory', __name__)
5
 
6
  # Initialize Salesforce connection
7
  sf = get_salesforce_connection()
8
- @orderhistory_blueprint.route("/orderhistory", methods=["GET"])
9
- def order_summary():
10
- email = session.get('user_email') # Fetch logged-in user's email
 
 
11
  if not email:
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
 
18
  FROM Order__c
19
  WHERE Customer_Email__c = '{email}'
20
  ORDER BY CreatedDate DESC
21
- LIMIT 1
22
  """)
23
- order = result.get("records", [])[0] if result.get("records") else None
24
 
25
- if not order:
26
- return render_template("order.html", order=None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- return render_template("order.html", order=order)
29
  except Exception as e:
30
- print(f"Error fetching order details: {str(e)}")
31
- return render_template("order.html", order=None, error=str(e))
 
 
1
  from flask import Blueprint, render_template, request, session, jsonify # Added jsonify import
2
  from salesforce import get_salesforce_connection
3
+ from datetime import datetime
4
+ import pytz # Library to handle timezone conversions
5
 
6
  orderhistory_blueprint = Blueprint('orderhistory', __name__)
7
 
8
  # Initialize Salesforce connection
9
  sf = get_salesforce_connection()
10
+
11
+
12
+ @orderhistory_blueprint.route("/order-history", methods=["GET"])
13
+ def order_history():
14
+ email = session.get('user_email') # Get logged-in user's email
15
  if not email:
16
  return redirect(url_for("login"))
17
 
18
  try:
19
+ # Fetch past orders for the user
20
  result = sf.query(f"""
21
+ SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c,
22
+ Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c, CreatedDate
23
  FROM Order__c
24
  WHERE Customer_Email__c = '{email}'
25
  ORDER BY CreatedDate DESC
 
26
  """)
 
27
 
28
+ print(f"Salesforce query result: {result}") # Debugging line
29
+
30
+ orders = result.get("records", []) # Fetch all orders
31
+
32
+ if not orders:
33
+ print("No orders found for this email.") # Debugging line
34
+
35
+ # Format the order details for better readability
36
+ for order in orders:
37
+ order_details = order.get("Order_Details__c", "")
38
+ items = order_details.split("\n") # Assuming each item is separated by a new line
39
+ formatted_items = []
40
+
41
+ # Loop through the items and format them as "item name * quantity"
42
+ for item in items:
43
+ item_details = item.split(" | ")
44
+ if len(item_details) > 1:
45
+ name = item_details[0].strip()
46
+ quantity = item_details[1].strip()
47
+ formatted_items.append(f"{name} * {quantity}")
48
+
49
+ # Join the formatted items into a single string
50
+ order['formatted_items'] = ", ".join(formatted_items)
51
+
52
+ # Get the order date and time from CreatedDate
53
+ created_date = order.get("CreatedDate", "")
54
+ if created_date:
55
+ # Convert CreatedDate to datetime object in UTC
56
+ utc_datetime = datetime.strptime(created_date, '%Y-%m-%dT%H:%M:%S.000+0000')
57
+ utc_datetime = utc_datetime.replace(tzinfo=pytz.UTC)
58
+
59
+ # Convert UTC datetime to the desired timezone (e.g., IST)
60
+ local_timezone = pytz.timezone('Asia/Kolkata') # Replace with your timezone
61
+ local_datetime = utc_datetime.astimezone(local_timezone)
62
+
63
+ # Format the date and time in the desired format
64
+ order['formatted_date'] = local_datetime.strftime('%B %d, %I:%M %p')
65
+
66
+ order_status = order.get("Order_Status__c", "N/A") # Default to "N/A" if no status
67
+ order['order_status'] = order_status
68
+
69
+
70
+ return render_template("order_history.html", orders=orders)
71
 
 
72
  except Exception as e:
73
+ print(f"Error fetching order history: {str(e)}")
74
+ return render_template("order_history.html", orders=[], error=str(e))
75
+