add main controller for dine360 dashboard and SaaS management

This commit is contained in:
Alaguraj0361 2026-10-12 18:17:33 +05:30
parent 6daa038bd4
commit 1af74bd16b

View File

@ -71,39 +71,62 @@ def get_dashboard_context(active_page='dashboard'):
}) })
except Exception as e: except Exception as e:
_logger.warning("Error fetching saas.plan: %s", e) _logger.warning("Error fetching saas.plan: %s", e)
try:
request.env.cr.rollback()
except Exception:
pass
if is_saas_master and 'saas.restaurant' in request.env: if is_saas_master and 'saas.restaurant' in request.env:
try: try:
records = request.env['saas.restaurant'].sudo().search([], order='id desc') records = request.env['saas.restaurant'].sudo().search([], order='id desc')
saas_stats['total'] = len(records) saas_stats['total'] = len(records)
for r in records: needed_fields = [
if r.status == 'active': 'id', 'name', 'owner_name', 'email', 'phone', 'city',
'database_name', 'subdomain', 'status', 'plan_id',
'billing_cycle', 'start_date', 'expiry_date'
]
fields_to_read = [f for f in needed_fields if f in request.env['saas.restaurant']._fields]
raw_data = records.read(fields_to_read)
for r_data in raw_data:
st = r_data.get('status') or 'active'
if st == 'active':
saas_stats['active'] += 1 saas_stats['active'] += 1
elif r.status == 'provisioning': elif st == 'provisioning':
saas_stats['provisioning'] += 1 saas_stats['provisioning'] += 1
elif r.status in ('expired', 'suspended', 'failed'): elif st in ('expired', 'suspended', 'failed'):
saas_stats['expired'] += 1 saas_stats['expired'] += 1
expiry_fmt = r.expiry_date.strftime('%d %b %Y') if r.expiry_date else 'N/A' exp_date = r_data.get('expiry_date')
expiry_fmt = exp_date.strftime('%d %b %Y') if hasattr(exp_date, 'strftime') else (str(exp_date) if exp_date else 'N/A')
plan_val = r_data.get('plan_id')
plan_name = plan_val[1] if isinstance(plan_val, (list, tuple)) and len(plan_val) > 1 else 'Standard'
r_id = r_data.get('id')
db_name_val = r_data.get('database_name') or ''
onboarded_restaurants.append({ onboarded_restaurants.append({
'id': r.id, 'id': r_id,
'name': r.name, 'name': r_data.get('name') or '',
'owner_name': r.owner_name or 'Admin', 'owner_name': r_data.get('owner_name') or 'Admin',
'email': r.email or '', 'email': r_data.get('email') or '',
'phone': r.phone or '', 'phone': r_data.get('phone') or '',
'city': r.city or '', 'city': r_data.get('city') or '',
'database_name': r.database_name or '', 'database_name': db_name_val,
'subdomain': r.subdomain or '', 'subdomain': r_data.get('subdomain') or '',
'status': r.status or 'active', 'status': st,
'plan_name': r.plan_id.name if r.plan_id else 'Standard', 'plan_name': plan_name,
'billing_cycle': 'Monthly' if r.billing_cycle == 'monthly' else 'Annual', 'billing_cycle': 'Monthly' if r_data.get('billing_cycle') == 'monthly' else 'Annual',
'start_date': str(r.start_date) if r.start_date else '', 'start_date': str(r_data.get('start_date') or ''),
'expiry_date': expiry_fmt, 'expiry_date': expiry_fmt,
'launch_url': f"/web/login?db={r.database_name}" if r.database_name else '#', 'launch_url': f"/web/login?db={db_name_val}" if db_name_val else '#',
'manage_url': f"/web#id={r.id}&model=saas.restaurant&view_type=form", 'manage_url': f"/web#id={r_id}&model=saas.restaurant&view_type=form",
}) })
except Exception as e: except Exception as e:
_logger.warning("Error fetching SaaS restaurants: %s", e) _logger.warning("Error fetching SaaS restaurants: %s", e)
try:
request.env.cr.rollback()
except Exception:
pass
first_res = onboarded_restaurants[0] if onboarded_restaurants else None first_res = onboarded_restaurants[0] if onboarded_restaurants else None
@ -115,6 +138,9 @@ def get_dashboard_context(active_page='dashboard'):
if 'restaurant.reservation' in request.env: if 'restaurant.reservation' in request.env:
try: try:
res_count = request.env['restaurant.reservation'].sudo().search_count([]) res_count = request.env['restaurant.reservation'].sudo().search_count([])
except Exception:
try:
request.env.cr.rollback()
except Exception: except Exception:
pass pass
@ -125,6 +151,9 @@ def get_dashboard_context(active_page='dashboard'):
orders = request.env['sale.order'].sudo().search([]) orders = request.env['sale.order'].sudo().search([])
orders_count += len(orders) orders_count += len(orders)
total_sales += sum(orders.mapped('amount_total')) total_sales += sum(orders.mapped('amount_total'))
except Exception:
try:
request.env.cr.rollback()
except Exception: except Exception:
pass pass
if 'pos.order' in request.env: if 'pos.order' in request.env:
@ -132,6 +161,9 @@ def get_dashboard_context(active_page='dashboard'):
pos_orders = request.env['pos.order'].sudo().search([]) pos_orders = request.env['pos.order'].sudo().search([])
orders_count += len(pos_orders) orders_count += len(pos_orders)
total_sales += sum(pos_orders.mapped('amount_total')) total_sales += sum(pos_orders.mapped('amount_total'))
except Exception:
try:
request.env.cr.rollback()
except Exception: except Exception:
pass pass
@ -140,15 +172,22 @@ def get_dashboard_context(active_page='dashboard'):
recent_activities = [] recent_activities = []
if is_saas_master and 'saas.restaurant' in request.env: if is_saas_master and 'saas.restaurant' in request.env:
try: try:
for r in request.env['saas.restaurant'].sudo().search([], order='create_date desc', limit=5): records = request.env['saas.restaurant'].sudo().search([], order='create_date desc', limit=5)
for r_data in records.read(['id', 'name', 'database_name', 'status', 'create_date']):
st = r_data.get('status') or 'active'
c_date = r_data.get('create_date')
time_str = c_date.strftime('%d %b, %H:%M') if hasattr(c_date, 'strftime') else (str(c_date) if c_date else '')
recent_activities.append({ recent_activities.append({
'title': f"Restaurant {r.status.capitalize() if r.status else 'Registered'}", 'title': f"Restaurant {st.capitalize() if st else 'Registered'}",
'subtitle': f"{r.name} ({r.database_name or 'N/A'})", 'subtitle': f"{r_data.get('name')} ({r_data.get('database_name') or 'N/A'})",
'time': r.create_date.strftime('%d %b, %H:%M') if getattr(r, 'create_date', False) else '', 'time': time_str,
'badge': (r.status or 'active').capitalize(), 'badge': (st or 'active').capitalize(),
'badge_type': 'completed' if r.status == 'active' else 'info', 'badge_type': 'completed' if st == 'active' else 'info',
'icon': 'fa fa-cutlery' 'icon': 'fa fa-cutlery'
}) })
except Exception:
try:
request.env.cr.rollback()
except Exception: except Exception:
pass pass
@ -181,6 +220,9 @@ def get_dashboard_context(active_page='dashboard'):
'name': fl.name, 'name': fl.name,
'table_count': t_count, 'table_count': t_count,
}) })
except Exception:
try:
request.env.cr.rollback()
except Exception: except Exception:
pass pass
@ -210,6 +252,10 @@ def get_dashboard_context(active_page='dashboard'):
table_stats['capacity'] = total_seats table_stats['capacity'] = total_seats
except Exception as e: except Exception as e:
_logger.warning("Error fetching restaurant.table: %s", e) _logger.warning("Error fetching restaurant.table: %s", e)
try:
request.env.cr.rollback()
except Exception:
pass
# 3. Reservations Data # 3. Reservations Data
reservations_list = [] reservations_list = []
@ -251,6 +297,10 @@ def get_dashboard_context(active_page='dashboard'):
}) })
except Exception as e: except Exception as e:
_logger.warning("Error fetching reservations: %s", e) _logger.warning("Error fetching reservations: %s", e)
try:
request.env.cr.rollback()
except Exception:
pass
# 4. Online Orders Data # 4. Online Orders Data
online_orders_data = { online_orders_data = {
@ -314,6 +364,10 @@ def get_dashboard_context(active_page='dashboard'):
online_orders_data['orders'] = orders_list online_orders_data['orders'] = orders_list
except Exception as e: except Exception as e:
_logger.warning("Error fetching sale.order: %s", e) _logger.warning("Error fetching sale.order: %s", e)
try:
request.env.cr.rollback()
except Exception:
pass
# 5. Backups Data # 5. Backups Data
backups_list = [] backups_list = []