Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -150,75 +150,91 @@ 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 |
-
|
|
|
|
| 154 |
try:
|
| 155 |
-
sf =
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 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 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 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 |
-
attributes = extract_attributes(extracted_text)
|
| 197 |
|
| 198 |
-
if not
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
if
|
| 204 |
-
|
| 205 |
-
response = sf.query(query)
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
-
|
| 214 |
else:
|
| 215 |
-
|
| 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"
|
| 222 |
|
| 223 |
# Function to pull structured data from Salesforce and display as a table
|
| 224 |
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 |
+
# Function to handle exit mode - sales
|
| 154 |
+
def subtract_stock_from_inventory(data):
|
| 155 |
try:
|
| 156 |
+
sf = authenticate_salesforce()
|
| 157 |
+
object_name = "Inventory_Management__c"
|
| 158 |
+
product_name = data.get("Productname__c")
|
| 159 |
+
quantity = data.get("Quantity__c", 0)
|
|
|
|
| 160 |
|
| 161 |
+
if not product_name:
|
| 162 |
+
return "Product name is missing in the data. Cannot update stock."
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
query = f"SELECT Id, Quantity_Sold__c FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"
|
| 165 |
+
response = sf.query(query)
|
| 166 |
+
|
| 167 |
+
if not response["records"]:
|
| 168 |
+
return f"No stock found for product '{product_name}'. Cannot update stock."
|
| 169 |
+
|
| 170 |
+
record_id = response["records"][0]["Id"]
|
| 171 |
+
current_quantity_sold = response["records"][0].get("Quantity_Sold__c", 0)
|
| 172 |
+
updated_quantity_sold = current_quantity_sold + quantity
|
| 173 |
+
|
| 174 |
+
sf.__getattr__(object_name).update(record_id, {"Quantity_Sold__c": updated_quantity_sold})
|
| 175 |
+
return f"Stock updated successfully in exit mode. Quantity sold for product '{product_name}': {updated_quantity_sold}."
|
| 176 |
+
except Exception as e:
|
| 177 |
+
return f"Error updating stock in Inventory_Management__c: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
# Function to handle exit mode - non-sales
|
| 180 |
+
def subtract_stock_from_non_sales(data):
|
| 181 |
+
try:
|
| 182 |
+
sf = authenticate_salesforce()
|
| 183 |
+
object_name = "Un_Billable__c"
|
| 184 |
+
product_name = data.get("Productname__c")
|
| 185 |
+
quantity = data.get("Quantity__c", 0)
|
| 186 |
+
|
| 187 |
+
if not product_name or not quantity:
|
| 188 |
+
return "Missing product name or quantity. Cannot process non-sales update."
|
| 189 |
|
| 190 |
+
schema = sf.__getattr__(object_name).describe()
|
|
|
|
|
|
|
| 191 |
valid_fields = {field["name"] for field in schema["fields"]}
|
| 192 |
|
| 193 |
+
if "Sold_Out__c" in valid_fields:
|
| 194 |
+
data["Sold_Out__c"] = quantity
|
|
|
|
| 195 |
|
| 196 |
+
filtered_record = {k: v for k, v in data.items() if k in valid_fields and v is not None}
|
| 197 |
+
sf.__getattr__(object_name).create(filtered_record)
|
| 198 |
+
return f"Non-sales data successfully updated in {object_name}."
|
| 199 |
+
except Exception as e:
|
| 200 |
+
return f"Error updating non-sales data in Un_Billable__c: {str(e)}"
|
| 201 |
|
| 202 |
+
# Unified function for processing
|
| 203 |
+
def process_image(image, quantity, mode, entry_type):
|
| 204 |
+
try:
|
| 205 |
+
extracted_text = extract_text(image)
|
| 206 |
+
attributes = find_attributes(extracted_text)
|
| 207 |
+
|
| 208 |
+
if "Productname__c" not in attributes or not attributes["Productname__c"]:
|
| 209 |
+
attributes["Productname__c"] = match_product_name(extracted_text)
|
| 210 |
+
|
| 211 |
+
attributes["Quantity__c"] = sanitize_numeric(quantity)
|
| 212 |
|
| 213 |
+
if not attributes:
|
| 214 |
+
return f"Extracted Text:\n{extracted_text}\n\nNo attributes found in the image.", None
|
|
|
|
| 215 |
|
| 216 |
+
file_path = save_to_excel(attributes)
|
| 217 |
+
|
| 218 |
+
if mode == "Entry":
|
| 219 |
+
if entry_type == "Sales":
|
| 220 |
+
message = add_sales_entry(attributes)
|
| 221 |
+
elif entry_type == "Non-Sales":
|
| 222 |
+
message = add_non_sales_entry(attributes)
|
| 223 |
+
else:
|
| 224 |
+
message = "Invalid entry type. Please select Sales or Non-Sales."
|
| 225 |
+
elif mode == "Exit":
|
| 226 |
+
if entry_type == "Sales":
|
| 227 |
+
message = subtract_stock_from_inventory(attributes)
|
| 228 |
+
elif entry_type == "Non-Sales":
|
| 229 |
+
message = subtract_stock_from_non_sales(attributes)
|
| 230 |
else:
|
| 231 |
+
message = "Invalid entry type. Please select Sales or Non-Sales."
|
| 232 |
else:
|
| 233 |
+
message = "Invalid mode. Please select Entry or Exit."
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
return f"Extracted Text:\n{extracted_text}\n\n{message}", file_path
|
| 236 |
except Exception as e:
|
| 237 |
+
return f"Error during processing: {str(e)}", None
|
| 238 |
|
| 239 |
# Function to pull structured data from Salesforce and display as a table
|
| 240 |
def pull_data_from_salesforce():
|