|  | from flask import Blueprint, render_template, request,redirect, session, jsonify | 
					
						
						|  | from salesforce import get_salesforce_connection | 
					
						
						|  | from datetime import datetime | 
					
						
						|  | import pytz | 
					
						
						|  |  | 
					
						
						|  | orderhistory_blueprint = Blueprint('orderhistory', __name__) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | sf = get_salesforce_connection() | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | @orderhistory_blueprint.route("/order-history", methods=["GET"]) | 
					
						
						|  | def order_history(): | 
					
						
						|  | 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, CreatedDate | 
					
						
						|  | FROM Order__c | 
					
						
						|  | WHERE Customer_Email__c = '{email}' | 
					
						
						|  | ORDER BY CreatedDate DESC | 
					
						
						|  | """) | 
					
						
						|  |  | 
					
						
						|  | print(f"Salesforce query result: {result}") | 
					
						
						|  |  | 
					
						
						|  | orders = result.get("records", []) | 
					
						
						|  |  | 
					
						
						|  | if not orders: | 
					
						
						|  | print("No orders found for this email.") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for order in orders: | 
					
						
						|  | order_details = order.get("Order_Details__c", "") | 
					
						
						|  | items = order_details.split("\n") | 
					
						
						|  | formatted_items = [] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | for item in items: | 
					
						
						|  | item_details = item.split(" | ") | 
					
						
						|  | if len(item_details) > 1: | 
					
						
						|  | name = item_details[0].strip() | 
					
						
						|  | quantity = item_details[1].strip() | 
					
						
						|  | formatted_items.append(f"{name} * {quantity}") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | order['formatted_items'] = ", ".join(formatted_items) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | created_date = order.get("CreatedDate", "") | 
					
						
						|  | if created_date: | 
					
						
						|  |  | 
					
						
						|  | utc_datetime = datetime.strptime(created_date, '%Y-%m-%dT%H:%M:%S.000+0000') | 
					
						
						|  | utc_datetime = utc_datetime.replace(tzinfo=pytz.UTC) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | local_timezone = pytz.timezone('Asia/Kolkata') | 
					
						
						|  | local_datetime = utc_datetime.astimezone(local_timezone) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | order['formatted_date'] = local_datetime.strftime('%B %d, %I:%M %p') | 
					
						
						|  |  | 
					
						
						|  | order_status = order.get("Order_Status__c", "N/A") | 
					
						
						|  | order['order_status'] = order_status | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | return render_template("order_history.html", orders=orders) | 
					
						
						|  |  | 
					
						
						|  | except Exception as e: | 
					
						
						|  | print(f"Error fetching order history: {str(e)}") | 
					
						
						|  | return render_template("order_history.html", orders=[], error=str(e)) | 
					
						
						|  |  | 
					
						
						|  |  |