# -*- coding: utf-8 -*- from odoo import http from odoo.http import request class TNCSCCommunityDashboardController(http.Controller): @http.route('/community/dashboard/stats', type='jsonrpc', auth='user') def get_dashboard_stats(self): env = request.env # 1. Membership Stats Partner = env['res.partner'] Tier = env['community.membership.tier'] total_members = Partner.search_count([('is_company', '=', False)]) active_members = Partner.search_count([('membership_state', '=', 'active')]) renewal_due = Partner.search_count([('membership_state', '=', 'renewal_due')]) expired_members = Partner.search_count([('membership_state', '=', 'expired')]) tiers_data = [] try: for tier in Tier.search([], order='sequence asc'): count = Partner.search_count([('membership_tier_id', '=', tier.id)]) tiers_data.append({ 'id': tier.id, 'name': tier.name, 'price': f"${tier.price:,.0f}" if tier.price else "$0", 'period': tier.period, 'count': count, }) except Exception: pass # 2. Events Stats Event = env['event.event'] total_events = Event.search_count([]) upcoming_events = [] try: events_records = Event.search([], order='date_begin asc', limit=6) for ev in events_records: city = 'Mississauga, ON' if ev.address_id and ev.address_id.city: city = f"{ev.address_id.city}, ON" upcoming_events.append({ 'id': ev.id, 'name': ev.name, 'date_begin': ev.date_begin.strftime('%b %d, %Y - %I:%M %p') if ev.date_begin else 'TBD', 'date_short': ev.date_begin.strftime('%d %b') if ev.date_begin else '', 'day': ev.date_begin.strftime('%d') if ev.date_begin else '', 'month_year': ev.date_begin.strftime('%b %Y') if ev.date_begin else '', 'city': city, 'stage': ev.stage_id.name if ev.stage_id else 'New', 'seats_taken': ev.seats_taken, 'seats_max': ev.seats_max, }) except Exception: pass # 3. Tamil School Stats Student = env.get('community.school.student') Class = env.get('community.school.class') Term = env.get('community.school.term') students_count = Student.search_count([]) if Student else 0 classes_count = Class.search_count([]) if Class else 0 terms_count = Term.search_count([]) if Term else 0 # 4. Classifieds Stats Classified = env.get('community.classified') classifieds_count = Classified.search_count([]) if Classified else 0 # 5. Benefits Stats Benefit = env.get('community.benefit') PartnerBenefit = env.get('community.benefit.partner') benefits_count = Benefit.search_count([]) if Benefit else 0 partners_count = PartnerBenefit.search_count([]) if PartnerBenefit else 0 # 6. Interac Transactions Stats Tx = env.get('payment.transaction') interac_count = 0 if Tx: try: interac_count = Tx.search_count([('provider_id.code', '=', 'interac')]) except Exception: pass return { 'membership': { 'total': total_members, 'active': active_members, 'renewal_due': renewal_due, 'expired': expired_members, 'tiers': tiers_data, }, 'events': { 'total': total_events, 'upcoming': upcoming_events, }, 'school': { 'students': students_count, 'classes': classes_count, 'terms': terms_count, }, 'classifieds': { 'total': classifieds_count, }, 'benefits': { 'total': benefits_count, 'partners': partners_count, }, 'interac': { 'total': interac_count, }, }