# Generated by Django 5.0 on 2024-01-14 16:59
from datetime import datetime

from django.db import migrations, models
from django.utils import timezone

VAT_RATE_REVERSE_MIGRATION = {2.6: 2.5, 3.8: 3.7, 8.1: 7.7}


def copy_values(apps, schema_editor):
    db_alias = schema_editor.connection.alias
    OrderItem = apps.get_model("kmuhelper", "OrderItem")

    for item in (
        OrderItem.objects.using(db_alias).all().select_related("order", "product")
    ):
        item.article_number = item.product.article_number
        item.quantity_description = item.product.quantity_description
        item.name = item.product.name
        item.vat_rate = item.product.vat_rate

        if (
            item.order.date
            < timezone.make_aware(
                datetime(year=2024, month=1, day=1, hour=0, minute=0, second=0)
            )
            and item.vat_rate in VAT_RATE_REVERSE_MIGRATION
        ):
            item.vat_rate = VAT_RATE_REVERSE_MIGRATION[item.vat_rate]

        item.save()


class Migration(migrations.Migration):
    dependencies = [
        ("kmuhelper", "0109_contactperson_is_default_paymentreceiver_is_default"),
    ]

    operations = [
        migrations.AddField(
            model_name="orderitem",
            name="article_number",
            field=models.CharField(
                default="", max_length=25, verbose_name="Artikelnummer"
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="orderitem",
            name="name",
            field=models.CharField(
                default="",
                help_text="Unterstützt i18n: '[:de]Deutsch[:fr]Français[:it]Italiano[:en]English[:]'",
                max_length=500,
                verbose_name="Name",
            ),
        ),
        migrations.AddField(
            model_name="orderitem",
            name="quantity_description",
            field=models.CharField(
                blank=True,
                default="Stück",
                help_text="Unterstützt i18n: '[:de]Deutsch[:fr]Français[:it]Italiano[:en]English[:]'",
                max_length=100,
                verbose_name="Mengenbezeichnung",
            ),
        ),
        migrations.AddField(
            model_name="orderitem",
            name="vat_rate",
            field=models.FloatField(
                choices=[
                    (0.0, "0.0% (Mehrwertsteuerfrei)"),
                    (2.6, "2.6% (Reduzierter Satz)"),
                    (3.8, "3.8% (Sondersatz für Beherbergung)"),
                    (8.1, "8.1% (Normalsatz)"),
                    (2.5, "2.5% (Bis 2023: Reduzierter Satz)"),
                    (3.7, "3.7% (Bis 2023: Sondersatz für Beherbergung)"),
                    (7.7, "7.7% (Bis 2023: Normalsatz)"),
                ],
                default=8.1,
                verbose_name="MwSt-Satz",
            ),
        ),
        migrations.RunPython(
            # This migration cannot be reverted!
            copy_values,
            reverse_code=None,
            elidable=True,
        ),
        migrations.RenameField(
            model_name="orderitem", old_name="product", new_name="linked_product"
        ),
        migrations.AlterField(
            model_name="orderitem",
            name="linked_product",
            field=models.ForeignKey(
                blank=True,
                default=None,
                null=True,
                on_delete=models.deletion.SET_NULL,
                to="kmuhelper.product",
                verbose_name="Verknüpftes Produkt",
            ),
        ),
    ]
