Reza-galaxy21 commited on
Commit
bbc2be0
·
verified ·
1 Parent(s): e276d84

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +30 -11
utils.py CHANGED
@@ -1,15 +1,34 @@
1
  import json
 
 
2
 
3
- def load_material_db(path="material_db.json"):
4
- with open(path, "r", encoding="utf-8") as f:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  return json.load(f)
6
 
7
- def filter_items(materials, pole_height, pole_power, conductor_size):
8
- matched = []
9
- for item in materials:
10
- cond = item["conditions"]
11
- if (cond["pole_height"] in ["-", pole_height]) and \
12
- (cond["pole_power"] in ["-", pole_power]) and \
13
- (cond["conductor_size"] in ["-", conductor_size]):
14
- matched.append(item)
15
- return matched
 
1
  import json
2
+ import pandas as pd
3
+ import os
4
 
5
+ DB_FILE = "material_db.json"
6
+
7
+ def build_db_from_excel(file_path, description_key):
8
+ df = pd.read_excel(file_path)
9
+ df = df.fillna("-")
10
+
11
+ items = []
12
+ for _, row in df.iterrows():
13
+ item = {
14
+ "item_code": str(row.get("کد فهرست بها", "")).strip(),
15
+ "item_name": str(row.get("شرح کالا", "")).strip(),
16
+ "quantity": float(row.get("مقدار", 0)),
17
+ "unit": str(row.get("واحد", "")).strip()
18
+ }
19
+ items.append(item)
20
+
21
+ return {description_key: items}
22
+
23
+ def save_material_db(data):
24
+ with open(DB_FILE, "w", encoding="utf-8") as f:
25
+ json.dump(data, f, ensure_ascii=False, indent=2)
26
+
27
+ def load_material_db():
28
+ if not os.path.exists(DB_FILE):
29
+ return {}
30
+ with open(DB_FILE, "r", encoding="utf-8") as f:
31
  return json.load(f)
32
 
33
+ def get_items_for_description(db, description):
34
+ return db.get(description, [])