nagasurendra's picture
Update order.py
06acbd0 verified
from flask import Blueprint, render_template, request, session, jsonify # Added jsonify import
from salesforce import get_salesforce_connection
order_blueprint = Blueprint('order', __name__)
# Initialize Salesforce connection
sf = get_salesforce_connection()
@order_blueprint.route("/order", methods=["GET"])
def order_summary():
email = session.get('user_email') # Fetch logged-in user's email
if not email:
return redirect(url_for("login"))
try:
# Fetch all pending orders for the user
result = sf.query(f"""
SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
Order_Status__c, Discount__c, Total_Bill__c
FROM Order__c
WHERE Customer_Email__c = '{email}' AND Billing_Status__c = 'pending'
ORDER BY CreatedDate DESC
""")
orders = result.get("records", [])
if not orders:
return render_template("order.html", orders=None, total_subtotal=0, total_discount=0, total_bill=0)
# Calculate aggregate totals
total_subtotal = sum(float(order['Total_Amount__c']) for order in orders)
total_discount = sum(float(order['Discount__c']) for order in orders)
total_bill = sum(float(order['Total_Bill__c']) for order in orders)
return render_template("order.html", orders=orders, total_subtotal=total_subtotal,
total_discount=total_discount, total_bill=total_bill)
except Exception as e:
print(f"Error fetching order details: {str(e)}")
return render_template("order.html", orders=None, total_subtotal=0, total_discount=0,
total_bill=0, error=str(e))