# Generated by Django 5.0.4 on 2024-04-17 23:03

from django.db import migrations, models


def migrate_address_fields(apps, schema_editor):
    PaymentReceiver = apps.get_model("kmuhelper", "PaymentReceiver")
    for receiver in PaymentReceiver.objects.all():
        receiver.invoice_street = receiver.invoice_address_1[:70]

        _index = receiver.invoice_address_2.index(" ")
        receiver.invoice_postcode = receiver.invoice_address_2[:_index]
        receiver.invoice_city = receiver.invoice_address_2[_index + 1 : _index + 36]

        receiver.save()


def migrate_address_fields_reverse(apps, schema_editor):
    PaymentReceiver = apps.get_model("kmuhelper", "PaymentReceiver")
    for receiver in PaymentReceiver.objects.all():
        receiver.invoice_address_1 = (
            f"{receiver.invoice_street} {receiver.invoice_street_nr}".strip()
        )
        receiver.invoice_address_2 = (
            f"{receiver.invoice_postcode} {receiver.invoice_city}".strip()
        )
        receiver.save()


class Migration(migrations.Migration):
    dependencies = [
        ("kmuhelper", "0114_adminlogentry_alter_order_payment_conditions"),
    ]

    operations = [
        migrations.AddField(
            model_name="paymentreceiver",
            name="invoice_city",
            field=models.CharField(default="", max_length=35, verbose_name="Ort"),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="paymentreceiver",
            name="invoice_postcode",
            field=models.CharField(
                default="", max_length=16, verbose_name="Postleitzahl"
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="paymentreceiver",
            name="invoice_street",
            field=models.CharField(
                default="",
                help_text="Strasse oder Postfach",
                max_length=70,
                verbose_name="Strasse",
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="paymentreceiver",
            name="invoice_street_nr",
            field=models.CharField(
                blank=True, default="", max_length=16, verbose_name="Hausnummer"
            ),
            preserve_default=False,
        ),
        migrations.RunPython(
            code=migrate_address_fields,
            reverse_code=migrate_address_fields_reverse,
            elidable=True,
        ),
        migrations.RemoveField(
            model_name="paymentreceiver",
            name="invoice_address_1",
        ),
        migrations.RemoveField(
            model_name="paymentreceiver",
            name="invoice_address_2",
        ),
        migrations.AlterField(
            model_name="paymentreceiver",
            name="invoice_country",
            field=models.CharField(
                choices=[("CH", "Schweiz"), ("LI", "Liechtenstein")],
                default="CH",
                max_length=2,
                verbose_name="Land",
            ),
        ),
        migrations.AlterField(
            model_name="paymentreceiver",
            name="invoice_name",
            field=models.CharField(
                help_text="Name / Firma", max_length=70, verbose_name="Kontoinhaber"
            ),
        ),
    ]
