gopichandra commited on
Commit
ac8bc3f
Β·
verified Β·
1 Parent(s): 04ced05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -72
app.py CHANGED
@@ -150,80 +150,70 @@ 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
- # Define the correct object and field mappings
162
- object_mapping = {
163
- ("Entry", "Sales"): ("VENKATA_RAMANA_MOTORS__c", "Quantity__c"),
164
- ("Entry", "Non-Sales"): ("UNBILLING_DATA__c", "TotalQuantity__c"),
165
- ("Exit", "Sales"): ("Inventory_Management__c", "Quantity_Sold__c"),
166
- ("Exit", "Non-Sales"): ("Un_Billable__c", "Sold_Out__c"),
167
- }
168
-
169
- object_name, field_name = object_mapping.get((mode, entry_type), (None, None))
170
- if not object_name or not field_name:
171
- return "❌ Invalid mode or entry type."
172
-
173
- product_field_name = "Product_Name__c"
174
- model_field_name = "Modal_Name__c"
175
-
176
- # Extract product name and model name using OCR and fuzzy matching
177
- product_name = match_product_name(extracted_text)
178
- attributes = extract_attributes(extracted_text)
179
- model_name = attributes.get("Model Name", "").strip()
180
-
181
- if not product_name:
182
- return "❌ Could not match a valid Product Name from the image."
183
-
184
- attributes["Product name"] = product_name
185
-
186
- # Standardize case and remove extra spaces for better accuracy
187
- product_name = product_name.strip().lower()
188
- model_name = model_name.lower()
189
-
190
- # Get valid fields for the object
191
- valid_fields = {field["name"] for field in sf.__getattr__(object_name).describe()["fields"]}
192
-
193
- # πŸš€ Exit Mode: Find Exact Matching Product or Model & Update Quantity
194
- if mode == "Exit":
195
- query = f"""
196
- SELECT Id, {field_name} FROM {object_name}
197
- WHERE LOWER({product_field_name}) = '{product_name}'
198
- OR LOWER({model_field_name}) = '{model_name}'
199
- LIMIT 1
200
- """
201
- response = sf.query(query)
202
-
203
- if response["records"]: # βœ… If record exists, update quantity
204
- record_id = response["records"][0]["Id"]
205
- existing_quantity = response["records"][0].get(field_name, 0)
206
- updated_quantity = existing_quantity + quantity # Increase quantity
207
-
208
- sf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})
209
-
210
- return f"βœ… Updated '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
211
- else: # ❌ If no match, create a new record
212
- filtered_attributes = {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
213
- filtered_attributes[field_name] = quantity
214
- sf.__getattr__(object_name).create(filtered_attributes)
215
-
216
- return f"πŸ†• Created new record for '{product_name}' in {object_name} with {field_name}: {quantity}."
217
-
218
- # πŸš€ Entry Mode: Always Create a New Record
219
- filtered_attributes = {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
220
- filtered_attributes[field_name] = quantity
221
- sf.__getattr__(object_name).create(filtered_attributes)
222
 
223
- return f"βœ… Successfully exported data to Salesforce object {object_name}."
 
224
 
225
- except Exception as e:
226
- return f"❌ Error interacting with Salesforce: {str(e)}"
227
 
228
  # Function to pull structured data from Salesforce and display as a table
229
  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
+ def export_entry_sales(sf, product_data):
154
+ """
155
+ Exports data to VENKATA_RAMANA_MOTORS__c and UNBILLING_DATA__c objects when entry and sales are clicked.
156
+ """
157
+ venkata_data = {
158
+ 'Product_Name__c': product_data['Product_Name__c'],
159
+ 'Modal_Name__c': product_data['Modal_Name__c'],
160
+ 'Quantity__c': product_data['Quantity']
161
+ }
162
+ sf.VENKATA_RAMANA_MOTORS__c.create(venkata_data)
163
+
164
+ unbilling_data = {
165
+ 'Product_Name__c': product_data['Product_Name__c'],
166
+ 'Modal_Name__c': product_data['Modal_Name__c'],
167
+ 'TotalQuantity__c': product_data['Quantity']
168
+ }
169
+ sf.UNBILLING_DATA__c.create(unbilling_data)
170
+
171
+ print("Entry and Sales data exported successfully.")
172
+
173
+ def update_exit_sales(sf, product_data):
174
+ """
175
+ Updates Inventory_Management__c and Un_Billable__c objects when exit mode sales is clicked.
176
+ """
177
+ # Find matching record in Inventory_Management__c
178
+ inventory_records = sf.query(f"""
179
+ SELECT Id, Quantity_Sold__c FROM Inventory_Management__c
180
+ WHERE Product_Name__c = '{product_data['Product_Name__c']}'
181
+ OR Modal_Name__c = '{product_data['Modal_Name__c']}'
182
+ """)
183
+
184
+ if inventory_records['records']:
185
+ inventory_id = inventory_records['records'][0]['Id']
186
+ new_quantity_sold = inventory_records['records'][0]['Quantity_Sold__c'] + product_data['Quantity']
187
+ sf.Inventory_Management__c.update(inventory_id, {'Quantity_Sold__c': new_quantity_sold})
188
+ print("Inventory Management updated successfully.")
189
+
190
+ # Find matching record in Un_Billable__c
191
+ unbillable_records = sf.query(f"""
192
+ SELECT Id, Sold_Out__c FROM Un_Billable__c
193
+ WHERE Product_Name__c = '{product_data['Product_Name__c']}'
194
+ OR Model_Name__c = '{product_data['Modal_Name__c']}'
195
+ """)
196
+
197
+ if unbillable_records['records']:
198
+ unbillable_id = unbillable_records['records'][0]['Id']
199
+ new_sold_out_quantity = unbillable_records['records'][0]['Sold_Out__c'] + product_data['Quantity']
200
+ sf.Un_Billable__c.update(unbillable_id, {'Sold_Out__c': new_sold_out_quantity})
201
+ print("Un_Billable updated successfully.")
202
+
203
+ # Example usage
204
+ sf = Salesforce(username='your_username', password='your_password', security_token='your_security_token')
205
+
206
+ product_data = {
207
+ 'Product_Name__c': 'Example Product',
208
+ 'Modal_Name__c': 'Example Model',
209
+ 'Quantity': 10
210
+ }
 
 
 
 
 
 
 
 
 
 
 
211
 
212
+ # When clicking entry and sales
213
+ export_entry_sales(sf, product_data)
214
 
215
+ # When clicking exit mode sales
216
+ update_exit_sales(sf, product_data)
217
 
218
  # Function to pull structured data from Salesforce and display as a table
219
  def pull_data_from_salesforce():