nagasurendra commited on
Commit
23cff82
·
verified ·
1 Parent(s): bd86767

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -158
app.py CHANGED
@@ -120,186 +120,116 @@ def login():
120
  return render_template("login.html")
121
 
122
  # Assuming you're fetching the order details and processing them like this
123
- @app.route('/reward_status')
124
- def reward_status():
125
- # Get the user email from session (assuming the session is already set during checkout)
126
  email = session.get('user_email')
127
-
128
  if not email:
129
  return "User not logged in", 400
130
 
131
- # Query Salesforce to fetch the user's reward points
132
  try:
133
- # Query the Customer_Login__c object based on the user's email
134
- query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'"
135
- user_data = sf.query(query)
136
-
137
- # Check if the user record was found
138
- if not user_data.get("records"):
139
- return "User not found", 400
140
-
141
- # Get the user's reward points from Salesforce
142
- user_points = user_data["records"][0].get("Reward_Points__c", 0)
143
-
144
- except Exception as e:
145
- return f"Error querying Salesforce: {str(e)}", 500
146
-
147
- # Define point ranges for each tier
148
- tiers = {
149
- "Bronze": 100, # Example: 0 to 100 for Bronze
150
- "Silver": 200, # Example: 100 to 200 for Silver
151
- "Gold": 300, # Example: 200 to 300 for Gold
152
- "Platinum": 500
153
- }
154
-
155
- # Logic for calculating the next tier's range and current progress
156
- current_tier = "Silver" # Example, dynamically determine this
157
-
158
- # Calculate the range for the current tier
159
- if current_tier == "Silver":
160
- start_point = 100 # Silver starts at 100
161
- end_point = 200 # Silver ends at 200
162
- elif current_tier == "Gold":
163
- start_point = 200 # Gold starts at 200
164
- end_point = 300 # Gold ends at 300
165
- else:
166
- start_point = 0 # Default start point for other tiers
167
- end_point = 100 # Default end point for other tiers
168
-
169
- # Calculate progress as a percentage
170
- if user_points >= start_point:
171
- progress_percentage = ((user_points - start_point) / (end_point - start_point)) * 100
172
- else:
173
- progress_percentage = 0
174
-
175
- # Calculate points to next tier
176
- points_needed_for_next_tier = end_point - user_points
177
- next_tier = "Gold" # Next tier name (you can dynamically calculate this)
178
- # Round the points before passing to the template
179
- user_points = round(user_points)
180
- points_needed_for_next_tier = round(points_needed_for_next_tier)
181
-
182
- # Pass these values to your template
183
- return render_template(
184
- 'reward_status.html',
185
- user_points=user_points,
186
- current_tier=current_tier,
187
- progress_percentage=progress_percentage,
188
- points_needed_for_next_tier=points_needed_for_next_tier,
189
- start_point=start_point,
190
- end_point=end_point,
191
- next_tier=next_tier,
192
- tiers=tiers
193
- )
194
- @app.route('/order_summary')
195
- def order_summary():
196
- email = session.get('user_email')
197
-
198
- if not email:
199
- print("User not logged in. Redirecting to login.")
200
- return "User not logged in", 400
201
-
202
- try:
203
- print(f"Fetching order details for email: {email}")
204
-
205
- query = f"""
206
- SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c, Order_Status__c, Discount__c, Total_Bill__c
207
  FROM Order__c
208
  WHERE Customer_Email__c = '{email}'
209
  ORDER BY CreatedDate DESC
210
  LIMIT 1
211
  """
212
- result = sf.query(query)
 
 
213
 
214
- if not result.get("records"):
215
- print("No order found for this user.")
216
- return "No order found for this user", 400
217
-
218
- order = result["records"][0]
219
  order_details = order.get("Order_Details__c", "")
220
-
221
  order_items = []
222
 
223
- if order_details:
224
- print("Processing order details...")
225
- for line in order_details.split('\n'):
226
- item_parts = line.split('|')
227
- if len(item_parts) >= 5:
228
- item_name = item_parts[0].strip()
229
- item_name_cleaned = ' '.join(item_name.split(' ')[:-1]).strip()
230
-
231
- print(f"Processing Item: {item_name_cleaned}")
232
-
233
- menu_query = f"""
234
- SELECT Name, Price__c, Image1__c, Ingredient_1__r.Ingredient_Name__c,
235
- Ingredient_1__r.Ingredient_Image__c, Ingredient_1__r.Health_Benefits__c,
236
- Ingredient_1__r.Fun_Facts__c, Ingredient_2__r.Ingredient_Name__c,
237
- Ingredient_2__r.Ingredient_Image__c, Ingredient_2__r.Health_Benefits__c,
238
- Ingredient_2__r.Fun_Facts__c
239
- FROM Menu_Item__c
240
- WHERE Name = '{item_name_cleaned}'
241
- """
242
- print(f"Executing Query for {item_name_cleaned}: {menu_query}")
243
-
244
- menu_result = sf.query(menu_query)
245
- print(f"Menu Result for {item_name_cleaned}: {menu_result}")
246
-
247
- if menu_result.get("records"):
248
- menu_item = menu_result["records"][0]
249
- ingredients = []
250
-
251
- # Check and add Ingredient 1
252
- if 'Ingredient_1__r' in menu_item:
253
- print(f"Ingredient 1 Found for {item_name_cleaned}: {menu_item['Ingredient_1__r']}")
254
- ingredients.append({
255
- "name": menu_item['Ingredient_1__r']['Ingredient_Name__c'],
256
- "image": menu_item['Ingredient_1__r']['Ingredient_Image__c'],
257
- "health_benefits": menu_item['Ingredient_1__r']['Health_Benefits__c'] or "No health benefits available",
258
- "fun_facts": menu_item['Ingredient_1__r']['Fun_Facts__c'] or "No fun facts available"
259
- })
260
- else:
261
- print(f"Ingredient 1 missing for {item_name_cleaned}")
262
-
263
- # Check and add Ingredient 2
264
- if 'Ingredient_2__r' in menu_item:
265
- print(f"Ingredient 2 Found for {item_name_cleaned}: {menu_item['Ingredient_2__r']}")
266
- ingredients.append({
267
- "name": menu_item['Ingredient_2__r']['Ingredient_Name__c'],
268
- "image": menu_item['Ingredient_2__r']['Ingredient_Image__c'],
269
- "health_benefits": menu_item['Ingredient_2__r']['Health_Benefits__c'] or "No health benefits available",
270
- "fun_facts": menu_item['Ingredient_2__r']['Fun_Facts__c'] or "No fun facts available"
271
- })
272
- else:
273
- print(f"Ingredient 2 missing for {item_name_cleaned}")
274
-
275
- # If ingredients are missing, log the missing item
276
- if not ingredients:
277
- print(f"No ingredients found for {item_name_cleaned}")
278
-
279
- order_items.append({
280
- "name": item_name_cleaned,
281
- "price": menu_item.get("Price__c"),
282
- "image_url": menu_item.get("Image1__c"),
283
- "ingredients": ingredients
284
  })
285
 
286
- else:
287
- print(f"Item not found in menu: {item_name_cleaned}")
288
-
289
- if not order_items:
290
- print("No items found in order details.")
291
- else:
292
- print(f"Total items extracted: {len(order_items)}")
293
 
294
- # Pass the order_items to the template
295
  return render_template(
296
- 'reward_status.html',
 
 
 
 
 
 
 
297
  order_items=order_items
298
  )
299
 
300
  except Exception as e:
301
- print(f"Error querying Salesforce: {str(e)}")
302
- return f"Error querying Salesforce: {str(e)}", 500
303
 
304
 
305
 
 
120
  return render_template("login.html")
121
 
122
  # Assuming you're fetching the order details and processing them like this
123
+ @@app.route('/combined_summary')
124
+ def combined_summary():
 
125
  email = session.get('user_email')
 
126
  if not email:
127
  return "User not logged in", 400
128
 
 
129
  try:
130
+ # ====== FETCH REWARDS ======
131
+ reward_query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'"
132
+ reward_data = sf.query(reward_query)
133
+ if not reward_data.get("records"):
134
+ return "Reward info not found", 400
135
+
136
+ user_points = reward_data["records"][0].get("Reward_Points__c", 0)
137
+
138
+ # Determine tier
139
+ tiers = {
140
+ "Bronze": 100,
141
+ "Silver": 200,
142
+ "Gold": 300,
143
+ "Platinum": 500
144
+ }
145
+ current_tier, next_tier = "Bronze", "Silver"
146
+ start_point, end_point = 0, 100
147
+ if user_points >= 100 and user_points < 200:
148
+ current_tier, next_tier = "Silver", "Gold"
149
+ start_point, end_point = 100, 200
150
+ elif user_points >= 200 and user_points < 300:
151
+ current_tier, next_tier = "Gold", "Platinum"
152
+ start_point, end_point = 200, 300
153
+ elif user_points >= 300:
154
+ current_tier, next_tier = "Platinum", "N/A"
155
+ start_point, end_point = 300, 500
156
+
157
+ progress_percentage = ((user_points - start_point) / (end_point - start_point)) * 100 if end_point != start_point else 100
158
+ points_needed_for_next_tier = max(0, end_point - user_points)
159
+
160
+ # ====== FETCH ORDER SUMMARY ======
161
+ order_query = f"""
162
+ SELECT Id, Customer_Name__c, Customer_Email__c, Total_Amount__c, Order_Details__c,
163
+ Order_Status__c, Discount__c, Total_Bill__c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  FROM Order__c
165
  WHERE Customer_Email__c = '{email}'
166
  ORDER BY CreatedDate DESC
167
  LIMIT 1
168
  """
169
+ order_result = sf.query(order_query)
170
+ if not order_result.get("records"):
171
+ return "No order found", 400
172
 
173
+ order = order_result["records"][0]
 
 
 
 
174
  order_details = order.get("Order_Details__c", "")
 
175
  order_items = []
176
 
177
+ for line in order_details.split('\n'):
178
+ item_parts = line.split('|')
179
+ if len(item_parts) >= 5:
180
+ item_name_raw = item_parts[0].strip()
181
+ item_name = ' '.join(item_name_raw.split(' ')[:-1]).strip()
182
+
183
+ menu_query = f"""
184
+ SELECT Name, Price__c, Image1__c,
185
+ Ingredient_1__r.Ingredient_Name__c, Ingredient_1__r.Ingredient_Image__c,
186
+ Ingredient_1__r.Health_Benefits__c, Ingredient_1__r.Fun_Facts__c,
187
+ Ingredient_2__r.Ingredient_Name__c, Ingredient_2__r.Ingredient_Image__c,
188
+ Ingredient_2__r.Health_Benefits__c, Ingredient_2__r.Fun_Facts__c
189
+ FROM Menu_Item__c
190
+ WHERE Name = '{item_name}'
191
+ """
192
+ menu_result = sf.query(menu_query)
193
+ ingredients = []
194
+
195
+ if menu_result.get("records"):
196
+ menu_item = menu_result["records"][0]
197
+ if 'Ingredient_1__r' in menu_item:
198
+ ingredients.append({
199
+ "name": menu_item['Ingredient_1__r']['Ingredient_Name__c'],
200
+ "image": menu_item['Ingredient_1__r']['Ingredient_Image__c'],
201
+ "health_benefits": menu_item['Ingredient_1__r']['Health_Benefits__c'] or "",
202
+ "fun_facts": menu_item['Ingredient_1__r']['Fun_Facts__c'] or ""
203
+ })
204
+ if 'Ingredient_2__r' in menu_item:
205
+ ingredients.append({
206
+ "name": menu_item['Ingredient_2__r']['Ingredient_Name__c'],
207
+ "image": menu_item['Ingredient_2__r']['Ingredient_Image__c'],
208
+ "health_benefits": menu_item['Ingredient_2__r']['Health_Benefits__c'] or "",
209
+ "fun_facts": menu_item['Ingredient_2__r']['Fun_Facts__c'] or ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  })
211
 
212
+ order_items.append({
213
+ "name": item_name,
214
+ "price": menu_item.get("Price__c"),
215
+ "image_url": menu_item.get("Image1__c"),
216
+ "ingredients": ingredients
217
+ })
 
218
 
 
219
  return render_template(
220
+ 'combined_summary.html',
221
+ user_points=round(user_points),
222
+ current_tier=current_tier,
223
+ next_tier=next_tier,
224
+ start_point=start_point,
225
+ end_point=end_point,
226
+ progress_percentage=round(progress_percentage),
227
+ points_needed_for_next_tier=round(points_needed_for_next_tier),
228
  order_items=order_items
229
  )
230
 
231
  except Exception as e:
232
+ return f"Error: {str(e)}", 500
 
233
 
234
 
235