nagasurendra's picture
Update order.py
6d168f2 verified
raw
history blame
3.1 kB
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 the most recent order 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}'
ORDER BY CreatedDate DESC
LIMIT 1
""")
order = result.get("records", [])[0] if result.get("records") else None
if not order:
return render_template("order.html", order=None)
return render_template("order.html", order=order)
except Exception as e:
print(f"Error fetching order details: {str(e)}")
return render_template("order.html", order=None, error=str(e))
@order_blueprint.route("/order/items", methods=["GET"])
def order_items():
email = session.get('user_email') # Fetch logged-in user's email
if not email:
print("No user email found. Redirecting to login.")
return redirect(url_for("login"))
try:
# Fetch the most recent order for the user
print(f"Fetching order details for email: {email}")
result = sf.query(f"""
SELECT Id, Order_Details__c
FROM Order__c
WHERE Customer_Email__c = '{email}'
ORDER BY CreatedDate DESC
LIMIT 1
""")
# Check if the query was successful and print the result
print("Query result:", result)
order = result.get("records", [])[0] if result.get("records") else None
if not order:
print("No order found for the user.")
return render_template("order_items.html", items=None)
# Extract item names from the order details
item_names = []
if order.get("Order_Details__c"):
print("Found order details. Extracting item names.")
for line in order["Order_Details__c"].split('\n'):
item_parts = line.split('|')
if item_parts:
item_name = item_parts[0].strip() # Assuming the item name is the first part
item_names.append(item_name)
print(f"Extracted item: {item_name}") # Print each extracted item name
if item_names:
print(f"Total items extracted: {len(item_names)}")
else:
print("No items found in order details.")
return render_template("order_items.html", items=item_names)
except Exception as e:
print(f"Error fetching order details: {str(e)}")
return render_template("order_items.html", items=None, error=str(e))