Subbu1304 commited on
Commit
eff2d36
·
verified ·
1 Parent(s): 9f19f0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -34
app.py CHANGED
@@ -1,34 +1,74 @@
1
- from flask import Flask, render_template, request, make_response
2
- import weasyprint # Required for PDF generation
3
-
4
- app = Flask(__name__)
5
-
6
- # Example route for rendering the order summary page
7
- @app.route("/order-summary", methods=["GET", "POST"])
8
- def order_summary():
9
- # Simulate some order data to be passed to the template (this can be dynamic data)
10
- order_data = {
11
- 'Order_Details__c': "1 x Zafrani Chicken 65 Biryani|Add-On: None|No special instructions|Price: 369",
12
- 'Total_Amount__c': 369.00,
13
- 'Discount__c': 0.00,
14
- 'Total_Bill__c': 387.45
15
- }
16
- return render_template("order_summary.html", order=order_data)
17
-
18
- # Route for creating the invoice PDF
19
- @app.route("/generate-invoice", methods=["POST"])
20
- def generate_invoice():
21
- order_data = request.form
22
- html = render_template("invoice_template.html", order=order_data) # Generate HTML using template
23
-
24
- # Convert the HTML to PDF using WeasyPrint
25
- pdf = weasyprint.HTML(string=html).write_pdf()
26
-
27
- # Send the PDF as a downloadable response
28
- response = make_response(pdf)
29
- response.headers['Content-Type'] = 'application/pdf'
30
- response.headers['Content-Disposition'] = 'inline; filename=invoice.pdf'
31
- return response
32
-
33
- if __name__ == "__main__":
34
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Blueprint, render_template, request,redirect, 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))