billing accoutn with all the mess here

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-05 03:59:54 +00:00
parent 6b291671bd
commit 6cf786b03f
41 changed files with 7257 additions and 685 deletions

View File

@@ -385,26 +385,94 @@ class AdminBillingViewSet(viewsets.ViewSet):
from django.db.models import Sum, Count
from ...auth.models import Account
from datetime import datetime, timedelta
from django.utils import timezone
# Date ranges
now = timezone.now()
this_month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
last_30_days = now - timedelta(days=30)
# Account stats
total_accounts = Account.objects.count()
active_accounts = Account.objects.filter(is_active=True).count()
new_accounts_this_month = Account.objects.filter(
created_at__gte=this_month_start
).count()
# Subscription stats
active_subscriptions = Account.objects.filter(
subscriptions__status='active'
).distinct().count()
# Revenue stats
total_revenue = Payment.objects.filter(
status='completed',
amount__gt=0
).aggregate(total=Sum('amount'))['total'] or 0
revenue_this_month = Payment.objects.filter(
status='completed',
processed_at__gte=this_month_start,
amount__gt=0
).aggregate(total=Sum('amount'))['total'] or 0
# Credit stats
credits_issued = CreditTransaction.objects.filter(
transaction_type='purchase',
created_at__gte=last_30_days
).aggregate(total=Sum('amount'))['total'] or 0
credits_used = abs(CreditTransaction.objects.filter(
transaction_type__in=['generate_content', 'keyword_research', 'ai_task'],
created_at__gte=last_30_days,
amount__lt=0
).aggregate(total=Sum('amount'))['total'] or 0)
# Payment/Invoice stats
pending_approvals = Payment.objects.filter(
status='pending_approval'
).count()
invoices_pending = Invoice.objects.filter(status='pending').count()
invoices_overdue = Invoice.objects.filter(
status='pending',
due_date__lt=now
).count()
# Recent activity
recent_payments = Payment.objects.filter(
status='completed'
).order_by('-processed_at')[:5]
recent_activity = [
{
'id': pay.id,
'type': 'payment',
'account_name': pay.account.name,
'amount': str(pay.amount),
'currency': pay.currency,
'timestamp': pay.processed_at.isoformat(),
'description': f'Payment received via {pay.payment_method}'
}
for pay in recent_payments
]
return Response({
'total_accounts': total_accounts,
'active_accounts': active_accounts,
'new_accounts_this_month': new_accounts_this_month,
'active_subscriptions': active_subscriptions,
'total_revenue': str(total_revenue),
'revenue_this_month': str(revenue_this_month),
'credits_issued_30d': credits_issued,
'credits_used_30d': credits_used,
'pending_approvals': pending_approvals,
'invoices_pending': Invoice.objects.filter(status='pending').count(),
'invoices_paid': Invoice.objects.filter(status='paid').count()
'invoices_pending': invoices_pending,
'invoices_overdue': invoices_overdue,
'recent_activity': recent_activity,
'system_health': {
'status': 'operational',
'last_check': now.isoformat()
}
})