# Generated by Django 5.2.9 on 2026-01-15 13:43

import django.db.models.deletion
from django.db import migrations, models

def migrate_root_account(apps, schema_editor):
    Commission = apps.get_model('wbcommission', 'Commission')
    Account = apps.get_model('wbcrm', 'Account')
    objs = []
    for c in Commission.objects.all():
        c.root_account = Account.objects.filter(tree_id=c.account.tree_id, level=0).first()
        objs.append(c)
    Commission.objects.bulk_update(objs, ["root_account"])

class Migration(migrations.Migration):

    dependencies = [
        ('wbcommission', '0011_commissionrole_unique_commission_role'),
        ('wbcrm', '0020_alter_product_unique_together_product_unique_product'),
    ]

    operations = [
        migrations.AddField(
            model_name='commission',
            name='root_account',
            field=models.ForeignKey(blank=True, null=True,on_delete=django.db.models.deletion.CASCADE, related_name='root_account_commissions', to='wbcrm.account', verbose_name='Root Account'),
            preserve_default=False,
        ),
        migrations.RunSQL(sql="SET CONSTRAINTS ALL IMMEDIATE;"),
        migrations.RunPython(migrate_root_account),
        migrations.RunSQL(sql="SET CONSTRAINTS ALL DEFERRED;"),
        migrations.AlterField(
            model_name='commission',
            name='root_account',
            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='root_account_commissions', to='wbcrm.account',
                                    verbose_name='Root Account'),
            preserve_default=False,
        ),
    ]
