Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -149,76 +149,155 @@ 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 |
-
|
| 153 |
-
def
|
| 154 |
try:
|
| 155 |
sf = Salesforce(
|
| 156 |
username=SALESFORCE_USERNAME,
|
| 157 |
password=SALESFORCE_PASSWORD,
|
| 158 |
-
security_token=SALESFORCE_SECURITY_TOKEN
|
| 159 |
)
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 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 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 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 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
|
| 190 |
sf_object = sf.__getattr__(object_name)
|
|
|
|
| 191 |
schema = sf_object.describe()
|
| 192 |
valid_fields = {field["name"] for field in schema["fields"]}
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
if not product_name:
|
| 199 |
-
return "Product name
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 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 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
-
|
| 214 |
else:
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
return f"✅ Data successfully exported to Salesforce object {object_name}."
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
-
return f"
|
| 222 |
|
| 223 |
# Function to pull structured data from Salesforce and display as a table
|
| 224 |
def pull_data_from_salesforce():
|
|
|
|
| 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 handle sales entry
|
| 153 |
+
def add_sales_entry(data):
|
| 154 |
try:
|
| 155 |
sf = Salesforce(
|
| 156 |
username=SALESFORCE_USERNAME,
|
| 157 |
password=SALESFORCE_PASSWORD,
|
| 158 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 159 |
)
|
| 160 |
|
| 161 |
+
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 162 |
+
sf_object = sf.__getattr__(object_name)
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
schema = sf_object.describe()
|
| 165 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
| 166 |
+
|
| 167 |
+
filtered_record = {k: v for k, v in data.items() if k in valid_fields and v is not None}
|
| 168 |
+
sf_object.create(filtered_record)
|
| 169 |
+
return f"Sales data successfully added to {object_name}."
|
| 170 |
+
except Exception as e:
|
| 171 |
+
return f"Error adding sales data to VENKATA_RAMANA_MOTORS__c: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
# Function to handle non-sales entry
|
| 174 |
+
def add_non_sales_entry(data):
|
| 175 |
+
try:
|
| 176 |
+
sf = Salesforce(
|
| 177 |
+
username=SALESFORCE_USERNAME,
|
| 178 |
+
password=SALESFORCE_PASSWORD,
|
| 179 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 180 |
+
)
|
| 181 |
|
| 182 |
+
object_name = "UNBILLING_DATA__c"
|
| 183 |
sf_object = sf.__getattr__(object_name)
|
| 184 |
+
|
| 185 |
schema = sf_object.describe()
|
| 186 |
valid_fields = {field["name"] for field in schema["fields"]}
|
| 187 |
|
| 188 |
+
# Add TotalQuantity__c to the data for non-sales
|
| 189 |
+
if "TotalQuantity__c" in valid_fields and "Quantity__c" in data:
|
| 190 |
+
data["TotalQuantity__c"] = data["Quantity__c"]
|
| 191 |
+
|
| 192 |
+
filtered_record = {k: v for k, v in data.items() if k in valid_fields and v is not None}
|
| 193 |
+
sf_object.create(filtered_record)
|
| 194 |
+
return f"Non-sales data successfully added to {object_name}."
|
| 195 |
+
except Exception as e:
|
| 196 |
+
return f"Error adding non-sales data to UNBILLING_DATA__c: {str(e)}"
|
| 197 |
+
|
| 198 |
+
# Function to handle exit mode - sales
|
| 199 |
+
def subtract_stock_from_inventory(data):
|
| 200 |
+
try:
|
| 201 |
+
sf = Salesforce(
|
| 202 |
+
username=SALESFORCE_USERNAME,
|
| 203 |
+
password=SALESFORCE_PASSWORD,
|
| 204 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
object_name = "Inventory_Management__c"
|
| 208 |
+
sf_object = sf.__getattr__(object_name)
|
| 209 |
+
|
| 210 |
+
product_name = data.get("Productname__c")
|
| 211 |
+
quantity = data.get("Quantity__c", 0)
|
| 212 |
|
| 213 |
if not product_name:
|
| 214 |
+
return "Product name is missing in the data. Cannot update stock."
|
| 215 |
|
| 216 |
+
# Query existing stock record
|
| 217 |
+
query = f"SELECT Id, Quantity_Sold__c FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"
|
| 218 |
+
response = sf.query(query)
|
| 219 |
+
|
| 220 |
+
if not response["records"]:
|
| 221 |
+
return f"No stock found for product '{product_name}'. Cannot update stock."
|
| 222 |
+
|
| 223 |
+
record_id = response["records"][0]["Id"]
|
| 224 |
+
current_quantity_sold = response["records"][0].get("Quantity_Sold__c", 0)
|
| 225 |
+
|
| 226 |
+
# Update the quantity sold
|
| 227 |
+
updated_quantity_sold = current_quantity_sold + quantity
|
| 228 |
|
| 229 |
+
sf_object.update(record_id, {"Quantity_Sold__c": updated_quantity_sold})
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
return f"Stock updated successfully in exit mode. Quantity sold for product '{product_name}': {updated_quantity_sold}."
|
| 232 |
+
except Exception as e:
|
| 233 |
+
return f"Error updating stock in Inventory_Management__c: {str(e)}"
|
| 234 |
+
|
| 235 |
+
# Function to handle exit mode - non-sales
|
| 236 |
+
def subtract_stock_from_non_sales(data):
|
| 237 |
+
try:
|
| 238 |
+
sf = Salesforce(
|
| 239 |
+
username=SALESFORCE_USERNAME,
|
| 240 |
+
password=SALESFORCE_PASSWORD,
|
| 241 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
object_name = "Un_Billable__c"
|
| 245 |
+
sf_object = sf.__getattr__(object_name)
|
| 246 |
+
|
| 247 |
+
schema = sf_object.describe()
|
| 248 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
| 249 |
+
|
| 250 |
+
# Add Sold_Out__c to the data for non-sales exit
|
| 251 |
+
if "Sold_Out__c" in valid_fields and "Quantity__c" in data:
|
| 252 |
+
data["Sold_Out__c"] = data["Quantity__c"]
|
| 253 |
+
|
| 254 |
+
filtered_record = {k: v for k, v in data.items() if k in valid_fields and v is not None}
|
| 255 |
+
sf_object.create(filtered_record)
|
| 256 |
+
return f"Non-sales data successfully updated in {object_name}."
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return f"Error updating non-sales data in Un_Billable__c: {str(e)}"
|
| 259 |
+
|
| 260 |
+
# Unified function for processing
|
| 261 |
+
def process_image(image, quantity, mode, entry_type):
|
| 262 |
+
try:
|
| 263 |
+
extracted_text = extract_text(image) # Extract text from image
|
| 264 |
+
attributes = find_attributes(extracted_text) # Find attributes from extracted text
|
| 265 |
+
|
| 266 |
+
if "Productname__c" not in attributes or not attributes["Productname__c"]:
|
| 267 |
+
attributes["Productname__c"] = match_product_name(extracted_text)
|
| 268 |
+
|
| 269 |
+
attributes["Quantity__c"] = sanitize_numeric(quantity)
|
| 270 |
+
|
| 271 |
+
if not attributes:
|
| 272 |
+
return f"Extracted Text:\n{extracted_text}\n\nNo attributes found in the image.", None
|
| 273 |
+
|
| 274 |
+
numbered_output = "\n".join(
|
| 275 |
+
[f"{key.replace('__c', '').replace('_', ' ').title()}: {value}" for key, value in attributes.items()]
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
file_path = save_to_excel(attributes)
|
| 279 |
+
|
| 280 |
+
if mode == "Entry":
|
| 281 |
+
if entry_type == "Sales":
|
| 282 |
+
message = add_sales_entry(attributes)
|
| 283 |
+
elif entry_type == "Non-Sales":
|
| 284 |
+
message = add_non_sales_entry(attributes)
|
| 285 |
+
else:
|
| 286 |
+
message = "Invalid entry type. Please select Sales or Non-Sales."
|
| 287 |
+
elif mode == "Exit":
|
| 288 |
+
if entry_type == "Sales":
|
| 289 |
+
message = subtract_stock_from_inventory(attributes)
|
| 290 |
+
elif entry_type == "Non-Sales":
|
| 291 |
+
message = subtract_stock_from_non_sales(attributes)
|
| 292 |
else:
|
| 293 |
+
message = "Invalid entry type. Please select Sales or Non-Sales."
|
| 294 |
else:
|
| 295 |
+
message = "Invalid mode. Please select Entry or Exit."
|
| 296 |
+
|
| 297 |
+
return f"Extracted Text:\n{extracted_text}\n\n{numbered_output}\n\n{message}", file_path
|
|
|
|
| 298 |
|
| 299 |
except Exception as e:
|
| 300 |
+
return f"Error during processing: {str(e)}", None
|
| 301 |
|
| 302 |
# Function to pull structured data from Salesforce and display as a table
|
| 303 |
def pull_data_from_salesforce():
|