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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -67
app.py CHANGED
@@ -150,73 +150,37 @@ 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 interact_with_salesforce(mode, entry_type, quantity, extracted_text):
154
- try:
155
- sf = Salesforce(
156
- username=SALESFORCE_USERNAME,
157
- password=SALESFORCE_PASSWORD,
158
- security_token=SALESFORCE_SECURITY_TOKEN
159
- )
160
-
161
- # Determine Salesforce Object and Quantity Field
162
- object_name = None
163
- product_field_name = "Productname__c" # Ensure correct field name for Entry Mode
164
- model_field_name = "Model__c" # Ensure correct field name for Entry Mode
165
- quantity_field = None
166
-
167
- if mode == "Entry":
168
- if entry_type == "Sales":
169
- object_name = "VENKATA_RAMANA_MOTORS__c"
170
- quantity_field = "Quantity__c" # Check the correct field name in Salesforce
171
- elif entry_type == "Non-Sales":
172
- object_name = "UNBILLING_DATA__c"
173
- quantity_field = "TotalQuantity__c"
174
-
175
- elif mode == "Exit": # Keep Exit Mode logic unchanged
176
- if entry_type == "Sales":
177
- object_name = "Inventory_Management__c"
178
- quantity_field = "Quantity_Sold__c"
179
- elif entry_type == "Non-Sales":
180
- object_name = "Un_Billable__c"
181
- quantity_field = "Sold_Out__c"
182
-
183
- if not object_name or not quantity_field:
184
- return "Invalid mode or entry type."
185
-
186
- # Extract Product Name or Model Name
187
- product_name = match_product_name(extracted_text)
188
- attributes = extract_attributes(extracted_text)
189
- model_name = attributes.get("Model Name", "").strip()
190
-
191
- if not product_name and not model_name:
192
- return "❌ No product name or model name detected. Please provide a valid product or model."
193
-
194
- # Build Query to Find Matching Product
195
- query_conditions = []
196
- if product_name:
197
- query_conditions.append(f"{product_field_name} = '{product_name}'")
198
- if model_name:
199
- query_conditions.append(f"{model_field_name} = '{model_name}'")
200
-
201
- query_condition_string = " OR ".join(query_conditions)
202
-
203
- query = f"SELECT Id, {quantity_field} FROM {object_name} WHERE {query_condition_string} LIMIT 1"
204
- response = sf.query(query)
205
-
206
- if response["records"]:
207
- record_id = response["records"][0]["Id"]
208
- existing_quantity = response["records"][0].get(quantity_field, 0)
209
-
210
- # Update quantity directly without modifying other fields
211
- updated_quantity = quantity # Directly set the new quantity
212
-
213
- sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
214
- return f"✅ Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
215
- else:
216
- return f"❌ No matching product found in {object_name}. Please check the product/model name."
217
-
218
- except Exception as e:
219
- return f"❌ Error interacting with Salesforce: {str(e)}"
220
 
221
  # Function to pull structured data from Salesforce and display as a table
222
  def pull_data_from_salesforce():
 
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():