Spaces:
Sleeping
Sleeping
Update orderhistory.py
Browse files- orderhistory.py +48 -2
orderhistory.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
-
from flask import Blueprint, render_template, request,redirect, session, jsonify
|
| 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 |
|
|
@@ -66,9 +68,53 @@ def order_history():
|
|
| 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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Blueprint, render_template, request, redirect, session, jsonify, send_file, url_for
|
| 2 |
from salesforce import get_salesforce_connection
|
| 3 |
from datetime import datetime
|
| 4 |
import pytz # Library to handle timezone conversions
|
| 5 |
+
from weasyprint import HTML
|
| 6 |
+
from io import BytesIO
|
| 7 |
|
| 8 |
orderhistory_blueprint = Blueprint('orderhistory', __name__)
|
| 9 |
|
|
|
|
| 68 |
order_status = order.get("Order_Status__c", "N/A") # Default to "N/A" if no status
|
| 69 |
order['order_status'] = order_status
|
| 70 |
|
|
|
|
| 71 |
return render_template("order_history.html", orders=orders)
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
print(f"Error fetching order history: {str(e)}")
|
| 75 |
return render_template("order_history.html", orders=[], error=str(e))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
@orderhistory_blueprint.route("/generate_invoice/<order_id>", methods=["GET"])
|
| 79 |
+
def generate_invoice(order_id):
|
| 80 |
+
email = session.get('user_email') # Get logged-in user's email
|
| 81 |
+
if not email:
|
| 82 |
+
return jsonify({"success": False, "message": "User not logged in"}), 400
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
# Fetch order details from Salesforce
|
| 86 |
+
result = sf.query(f"""
|
| 87 |
+
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
|
| 88 |
+
Order_Status__c, Discount__c, Total_Bill__c, CreatedDate
|
| 89 |
+
FROM Order__c
|
| 90 |
+
WHERE Id = '{order_id}'
|
| 91 |
+
""")
|
| 92 |
+
order = result.get("records", [])[0] if result.get("records") else None
|
| 93 |
+
|
| 94 |
+
if not order:
|
| 95 |
+
return jsonify({"success": False, "message": "Order not found"}), 404
|
| 96 |
+
|
| 97 |
+
# Format order details for better readability
|
| 98 |
+
order_details = order.get("Order_Details__c", "")
|
| 99 |
+
items = order_details.split("\n")
|
| 100 |
+
formatted_items = []
|
| 101 |
+
|
| 102 |
+
for item in items:
|
| 103 |
+
item_details = item.split(" | ")
|
| 104 |
+
if len(item_details) > 1:
|
| 105 |
+
name = item_details[0].strip()
|
| 106 |
+
quantity = item_details[1].strip()
|
| 107 |
+
formatted_items.append(f"{name} * {quantity}")
|
| 108 |
+
|
| 109 |
+
# Render HTML invoice template
|
| 110 |
+
rendered_html = render_template('invoice.html', order=order, formatted_items=formatted_items)
|
| 111 |
+
|
| 112 |
+
# Convert HTML to PDF using WeasyPrint
|
| 113 |
+
pdf = HTML(string=rendered_html).write_pdf()
|
| 114 |
+
|
| 115 |
+
# Send the PDF as a downloadable file
|
| 116 |
+
return send_file(BytesIO(pdf), as_attachment=True, download_name=f"invoice_{order_id}.pdf", mimetype="application/pdf")
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Error generating invoice: {str(e)}")
|
| 120 |
+
return jsonify({"success": False, "message": str(e)}), 500
|