File size: 2,680 Bytes
46f20e2
60c7f69
 
 
 
 
 
97e16dc
60c7f69
 
 
 
 
 
 
 
7e82995
60c7f69
 
 
13d0646
60c7f69
13d0646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60c7f69
13d0646
 
 
 
 
7e82995
60c7f69
 
7e82995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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))