nagasurendra's picture
Update order.py
13d0646 verified
raw
history blame
2.68 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, Billing_Status__c
FROM Order__c
WHERE Customer_Email__c = '{email}'
ORDER BY CreatedDate DESC
LIMIT 2
""")
orders = result.get("records", [])
current_order = orders[0] if orders else None
previous_order = orders[1] if len(orders) > 1 else None
# Check if previous order exists and if Billing_Status is Pending
if previous_order and previous_order.get('Billing_Status__c') == 'Pending':
# Combine details from previous order with the current order
combined_order_details = f"{previous_order['Order_Details__c']}\n{current_order['Order_Details__c']}"
combined_total = previous_order['Total_Bill__c'] + current_order['Total_Bill__c']
combined_discount = previous_order['Discount__c'] + current_order['Discount__c']
combined_total_bill = combined_total - combined_discount
# Update the previous order's Billing Status to "Completed" after combining the details
sf.update('Order__c', previous_order['Id'], {
'Billing_Status__c': 'Completed',
})
# Prepare the combined order object
order = {
'Order_Details__c': combined_order_details,
'Total_Amount__c': current_order['Total_Amount__c'],
'Discount__c': combined_discount,
'Total_Bill__c': combined_total_bill,
'Order_Status__c': current_order['Order_Status__c'],
'Billing_Status__c': 'Completed', # Mark as completed
}
else:
# No previous order with pending billing status
order = current_order
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))