|
"""
|
|
وحدة إدارة حالة الجلسة
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
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']
|
|
|
|
|
|
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 |