Fixing PLans page
This commit is contained in:
@@ -578,4 +578,176 @@ class AdminBillingViewSet(viewsets.ViewSet):
|
||||
continue
|
||||
|
||||
return Response({'success': True})
|
||||
|
||||
def invoices(self, request):
|
||||
"""List all invoices (admin view)"""
|
||||
from igny8_core.business.billing.models import Invoice
|
||||
invoices = Invoice.objects.all().select_related('account').order_by('-created_at')[:100]
|
||||
data = [{
|
||||
'id': inv.id,
|
||||
'invoice_number': inv.invoice_number,
|
||||
'account_name': inv.account.name if inv.account else 'N/A',
|
||||
'status': inv.status,
|
||||
'total_amount': str(inv.total),
|
||||
'created_at': inv.created_at.isoformat()
|
||||
} for inv in invoices]
|
||||
return Response({'results': data})
|
||||
|
||||
def payments(self, request):
|
||||
"""List all payments (admin view)"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
payments = Payment.objects.all().select_related('account', 'invoice').order_by('-created_at')[:100]
|
||||
data = [{
|
||||
'id': pay.id,
|
||||
'account_name': pay.account.name if pay.account else 'N/A',
|
||||
'invoice_number': pay.invoice.invoice_number if pay.invoice else 'N/A',
|
||||
'amount': str(pay.amount),
|
||||
'status': pay.status,
|
||||
'payment_method': pay.payment_method,
|
||||
'created_at': pay.created_at.isoformat()
|
||||
} for pay in payments]
|
||||
return Response({'results': data})
|
||||
|
||||
def pending_payments(self, request):
|
||||
"""List pending payments awaiting approval"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
payments = Payment.objects.filter(status='pending_approval').select_related('account', 'invoice').order_by('-created_at')
|
||||
data = [{
|
||||
'id': pay.id,
|
||||
'account_name': pay.account.name if pay.account else 'N/A',
|
||||
'invoice_number': pay.invoice.invoice_number if pay.invoice else 'N/A',
|
||||
'amount': str(pay.amount),
|
||||
'payment_method': pay.payment_method,
|
||||
'manual_reference': pay.manual_reference,
|
||||
'manual_notes': pay.manual_notes,
|
||||
'created_at': pay.created_at.isoformat()
|
||||
} for pay in payments]
|
||||
return Response({'results': data})
|
||||
|
||||
def approve_payment(self, request, pk):
|
||||
"""Approve a pending payment"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
try:
|
||||
payment = Payment.objects.get(pk=pk, status='pending_approval')
|
||||
payment.status = 'completed'
|
||||
payment.processed_at = timezone.now()
|
||||
payment.save()
|
||||
|
||||
# If payment has an invoice, mark it as paid
|
||||
if payment.invoice:
|
||||
payment.invoice.status = 'paid'
|
||||
payment.invoice.paid_at = timezone.now()
|
||||
payment.invoice.save()
|
||||
|
||||
return Response({'success': True, 'message': 'Payment approved'})
|
||||
except Payment.DoesNotExist:
|
||||
return Response({'error': 'Payment not found or not pending'}, status=404)
|
||||
|
||||
def reject_payment(self, request, pk):
|
||||
"""Reject a pending payment"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
try:
|
||||
payment = Payment.objects.get(pk=pk, status='pending_approval')
|
||||
payment.status = 'failed'
|
||||
payment.failed_at = timezone.now()
|
||||
payment.failure_reason = request.data.get('reason', 'Rejected by admin')
|
||||
payment.save()
|
||||
|
||||
return Response({'success': True, 'message': 'Payment rejected'})
|
||||
except Payment.DoesNotExist:
|
||||
return Response({'error': 'Payment not found or not pending'}, status=404)
|
||||
|
||||
def payment_method_configs(self, request):
|
||||
"""List or create payment method configurations"""
|
||||
from igny8_core.business.billing.models import PaymentMethodConfig
|
||||
if request.method == 'GET':
|
||||
configs = PaymentMethodConfig.objects.all()
|
||||
data = [{
|
||||
'id': c.id,
|
||||
'type': c.type,
|
||||
'name': c.name,
|
||||
'description': c.description,
|
||||
'is_enabled': c.is_enabled,
|
||||
'sort_order': c.sort_order
|
||||
} for c in configs]
|
||||
return Response({'results': data})
|
||||
else:
|
||||
# Handle POST for creating new config
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
|
||||
def payment_method_config(self, request, pk):
|
||||
"""Get, update, or delete a payment method config"""
|
||||
from igny8_core.business.billing.models import PaymentMethodConfig
|
||||
try:
|
||||
config = PaymentMethodConfig.objects.get(pk=pk)
|
||||
if request.method == 'GET':
|
||||
return Response({
|
||||
'id': config.id,
|
||||
'type': config.type,
|
||||
'name': config.name,
|
||||
'description': config.description,
|
||||
'is_enabled': config.is_enabled,
|
||||
'sort_order': config.sort_order
|
||||
})
|
||||
elif request.method in ['PATCH', 'PUT']:
|
||||
# Update config
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
elif request.method == 'DELETE':
|
||||
# Delete config
|
||||
config.delete()
|
||||
return Response({'success': True})
|
||||
except PaymentMethodConfig.DoesNotExist:
|
||||
return Response({'error': 'Config not found'}, status=404)
|
||||
|
||||
def account_payment_methods(self, request):
|
||||
"""List or create account payment methods"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
if request.method == 'GET':
|
||||
methods = AccountPaymentMethod.objects.all().select_related('account')[:100]
|
||||
data = [{
|
||||
'id': str(m.id),
|
||||
'account_name': m.account.name if m.account else 'N/A',
|
||||
'type': m.type,
|
||||
'display_name': m.display_name,
|
||||
'is_default': m.is_default
|
||||
} for m in methods]
|
||||
return Response({'results': data})
|
||||
else:
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
|
||||
def account_payment_method(self, request, pk):
|
||||
"""Get, update, or delete an account payment method"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
try:
|
||||
method = AccountPaymentMethod.objects.get(pk=pk)
|
||||
if request.method == 'GET':
|
||||
return Response({
|
||||
'id': str(method.id),
|
||||
'account_name': method.account.name if method.account else 'N/A',
|
||||
'type': method.type,
|
||||
'display_name': method.display_name,
|
||||
'is_default': method.is_default
|
||||
})
|
||||
elif request.method in ['PATCH', 'PUT']:
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
elif request.method == 'DELETE':
|
||||
method.delete()
|
||||
return Response({'success': True})
|
||||
except AccountPaymentMethod.DoesNotExist:
|
||||
return Response({'error': 'Method not found'}, status=404)
|
||||
|
||||
def set_default_account_payment_method(self, request, pk):
|
||||
"""Set an account payment method as default"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
try:
|
||||
method = AccountPaymentMethod.objects.get(pk=pk)
|
||||
# Unset other defaults for this account
|
||||
AccountPaymentMethod.objects.filter(account=method.account, is_default=True).update(is_default=False)
|
||||
# Set this as default
|
||||
method.is_default = True
|
||||
method.save()
|
||||
return Response({'success': True, 'message': 'Payment method set as default'})
|
||||
except AccountPaymentMethod.DoesNotExist:
|
||||
return Response({'error': 'Method not found'}, status=404)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user