gopichandra commited on
Commit
e85a59e
Β·
verified Β·
1 Parent(s): 92a9f78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -83
app.py CHANGED
@@ -150,56 +150,10 @@ def filter_valid_attributes(attributes, valid_fields):
150
  return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
151
 
152
  #πŸ“Š Function to interact with Salesforce based on mode and type
153
- def update_salesforce(mode, sale_type, product_name, model_name, quantity):
154
- # Connect to Salesforce
155
- sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
156
-
157
- if mode == 'entry':
158
- if sale_type == 'sales':
159
- sf.VENKATA_RAMANA_MOTORS__c.update(
160
- {'Name': product_name, 'Model__c': model_name, 'Quantity__c': quantity}
161
- )
162
- elif sale_type == 'non_sales':
163
- sf.UNBILLING_DATA__c.update(
164
- {'Name': product_name, 'Model__c': model_name, 'TotalQuantity__c': quantity}
165
- )
166
-
167
- elif mode == 'exit':
168
- if sale_type == 'sales':
169
- # Find the matched product name or model name and update
170
- query = f"SELECT Id FROM Inventory_Management__c WHERE Name='{product_name}' OR Model__c='{model_name}'"
171
- records = sf.query(query)['records']
172
- if records:
173
- sf.Inventory_Management__c.update(
174
- records[0]['Id'], {'Quantity_Sold__c': quantity}
175
- )
176
- elif sale_type == 'non_sales':
177
- # Find the matched product name or model name and update
178
- query = f"SELECT Id FROM Un_Billable__c WHERE Name='{product_name}' OR Model__c='{model_name}'"
179
- records = sf.query(query)['records']
180
- if records:
181
- sf.Un_Billable__c.update(
182
- records[0]['Id'], {'Sold_Out__c': quantity}
183
- )
184
-
185
- # Function to pull structured data from Salesforce and display as a table
186
- def pull_data_from_salesforce():
187
- try:
188
- sf = Salesforce(
189
- username=SALESFORCE_USERNAME,
190
- password=SALESFORCE_PASSWORD,
191
- security_token=SALESFORCE_SECURITY_TOKEN
192
- )
193
-
194
- query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
195
- response = sf.query_all(query)
196
-
197
- records = response.get("records", [])
198
- if not records:
199
- return "No data found in Salesforce.", None, None, None
200
-
201
- df = pd.DataFrame(records)
202
- df = df.drop(columns=['attributes'], errors='ignore')
203
  def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
204
  try:
205
  sf = Salesforce(
@@ -208,23 +162,21 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
208
  security_token=SALESFORCE_SECURITY_TOKEN
209
  )
210
 
211
- # Determine Salesforce Object and Quantity Field for Entry Mode
212
  object_name = None
213
- product_field_name = "Product_Name__c" # Needs verification
214
- model_field_name = "Modal_Name__c" # Needs verification
215
  quantity_field = None
216
 
217
  if mode == "Entry":
218
  if entry_type == "Sales":
219
  object_name = "VENKATA_RAMANA_MOTORS__c"
220
- quantity_field = "Quantity__c" # Needs verification
221
- product_field_name = "Productname__c" # Adjusted based on ENTRY Mode fields
222
- model_field_name = "Model__c" # Adjusted based on ENTRY Mode fields
223
  elif entry_type == "Non-Sales":
224
  object_name = "UNBILLING_DATA__c"
225
  quantity_field = "TotalQuantity__c"
226
 
227
- elif mode == "Exit": # Exit Mode is working fine, so we don't touch it
228
  if entry_type == "Sales":
229
  object_name = "Inventory_Management__c"
230
  quantity_field = "Quantity_Sold__c"
@@ -233,44 +185,42 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
233
  quantity_field = "Sold_Out__c"
234
 
235
  if not object_name or not quantity_field:
236
- return "Invalid mode or entry type."
237
 
238
- # Extract Product Name or Model Name
239
  product_name = match_product_name(extracted_text)
240
  attributes = extract_attributes(extracted_text)
241
  model_name = attributes.get("Model Name", "").strip()
242
 
243
- if not product_name and not model_name:
244
- return "❌ No product name or model name detected. Please provide a valid product or model."
 
245
 
246
- # Adjust the field names for Entry Mode to match correct fields in Salesforce
247
- if mode == "Entry":
248
- product_field_name = "Productname__c" # Adjusted based on ENTRY Mode
249
- model_field_name = "Model__c" # Adjusted based on ENTRY Mode
 
 
250
 
251
- # Build Query to Find Matching Product
252
- query_conditions = []
253
- if product_name:
254
- query_conditions.append(f"{product_field_name} = '{product_name}'")
255
- if model_name:
256
- query_conditions.append(f"{model_field_name} = '{model_name}'")
257
 
258
- query_condition_string = " OR ".join(query_conditions)
 
259
 
260
- query = f"SELECT Id, {quantity_field} FROM {object_name} WHERE {query_condition_string} LIMIT 1"
261
- response = sf.query(query)
 
262
 
263
- if response["records"]:
264
- record_id = response["records"][0]["Id"]
265
- existing_quantity = response["records"][0].get(quantity_field, 0)
266
 
267
- # Update quantity directly
268
- updated_quantity = quantity # Just set the new quantity without adding/subtracting
 
 
269
 
270
- sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
271
- return f"βœ… Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
272
- else:
273
- return f"❌ No matching product found in {object_name}. Check field names or product/model data."
274
 
275
  except Exception as e:
276
  return f"❌ Error interacting with Salesforce: {str(e)}"
 
150
  return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
151
 
152
  #πŸ“Š Function to interact with Salesforce based on mode and type
153
+ def filter_valid_attributes(attributes, valid_fields):
154
+ return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
155
+
156
+ #πŸ“Š Function to interact with Salesforce based on mode and type
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
158
  try:
159
  sf = Salesforce(
 
162
  security_token=SALESFORCE_SECURITY_TOKEN
163
  )
164
 
165
+ # Define Salesforce Object & Fields
166
  object_name = None
167
+ product_field_name = "Product_Name__c"
168
+ model_field_name = "Modal_Name__c"
169
  quantity_field = None
170
 
171
  if mode == "Entry":
172
  if entry_type == "Sales":
173
  object_name = "VENKATA_RAMANA_MOTORS__c"
174
+ quantity_field = "Quantity__c"
 
 
175
  elif entry_type == "Non-Sales":
176
  object_name = "UNBILLING_DATA__c"
177
  quantity_field = "TotalQuantity__c"
178
 
179
+ elif mode == "Exit": # βœ… Only modify Exit Mode logic
180
  if entry_type == "Sales":
181
  object_name = "Inventory_Management__c"
182
  quantity_field = "Quantity_Sold__c"
 
185
  quantity_field = "Sold_Out__c"
186
 
187
  if not object_name or not quantity_field:
188
+ return "❌ Invalid mode or entry type."
189
 
190
+ # πŸ”Ή Extract Product Name or Model Name from OCR text
191
  product_name = match_product_name(extracted_text)
192
  attributes = extract_attributes(extracted_text)
193
  model_name = attributes.get("Model Name", "").strip()
194
 
195
+ if mode == "Exit":
196
+ if not product_name and not model_name:
197
+ return "❌ No product name or model name detected. Please provide a valid product or model."
198
 
199
+ # βœ… Build Query to Find Matching Product in Exit Mode
200
+ query_conditions = []
201
+ if product_name:
202
+ query_conditions.append(f"{product_field_name} = '{product_name}'")
203
+ if model_name:
204
+ query_conditions.append(f"{model_field_name} = '{model_name}'")
205
 
206
+ query_condition_string = " OR ".join(query_conditions)
 
 
 
 
 
207
 
208
+ query = f"SELECT Id, {quantity_field} FROM {object_name} WHERE {query_condition_string} LIMIT 1"
209
+ response = sf.query(query)
210
 
211
+ if response["records"]:
212
+ record_id = response["records"][0]["Id"]
213
+ existing_quantity = response["records"][0].get(quantity_field, 0)
214
 
215
+ # βœ… Directly update the quantity (do not add or subtract)
216
+ updated_quantity = quantity
 
217
 
218
+ sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
219
+ return f"βœ… Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
220
+ else:
221
+ return f"❌ No matching product found in {object_name}. Please check the product/model name."
222
 
223
+ return "βœ… Entry mode unchanged. Data will be processed normally."
 
 
 
224
 
225
  except Exception as e:
226
  return f"❌ Error interacting with Salesforce: {str(e)}"