# Generated by Django 3.1.3 on 2020-12-21 11:28

from django.db import migrations, models


def set_total_prices(apps, schema_editor):
    OrderLine = apps.get_model("order", "OrderLine")
    lines = []
    for line in OrderLine.objects.filter(
        total_price_gross_amount__isnull=True
    ).iterator():
        line.total_price_gross_amount = line.unit_price_gross_amount * line.quantity
        line.total_price_net_amount = line.unit_price_net_amount * line.quantity
        lines.append(line)

    OrderLine.objects.bulk_update(
        lines, ["total_price_gross_amount", "total_price_net_amount"], batch_size=1000
    )


class Migration(migrations.Migration):

    dependencies = [
        ("order", "0093_auto_20201130_1406"),
    ]

    operations = [
        migrations.AddField(
            model_name="orderline",
            name="total_price_gross_amount",
            field=models.DecimalField(
                blank=True, decimal_places=3, max_digits=12, null=True
            ),
        ),
        migrations.AddField(
            model_name="orderline",
            name="total_price_net_amount",
            field=models.DecimalField(
                blank=True, decimal_places=3, max_digits=12, null=True
            ),
        ),
        migrations.RunPython(
            set_total_prices,
            migrations.RunPython.noop,
        ),
        migrations.AlterField(
            model_name="orderline",
            name="total_price_gross_amount",
            field=models.DecimalField(decimal_places=3, max_digits=12),
        ),
        migrations.AlterField(
            model_name="orderline",
            name="total_price_net_amount",
            field=models.DecimalField(decimal_places=3, max_digits=12),
        ),
    ]
