File size: 2,533 Bytes
bbc2be0
46b6385
a15ad18
 
 
889c29c
6d88ddb
fe8fdf5
 
 
 
46b6385
cf202e9
471a2d4
 
5ce97a5
46b6385
cf202e9
 
 
 
 
 
46b6385
cf202e9
6d88ddb
 
bbc2be0
fe8fdf5
e38cf15
 
fe8fdf5
 
 
 
6d88ddb
abd41b9
 
e38cf15
cf202e9
bbc2be0
6d88ddb
889c29c
a15ad18
 
 
6d88ddb
46b6385
a15ad18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe8fdf5
a15ad18
 
fe8fdf5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pandas as pd
import json
import os

DB_FILE = "material_db.json"

def parse_excel_file(file_path):
    try:
        df = pd.read_excel(file_path, header=2)
    except Exception as e:
        raise ValueError(f"❌ خطا در خواندن فایل اکسل: {str(e)}")

    column_mapping = {
        "کد": ["کد فهرست بها", "کد"],
        "عنوان": ["عنوان", "شرح کالا", "نام کالا"],
        "تعداد": ["تعداد", "تعداد مورد نیاز"],
        "واحد": ["واحد", "واحد شمارش", "نوع واحد"]
    }

    resolved_columns = {}
    for key, options in column_mapping.items():
        found = next((col for col in options if col in df.columns), None)
        if not found:
            raise ValueError(f"❌ ستون «{key}» در فایل پیدا نشد.")
        resolved_columns[key] = found

    materials = []
    for _, row in df.iterrows():
        try:
            quantity = float(row[resolved_columns["تعداد"]])
            if pd.isna(quantity) or quantity <= 0:
                continue
        except:
            continue

        material = {
            "code": str(row[resolved_columns["کد"]]).strip(),
            "description": str(row[resolved_columns["عنوان"]]).strip(),
            "quantity": quantity,
            "unit": str(row[resolved_columns["واحد"]]).strip()
        }
        materials.append(material)

    if not materials:
        raise ValueError("⚠️ هیچ ماده معتبری از فایل استخراج نشد.")

    return materials

def load_projects():
    if not os.path.exists(DB_FILE):
        return []  # فایل وجود نداره؟ یعنی دیتابیس خالیه
    with open(DB_FILE, "r", encoding="utf-8") as f:
        try:
            return json.load(f)
        except json.JSONDecodeError:
            return []  # خراب بود؟ فایل رو صفر در نظر بگیر

def save_project(description, materials):
    projects = load_projects()

    for p in projects:
        if p.get("description") == description:
            raise ValueError("⚠️ پروژه‌ای با این توصیف قبلاً ثبت شده است.")

    projects.append({
        "description": description,
        "materials": materials
    })

    try:
        with open(DB_FILE, "w", encoding="utf-8") as f:
            json.dump(projects, f, indent=2, ensure_ascii=False)
    except Exception as e:
        raise ValueError(f"❌ خطا در ذخیره فایل JSON: {str(e)}")