Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,10 +149,6 @@ def extract_attributes(extracted_text):
|
|
| 149 |
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 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:
|
|
@@ -162,68 +158,86 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 162 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 163 |
)
|
| 164 |
|
| 165 |
-
#
|
| 166 |
object_name = None
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
|
| 171 |
if mode == "Entry":
|
| 172 |
if entry_type == "Sales":
|
| 173 |
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 174 |
-
|
| 175 |
elif entry_type == "Non-Sales":
|
| 176 |
object_name = "UNBILLING_DATA__c"
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
elif mode == "Exit": # β
Only modify Exit Mode logic
|
| 180 |
if entry_type == "Sales":
|
| 181 |
object_name = "Inventory_Management__c"
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
elif entry_type == "Non-Sales":
|
| 184 |
object_name = "Un_Billable__c"
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
#
|
| 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
|
| 196 |
-
|
| 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 |
-
|
| 207 |
|
| 208 |
-
|
|
|
|
| 209 |
response = sf.query(query)
|
| 210 |
|
| 211 |
if response["records"]:
|
| 212 |
record_id = response["records"][0]["Id"]
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 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"
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
except Exception as e:
|
| 226 |
return f"β Error interacting with Salesforce: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# Rename columns for better readability
|
| 229 |
df.rename(columns={
|
|
|
|
| 149 |
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:
|
|
|
|
| 158 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 159 |
)
|
| 160 |
|
| 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" # Correct field for product name in the object
|
| 165 |
+
model_field_name = "Modal_Name__c" # Correct field for model name in the object
|
| 166 |
|
| 167 |
if mode == "Entry":
|
| 168 |
if entry_type == "Sales":
|
| 169 |
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 170 |
+
field_name = "Quantity__c"
|
| 171 |
elif entry_type == "Non-Sales":
|
| 172 |
object_name = "UNBILLING_DATA__c"
|
| 173 |
+
field_name = "TotalQuantity__c"
|
| 174 |
+
elif mode == "Exit":
|
|
|
|
| 175 |
if entry_type == "Sales":
|
| 176 |
object_name = "Inventory_Management__c"
|
| 177 |
+
product_field_name = "Product_Name__c"
|
| 178 |
+
model_field_name = "Modal_Name__c"
|
| 179 |
+
field_name = "Quantity_Sold__c"
|
| 180 |
elif entry_type == "Non-Sales":
|
| 181 |
object_name = "Un_Billable__c"
|
| 182 |
+
product_field_name = "Product_Name__c"
|
| 183 |
+
model_field_name = "Model_Name__c"
|
| 184 |
+
field_name = "Sold_Out__c"
|
| 185 |
+
|
| 186 |
+
if not object_name or not field_name:
|
| 187 |
+
return "Invalid mode or entry type."
|
| 188 |
|
| 189 |
+
# Get valid fields for the specified Salesforce object
|
| 190 |
+
sf_object = sf.__getattr__(object_name)
|
| 191 |
+
schema = sf_object.describe()
|
| 192 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
| 193 |
|
| 194 |
+
# Extract product name or model number
|
| 195 |
product_name = match_product_name(extracted_text)
|
| 196 |
attributes = extract_attributes(extracted_text)
|
|
|
|
| 197 |
|
| 198 |
+
if not product_name:
|
| 199 |
+
return "Product name could not be matched from the extracted text."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
attributes["Product name"] = product_name
|
| 202 |
|
| 203 |
+
if mode == "Exit":
|
| 204 |
+
query = f"SELECT Id, {field_name} FROM {object_name} WHERE {product_field_name} = '{product_name}' OR {model_field_name} = '{attributes.get('Model Name', '')}' LIMIT 1"
|
| 205 |
response = sf.query(query)
|
| 206 |
|
| 207 |
if response["records"]:
|
| 208 |
record_id = response["records"][0]["Id"]
|
| 209 |
+
updated_quantity = quantity # Overwrite the quantity, don't add
|
| 210 |
+
sf_object.update(record_id, {field_name: updated_quantity})
|
| 211 |
+
return f"Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
+
return f"No matching record found for product '{product_name}' in {object_name}."
|
| 214 |
+
else:
|
| 215 |
+
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 216 |
+
filtered_attributes[field_name] = quantity
|
| 217 |
+
sf_object.create(filtered_attributes)
|
| 218 |
+
return f"β
Data successfully exported to Salesforce object {object_name}."
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
return f"β Error interacting with Salesforce: {str(e)}"
|
| 222 |
+
|
| 223 |
+
# Function to pull structured data from Salesforce and display as a table
|
| 224 |
+
def pull_data_from_salesforce():
|
| 225 |
+
try:
|
| 226 |
+
sf = Salesforce(
|
| 227 |
+
username=SALESFORCE_USERNAME,
|
| 228 |
+
password=SALESFORCE_PASSWORD,
|
| 229 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
|
| 233 |
+
response = sf.query_all(query)
|
| 234 |
+
|
| 235 |
+
records = response.get("records", [])
|
| 236 |
+
if not records:
|
| 237 |
+
return "No data found in Salesforce.", None, None, None
|
| 238 |
+
|
| 239 |
+
df = pd.DataFrame(records)
|
| 240 |
+
df = df.drop(columns=['attributes'], errors='ignore')
|
| 241 |
|
| 242 |
# Rename columns for better readability
|
| 243 |
df.rename(columns={
|