|
from flask import Blueprint, render_template, request, session, jsonify |
|
from salesforce import get_salesforce_connection |
|
|
|
order_blueprint = Blueprint('order', __name__) |
|
|
|
|
|
sf = get_salesforce_connection() |
|
@order_blueprint.route("/order", methods=["GET"]) |
|
def order_summary(): |
|
email = session.get('user_email') |
|
if not email: |
|
return redirect(url_for("login")) |
|
|
|
try: |
|
|
|
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') |
|
if not email: |
|
print("No user email found. Redirecting to login.") |
|
return redirect(url_for("login")) |
|
|
|
try: |
|
|
|
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 |
|
""") |
|
|
|
|
|
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) |
|
|
|
|
|
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() |
|
item_names.append(item_name) |
|
print(f"Extracted item: {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)) |
|
|