trash models added, first attempt for remainign issues

This commit is contained in:
IGNY8 VPS (Salman)
2026-01-12 13:39:42 +00:00
parent 28cb698579
commit 7d4d309677
20 changed files with 1084 additions and 106 deletions

View File

@@ -508,6 +508,53 @@ class CreditService:
return account.credits
@staticmethod
@transaction.atomic
def reset_credits_for_renewal(account, new_amount, description, metadata=None):
"""
Reset credits for subscription renewal (sets credits to new_amount instead of adding).
This is used when a subscription renews - the credits are reset to the full
plan amount, not added to existing balance.
Args:
account: Account instance
new_amount: Number of credits to set (plan's included_credits)
description: Description of the transaction
metadata: Optional metadata dict
Returns:
int: New credit balance
"""
old_balance = account.credits
account.credits = new_amount
account.save(update_fields=['credits'])
# Calculate the change for the transaction record
change_amount = new_amount - old_balance
# Create CreditTransaction - use 'subscription' type for renewal
CreditTransaction.objects.create(
account=account,
transaction_type='subscription', # Uses 'Subscription Renewal' display
amount=change_amount, # Can be positive or negative depending on usage
balance_after=account.credits,
description=description,
metadata={
**(metadata or {}),
'reset_from': old_balance,
'reset_to': new_amount,
'is_renewal_reset': True
}
)
logger.info(
f"Credits reset for renewal: Account {account.id} - "
f"from {old_balance} to {new_amount} (change: {change_amount})"
)
return account.credits
@staticmethod
@transaction.atomic
def deduct_credits_for_image(

View File

@@ -720,12 +720,11 @@ def _handle_invoice_paid(invoice: dict):
logger.info(f"Skipping initial invoice for subscription {subscription_id}")
return
# Add monthly credits for renewal
# Reset credits for renewal (set to full plan amount, not add)
if plan.included_credits and plan.included_credits > 0:
CreditService.add_credits(
CreditService.reset_credits_for_renewal(
account=account,
amount=plan.included_credits,
transaction_type='subscription',
new_amount=plan.included_credits,
description=f'Monthly renewal: {plan.name}',
metadata={
'plan_id': str(plan.id),
@@ -734,7 +733,7 @@ def _handle_invoice_paid(invoice: dict):
)
logger.info(
f"Renewal credits added for account {account.id}: "
f"Renewal credits reset for account {account.id}: "
f"plan={plan.name}, credits={plan.included_credits}"
)