File size: 9,022 Bytes
82676b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""

وحدة إدارة حالة الجلسة

"""

import streamlit as st
from datetime import datetime
import pandas as pd
import config


def initialize_session_state():
    """

    تهيئة متغيرات حالة الجلسة

    """
    # المتغيرات الرئيسية لحالة المستخدم
    if 'is_authenticated' not in st.session_state:
        st.session_state.is_authenticated = False
    
    if 'user_info' not in st.session_state:
        st.session_state.user_info = None
    
    # المتغيرات المتعلقة بالمناقصات والمشاريع
    if 'current_project' not in st.session_state:
        st.session_state.current_project = None
    
    if 'current_pricing' not in st.session_state:
        st.session_state.current_pricing = None
    
    if 'pricing_history' not in st.session_state:
        st.session_state.pricing_history = []
    
    # المتغيرات المتعلقة بالمحتوى المحلي
    if 'local_content_products' not in st.session_state:
        st.session_state.local_content_products = pd.DataFrame({
            'المنتج': [],
            'الكمية': [],
            'سعر_الوحدة': [],
            'التكلفة_الإجمالية': [],
            'نسبة_المحتوى_المحلي': []
        })
    
    if 'local_content_services' not in st.session_state:
        st.session_state.local_content_services = pd.DataFrame({
            'الخدمة': [],
            'التكلفة': [],
            'نسبة_المحتوى_المحلي': []
        })
    
    if 'local_content_labor' not in st.session_state:
        st.session_state.local_content_labor = pd.DataFrame({
            'فئة_العمالة': [],
            'العدد': [],
            'الراتب_الشهري': [],
            'المدة_بالأشهر': [],
            'نسبة_المحتوى_المحلي': []
        })
    
    # المتغيرات المتعلقة بتحليل المستندات
    if 'current_document' not in st.session_state:
        st.session_state.current_document = None
    
    if 'analyzed_documents' not in st.session_state:
        st.session_state.analyzed_documents = []
    
    # المتغيرات المتعلقة بالتسعير
    if 'manual_items' not in st.session_state:
        st.session_state.manual_items = pd.DataFrame({
            'رقم البند': [],
            'وصف البند': [],
            'الوحدة': [],
            'الكمية': [],
            'سعر الوحدة': [],
            'الإجمالي': []
        })
    
    # المتغيرات المتعلقة بالمصادر
    if 'resources' not in st.session_state:
        st.session_state.resources = []
    
    # المتغيرات المتعلقة بالإعدادات
    if 'settings' not in st.session_state:
        st.session_state.settings = {
            'ui_theme': config.UI_THEME,
            'locale': config.LOCALE,
            'enable_animations': config.ENABLE_ANIMATIONS
        }


def save_current_pricing():
    """

    حفظ التسعير الحالي في سجل التسعير

    

    الإرجاع:

        True في حالة النجاح، False في حالة الفشل

    """
    try:
        if st.session_state.current_pricing:
            # إضافة معلومات إضافية
            pricing_entry = st.session_state.current_pricing.copy()
            pricing_entry['timestamp'] = datetime.now()
            
            # إضافة إلى سجل التسعير
            st.session_state.pricing_history.append(pricing_entry)
            
            return True
        return False
    except Exception as e:
        print(f"خطأ في حفظ التسعير الحالي: {str(e)}")
        return False


def set_current_project(project_data):
    """

    تعيين المشروع الحالي

    

    المعلمات:

        project_data: بيانات المشروع

    """
    st.session_state.current_project = project_data


def set_current_pricing(pricing_data):
    """

    تعيين التسعير الحالي

    

    المعلمات:

        pricing_data: بيانات التسعير

    """
    st.session_state.current_pricing = pricing_data


def set_current_document(document_data):
    """

    تعيين المستند الحالي

    

    المعلمات:

        document_data: بيانات المستند

    """
    st.session_state.current_document = document_data


def clear_session():
    """

    مسح بيانات الجلسة الحالية

    """
    # الاحتفاظ بحالة المصادقة والمستخدم
    is_authenticated = st.session_state.is_authenticated
    user_info = st.session_state.user_info
    settings = st.session_state.settings
    
    # مسح المتغيرات
    st.session_state.clear()
    
    # إعادة تعيين حالة المصادقة والمستخدم
    st.session_state.is_authenticated = is_authenticated
    st.session_state.user_info = user_info
    st.session_state.settings = settings
    
    # إعادة تهيئة متغيرات الجلسة
    initialize_session_state()


def export_session_state():
    """

    تصدير حالة الجلسة الحالية

    

    الإرجاع:

        قاموس يحتوي على حالة الجلسة

    """
    # إنشاء نسخة من حالة الجلسة
    session_data = {}
    
    # تخزين البيانات الرئيسية
    if st.session_state.current_project:
        session_data['current_project'] = st.session_state.current_project
    
    if st.session_state.current_pricing:
        session_data['current_pricing'] = st.session_state.current_pricing
    
    if st.session_state.pricing_history:
        session_data['pricing_history'] = st.session_state.pricing_history
    
    # تحويل DataFrames إلى قوائم من القواميس
    if 'local_content_products' in st.session_state and not st.session_state.local_content_products.empty:
        session_data['local_content_products'] = st.session_state.local_content_products.to_dict('records')
    
    if 'local_content_services' in st.session_state and not st.session_state.local_content_services.empty:
        session_data['local_content_services'] = st.session_state.local_content_services.to_dict('records')
    
    if 'local_content_labor' in st.session_state and not st.session_state.local_content_labor.empty:
        session_data['local_content_labor'] = st.session_state.local_content_labor.to_dict('records')
    
    # تخزين البيانات الأخرى
    if 'manual_items' in st.session_state and not st.session_state.manual_items.empty:
        session_data['manual_items'] = st.session_state.manual_items.to_dict('records')
    
    if st.session_state.resources:
        session_data['resources'] = st.session_state.resources
    
    # إضافة بيانات الوقت
    session_data['exported_at'] = datetime.now().isoformat()
    
    return session_data


def import_session_state(session_data):
    """

    استيراد حالة الجلسة

    

    المعلمات:

        session_data: قاموس يحتوي على حالة الجلسة

        

    الإرجاع:

        True في حالة النجاح، False في حالة الفشل

    """
    try:
        # استيراد البيانات الرئيسية
        if 'current_project' in session_data:
            st.session_state.current_project = session_data['current_project']
        
        if 'current_pricing' in session_data:
            st.session_state.current_pricing = session_data['current_pricing']
        
        if 'pricing_history' in session_data:
            st.session_state.pricing_history = session_data['pricing_history']
        
        # استيراد DataFrames
        if 'local_content_products' in session_data:
            st.session_state.local_content_products = pd.DataFrame(session_data['local_content_products'])
        
        if 'local_content_services' in session_data:
            st.session_state.local_content_services = pd.DataFrame(session_data['local_content_services'])
        
        if 'local_content_labor' in session_data:
            st.session_state.local_content_labor = pd.DataFrame(session_data['local_content_labor'])
        
        # استيراد البيانات الأخرى
        if 'manual_items' in session_data:
            st.session_state.manual_items = pd.DataFrame(session_data['manual_items'])
        
        if 'resources' in session_data:
            st.session_state.resources = session_data['resources']
        
        return True
    except Exception as e:
        print(f"خطأ في استيراد حالة الجلسة: {str(e)}")
        return False