gopichandra commited on
Commit
53b2fcb
·
verified ·
1 Parent(s): 6e97cfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -54
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
- def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
 
154
  try:
155
- sf = Salesforce(
156
- username=SALESFORCE_USERNAME,
157
- password=SALESFORCE_PASSWORD,
158
- security_token=SALESFORCE_SECURITY_TOKEN
159
- )
160
 
161
- # Mapping mode and entry_type to Salesforce object and field
162
- object_name = None
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
- if mode == "Entry":
168
- if entry_type == "Sales":
169
- object_name = "VENKATA_RAMANA_MOTORS__c"
170
- field_name = "Quantity__c"
171
- elif entry_type == "Non-Sales":
172
- object_name = "UNBILLING_DATA__c"
173
- field_name = "TotalQuantity__c"
174
- elif mode == "Exit":
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
- if not object_name or not field_name:
187
- return "Invalid mode or entry type."
 
 
 
 
 
 
 
 
188
 
189
- # Get valid fields for the specified Salesforce object
190
- sf_object = sf.__getattr__(object_name)
191
- schema = sf_object.describe()
192
  valid_fields = {field["name"] for field in schema["fields"]}
193
 
194
- # Extract product name or model number
195
- product_name = match_product_name(extracted_text)
196
- attributes = extract_attributes(extracted_text)
197
 
198
- if not product_name:
199
- return "Product name could not be matched from the extracted text."
 
 
 
200
 
201
- attributes["Product name"] = product_name
 
 
 
 
 
 
 
 
 
202
 
203
- if mode == "Exit":
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
- if response["records"]:
208
- record_id = response["records"][0]["Id"]
209
- updated_quantity = quantity # Overwrite the quantity, don't add
210
- sf_object.update(record_id, {field_name: updated_quantity})
211
- return f"Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
 
 
 
 
 
 
 
 
 
212
  else:
213
- return f"No matching record found for product '{product_name}' in {object_name}."
214
  else:
215
- filtered_attributes = filter_valid_attributes(attributes, valid_fields)
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"Error interacting with Salesforce: {str(e)}"
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():