Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -229,9 +229,55 @@ def export_to_salesforce(mode, entry_type, quantity, edited_df):
|
|
| 229 |
try:
|
| 230 |
# Convert edited DataFrame back to dictionary
|
| 231 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
| 232 |
-
|
| 233 |
# Export to Salesforce
|
| 234 |
message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
return message
|
| 236 |
except Exception as e:
|
| 237 |
return f"❌ Error exporting to Salesforce: {str(e)}"
|
|
@@ -244,21 +290,21 @@ def pull_data_from_salesforce(data_type):
|
|
| 244 |
password=SALESFORCE_PASSWORD,
|
| 245 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 246 |
)
|
| 247 |
-
|
| 248 |
if data_type == "Inventory":
|
| 249 |
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c, Price__c FROM Inventory_Management__c LIMIT 100"
|
| 250 |
else:
|
| 251 |
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c, Price__c FROM Un_Billable__c LIMIT 100"
|
| 252 |
-
|
| 253 |
response = sf.query_all(query)
|
| 254 |
records = response.get("records", [])
|
| 255 |
-
|
| 256 |
if not records:
|
| 257 |
return "No data found in Salesforce.", None, None, None
|
| 258 |
-
|
| 259 |
df = pd.DataFrame(records)
|
| 260 |
df = df.drop(columns=['attributes'], errors='ignore')
|
| 261 |
-
|
| 262 |
# Rename columns for better readability
|
| 263 |
df.rename(columns={
|
| 264 |
"Productname__c": "Product Name",
|
|
@@ -270,10 +316,10 @@ def pull_data_from_salesforce(data_type):
|
|
| 270 |
"soldstock__c": "Sold Stock",
|
| 271 |
"Price__c": "Price"
|
| 272 |
}, inplace=True)
|
| 273 |
-
|
| 274 |
excel_path = "salesforce_data.xlsx"
|
| 275 |
df.to_excel(excel_path, index=False)
|
| 276 |
-
|
| 277 |
# Generate interactive vertical bar graph using Matplotlib
|
| 278 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 279 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
|
@@ -286,7 +332,7 @@ def pull_data_from_salesforce(data_type):
|
|
| 286 |
plt.savefig(buffer, format="png")
|
| 287 |
buffer.seek(0)
|
| 288 |
img = Image.open(buffer)
|
| 289 |
-
|
| 290 |
return df, excel_path, img
|
| 291 |
except Exception as e:
|
| 292 |
return f"Error fetching data: {str(e)}", None, None, None
|
|
|
|
| 229 |
try:
|
| 230 |
# Convert edited DataFrame back to dictionary
|
| 231 |
edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
|
| 232 |
+
|
| 233 |
# Export to Salesforce
|
| 234 |
message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
|
| 235 |
+
|
| 236 |
+
# Fetch the Price__c field value from Salesforce
|
| 237 |
+
if mode == "Exit" and entry_type == "Sales":
|
| 238 |
+
sf = Salesforce(
|
| 239 |
+
username=SALESFORCE_USERNAME,
|
| 240 |
+
password=SALESFORCE_PASSWORD,
|
| 241 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
| 242 |
+
)
|
| 243 |
+
object_name = "Inventory_Management__c"
|
| 244 |
+
product_field_name = "Productname__c"
|
| 245 |
+
model_field_name = "Model__c"
|
| 246 |
+
stage_field_name = "Stage__c"
|
| 247 |
+
hp_field_name = "H_p__c"
|
| 248 |
+
price_field_name = "Price__c"
|
| 249 |
+
|
| 250 |
+
query_conditions = [f"{product_field_name} = '{edited_attributes.get('Product name', '')}'"]
|
| 251 |
+
if "Model Name" in edited_attributes and edited_attributes["Model Name"]:
|
| 252 |
+
query_conditions.append(f"{model_field_name} = '{edited_attributes['Model Name']}'")
|
| 253 |
+
if "Stage" in edited_attributes and edited_attributes["Stage"]:
|
| 254 |
+
query_conditions.append(f"{stage_field_name} = '{edited_attributes['Stage']}'")
|
| 255 |
+
if "H.P." in edited_attributes and edited_attributes["H.P."]:
|
| 256 |
+
query_conditions.append(f"{hp_field_name} = '{edited_attributes['H.P.']}'")
|
| 257 |
+
|
| 258 |
+
query = f"SELECT {product_field_name}, {model_field_name}, {stage_field_name}, {hp_field_name}, {price_field_name} FROM {object_name} WHERE {' AND '.join(query_conditions)} LIMIT 1"
|
| 259 |
+
response = sf.query(query)
|
| 260 |
+
|
| 261 |
+
if response["records"]:
|
| 262 |
+
record = response["records"][0]
|
| 263 |
+
product_name = record.get(product_field_name, "N/A")
|
| 264 |
+
model = record.get(model_field_name, "N/A")
|
| 265 |
+
stage = record.get(stage_field_name, "N/A")
|
| 266 |
+
hp = record.get(hp_field_name, "N/A")
|
| 267 |
+
price = record.get(price_field_name, "N/A")
|
| 268 |
+
|
| 269 |
+
# Append the details to the result message
|
| 270 |
+
message += (
|
| 271 |
+
f"\nDetails:\n"
|
| 272 |
+
f"Product Name: {product_name}\n"
|
| 273 |
+
f"Model: {model}\n"
|
| 274 |
+
f"Stage: {stage}\n"
|
| 275 |
+
f"H.P.: {hp}\n"
|
| 276 |
+
f"Price: {price}"
|
| 277 |
+
)
|
| 278 |
+
else:
|
| 279 |
+
message += "\n❌ No matching record found to fetch additional details."
|
| 280 |
+
|
| 281 |
return message
|
| 282 |
except Exception as e:
|
| 283 |
return f"❌ Error exporting to Salesforce: {str(e)}"
|
|
|
|
| 290 |
password=SALESFORCE_PASSWORD,
|
| 291 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 292 |
)
|
| 293 |
+
|
| 294 |
if data_type == "Inventory":
|
| 295 |
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c, Price__c FROM Inventory_Management__c LIMIT 100"
|
| 296 |
else:
|
| 297 |
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c, Price__c FROM Un_Billable__c LIMIT 100"
|
| 298 |
+
|
| 299 |
response = sf.query_all(query)
|
| 300 |
records = response.get("records", [])
|
| 301 |
+
|
| 302 |
if not records:
|
| 303 |
return "No data found in Salesforce.", None, None, None
|
| 304 |
+
|
| 305 |
df = pd.DataFrame(records)
|
| 306 |
df = df.drop(columns=['attributes'], errors='ignore')
|
| 307 |
+
|
| 308 |
# Rename columns for better readability
|
| 309 |
df.rename(columns={
|
| 310 |
"Productname__c": "Product Name",
|
|
|
|
| 316 |
"soldstock__c": "Sold Stock",
|
| 317 |
"Price__c": "Price"
|
| 318 |
}, inplace=True)
|
| 319 |
+
|
| 320 |
excel_path = "salesforce_data.xlsx"
|
| 321 |
df.to_excel(excel_path, index=False)
|
| 322 |
+
|
| 323 |
# Generate interactive vertical bar graph using Matplotlib
|
| 324 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 325 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
|
|
|
| 332 |
plt.savefig(buffer, format="png")
|
| 333 |
buffer.seek(0)
|
| 334 |
img = Image.open(buffer)
|
| 335 |
+
|
| 336 |
return df, excel_path, img
|
| 337 |
except Exception as e:
|
| 338 |
return f"Error fetching data: {str(e)}", None, None, None
|