pcsales / main.py
omgy's picture
Upload 12 files
943deb6 verified
import streamlit as st
import os
import sys
# Add the current directory to Python path
sys.path.append(os.path.dirname(__file__))
# MUST BE FIRST - Page configuration
st.set_page_config(
page_title="Sales Management System",
page_icon="πŸ“Š",
layout="wide",
initial_sidebar_state="expanded"
)
# Import utilities - Create these if they don't exist
try:
from utils.styling import apply_custom_css
from utils.helpers import init_session_state, check_module_availability
from components.database_status import show_database_status
except ImportError:
# Create basic fallbacks if utils don't exist
def apply_custom_css():
st.markdown("""
<style>
.main-header { color: #1f77b4; }
.section-header { color: #2e86ab; margin-top: 2rem; }
</style>
""", unsafe_allow_html=True)
def init_session_state():
if 'db' not in st.session_state:
st.session_state.db = None
if 'data_processor' not in st.session_state:
st.session_state.data_processor = None
if 'analytics' not in st.session_state:
st.session_state.analytics = None
if 'whatsapp_manager' not in st.session_state:
st.session_state.whatsapp_manager = None
def check_module_availability():
try:
import pandas, plotly, sqlite3
MODULES_AVAILABLE = True
except ImportError:
MODULES_AVAILABLE = False
try:
import pywhatkit
WHATSAPP_AVAILABLE = True
except ImportError:
WHATSAPP_AVAILABLE = False
return MODULES_AVAILABLE, WHATSAPP_AVAILABLE
def show_database_status(db):
if db:
try:
health = db.execute_query("SELECT COUNT(*) FROM sqlite_master", log_action=False)
st.sidebar.success("βœ… Database Connected")
except:
st.sidebar.error("❌ Database Error")
else:
st.sidebar.warning("⚠️ Database Not Initialized")
# Apply custom CSS
apply_custom_css()
# Initialize session state
init_session_state()
# Check module availability
MODULES_AVAILABLE, WHATSAPP_AVAILABLE = check_module_availability()
# Initialize components with error handling
if MODULES_AVAILABLE:
try:
from database import DatabaseManager
from data_processor import DataProcessor
from analytics import Analytics
if st.session_state.db is None:
st.session_state.db = DatabaseManager()
st.success("βœ… Database initialized successfully!")
if st.session_state.data_processor is None:
st.session_state.data_processor = DataProcessor(st.session_state.db)
if st.session_state.analytics is None:
st.session_state.analytics = Analytics(st.session_state.db)
if WHATSAPP_AVAILABLE and st.session_state.whatsapp_manager is None:
try:
from whatsapp_manager import WhatsAppManager
st.session_state.whatsapp_manager = WhatsAppManager(st.session_state.db)
st.success("βœ… WhatsApp Manager initialized!")
except Exception as e:
st.warning(f"⚠️ WhatsApp Manager not available: {e}")
st.session_state.whatsapp_manager = None
except Exception as e:
st.error(f"❌ Application initialization failed: {e}")
st.info("Please check that all required files are in the correct location.")
# Assign to local variables for easier access
db = st.session_state.db
data_processor = st.session_state.data_processor
analytics = st.session_state.analytics
whatsapp_manager = st.session_state.whatsapp_manager
# Add this in your main content area (before page routing)
st.sidebar.markdown("""
<div style='text-align: center; margin: 20px 0; margin-top:-10px'>
<img src='https://tse4.mm.bing.net/th/id/OIP.bvMgrnyDHrBdq_MmZeP8XgHaHa?rs=1&pid=ImgDetMain&o=7&rm=3'
style='width: 200px; height: auto; margin-bottom: 10px;'>
</div>
""", unsafe_allow_html=True)
st.sidebar.markdown("<h2 style='text-align: center;'>πŸš€ Sales Management</h2>", unsafe_allow_html=True)
page = st.sidebar.radio("Navigation", [
"πŸ“Š System Dashboard", "πŸ‘₯ Customers", "πŸ’° Sales", "πŸ’³ Payments",
"🎯 Demos", "🀝 Distributors", "πŸ” File Viewer", "πŸ“€ Data Import", "πŸ“Š Power BI Dashboard", "πŸ“ˆ Reports"
], index=0)
# app.py (add this)
from sales_data_processor import SalesDataProcessor
# Initialize in your main app
if 'data_processor' not in st.session_state:
st.session_state.data_processor = SalesDataProcessor(db)
def show_basic_dashboard(db, analytics):
st.title("πŸ“Š Sales Dashboard")
if db and analytics:
try:
sales_summary = analytics.get_sales_summary()
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("Total Sales", f"β‚Ή{sales_summary.get('total_sales', 0):,.0f}")
with col2:
st.metric("Pending Payments", f"β‚Ή{sales_summary.get('pending_amount', 0):,.0f}")
with col3:
st.metric("Total Transactions", sales_summary.get('total_transactions', 0))
with col4:
st.metric("Avg Sale", f"β‚Ή{sales_summary.get('avg_sale_value', 0):,.0f}")
except Exception as e:
st.error(f"Error loading dashboard data: {e}")
else:
st.warning("Database or analytics not available")
# Page routing with error handling
try:
if page == "πŸ“Š System Dashboard":
try:
from pages.system_dashboard import create_dashboard
create_dashboard(db, analytics)
except ImportError:
st.error("Dashboard page not available. Creating basic dashboard...")
show_basic_dashboard(db, analytics)
elif page == "πŸ‘₯ Customers":
try:
from pages.customers import show_customers_page
show_customers_page(db, whatsapp_manager)
except ImportError:
st.error("Customers page not available")
elif page == "πŸ’° Sales":
try:
from pages.sales import show_sales_page
show_sales_page(db, whatsapp_manager)
except ImportError:
st.error("Sales page not available")
elif page == "🎯 Demos":
from pages.demos import show_demos_page
show_demos_page(db, whatsapp_manager)
elif page == "πŸ’³ Payments":
from pages.payments import show_payments_page
show_payments_page(db, whatsapp_manager)
elif page == "🀝 Distributors":
try:
from pages.distributors import show_distributors_page
show_distributors_page(db, whatsapp_manager)
except ImportError:
st.error("Distributors page not available")
elif page == "πŸ” File Viewer":
try:
from pages.file_viewer import show_file_viewer_page
show_file_viewer_page(db, data_processor)
except ImportError:
st.error("File Viewer page not available")
elif page == "πŸ“€ Data Import":
try:
from pages.data_import import show_data_import_page
show_data_import_page(db, data_processor)
except ImportError:
st.error("Data Import page not available")
elif page == "πŸ“Š Power BI Dashboard":
try:
from pages.dashboard import create_dashboard
create_dashboard(db, analytics)
except ImportError:
st.error("Dashboard page not available. Creating basic dashboard...")
show_basic_dashboard(db, analytics)
elif page == "πŸ“ˆ Reports":
try:
from pages.reports import show_reports_page
show_reports_page(db,whatsapp_manager)
except ImportError:
st.error("Reports page not available")
except Exception as e:
st.error(f"Application error: {e}")
st.info("Please check the console for more details.")
st.sidebar.markdown("---")
st.sidebar.info("πŸš€ Sales Management System v2.0")
# Basic dashboard fallback