# Generated by Django 3.2.20 on 2023-08-01 09:15
import django.db.models.deletion
from django.db import migrations, models


def move_to_uuid_field(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    user = apps.get_model('core', 'user')
    payment_point = apps.get_model('payroll', 'paymentpoint')
    historical_payment_point = apps.get_model('payroll', 'historicalpaymentpoint')

    historical_payment_point.objects.using(db_alias).update(
        ppm=user.objects.using(db_alias).filter(i_user__id=models.F('i_user_id')).first()
    )

    payment_point.objects.using(db_alias).update(
        ppm=user.objects.using(db_alias).filter(i_user__id=models.F('i_user_id')).first()
    )


class Migration(migrations.Migration):
    dependencies = [
        ('payroll', '0004_add_payroll_rights_to_admin'),
    ]

    operations = [
        migrations.RenameField(
            model_name='historicalpaymentpoint',
            old_name='ppm',
            new_name='ppm_temp'
        ),
        migrations.RenameField(
            model_name='paymentpoint',
            old_name='ppm',
            new_name='ppm_temp'
        ),
        migrations.AddField(
            model_name='historicalpaymentpoint',
            name='ppm',
            field=models.ForeignKey(blank=True, db_constraint=False, null=True,
                                    on_delete=django.db.models.deletion.DO_NOTHING, related_name='+',
                                    to='core.user')
        ),
        migrations.AddField(
            model_name='paymentpoint',
            name='ppm',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.DO_NOTHING,
                                    to='core.user')
        ),
        migrations.RunPython(move_to_uuid_field),
        migrations.RemoveField(
            model_name='historicalpaymentpoint',
            name='ppm_temp'
        ),
        migrations.RemoveField(
            model_name='paymentpoint',
            name='ppm_temp'
        )
    ]
