Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 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():
|