gopichandra commited on
Commit
9721b35
·
verified ·
1 Parent(s): 532c0f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -161,8 +161,9 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
161
  # Mapping mode and entry_type to Salesforce object and field
162
  object_name = None
163
  field_name = None
 
164
  product_field_name = "Product_Name__c"
165
- model_field_name = "Modal_Name__c" # Correct field for model name
166
 
167
  if mode == "Entry":
168
  if entry_type == "Sales":
@@ -174,12 +175,14 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
174
  elif mode == "Exit":
175
  if entry_type == "Sales":
176
  object_name = "Inventory_Management__c"
177
- field_name = "Quantity_Sold__c"
 
178
  elif entry_type == "Non-Sales":
179
  object_name = "Un_Billable__c"
180
- field_name = "Sold_Out__c"
 
181
 
182
- if not object_name or not field_name:
183
  return "Invalid mode or entry type."
184
 
185
  # Get valid fields for the specified Salesforce object
@@ -205,14 +208,14 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
205
  query_conditions.append(f"{model_field_name} = '{model_name}'")
206
  query_conditions.append(f"{product_field_name} = '{product_name}'")
207
 
208
- query = f"SELECT Id, {field_name} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
209
  response = sf.query(query)
210
 
211
  if response["records"]:
212
  record_id = response["records"][0]["Id"]
213
- updated_quantity = quantity # Overwrite the quantity
214
- sf_object.update(record_id, {field_name: updated_quantity})
215
- return f"✅ Updated record for product '{product_name}' ({model_name}) in {object_name}. New {field_name}: {updated_quantity}."
216
  else:
217
  return f"❌ No matching record found for product '{product_name}' ({model_name}) in {object_name}."
218
 
@@ -225,6 +228,7 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
225
 
226
  except Exception as e:
227
  return f"❌ Error interacting with Salesforce: {str(e)}"
 
228
  # Function to pull structured data from Salesforce and display as a table
229
  def pull_data_from_salesforce():
230
  try:
@@ -234,21 +238,29 @@ def pull_data_from_salesforce():
234
  security_token=SALESFORCE_SECURITY_TOKEN
235
  )
236
 
237
- query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
238
- response = sf.query_all(query)
 
 
 
239
 
240
- records = response.get("records", [])
241
- if not records:
 
 
242
  return "No data found in Salesforce.", None, None, None
243
-
 
244
  df = pd.DataFrame(records)
245
  df = df.drop(columns=['attributes'], errors='ignore')
246
 
247
  # Rename columns for better readability
248
  df.rename(columns={
249
  "Product_Name__c": "Product Name",
250
- "Modal_Name__c": "Model Name",
251
- "Current_Stocks__c": "Current Stocks"
 
 
252
  }, inplace=True)
253
 
254
  excel_path = "salesforce_data.xlsx"
 
161
  # Mapping mode and entry_type to Salesforce object and field
162
  object_name = None
163
  field_name = None
164
+ field_names = []
165
  product_field_name = "Product_Name__c"
166
+ model_field_name = None # Correct field for model name
167
 
168
  if mode == "Entry":
169
  if entry_type == "Sales":
 
175
  elif mode == "Exit":
176
  if entry_type == "Sales":
177
  object_name = "Inventory_Management__c"
178
+ field_names = ["Quantity_Sold__c", "soldstock__c"]
179
+ model_field_name = "Modal_Name__c"
180
  elif entry_type == "Non-Sales":
181
  object_name = "Un_Billable__c"
182
+ field_names = ["Sold_Out__c", "soldstock__c"]
183
+ model_field_name = "Model_Name__c"
184
 
185
+ if not object_name or (not field_name and not field_names):
186
  return "Invalid mode or entry type."
187
 
188
  # Get valid fields for the specified Salesforce object
 
208
  query_conditions.append(f"{model_field_name} = '{model_name}'")
209
  query_conditions.append(f"{product_field_name} = '{product_name}'")
210
 
211
+ query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
212
  response = sf.query(query)
213
 
214
  if response["records"]:
215
  record_id = response["records"][0]["Id"]
216
+ updated_fields = {field: quantity for field in field_names}
217
+ sf_object.update(record_id, updated_fields)
218
+ return f"✅ Updated record for product '{product_name}' ({model_name}) in {object_name}. Updated fields: {updated_fields}."
219
  else:
220
  return f"❌ No matching record found for product '{product_name}' ({model_name}) in {object_name}."
221
 
 
228
 
229
  except Exception as e:
230
  return f"❌ Error interacting with Salesforce: {str(e)}"
231
+
232
  # Function to pull structured data from Salesforce and display as a table
233
  def pull_data_from_salesforce():
234
  try:
 
238
  security_token=SALESFORCE_SECURITY_TOKEN
239
  )
240
 
241
+ query_inventory = "SELECT Product_Name__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"
242
+ query_unbillable = "SELECT Product_Name__c, Current_Stock__c, soldstock__c FROM Un_Billable__c LIMIT 100"
243
+
244
+ response_inventory = sf.query_all(query_inventory)
245
+ response_unbillable = sf.query_all(query_unbillable)
246
 
247
+ records_inventory = response_inventory.get("records", [])
248
+ records_unbillable = response_unbillable.get("records", [])
249
+
250
+ if not records_inventory and not records_unbillable:
251
  return "No data found in Salesforce.", None, None, None
252
+
253
+ records = records_inventory + records_unbillable
254
  df = pd.DataFrame(records)
255
  df = df.drop(columns=['attributes'], errors='ignore')
256
 
257
  # Rename columns for better readability
258
  df.rename(columns={
259
  "Product_Name__c": "Product Name",
260
+ "Modal_Name__c": "Model Name (Inventory)",
261
+ "Model_Name__c": "Model Name (Unbillable)",
262
+ "Current_Stocks__c": "Current Stocks",
263
+ "soldstock__c": "Sold Stock"
264
  }, inplace=True)
265
 
266
  excel_path = "salesforce_data.xlsx"