utkarsh134 commited on
Commit
6ec1187
·
verified ·
1 Parent(s): fce5055

Update parsing.py

Browse files
Files changed (1) hide show
  1. parsing.py +41 -53
parsing.py CHANGED
@@ -5,6 +5,7 @@ import json
5
  import re
6
  from dotenv import load_dotenv
7
  import requests
 
8
  from datetime import datetime
9
  load_dotenv()
10
 
@@ -107,61 +108,38 @@ def parse_data_with_llm(extracted_text):
107
 
108
  def compute_values(data):
109
  """Perform metal calculations and format diamonds separately."""
110
- # Fetch current prices
111
  gold_price, platinum_price = fetch_metal_prices()
112
 
113
- # Calculate prices for different karats
114
- price_14k = (14 / 24) * gold_price
115
- price_18k = (18 / 24) * gold_price
116
- price_pt = platinum_price
117
-
118
- # Calculate GBP prices (USD price * (1/0.78))
119
- gbp_14k = price_14k * (1/0.78)
120
- gbp_18k = price_18k * (1/0.78)
121
- gbp_pt = price_pt * (1/0.78)
122
-
123
- # Rest of the existing calculations
124
- wt_14k = sum(float(x) for x in data.get("Wt. in gram 14K", [])) if isinstance(data.get("Wt. in gram 14K"), list) else float(data.get("Wt. in gram 14K", 0))
125
- wt_18k = sum(float(x) for x in data.get("Wt. in gram 18K", [])) if isinstance(data.get("Wt. in gram 18K"), list) else float(data.get("Wt. in gram 18K", 0))
126
- wt_plt = sum(float(x) for x in data.get("Wt. in gram PLT", [])) if isinstance(data.get("Wt. in gram PLT"), list) else float(data.get("Wt. in gram PLT", 0))
127
-
128
- # Calculate rates
129
- gold_rate_14k = round((2800 / 31.1035) * 0.585 * 1.1, 2)
130
- gold_rate_18k = round((2800 / 31.1035) * 0.75 * 1.1, 2)
131
- plat_rate = round((1040 / 31.1035) * 0.95 * 1.2, 2)
132
-
133
- # Calculate values with rounding
134
- gold_value_14k = round(wt_14k * gold_rate_14k, 2)
135
- gold_value_18k = round(wt_18k * gold_rate_18k, 2)
136
- plat_value = round(wt_plt * plat_rate, 2)
137
-
138
- # Calculate labor costs
139
- labour_14k = round(12 if (wt_14k * 7) < 12 else (wt_14k * 7), 2)
140
- labour_18k = round(12 if (wt_18k * 7) < 12 else (wt_18k * 7), 2)
141
- labour_pt = round(15 if (wt_plt * 11) < 15 else (wt_plt * 11), 2)
142
-
143
  metal_data = {
144
  "Customer Ref": data.get("Customer Ref", ""),
145
- "Gold Lock @ $ 2800": 2800,
146
- "Platinum Lock $ 1040": 1040,
147
- "Wt. in gram 14K": wt_14k,
148
- "Wt. in gram 18K": wt_18k,
149
- "Wt. in gram PLT": wt_plt,
150
- "Gold Rate (including loss) 14KT": gold_rate_14k,
151
- "Gold Rate (including loss) 18KT": gold_rate_18k,
152
- "Plat rate (including Lab)": plat_rate,
153
- "Gold Value 14K": gold_value_14k,
154
- "Gold Value 18K": gold_value_18k,
155
- "Plat Value": plat_value,
156
- "Labour 14K": labour_14k,
157
- "Labour 18K": labour_18k,
158
- "Labour PT": labour_pt,
159
- "Price of USD 14K": f"$ {price_14k:.2f}",
160
- "Price of USD 18K": f"$ {price_18k:.2f}",
161
- "Price of USD PT": f"$ {price_pt:.2f}",
162
- "Price of GBP 14K @ 1/0.78": f"£ {gbp_14k:.2f}",
163
- "Price of GBP 18K @ 1/0.78": f"£ {gbp_18k:.2f}",
164
- "Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}"
165
  }
166
 
167
  # Process diamonds data
@@ -169,9 +147,19 @@ def compute_values(data):
169
  for diamond in diamonds:
170
  if "Dia Qty" in diamond and "Dia Wt." in diamond:
171
  try:
172
- diamond["Diamond Weight"] = float(diamond["Dia Qty"]) * float(diamond["Dia Wt."])
 
 
 
 
 
173
  except (ValueError, TypeError):
174
  diamond["Diamond Weight"] = 0
 
 
 
 
 
175
 
176
  return metal_data, diamonds
177
 
@@ -219,4 +207,4 @@ def fetch_metal_prices():
219
  return gold_price, platinum_price
220
  except (requests.exceptions.RequestException, KeyError, ValueError) as e:
221
  print(f"Error fetching metal prices: {e}")
222
- return 0, 0
 
5
  import re
6
  from dotenv import load_dotenv
7
  import requests
8
+ import math
9
  from datetime import datetime
10
  load_dotenv()
11
 
 
108
 
109
  def compute_values(data):
110
  """Perform metal calculations and format diamonds separately."""
111
+ # Fetch current prices at the start
112
  gold_price, platinum_price = fetch_metal_prices()
113
 
114
+ # Use fetched prices with fallback values
115
+ gold_lock = gold_price if gold_price > 0 else 2800
116
+ platinum_lock = platinum_price if platinum_price > 0 else 1040
117
+
118
+ # Create the column names with current prices
119
+ gold_lock_column = f"Gold Lock @ $ {gold_lock:.2f}"
120
+ platinum_lock_column = f"Platinum Lock $ {platinum_lock:.2f}"
121
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  metal_data = {
123
  "Customer Ref": data.get("Customer Ref", ""),
124
+ gold_lock_column: gold_lock, # Use the dynamic column names
125
+ platinum_lock_column: platinum_lock,
126
+ "Wt. in gram 14K": sum(float(x) for x in data.get("Wt. in gram 14K", [])) if isinstance(data.get("Wt. in gram 14K"), list) else float(data.get("Wt. in gram 14K", 0)),
127
+ "Wt. in gram 18K": sum(float(x) for x in data.get("Wt. in gram 18K", [])) if isinstance(data.get("Wt. in gram 18K"), list) else float(data.get("Wt. in gram 18K", 0)),
128
+ "Wt. in gram PLT": sum(float(x) for x in data.get("Wt. in gram PLT", [])) if isinstance(data.get("Wt. in gram PLT"), list) else float(data.get("Wt. in gram PLT", 0)),
129
+ "Gold Rate (including loss) 14KT": round((gold_lock / 31.1035) * 0.585 * 1.1, 2),
130
+ "Gold Rate (including loss) 18KT": round((gold_lock / 31.1035) * 0.75 * 1.1, 2),
131
+ "Plat rate (including Lab)": round((platinum_lock / 31.1035) * 0.95 * 1.2, 2),
132
+ "Gold Value 14K": round(sum(float(x) for x in data.get("Wt. in gram 14K", [])) * round((gold_lock / 31.1035) * 0.585 * 1.1, 2), 2),
133
+ "Gold Value 18K": round(sum(float(x) for x in data.get("Wt. in gram 18K", [])) * round((gold_lock / 31.1035) * 0.75 * 1.1, 2), 2),
134
+ "Plat Value": round(sum(float(x) for x in data.get("Wt. in gram PLT", [])) * round((platinum_lock / 31.1035) * 0.95 * 1.2, 2), 2),
135
+ "Labour 14K": round(12 if (sum(float(x) for x in data.get("Wt. in gram 14K", [])) * 7) < 12 else (sum(float(x) for x in data.get("Wt. in gram 14K", [])) * 7), 2),
136
+ "Labour 18K": round(12 if (sum(float(x) for x in data.get("Wt. in gram 18K", [])) * 7) < 12 else (sum(float(x) for x in data.get("Wt. in gram 18K", [])) * 7), 2),
137
+ "Labour PT": round(15 if (sum(float(x) for x in data.get("Wt. in gram PLT", [])) * 11) < 15 else (sum(float(x) for x in data.get("Wt. in gram PLT", [])) * 11), 2),
138
+ "Finding For Gold 14KT": 0,
139
+ "Finding For Gold 18KT": 0,
140
+ "Finding for Plt.": 0,
141
+ "Rhodium": 0,
142
+ "Stamping": 1
 
143
  }
144
 
145
  # Process diamonds data
 
147
  for diamond in diamonds:
148
  if "Dia Qty" in diamond and "Dia Wt." in diamond:
149
  try:
150
+ # Convert string values to float and handle potential errors
151
+ qty = float(diamond["Dia Qty"])
152
+ wt = float(diamond["Dia Wt."])
153
+ diamond["Diamond Weight"] = round(qty * wt, 3) # Round to 3 decimal places
154
+ diamond["Diamond Rate"] = 0 # This will be updated later with user input
155
+ diamond["Diamond Value"] = 0 # This will be calculated after rate is provided
156
  except (ValueError, TypeError):
157
  diamond["Diamond Weight"] = 0
158
+ diamond["Diamond Rate"] = 0
159
+ diamond["Diamond Value"] = 0
160
+
161
+ # Remove all price calculations from here as they'll be done in main.py
162
+ # after diamond rates are applied
163
 
164
  return metal_data, diamonds
165
 
 
207
  return gold_price, platinum_price
208
  except (requests.exceptions.RequestException, KeyError, ValueError) as e:
209
  print(f"Error fetching metal prices: {e}")
210
+ return 0, 0