mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-01-20 03:40:26 +00:00
Update miniapp.py
This commit is contained in:
@@ -6628,21 +6628,14 @@ async def purchase_tariff_endpoint(
|
||||
|
||||
def _get_user_period_discount(user, period_days: int) -> int:
|
||||
"""Получает скидку пользователя на период (унифицировано с ботом)."""
|
||||
# Используем ту же логику, что и в боте: getattr(db_user, 'promo_group', None)
|
||||
promo_group = getattr(user, 'promo_group', None) if user else None
|
||||
print(f"[MINIAPP _get_user_period_discount] user={user}, period_days={period_days}")
|
||||
print(f"[MINIAPP _get_user_period_discount] promo_group={promo_group}")
|
||||
|
||||
if promo_group:
|
||||
# Используем метод get_discount_percent с категорией "period"
|
||||
discount = promo_group.get_discount_percent("period", period_days)
|
||||
print(f"[MINIAPP _get_user_period_discount] promo_group.get_discount_percent('period', {period_days})={discount}")
|
||||
if discount > 0:
|
||||
return discount
|
||||
|
||||
# Проверяем персональную скидку
|
||||
personal_discount = get_user_active_promo_discount_percent(user) if user else 0
|
||||
print(f"[MINIAPP _get_user_period_discount] personal_discount={personal_discount}")
|
||||
return personal_discount
|
||||
|
||||
|
||||
@@ -6666,27 +6659,15 @@ def _calculate_tariff_switch_cost(
|
||||
Логика унифицирована с ботом (tariff_purchase.py).
|
||||
|
||||
Формула: (new_monthly - current_monthly) * remaining_days / 30
|
||||
Скидка промогруппы применяется к обоим тарифам одинаково.
|
||||
Скидка применяется к обоим тарифам одинаково.
|
||||
|
||||
Returns:
|
||||
(cost_kopeks, is_upgrade) - стоимость доплаты и флаг апгрейда
|
||||
"""
|
||||
# Берём месячную цену (30 дней) как базу (с fallback)
|
||||
current_monthly = _get_tariff_monthly_price(current_tariff)
|
||||
new_monthly = _get_tariff_monthly_price(new_tariff)
|
||||
|
||||
# DEBUG: логируем начальные значения
|
||||
print(f"[MINIAPP SWITCH DEBUG] current_tariff={current_tariff.name}, new_tariff={new_tariff.name}")
|
||||
print(f"[MINIAPP SWITCH DEBUG] current_monthly={current_monthly}, new_monthly={new_monthly}")
|
||||
print(f"[MINIAPP SWITCH DEBUG] remaining_days={remaining_days}")
|
||||
print(f"[MINIAPP SWITCH DEBUG] user={user}, promo_group={promo_group}")
|
||||
|
||||
user_promo_group = getattr(user, 'promo_group', None) if user else None
|
||||
print(f"[MINIAPP SWITCH DEBUG] user.promo_group={user_promo_group}")
|
||||
|
||||
# Получаем скидку (унифицировано с ботом)
|
||||
discount_percent = _get_user_period_discount(user, 30) if user else 0
|
||||
print(f"[MINIAPP SWITCH DEBUG] discount_percent from _get_user_period_discount={discount_percent}")
|
||||
|
||||
# Fallback на promo_group.period_discounts если user не передан
|
||||
if discount_percent == 0 and promo_group:
|
||||
@@ -6698,25 +6679,17 @@ def _calculate_tariff_switch_cost(
|
||||
break
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
print(f"[MINIAPP SWITCH DEBUG] discount_percent after fallback={discount_percent}")
|
||||
|
||||
# Применяем скидку к обоим тарифам
|
||||
if discount_percent > 0:
|
||||
current_monthly = _apply_promo_discount(current_monthly, discount_percent)
|
||||
new_monthly = _apply_promo_discount(new_monthly, discount_percent)
|
||||
print(f"[MINIAPP SWITCH DEBUG] after discount: current_monthly={current_monthly}, new_monthly={new_monthly}")
|
||||
|
||||
price_diff = new_monthly - current_monthly
|
||||
print(f"[MINIAPP SWITCH DEBUG] price_diff={price_diff}")
|
||||
|
||||
if price_diff <= 0:
|
||||
# Даунгрейд или равная цена - бесплатно
|
||||
print(f"[MINIAPP SWITCH DEBUG] downgrade/equal, returning 0")
|
||||
return 0, False
|
||||
|
||||
# Апгрейд - рассчитываем доплату пропорционально оставшимся дням
|
||||
upgrade_cost = int(price_diff * remaining_days / 30)
|
||||
print(f"[MINIAPP SWITCH DEBUG] upgrade_cost={upgrade_cost}")
|
||||
return upgrade_cost, True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user