Spaces:
Runtime error
Runtime error
Update app.py
#3
by
gopichandra
- opened
app.py
CHANGED
@@ -171,7 +171,7 @@ def interact_with_salesforce(mode, entry_type, quantity, attributes):
|
|
171 |
query_conditions = [f"{product_field_name} = '{attributes['Product name']}'"]
|
172 |
if "Model Name" in attributes and attributes["Model Name"]:
|
173 |
query_conditions.append(f"{model_field_name} = '{attributes['Model Name']}'")
|
174 |
-
if "Stage" in attributes and attributes["Stage"]:
|
175 |
query_conditions.append(f"{stage_field_name} = '{attributes['Stage']}'")
|
176 |
if "H.P." in attributes and attributes["H.P."]:
|
177 |
query_conditions.append(f"{hp_field_name} = '{attributes['H.P.']}'")
|
@@ -220,10 +220,49 @@ def export_to_salesforce(mode, entry_type, quantity, edited_df):
|
|
220 |
try:
|
221 |
# Convert edited DataFrame back to dictionary
|
222 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
223 |
-
|
224 |
# Export to Salesforce
|
225 |
message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
except Exception as e:
|
228 |
return f"❌ Error exporting to Salesforce: {str(e)}"
|
229 |
|
@@ -239,9 +278,9 @@ def pull_data_from_salesforce(data_type):
|
|
239 |
)
|
240 |
|
241 |
if data_type == "Inventory":
|
242 |
-
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c,
|
243 |
else:
|
244 |
-
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c,
|
245 |
|
246 |
response = sf.query_all(query)
|
247 |
records = response.get("records", [])
|
@@ -251,6 +290,11 @@ def pull_data_from_salesforce(data_type):
|
|
251 |
|
252 |
df = pd.DataFrame(records)
|
253 |
df = df.drop(columns=['attributes'], errors='ignore')
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
# Rename columns for better readability
|
256 |
df.rename(columns={
|
@@ -261,7 +305,7 @@ def pull_data_from_salesforce(data_type):
|
|
261 |
"Current_Stocks__c": "Current Stocks",
|
262 |
"Current_Stock__c": "Current Stocks",
|
263 |
"soldstock__c": "Sold Stock",
|
264 |
-
"
|
265 |
}, inplace=True)
|
266 |
|
267 |
excel_path = "salesforce_data.xlsx"
|
|
|
171 |
query_conditions = [f"{product_field_name} = '{attributes['Product name']}'"]
|
172 |
if "Model Name" in attributes and attributes["Model Name"]:
|
173 |
query_conditions.append(f"{model_field_name} = '{attributes['Model Name']}'")
|
174 |
+
if "Stage" in attributes and attributes["Stage"] != "":
|
175 |
query_conditions.append(f"{stage_field_name} = '{attributes['Stage']}'")
|
176 |
if "H.P." in attributes and attributes["H.P."]:
|
177 |
query_conditions.append(f"{hp_field_name} = '{attributes['H.P.']}'")
|
|
|
220 |
try:
|
221 |
# Convert edited DataFrame back to dictionary
|
222 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
223 |
+
|
224 |
# Export to Salesforce
|
225 |
message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
|
226 |
+
|
227 |
+
# Fetch the price from Inventory_Management__c based on attributes
|
228 |
+
try:
|
229 |
+
sf = Salesforce(
|
230 |
+
username=SALESFORCE_USERNAME,
|
231 |
+
password=SALESFORCE_PASSWORD,
|
232 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
233 |
+
)
|
234 |
+
product_name = edited_attributes.get("Product name", "")
|
235 |
+
model_name = edited_attributes.get("Model Name", "")
|
236 |
+
stage = edited_attributes.get("Stage", "")
|
237 |
+
|
238 |
+
# Build the query
|
239 |
+
query_conditions = []
|
240 |
+
if product_name:
|
241 |
+
query_conditions.append(f"Productname__c = '{product_name}'")
|
242 |
+
if model_name:
|
243 |
+
query_conditions.append(f"Model__c = '{model_name}'")
|
244 |
+
if stage:
|
245 |
+
query_conditions.append(f"Stage__c = '{stage}'")
|
246 |
+
|
247 |
+
if query_conditions:
|
248 |
+
query = f"SELECT Price__c FROM Inventory_Management__c WHERE {' AND '.join(query_conditions)} LIMIT 1"
|
249 |
+
response = sf.query(query)
|
250 |
+
|
251 |
+
if response["records"]:
|
252 |
+
price = response["records"][0].get("Price__c", None)
|
253 |
+
if price:
|
254 |
+
price_message = f"The estimated price for the {product_name} with {model_name} at {stage} is ₹{price:,}."
|
255 |
+
return f"{message}\n\n{price_message}"
|
256 |
+
else:
|
257 |
+
return f"{message}\n\nPrice information not available for the specified product."
|
258 |
+
else:
|
259 |
+
return f"{message}\n\nNo matching record found for the specified product."
|
260 |
+
else:
|
261 |
+
return f"{message}\n\nInsufficient data to fetch price information."
|
262 |
+
|
263 |
+
except Exception as e:
|
264 |
+
return f"{message}\n\nError fetching price information: {str(e)}"
|
265 |
+
|
266 |
except Exception as e:
|
267 |
return f"❌ Error exporting to Salesforce: {str(e)}"
|
268 |
|
|
|
278 |
)
|
279 |
|
280 |
if data_type == "Inventory":
|
281 |
+
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c,Last_Modified_Date__c FROM Inventory_Management__c LIMIT 100"
|
282 |
else:
|
283 |
+
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c, Last_Modified_Date__c FROM Un_Billable__c LIMIT 100"
|
284 |
|
285 |
response = sf.query_all(query)
|
286 |
records = response.get("records", [])
|
|
|
290 |
|
291 |
df = pd.DataFrame(records)
|
292 |
df = df.drop(columns=['attributes'], errors='ignore')
|
293 |
+
|
294 |
+
# Format the Last_Modified_Date__c field to show only the date
|
295 |
+
if "Last_Modified_Date__c" in df.columns:
|
296 |
+
df["Last_Modified_Date__c"] = pd.to_datetime(df["Last_Modified_Date__c"]).dt.date
|
297 |
+
|
298 |
|
299 |
# Rename columns for better readability
|
300 |
df.rename(columns={
|
|
|
305 |
"Current_Stocks__c": "Current Stocks",
|
306 |
"Current_Stock__c": "Current Stocks",
|
307 |
"soldstock__c": "Sold Stock",
|
308 |
+
"Last_Modified_Date__c": "Last Modified Date"
|
309 |
}, inplace=True)
|
310 |
|
311 |
excel_path = "salesforce_data.xlsx"
|