Reza-galaxy21 commited on
Commit
6d88ddb
·
verified ·
1 Parent(s): 479de13

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +14 -27
utils.py CHANGED
@@ -1,34 +1,21 @@
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, [])
 
 
1
  import pandas as pd
 
2
 
3
+ def parse_excel_file(file_path):
 
 
4
  df = pd.read_excel(file_path)
 
5
 
6
+ required_columns = ["کد فهرست بها", "شرح", "مقدار", "واحد"]
7
+ for col in required_columns:
8
+ if col not in df.columns:
9
+ raise ValueError(f"ستون «{col}» در فایل پیدا نشد.")
10
+
11
+ materials = []
12
  for _, row in df.iterrows():
13
+ material = {
14
+ "code": str(row["کد فهرست بها"]).strip(),
15
+ "description": str(row["شرح"]).strip(),
16
+ "quantity": float(row["مقدار"]),
17
+ "unit": str(row["واحد"]).strip()
18
  }
19
+ materials.append(material)
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ return materials