geethareddy commited on
Commit
ac4d6d8
·
verified ·
1 Parent(s): 0fe51ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -10
app.py CHANGED
@@ -64,33 +64,59 @@ def load_menu_from_salesforce():
64
  except Exception as e:
65
  return []
66
 
 
 
 
 
 
 
 
 
 
67
  # Cart Management Functions
68
  cart = []
69
  total_cart_cost = 0
 
70
 
71
  # Function to add item to the cart and Salesforce
72
- def add_to_cart(item_name, item_price, quantity, special_instructions):
73
- global total_cart_cost
74
  total_cost = item_price * quantity
 
75
 
76
- # Store the cart item in Salesforce
 
 
 
 
 
 
 
 
 
 
 
 
77
  cart_item = sf.Cart_Item__c.create({
78
  'Name': item_name,
79
  'Quantity__c': quantity,
80
  'Price__c': item_price,
81
  'Special_Instructions__c': special_instructions,
 
 
82
  })
83
 
84
- # Update the total cart cost
85
  cart.append({
86
  'name': item_name,
87
  'price': item_price,
88
  'quantity': quantity,
89
  'instructions': special_instructions,
90
- 'total_cost': total_cost
 
91
  })
92
 
93
- total_cart_cost += total_cost
94
  return cart, total_cart_cost
95
 
96
  # Function to remove item from the cart
@@ -98,6 +124,12 @@ def remove_from_cart(index):
98
  global total_cart_cost
99
  total_cart_cost -= cart[index]['total_cost']
100
  cart.pop(index)
 
 
 
 
 
 
101
  return cart, total_cart_cost
102
 
103
  # Checkout Summary
@@ -107,6 +139,8 @@ def proceed_to_checkout():
107
  summary += f"{item['name']} (x{item['quantity']}) - ${item['total_cost']:.2f}\n"
108
  if item['instructions']:
109
  summary += f"Instructions: {item['instructions']}\n"
 
 
110
  summary += f"Total Bill: ${total_cart_cost:.2f}"
111
  return summary
112
 
@@ -133,7 +167,7 @@ def filter_menu(preference):
133
  html_content += f"""
134
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
135
  <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
136
- onclick="openItemModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
137
  <div style="padding: 10px;">
138
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
139
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
@@ -146,11 +180,20 @@ def filter_menu(preference):
146
 
147
  if not any(filtered_data.values()):
148
  return "<p>No items match your filter.</p>"
149
-
150
  return html_content
151
 
152
  # Modal Simulation Function (handled in Python)
153
  def open_modal(item_name, item_image, item_description, item_price):
 
 
 
 
 
 
 
 
 
 
154
  modal_content = f"""
155
  <div style="background: white; padding: 20px; width: 60%; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: absolute; top: 20%; left: 20%; z-index: 1000;">
156
  <img src="{item_image}" style="width: 100%; height: 250px; object-fit: cover; border-radius: 8px;">
@@ -160,8 +203,10 @@ def open_modal(item_name, item_image, item_description, item_price):
160
  <label for="quantity">Quantity:</label>
161
  <input type="number" id="quantity" value="1" min="1" style="width: 50px;">
162
  <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
163
- <button onclick="addToCart()" style="background-color: #28a745; color: white; padding: 10px 20px; border: none; font-size: 14px; border-radius: 5px; cursor: pointer;" >Add to Cart</button>
164
- <button onclick="closeModal()" style="background-color: #ff5722; color: white; padding: 10px 20px; border: none; font-size: 14px; border-radius: 5px; cursor: pointer;" >Close</button>
 
 
165
  </div>
166
  """
167
  return gr.HTML(modal_content)
 
64
  except Exception as e:
65
  return []
66
 
67
+ # Function to load add-ons data from Salesforce
68
+ def load_add_ons_from_salesforce():
69
+ try:
70
+ query = "SELECT Name, Price__c FROM Add_Ons__c"
71
+ result = sf.query(query)
72
+ return result['records']
73
+ except Exception as e:
74
+ return []
75
+
76
  # Cart Management Functions
77
  cart = []
78
  total_cart_cost = 0
79
+ cart_id = None # Store the Cart__c record ID
80
 
81
  # Function to add item to the cart and Salesforce
82
+ def add_to_cart(item_name, item_price, quantity, special_instructions, selected_addons):
83
+ global total_cart_cost, cart_id
84
  total_cost = item_price * quantity
85
+ addons_cost = sum([addon['Price__c'] for addon in selected_addons])
86
 
87
+ # If this is the first item, create a new Cart__c record in Salesforce
88
+ if not cart_id:
89
+ cart_record = sf.Cart__c.create({
90
+ 'Total_Cost__c': total_cost + addons_cost
91
+ })
92
+ cart_id = cart_record['Id']
93
+ else:
94
+ # Update the total cost of the cart
95
+ sf.Cart__c.update(cart_id, {
96
+ 'Total_Cost__c': total_cart_cost + total_cost + addons_cost
97
+ })
98
+
99
+ # Create the Cart_Item__c record and link it to Cart__c
100
  cart_item = sf.Cart_Item__c.create({
101
  'Name': item_name,
102
  'Quantity__c': quantity,
103
  'Price__c': item_price,
104
  'Special_Instructions__c': special_instructions,
105
+ 'Cart__c': cart_id, # Link this item to the Cart__c
106
+ 'Add_ons__c': [addon['Id'] for addon in selected_addons] # Link selected add-ons
107
  })
108
 
109
+ # Add the item to the cart list (for local tracking)
110
  cart.append({
111
  'name': item_name,
112
  'price': item_price,
113
  'quantity': quantity,
114
  'instructions': special_instructions,
115
+ 'total_cost': total_cost + addons_cost,
116
+ 'addons': selected_addons
117
  })
118
 
119
+ total_cart_cost += total_cost + addons_cost
120
  return cart, total_cart_cost
121
 
122
  # Function to remove item from the cart
 
124
  global total_cart_cost
125
  total_cart_cost -= cart[index]['total_cost']
126
  cart.pop(index)
127
+
128
+ # Update the Cart__c record in Salesforce to reflect the new total cost
129
+ sf.Cart__c.update(cart_id, {
130
+ 'Total_Cost__c': total_cart_cost
131
+ })
132
+
133
  return cart, total_cart_cost
134
 
135
  # Checkout Summary
 
139
  summary += f"{item['name']} (x{item['quantity']}) - ${item['total_cost']:.2f}\n"
140
  if item['instructions']:
141
  summary += f"Instructions: {item['instructions']}\n"
142
+ if item['addons']:
143
+ summary += f"Add-ons: {', '.join([addon['Name'] for addon in item['addons']])}\n"
144
  summary += f"Total Bill: ${total_cart_cost:.2f}"
145
  return summary
146
 
 
167
  html_content += f"""
168
  <div style="border: 1px solid #ddd; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); overflow: hidden; height: 350px;">
169
  <img src="{item.get('Image1__c', '')}" style="width: 100%; height: 200px; object-fit: cover;"
170
+ onclick="openModal('{item['Name']}', '{item.get('Image2__c', '')}', '{item['Description__c']}', '${item['Price__c']}')">
171
  <div style="padding: 10px;">
172
  <h3 style='font-size: 1.2em; text-align: center;'>{item['Name']}</h3>
173
  <p style='font-size: 1.1em; color: green; text-align: center;'>${item['Price__c']}</p>
 
180
 
181
  if not any(filtered_data.values()):
182
  return "<p>No items match your filter.</p>"
 
183
  return html_content
184
 
185
  # Modal Simulation Function (handled in Python)
186
  def open_modal(item_name, item_image, item_description, item_price):
187
+ add_ons = load_add_ons_from_salesforce()
188
+ add_ons_html = ""
189
+ for add_on in add_ons:
190
+ add_ons_html += f"""
191
+ <label>
192
+ <input type="checkbox" name="add-on" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
193
+ {add_on['Name']} + ${add_on['Price__c']}
194
+ </label><br>
195
+ """
196
+
197
  modal_content = f"""
198
  <div style="background: white; padding: 20px; width: 60%; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); position: absolute; top: 20%; left: 20%; z-index: 1000;">
199
  <img src="{item_image}" style="width: 100%; height: 250px; object-fit: cover; border-radius: 8px;">
 
203
  <label for="quantity">Quantity:</label>
204
  <input type="number" id="quantity" value="1" min="1" style="width: 50px;">
205
  <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
206
+ <h4>Select Add-ons:</h4>
207
+ {add_ons_html}
208
+ <button onclick="addToCart()" style="background-color: #28a745; color: white; padding: 10px 20px; border: none; font-size: 14px; border-radius: 5px; cursor: pointer;">Add to Cart</button>
209
+ <button onclick="closeModal()" style="background-color: #ff5722; color: white; padding: 10px 20px; border: none; font-size: 14px; border-radius: 5px; cursor: pointer;">Close</button>
210
  </div>
211
  """
212
  return gr.HTML(modal_content)