uchill/backend/apps/subscriptions/management/commands/create_duration_discounts.py

64 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Команда для создания скидок за длительность для ежемесячных тарифов.
"""
from django.core.management.base import BaseCommand
from apps.subscriptions.models import SubscriptionPlan, DurationDiscount
from decimal import Decimal
class Command(BaseCommand):
help = 'Создает скидки за длительность для ежемесячных тарифов'
def handle(self, *args, **options):
self.stdout.write('Создание скидок за длительность...')
# Получаем все ежемесячные тарифы
monthly_plans = SubscriptionPlan.objects.filter(
subscription_type='monthly',
is_active=True
)
if not monthly_plans.exists():
self.stdout.write(self.style.WARNING('Не найдено активных ежемесячных тарифов'))
return
# Стандартные скидки
discounts_data = [
{'duration_days': 90, 'discount_percent': Decimal('7.00')}, # 3 месяца - 7%
{'duration_days': 180, 'discount_percent': Decimal('12.00')}, # 6 месяцев - 12%
{'duration_days': 365, 'discount_percent': Decimal('18.00')}, # 12 месяцев - 18%
]
created_count = 0
updated_count = 0
for plan in monthly_plans:
self.stdout.write(f'\nТариф: {plan.name}')
for discount_data in discounts_data:
discount, created = DurationDiscount.objects.update_or_create(
plan=plan,
duration_days=discount_data['duration_days'],
defaults={
'discount_percent': discount_data['discount_percent']
}
)
if created:
created_count += 1
self.stdout.write(
self.style.SUCCESS(
f' ✓ Создана скидка: {discount_data["duration_days"]} дней ({discount_data["duration_days"]/30:.0f} мес) - {discount_data["discount_percent"]}%'
)
)
else:
updated_count += 1
self.stdout.write(
self.style.WARNING(
f' ⚠ Обновлена скидка: {discount_data["duration_days"]} дней ({discount_data["duration_days"]/30:.0f} мес) - {discount_data["discount_percent"]}%'
)
)
self.stdout.write(self.style.SUCCESS(f'\n✓ Готово! Создано: {created_count}, Обновлено: {updated_count}'))