from django.db import migrations # تغییرِ ترکیبِ رایگان/پولی طبق تصمیمِ محصول: # رایگان (بدون اکانت): مشتریان، پیام گروهی، فاکتور # پولی (فقط با جم): دفتر درآمد و هزینه، انبارداری، پیگیری سفارش # # سه ابزارِ رایگان عمدا همان‌هایی‌اند که بدونِ سرور هم معنی دارند (روی خودِ # گوشی کار می‌کنند) تا واقعا بدونِ حساب کاربری قابل‌استفاده باشند. FREE_KEYS = ["customers", "sms_bank", "invoice"] PAID_KEYS = ["ledger", "inventory", "orders"] # قیمتِ ابزارهایی که تازه پولی شده‌اند (اگر tier نداشته باشند). PAID_TIERS = { "ledger": [(1, 900), (3, 2400), (6, 4500)], "inventory": [(1, 800), (3, 2200), (6, 4000)], "orders": [(1, 900), (3, 2400), (6, 4500)], } def apply_split(apps, schema_editor): FeaturePlan = apps.get_model("billing", "FeaturePlan") FeaturePlanTier = apps.get_model("billing", "FeaturePlanTier") FeaturePlan.objects.filter(key__in=FREE_KEYS).update(is_free=True, is_purchasable=False) # ابزارِ رایگان نباید قیمت داشته باشد. FeaturePlanTier.objects.filter(feature__key__in=FREE_KEYS).delete() FeaturePlan.objects.filter(key__in=PAID_KEYS).update(is_free=False, is_purchasable=True) for key, tiers in PAID_TIERS.items(): feature = FeaturePlan.objects.filter(key=key).first() if feature is None: continue for months, price in tiers: FeaturePlanTier.objects.update_or_create( feature=feature, duration_months=months, defaults={"gem_price": price} ) def revert_split(apps, schema_editor): # برگشت به ترکیبِ قبلی (customers/ledger رایگان بودند). FeaturePlan = apps.get_model("billing", "FeaturePlan") FeaturePlan.objects.filter(key__in=["customers", "ledger"]).update(is_free=True, is_purchasable=False) FeaturePlan.objects.filter(key__in=["invoice", "sms_bank", "inventory", "orders"]).update( is_free=False, is_purchasable=True ) class Migration(migrations.Migration): dependencies = [("billing", "0003_seed_feature_plans")] operations = [migrations.RunPython(apply_split, revert_split)]