Spaces:
Sleeping
Sleeping
Update order.py
Browse files
order.py
CHANGED
@@ -1,14 +1,10 @@
|
|
1 |
-
from flask import Blueprint, render_template, request, session, jsonify
|
2 |
from salesforce import get_salesforce_connection
|
3 |
-
from io import BytesIO
|
4 |
-
from reportlab.lib.pagesizes import letter
|
5 |
-
from reportlab.pdfgen import canvas
|
6 |
|
7 |
order_blueprint = Blueprint('order', __name__)
|
8 |
|
9 |
# Initialize Salesforce connection
|
10 |
sf = get_salesforce_connection()
|
11 |
-
|
12 |
@order_blueprint.route("/order", methods=["GET"])
|
13 |
def order_summary():
|
14 |
email = session.get('user_email') # Fetch logged-in user's email
|
@@ -34,50 +30,8 @@ def order_summary():
|
|
34 |
print(f"Error fetching order details: {str(e)}")
|
35 |
return render_template("order.html", order=None, error=str(e))
|
36 |
|
37 |
-
# New route to generate the invoice
|
38 |
-
@order_blueprint.route("/generate_invoice/<order_id>", methods=["GET"])
|
39 |
-
def generate_invoice(order_id):
|
40 |
-
email = session.get('user_email') # Fetch logged-in user's email
|
41 |
-
if not email:
|
42 |
-
return jsonify({"success": False, "message": "User not logged in"}), 400
|
43 |
-
|
44 |
-
try:
|
45 |
-
# Fetch order details from Salesforce
|
46 |
-
result = sf.query(f"""
|
47 |
-
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c
|
48 |
-
FROM Order__c
|
49 |
-
WHERE Id = '{order_id}'
|
50 |
-
""")
|
51 |
-
order = result.get("records", [])[0] if result.get("records") else None
|
52 |
|
53 |
-
if not order:
|
54 |
-
return jsonify({"success": False, "message": "Order not found"}), 404
|
55 |
|
56 |
-
# Create PDF invoice
|
57 |
-
buffer = BytesIO()
|
58 |
-
c = canvas.Canvas(buffer, pagesize=letter)
|
59 |
-
c.drawString(100, 750, f"Invoice for Order ID: {order_id}")
|
60 |
-
c.drawString(100, 730, f"Customer Name: {order['Customer_Name__c']}")
|
61 |
-
c.drawString(100, 710, f"Customer Email: {order['Customer_Email__c']}")
|
62 |
-
c.drawString(100, 690, f"Total Amount: ${order['Total_Amount__c']}")
|
63 |
-
c.drawString(100, 670, f"Discount: ${order['Discount__c']}")
|
64 |
-
c.drawString(100, 650, f"Total Bill: ${order['Total_Bill__c']}")
|
65 |
-
|
66 |
-
# Add order details (split by new lines)
|
67 |
-
order_details = order.get("Order_Details__c", "").split("\n")
|
68 |
-
y_position = 630
|
69 |
-
for line in order_details:
|
70 |
-
c.drawString(100, y_position, line)
|
71 |
-
y_position -= 20
|
72 |
|
73 |
-
# Finalize the PDF
|
74 |
-
c.showPage()
|
75 |
-
c.save()
|
76 |
|
77 |
-
|
78 |
-
buffer.seek(0)
|
79 |
-
return send_file(buffer, as_attachment=True, download_name=f"invoice_{order_id}.pdf", mimetype="application/pdf")
|
80 |
-
|
81 |
-
except Exception as e:
|
82 |
-
print(f"Error generating invoice: {str(e)}")
|
83 |
-
return jsonify({"success": False, "message": str(e)}), 500
|
|
|
1 |
+
from flask import Blueprint, render_template, request, session, jsonify # Added jsonify import
|
2 |
from salesforce import get_salesforce_connection
|
|
|
|
|
|
|
3 |
|
4 |
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
|
|
|
30 |
print(f"Error fetching order details: {str(e)}")
|
31 |
return render_template("order.html", order=None, error=str(e))
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
|
|
|
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
36 |
|
37 |
+
|
|
|
|
|
|
|
|
|
|
|
|