# Generated by Django 3.2.2 on 2022-03-14 20:01
from decimal import Decimal

from django.db import migrations
from django.db.models import F
from django.db.models.functions import Greatest


def migrate_voucher_budget_use(apps, schema_editor):
    OrderPosition = apps.get_model('pretixbase', 'OrderPosition')  # noqa
    OrderPosition.all.filter(
        price_before_voucher__isnull=False
    ).exclude(price=F('price_before_voucher')).update(
        voucher_budget_use=Greatest(F('price_before_voucher') - F('price'), Decimal('0.00'))
    )


class Migration(migrations.Migration):
    dependencies = [
        ('pretixbase', '0210_auto_20220303_2017'),
    ]

    operations = [
        migrations.RunPython(
            migrate_voucher_budget_use,
            migrations.RunPython.noop,
        ),
    ]
