# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-11-03 11:03
from __future__ import unicode_literals

from decimal import Decimal

import django.db.migrations.operations.special
import django.db.models.deletion
import i18nfield.fields
from django.db import migrations, models


def fee_converter(app, schema_editor):
    OrderFee = app.get_model('pretixbase', 'OrderFee')
    Order = app.get_model('pretixbase', 'Order')

    of = []
    for o in Order.objects.exclude(payment_fee=Decimal('0.00')).iterator():
        of.append(OrderFee(
            order=o,
            value=o.payment_fee,
            fee_type='payment',
            tax_rate=o.payment_fee_tax_rate,
            tax_rule=o.payment_fee_tax_rule,
            tax_value=o.payment_fee_tax_value,
            internal_type=o.payment_provider
        ))
        if len(of) > 900:
            OrderFee.objects.bulk_create(of)
            of = []
    OrderFee.objects.bulk_create(of)


def assign_positions(app, schema_editor):
    Invoice = app.get_model('pretixbase', 'Invoice')

    for i in Invoice.objects.iterator():
        for j, l in enumerate(i.lines.all()):
            l.position = j
            l.save()


def clear_quota_caches(app, schema_editor):
    Quota = app.get_model('pretixbase', 'Quota')
    Quota.objects.all().update(cached_availability_time=None)


class Migration(migrations.Migration):

    replaces = [('pretixbase', '0076_orderfee'), ('pretixbase', '0077_auto_20170829_1126'), ('pretixbase', '0078_auto_20171003_1650'), ('pretixbase', '0079_auto_20171010_2117'), ('pretixbase', '0080_auto_20171016_1553'), ('pretixbase', '0081_quota_cached_availability_paid_orders'), ('pretixbase', '0082_invoiceaddress_internal_reference')]

    dependencies = [
        ('pretixbase', '0075_auto_20170828_0901'),
    ]

    operations = [
        migrations.CreateModel(
            name='OrderFee',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('value', models.DecimalField(decimal_places=2, max_digits=10, verbose_name='Value')),
                ('description', models.CharField(blank=True, max_length=190)),
                ('internal_type', models.CharField(blank=True, max_length=255)),
                ('fee_type', models.CharField(choices=[('payment', 'Payment method fee'), ('shipping', 'Shipping fee')], max_length=100)),
                ('tax_rate', models.DecimalField(decimal_places=2, max_digits=7, verbose_name='Tax rate')),
                ('tax_value', models.DecimalField(decimal_places=2, max_digits=10, verbose_name='Tax value')),
                ('order', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='fees', to='pretixbase.Order', verbose_name='Order')),
                ('tax_rule', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='pretixbase.TaxRule')),
            ],
        ),
        migrations.RunPython(
            code=fee_converter,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.RemoveField(
            model_name='order',
            name='payment_fee',
        ),
        migrations.RemoveField(
            model_name='order',
            name='payment_fee_tax_rate',
        ),
        migrations.RemoveField(
            model_name='order',
            name='payment_fee_tax_rule',
        ),
        migrations.RemoveField(
            model_name='order',
            name='payment_fee_tax_value',
        ),
        migrations.AddField(
            model_name='invoiceline',
            name='position',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AlterField(
            model_name='orderfee',
            name='fee_type',
            field=models.CharField(choices=[('payment', 'Payment fee'), ('shipping', 'Shipping fee'), ('other', 'Other fees')], max_length=100),
        ),
        migrations.RunPython(
            code=assign_positions,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.AlterModelOptions(
            name='invoiceline',
            options={'ordering': ('position', 'pk')},
        ),
        migrations.AddField(
            model_name='quota',
            name='cached_availability_number',
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='quota',
            name='cached_availability_state',
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='quota',
            name='cached_availability_time',
            field=models.DateTimeField(blank=True, null=True),
        ),
        migrations.AlterField(
            model_name='eventmetaproperty',
            name='default',
            field=models.TextField(blank=True),
        ),
        migrations.AlterField(
            model_name='taxrule',
            name='eu_reverse_charge',
            field=models.BooleanField(default=False, help_text='Not recommended. Most events will NOT be qualified for reverse charge since the place of taxation is the location of the event. This option disables charging VAT for all customers outside the EU and for business customers in different EU countries who entered a valid EU VAT ID. Only enable this option after consulting a tax counsel. No warranty given for correct tax calculation. USE AT YOUR OWN RISK.', verbose_name='Use EU reverse charge taxation rules'),
        ),
        migrations.AddField(
            model_name='logentry',
            name='api_token',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='pretixbase.TeamAPIToken'),
        ),
        migrations.AlterField(
            model_name='event',
            name='name',
            field=i18nfield.fields.I18nCharField(max_length=200, verbose_name='Event name'),
        ),
        migrations.AlterField(
            model_name='item',
            name='category',
            field=models.ForeignKey(blank=True, help_text='If you have many products, you can optionally sort them into categories to keep things organized.', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='items', to='pretixbase.ItemCategory', verbose_name='Category'),
        ),
        migrations.AlterField(
            model_name='cartposition',
            name='cart_id',
            field=models.CharField(blank=True, db_index=True, max_length=255, null=True, verbose_name='Cart ID (e.g. session key)'),
        ),
        migrations.AddField(
            model_name='quota',
            name='cached_availability_paid_orders',
            field=models.PositiveIntegerField(blank=True, null=True),
        ),
        migrations.RunPython(
            code=clear_quota_caches,
            reverse_code=django.db.migrations.operations.special.RunPython.noop,
        ),
        migrations.AddField(
            model_name='invoiceaddress',
            name='internal_reference',
            field=models.TextField(blank=True, help_text='This reference will be printed on your invoice for your convenience.', verbose_name='Internal reference'),
        ),
        migrations.AddField(
            model_name='invoice',
            name='internal_reference',
            field=models.TextField(blank=True),
        ),
    ]
