# Generated by Django 3.2.2 on 2021-05-14 12:38

from django.db import migrations, models


def set_undiscounted_values(apps, schema_editor):
    OrderLine = apps.get_model("order", "OrderLine")
    for line in OrderLine.objects.all().iterator():
        undiscounted_unit_price_net = (
            line.unit_price_net_amount + line.unit_discount_amount
        )
        undiscounted_unit_price_gross = (
            line.unit_price_gross_amount + line.unit_discount_amount
        )
        line.undiscounted_unit_price_net_amount = undiscounted_unit_price_net
        line.undiscounted_unit_price_gross_amount = undiscounted_unit_price_gross
        line.undiscounted_total_price_net_amount = (
            undiscounted_unit_price_net * line.quantity
        )
        line.undiscounted_total_price_gross_amount = (
            undiscounted_unit_price_gross * line.quantity
        )
        line.save(
            update_fields=[
                "undiscounted_unit_price_net_amount",
                "undiscounted_unit_price_gross_amount",
                "undiscounted_total_price_net_amount",
                "undiscounted_total_price_gross_amount",
            ]
        )


class Migration(migrations.Migration):

    dependencies = [
        ("order", "0108_origin_not_null"),
    ]

    operations = [
        migrations.AddField(
            model_name="orderline",
            name="undiscounted_total_price_gross_amount",
            field=models.DecimalField(decimal_places=3, default=0, max_digits=12),
        ),
        migrations.AddField(
            model_name="orderline",
            name="undiscounted_total_price_net_amount",
            field=models.DecimalField(decimal_places=3, default=0, max_digits=12),
        ),
        migrations.AddField(
            model_name="orderline",
            name="undiscounted_unit_price_gross_amount",
            field=models.DecimalField(decimal_places=3, default=0, max_digits=12),
        ),
        migrations.AddField(
            model_name="orderline",
            name="undiscounted_unit_price_net_amount",
            field=models.DecimalField(decimal_places=3, default=0, max_digits=12),
        ),
        migrations.RunPython(set_undiscounted_values, migrations.RunPython.noop),
    ]
