File size: 4,080 Bytes
cad40f7
fd5c6b1
 
 
cad40f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# views/cart_page.py

from flask import render_template, session, redirect, url_for
import salesforce  # Make sure you have your Salesforce connection here

def cart_page():
    email = session.get('user_email')
    if not email:
        return redirect(url_for("login"))

    try:
        # Fetch cart items with Category and Section
        result = salesforce.sf.query(f"""
            SELECT Name, Price__c, Quantity__c, Add_Ons__c, Add_Ons_Price__c, Image1__c, Instructions__c, Category__c, Section__c
            FROM Cart_Item__c
            WHERE Customer_Email__c = '{email}'
        """)
        cart_items = result.get("records", [])

        subtotal = sum(item['Price__c'] for item in cart_items)

        # Fetch reward points
        customer_result = salesforce.sf.query(f"""
            SELECT Reward_Points__c 
            FROM Customer_Login__c
            WHERE Email__c = '{email}'
        """)
        reward_points = customer_result['records'][0].get('Reward_Points__c', 0) if customer_result['records'] else 0

        # Fetch coupons for the user
        coupon_result = salesforce.sf.query(f"""
            SELECT Coupon_Code__c FROM Referral_Coupon__c WHERE Referral_Email__c = '{email}'
        """)
        coupons = []
        if coupon_result["records"]:
            raw_coupons = coupon_result["records"][0].get("Coupon_Code__c", "")
            coupons = raw_coupons.split("\n") if raw_coupons else []

        # Initialize suggestions as an empty list
        suggestions = []

        # If there are items in the cart, fetch suggestions
        if cart_items:
            first_item = cart_items[0]
            item_category = first_item.get('Category__c', 'All')  # Default to 'All' if not found
            item_section = first_item.get('Section__c', 'Biryanis')  # Default to 'Biryanis' if not found

            # Define section-to-complementary section mapping
            complementary_sections = {
                'Breads': ['Curries', 'Biryanis', 'Starters'],
                'Biryanis': ['Curries', 'Starters', 'Desserts'],
                'Curries': ['Biryanis', 'Breads', 'Starters'],
                'Starters': ['Biryanis', 'Curries', 'Desserts'],
                'Desserts': ['Biryanis', 'Curries', 'Soft Drinks'],
                'Soft Drinks': ['Starters', 'Biryanis', 'Curries']
            }

            suggested_sections = complementary_sections.get(item_section, [])

            try:
                for suggested_section in suggested_sections:
                    if item_category == "All":
                        query = f"""
                            SELECT Name, Price__c, Image1__c
                            FROM Menu_Item__c
                            WHERE Section__c = '{suggested_section}' 
                            AND (Veg_NonVeg__c = 'Veg' OR Veg_NonVeg__c = 'Non veg')
                            LIMIT 4
                        """
                    else:
                        query = f"""
                            SELECT Name, Price__c, Image1__c
                            FROM Menu_Item__c
                            WHERE Section__c = '{suggested_section}' 
                            AND Veg_NonVeg__c = '{item_category}'
                            LIMIT 4
                        """
                    suggestion_result = salesforce.sf.query(query)
                    suggestions.extend(suggestion_result.get("records", []))

                if len(suggestions) > 4:
                    suggestions = suggestions[:4]

            except Exception as e:
                print(f"Error fetching suggestions: {e}")

        return render_template(
            "cart.html",
            cart_items=cart_items,
            subtotal=subtotal,
            reward_points=reward_points,
            customer_email=email,
            coupons=coupons,
            suggestions=suggestions
        )

    except Exception as e:
        print(f"Error fetching cart items: {e}")
        return render_template("cart.html", cart_items=[], subtotal=0, reward_points=0, coupons=[], suggestions=[])