gopichandra commited on
Commit
a795d87
·
verified ·
1 Parent(s): 81f85f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -58
app.py CHANGED
@@ -15,64 +15,15 @@ import kaleido # Ensure kaleido is imported
15
  ATTRIBUTE_MAPPING = {
16
  "Product name": "Productname__c",
17
  "Colour": "Colour__c",
18
- "Motortype": "Motortype__c",
19
  "Frequency": "Frequency__c",
20
- "Grossweight": "Grossweight__c",
21
- "Ratio": "Ratio__c",
22
- "MotorFrame": "Motorframe__c",
23
  "Model": "Model__c",
24
  "Speed": "Speed__c",
25
  "Quantity": "Quantity__c",
26
  "Voltage": "Voltage__c",
27
- "Material": "Material__c",
28
  "Type": "Type__c",
29
- "Horsepower": "Horsepower__c",
30
- "Consignee": "Consignee__c",
31
- "LOT": "LOT__c",
32
  "Stage": "Stage__c",
33
  "Outlet": "Outlet__c",
34
- "Serialnumber": "Serialnumber__c",
35
- "HeadSize": "Headsize__c",
36
- "Deliverysize": "Deliverysize__c",
37
  "Phase": "Phase__c",
38
- "Size": "Size__c",
39
- "MRP": "MRP__c",
40
- "Usebefore": "Usebefore__c",
41
- "Height": "Height__c",
42
- "MaximumDischarge Flow": "Maximumdischargeflow__c",
43
- "DischargeRange": "Dischargeflow__c",
44
- "Assembledby": "Manufacturer__c",
45
- "Manufacturedate": "Manufacturedate__c",
46
- "Companyname": "Companyname__c",
47
- "Customercarenumber": "Customercarenumber__c",
48
- "SellerAddress": "Selleraddress__c",
49
- "Selleremail": "Selleremail__c",
50
- "GSTIN": "GSTIN__c",
51
- "Totalamount": "Totalamount__c",
52
- "Paymentstatus": "Paymentstatus__c",
53
- "Paymentmethod": "Paymentstatus__c",
54
- "Invoicedate": "Manufacturedate__c",
55
- "Warranty": "Warranty__c",
56
- "Brand": "Brand__c",
57
- "Motorhorsepower": "Motorhorsepower__c",
58
- "Power": "Power__c",
59
- "Motorphase": "Motorphase__c",
60
- "Enginetype": "Enginetype__c",
61
- "Tankcapacity": "Tankcapacity__c",
62
- "Head": "Head__c",
63
- "Usage/Application": "Usage_Application__c",
64
- "Volts": "volts__c",
65
- "Hertz": "Hertz__c",
66
- "Frame": "frame__c",
67
- "Mounting": "Mounting__c",
68
- "Tollfreenumber": "Tollfreenumber__c",
69
- "Pipesize": "Pipesize__c",
70
- "Manufacturer": "Manufacturer__c",
71
- "Office": "Office__c",
72
- "SRnumber": "SRnumber__c",
73
- "TypeOfEndUse": "TypeOfEndUse__c",
74
- "Model Name": "Model_Name_Number__c",
75
- "coolingmethod": "coolingmethod__c",
76
  "H.P.": "H_p__c"
77
  }
78
 
@@ -141,6 +92,8 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
141
  field_names = []
142
  product_field_name = "Productname__c"
143
  model_field_name = "Model__c"
 
 
144
 
145
  if mode == "Entry":
146
  if entry_type == "Sales":
@@ -153,11 +106,9 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
153
  if entry_type == "Sales":
154
  object_name = "Inventory_Management__c"
155
  field_names = ["Quantity_Sold__c", "soldstock__c"]
156
- model_field_name = "Model__c"
157
  elif entry_type == "Non-Sales":
158
  object_name = "Un_Billable__c"
159
  field_names = ["Sold_Out__c", "soldstock__c"]
160
- model_field_name = "Model__c"
161
 
162
  if not object_name or (not field_name and not field_names):
163
  return "Invalid mode or entry type."
@@ -171,6 +122,8 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
171
  product_name = match_product_name(extracted_text)
172
  attributes = extract_attributes(extracted_text)
173
  model_name = attributes.get("Model Name", "").strip()
 
 
174
 
175
  if not product_name:
176
  return "Product name could not be matched from the extracted text."
@@ -179,22 +132,35 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
179
 
180
  # Handling "Exit" Mode (Updating Records)
181
  if mode == "Exit":
182
- # Query should only match exact product name or exact model name
183
- query_conditions = []
184
  if model_name:
185
- query_conditions.append(f"{model_field_name} = '{Model__c}'")
186
- query_conditions.append(f"{product_field_name} = '{Productname__c}'")
 
 
 
187
 
188
- query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
189
  response = sf.query(query)
190
 
191
  if response["records"]:
192
  record_id = response["records"][0]["Id"]
193
  updated_fields = {field: quantity for field in field_names}
194
  sf_object.update(record_id, updated_fields)
195
- return f"✅ Updated record for product '{Productname__c}' ({Model__c}) in {object_name}. Updated fields: {updated_fields}."
196
  else:
197
- return f"❌ No matching record found for product '{Productname__c}' ({Model__c}) in {object_name}."
 
 
 
 
 
 
 
 
 
 
198
 
199
  # Handling "Entry" Mode (Creating Records)
200
  else:
 
15
  ATTRIBUTE_MAPPING = {
16
  "Product name": "Productname__c",
17
  "Colour": "Colour__c",
 
18
  "Frequency": "Frequency__c",
 
 
 
19
  "Model": "Model__c",
20
  "Speed": "Speed__c",
21
  "Quantity": "Quantity__c",
22
  "Voltage": "Voltage__c",
 
23
  "Type": "Type__c",
 
 
 
24
  "Stage": "Stage__c",
25
  "Outlet": "Outlet__c",
 
 
 
26
  "Phase": "Phase__c",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  "H.P.": "H_p__c"
28
  }
29
 
 
92
  field_names = []
93
  product_field_name = "Productname__c"
94
  model_field_name = "Model__c"
95
+ stage_field_name = "Stage__c"
96
+ hp_field_name = "H_p__c"
97
 
98
  if mode == "Entry":
99
  if entry_type == "Sales":
 
106
  if entry_type == "Sales":
107
  object_name = "Inventory_Management__c"
108
  field_names = ["Quantity_Sold__c", "soldstock__c"]
 
109
  elif entry_type == "Non-Sales":
110
  object_name = "Un_Billable__c"
111
  field_names = ["Sold_Out__c", "soldstock__c"]
 
112
 
113
  if not object_name or (not field_name and not field_names):
114
  return "Invalid mode or entry type."
 
122
  product_name = match_product_name(extracted_text)
123
  attributes = extract_attributes(extracted_text)
124
  model_name = attributes.get("Model Name", "").strip()
125
+ stage = attributes.get("Stage", "").strip()
126
+ hp = attributes.get("H.P.", "").strip()
127
 
128
  if not product_name:
129
  return "Product name could not be matched from the extracted text."
 
132
 
133
  # Handling "Exit" Mode (Updating Records)
134
  if mode == "Exit":
135
+ # Query should match exact product name, model name, stage, and hp if available
136
+ query_conditions = [f"{product_field_name} = '{product_name}'"]
137
  if model_name:
138
+ query_conditions.append(f"{model_field_name} = '{model_name}'")
139
+ if stage:
140
+ query_conditions.append(f"{stage_field_name} = '{stage}'")
141
+ if hp:
142
+ query_conditions.append(f"{hp_field_name} = '{hp}'")
143
 
144
+ query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' AND '.join(query_conditions)} LIMIT 1"
145
  response = sf.query(query)
146
 
147
  if response["records"]:
148
  record_id = response["records"][0]["Id"]
149
  updated_fields = {field: quantity for field in field_names}
150
  sf_object.update(record_id, updated_fields)
151
+ return f"✅ Updated record for product '{product_name}' ({model_name}) in {object_name}. Updated fields: {updated_fields}."
152
  else:
153
+ # If no matching record found with all conditions, try with only product name
154
+ query_conditions = [f"{product_field_name} = '{product_name}'"]
155
+ query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' AND '.join(query_conditions)} LIMIT 1"
156
+ response = sf.query(query)
157
+ if response["records"]:
158
+ record_id = response["records"][0]["Id"]
159
+ updated_fields = {field: quantity for field in field_names}
160
+ sf_object.update(record_id, updated_fields)
161
+ return f"✅ Updated record for product '{product_name}' in {object_name}. Updated fields: {updated_fields}."
162
+ else:
163
+ return f"❌ No matching record found for product '{product_name}' in {object_name}."
164
 
165
  # Handling "Entry" Mode (Creating Records)
166
  else: