trash models added, first attempt for remainign issues
This commit is contained in:
@@ -284,8 +284,8 @@ class PaymentAdmin(ExportMixin, AccountAdminMixin, SimpleHistoryAdmin, Igny8Mode
|
||||
account.status = 'active'
|
||||
account.save()
|
||||
|
||||
# Add Credits (check if not already added)
|
||||
from igny8_core.business.billing.models import CreditTransaction
|
||||
# Add or Reset Credits (check if not already added)
|
||||
from igny8_core.business.billing.models import CreditTransaction, Invoice
|
||||
existing_credit = CreditTransaction.objects.filter(
|
||||
account=account,
|
||||
metadata__payment_id=obj.id
|
||||
@@ -294,32 +294,65 @@ class PaymentAdmin(ExportMixin, AccountAdminMixin, SimpleHistoryAdmin, Igny8Mode
|
||||
if not existing_credit:
|
||||
credits_to_add = 0
|
||||
plan_name = ''
|
||||
is_renewal = False
|
||||
|
||||
if subscription and subscription.plan:
|
||||
credits_to_add = subscription.plan.included_credits
|
||||
plan_name = subscription.plan.name
|
||||
# Check if this is a renewal (previous paid invoices exist)
|
||||
previous_paid = Invoice.objects.filter(
|
||||
subscription=subscription,
|
||||
status='paid'
|
||||
).exclude(id=invoice.id if invoice else None).exists()
|
||||
is_renewal = previous_paid
|
||||
elif account and account.plan:
|
||||
credits_to_add = account.plan.included_credits
|
||||
plan_name = account.plan.name
|
||||
# Check renewal by account history
|
||||
is_renewal = CreditTransaction.objects.filter(
|
||||
account=account,
|
||||
transaction_type='subscription'
|
||||
).exists()
|
||||
|
||||
if credits_to_add > 0:
|
||||
CreditService.add_credits(
|
||||
account=account,
|
||||
amount=credits_to_add,
|
||||
transaction_type='subscription',
|
||||
description=f'{plan_name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id if subscription else None,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': obj.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
self.message_user(
|
||||
request,
|
||||
f'✓ Payment approved: Account activated, {credits_to_add} credits added',
|
||||
level='SUCCESS'
|
||||
)
|
||||
if is_renewal:
|
||||
# Renewal: Reset credits to full plan amount
|
||||
CreditService.reset_credits_for_renewal(
|
||||
account=account,
|
||||
new_amount=credits_to_add,
|
||||
description=f'{plan_name} Renewal - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id if subscription else None,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': obj.id,
|
||||
'approved_by': request.user.email,
|
||||
'is_renewal': True
|
||||
}
|
||||
)
|
||||
self.message_user(
|
||||
request,
|
||||
f'✓ Renewal approved: Account activated, credits reset to {credits_to_add}',
|
||||
level='SUCCESS'
|
||||
)
|
||||
else:
|
||||
# Initial: Add credits
|
||||
CreditService.add_credits(
|
||||
account=account,
|
||||
amount=credits_to_add,
|
||||
transaction_type='subscription',
|
||||
description=f'{plan_name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id if subscription else None,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': obj.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
self.message_user(
|
||||
request,
|
||||
f'✓ Payment approved: Account activated, {credits_to_add} credits added',
|
||||
level='SUCCESS'
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
self.message_user(
|
||||
@@ -377,37 +410,83 @@ class PaymentAdmin(ExportMixin, AccountAdminMixin, SimpleHistoryAdmin, Igny8Mode
|
||||
account.status = 'active'
|
||||
account.save()
|
||||
|
||||
# Add Credits
|
||||
# Add or Reset Credits based on whether this is a renewal
|
||||
# Check if there are previous paid invoices for this subscription (renewal)
|
||||
from igny8_core.business.billing.models import Invoice, CreditTransaction
|
||||
is_renewal = False
|
||||
if subscription:
|
||||
previous_paid_invoices = Invoice.objects.filter(
|
||||
subscription=subscription,
|
||||
status='paid'
|
||||
).exclude(id=invoice.id).exists()
|
||||
is_renewal = previous_paid_invoices
|
||||
|
||||
credits_added = 0
|
||||
if subscription and subscription.plan and subscription.plan.included_credits > 0:
|
||||
credits_added = subscription.plan.included_credits
|
||||
CreditService.add_credits(
|
||||
account=account,
|
||||
amount=credits_added,
|
||||
transaction_type='subscription',
|
||||
description=f'{subscription.plan.name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
if is_renewal:
|
||||
# Renewal: Reset credits to full plan amount
|
||||
CreditService.reset_credits_for_renewal(
|
||||
account=account,
|
||||
new_amount=credits_added,
|
||||
description=f'{subscription.plan.name} Renewal - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email,
|
||||
'is_renewal': True
|
||||
}
|
||||
)
|
||||
else:
|
||||
# Initial subscription: Add credits
|
||||
CreditService.add_credits(
|
||||
account=account,
|
||||
amount=credits_added,
|
||||
transaction_type='subscription',
|
||||
description=f'{subscription.plan.name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'subscription_id': subscription.id,
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
elif account and account.plan and account.plan.included_credits > 0:
|
||||
credits_added = account.plan.included_credits
|
||||
CreditService.add_credits(
|
||||
# Check renewal by looking at account credit transactions
|
||||
previous_subscriptions = CreditTransaction.objects.filter(
|
||||
account=account,
|
||||
amount=credits_added,
|
||||
transaction_type='subscription',
|
||||
description=f'{account.plan.name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
transaction_type='subscription'
|
||||
).exists()
|
||||
if previous_subscriptions:
|
||||
# Renewal: Reset credits
|
||||
CreditService.reset_credits_for_renewal(
|
||||
account=account,
|
||||
new_amount=credits_added,
|
||||
description=f'{account.plan.name} Renewal - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email,
|
||||
'is_renewal': True
|
||||
}
|
||||
)
|
||||
else:
|
||||
CreditService.add_credits(
|
||||
account=account,
|
||||
amount=credits_added,
|
||||
transaction_type='subscription',
|
||||
description=f'{account.plan.name} - Invoice {invoice.invoice_number}',
|
||||
metadata={
|
||||
'invoice_id': invoice.id,
|
||||
'payment_id': payment.id,
|
||||
'approved_by': request.user.email
|
||||
}
|
||||
)
|
||||
|
||||
successful.append(f'Payment #{payment.id} - {account.name} - Invoice {invoice.invoice_number} - {credits_added} credits')
|
||||
renewal_label = ' (renewal reset)' if is_renewal or (account.plan and CreditTransaction.objects.filter(account=account, transaction_type='subscription').count() > 0) else ''
|
||||
successful.append(f'Payment #{payment.id} - {account.name} - Invoice {invoice.invoice_number} - {credits_added} credits{renewal_label}')
|
||||
|
||||
except Exception as e:
|
||||
errors.append(f'Payment #{payment.id}: {str(e)}')
|
||||
|
||||
Reference in New Issue
Block a user