Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,20 +158,23 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 158 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 159 |
)
|
| 160 |
|
| 161 |
-
# Determine Salesforce Object and Quantity Field
|
| 162 |
object_name = None
|
| 163 |
-
product_field_name = "Product_Name__c"
|
| 164 |
-
model_field_name = "Modal_Name__c"
|
| 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"
|
|
|
|
|
|
|
| 171 |
elif entry_type == "Non-Sales":
|
| 172 |
object_name = "UNBILLING_DATA__c"
|
| 173 |
quantity_field = "TotalQuantity__c"
|
| 174 |
-
|
|
|
|
| 175 |
if entry_type == "Sales":
|
| 176 |
object_name = "Inventory_Management__c"
|
| 177 |
quantity_field = "Quantity_Sold__c"
|
|
@@ -190,6 +193,11 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 190 |
if not product_name and not model_name:
|
| 191 |
return "❌ No product name or model name detected. Please provide a valid product or model."
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
# Build Query to Find Matching Product
|
| 194 |
query_conditions = []
|
| 195 |
if product_name:
|
|
@@ -212,7 +220,7 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 212 |
sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
|
| 213 |
return f"✅ Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
|
| 214 |
else:
|
| 215 |
-
return f"❌ No matching product found in {object_name}."
|
| 216 |
|
| 217 |
except Exception as e:
|
| 218 |
return f"❌ Error interacting with Salesforce: {str(e)}"
|
|
@@ -235,6 +243,80 @@ def pull_data_from_salesforce():
|
|
| 235 |
|
| 236 |
df = pd.DataFrame(records)
|
| 237 |
df = df.drop(columns=['attributes'], errors='ignore')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# Rename columns for better readability
|
| 240 |
df.rename(columns={
|
|
|
|
| 158 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 159 |
)
|
| 160 |
|
| 161 |
+
# Determine Salesforce Object and Quantity Field for Entry Mode
|
| 162 |
object_name = None
|
| 163 |
+
product_field_name = "Product_Name__c" # Needs verification
|
| 164 |
+
model_field_name = "Modal_Name__c" # Needs verification
|
| 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" # Needs verification
|
| 171 |
+
product_field_name = "Productname__c" # Adjusted based on ENTRY Mode fields
|
| 172 |
+
model_field_name = "Model__c" # Adjusted based on ENTRY Mode fields
|
| 173 |
elif entry_type == "Non-Sales":
|
| 174 |
object_name = "UNBILLING_DATA__c"
|
| 175 |
quantity_field = "TotalQuantity__c"
|
| 176 |
+
|
| 177 |
+
elif mode == "Exit": # Exit Mode is working fine, so we don't touch it
|
| 178 |
if entry_type == "Sales":
|
| 179 |
object_name = "Inventory_Management__c"
|
| 180 |
quantity_field = "Quantity_Sold__c"
|
|
|
|
| 193 |
if not product_name and not model_name:
|
| 194 |
return "❌ No product name or model name detected. Please provide a valid product or model."
|
| 195 |
|
| 196 |
+
# Adjust the field names for Entry Mode to match correct fields in Salesforce
|
| 197 |
+
if mode == "Entry":
|
| 198 |
+
product_field_name = "Productname__c" # Adjusted based on ENTRY Mode
|
| 199 |
+
model_field_name = "Model__c" # Adjusted based on ENTRY Mode
|
| 200 |
+
|
| 201 |
# Build Query to Find Matching Product
|
| 202 |
query_conditions = []
|
| 203 |
if product_name:
|
|
|
|
| 220 |
sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
|
| 221 |
return f"✅ Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
|
| 222 |
else:
|
| 223 |
+
return f"❌ No matching product found in {object_name}. Check field names or product/model data."
|
| 224 |
|
| 225 |
except Exception as e:
|
| 226 |
return f"❌ Error interacting with Salesforce: {str(e)}"
|
|
|
|
| 243 |
|
| 244 |
df = pd.DataFrame(records)
|
| 245 |
df = df.drop(columns=['attributes'], errors='ignore')
|
| 246 |
+
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
| 247 |
+
try:
|
| 248 |
+
sf = Salesforce(
|
| 249 |
+
username=SALESFORCE_USERNAME,
|
| 250 |
+
password=SALESFORCE_PASSWORD,
|
| 251 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Determine Salesforce Object and Quantity Field for Entry Mode
|
| 255 |
+
object_name = None
|
| 256 |
+
product_field_name = "Product_Name__c" # Needs verification
|
| 257 |
+
model_field_name = "Modal_Name__c" # Needs verification
|
| 258 |
+
quantity_field = None
|
| 259 |
+
|
| 260 |
+
if mode == "Entry":
|
| 261 |
+
if entry_type == "Sales":
|
| 262 |
+
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 263 |
+
quantity_field = "Quantity__c" # Needs verification
|
| 264 |
+
product_field_name = "Productname__c" # Adjusted based on ENTRY Mode fields
|
| 265 |
+
model_field_name = "Model__c" # Adjusted based on ENTRY Mode fields
|
| 266 |
+
elif entry_type == "Non-Sales":
|
| 267 |
+
object_name = "UNBILLING_DATA__c"
|
| 268 |
+
quantity_field = "TotalQuantity__c"
|
| 269 |
+
|
| 270 |
+
elif mode == "Exit": # Exit Mode is working fine, so we don't touch it
|
| 271 |
+
if entry_type == "Sales":
|
| 272 |
+
object_name = "Inventory_Management__c"
|
| 273 |
+
quantity_field = "Quantity_Sold__c"
|
| 274 |
+
elif entry_type == "Non-Sales":
|
| 275 |
+
object_name = "Un_Billable__c"
|
| 276 |
+
quantity_field = "Sold_Out__c"
|
| 277 |
+
|
| 278 |
+
if not object_name or not quantity_field:
|
| 279 |
+
return "Invalid mode or entry type."
|
| 280 |
+
|
| 281 |
+
# Extract Product Name or Model Name
|
| 282 |
+
product_name = match_product_name(extracted_text)
|
| 283 |
+
attributes = extract_attributes(extracted_text)
|
| 284 |
+
model_name = attributes.get("Model Name", "").strip()
|
| 285 |
+
|
| 286 |
+
if not product_name and not model_name:
|
| 287 |
+
return "❌ No product name or model name detected. Please provide a valid product or model."
|
| 288 |
+
|
| 289 |
+
# Adjust the field names for Entry Mode to match correct fields in Salesforce
|
| 290 |
+
if mode == "Entry":
|
| 291 |
+
product_field_name = "Productname__c" # Adjusted based on ENTRY Mode
|
| 292 |
+
model_field_name = "Model__c" # Adjusted based on ENTRY Mode
|
| 293 |
+
|
| 294 |
+
# Build Query to Find Matching Product
|
| 295 |
+
query_conditions = []
|
| 296 |
+
if product_name:
|
| 297 |
+
query_conditions.append(f"{product_field_name} = '{product_name}'")
|
| 298 |
+
if model_name:
|
| 299 |
+
query_conditions.append(f"{model_field_name} = '{model_name}'")
|
| 300 |
+
|
| 301 |
+
query_condition_string = " OR ".join(query_conditions)
|
| 302 |
+
|
| 303 |
+
query = f"SELECT Id, {quantity_field} FROM {object_name} WHERE {query_condition_string} LIMIT 1"
|
| 304 |
+
response = sf.query(query)
|
| 305 |
+
|
| 306 |
+
if response["records"]:
|
| 307 |
+
record_id = response["records"][0]["Id"]
|
| 308 |
+
existing_quantity = response["records"][0].get(quantity_field, 0)
|
| 309 |
+
|
| 310 |
+
# Update quantity directly
|
| 311 |
+
updated_quantity = quantity # Just set the new quantity without adding/subtracting
|
| 312 |
+
|
| 313 |
+
sf.__getattr__(object_name).update(record_id, {quantity_field: updated_quantity})
|
| 314 |
+
return f"✅ Successfully updated quantity for '{product_name or model_name}' in {object_name}. New {quantity_field}: {updated_quantity}."
|
| 315 |
+
else:
|
| 316 |
+
return f"❌ No matching product found in {object_name}. Check field names or product/model data."
|
| 317 |
+
|
| 318 |
+
except Exception as e:
|
| 319 |
+
return f"❌ Error interacting with Salesforce: {str(e)}"
|
| 320 |
|
| 321 |
# Rename columns for better readability
|
| 322 |
df.rename(columns={
|