# Generated by Django 5.2.9 on 2026-01-21 12:55
from datetime import date

import django.contrib.postgres.constraints
import django.contrib.postgres.fields.ranges
import django.db.models.deletion
from django.db import migrations, models
from psycopg.types.range import DateRange

def migrate_vat(apps, schema_editor):
    EntryAccountingInformation = apps.get_model('wbaccounting', 'EntryAccountingInformation')
    Vat = apps.get_model('wbaccounting', 'Vat')
    VatRate = apps.get_model('wbaccounting', 'VatRate')
    objs = []
    default_vat = Vat.objects.create(name=str(0.08), primary=True)
    VatRate.objects.create(rate=0.08, vat=default_vat, timespan=DateRange(date(2010, 1, 1), None))
    for entry in EntryAccountingInformation.objects.all():
        if entry.old_vat:
            try:
                rate = VatRate.objects.get(rate=entry.old_vat)
                entry.vat = rate.vat
            except VatRate.DoesNotExist:
                vat = Vat.objects.create(name=str(entry.old_vat))
                rate = VatRate.objects.create(rate=entry.old_vat, vat=vat, timespan=DateRange(date(2010,1,1), None))
                entry.vat = rate.vat
        else:
            entry.vat = default_vat
        objs.append(entry)
    EntryAccountingInformation.objects.bulk_update(objs, ["vat"])

class Migration(migrations.Migration):

    dependencies = [
        ('wbaccounting', '0012_entryaccountinginformation_external_invoice_users'),
    ]

    operations = [
        migrations.CreateModel(
            name='Vat',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ("primary", models.BooleanField(default=False, verbose_name='Primary'))
            ],
        ),
        migrations.RenameField(model_name='entryaccountinginformation', old_name="vat", new_name="old_vat",),
        migrations.AddField(
            model_name='entryaccountinginformation',
            name='vat',
            field=models.ForeignKey(default=None, blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='vat_recipients', to='wbaccounting.vat', verbose_name='VAT'),
            preserve_default=False,
        ),
        migrations.CreateModel(
            name='VatRate',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('timespan', django.contrib.postgres.fields.ranges.DateRangeField(verbose_name='Timespan')),
                ('rate', models.DecimalField(decimal_places=4, max_digits=5)),
                ('vat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rates', to='wbaccounting.vat')),
            ],
            options={
                'verbose_name': 'Vat Rate',
                'verbose_name_plural': 'Vat Rates',
                'constraints': [django.contrib.postgres.constraints.ExclusionConstraint(expressions=[('timespan', '&&'), ('vat', '=')], name='exclude_overlapping_vat_rates')],
            },
        ),
        migrations.RunPython(migrate_vat),
        migrations.RemoveField(model_name='entryaccountinginformation', name="old_vat"),
        migrations.AlterField(
            model_name='entryaccountinginformation',
            name='vat',
            field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.PROTECT,
                                    related_name='vat_recipients', to='wbaccounting.vat', verbose_name='VAT'),
            preserve_default=False,
        ),
        migrations.AlterModelOptions(
            name='vat',
            options={'verbose_name': 'Vat Group', 'verbose_name_plural': 'Vat Groups'},
        ),
        migrations.AddConstraint(
            model_name='vat',
            constraint=models.UniqueConstraint(fields=('primary',), name='unique_primary_vat_group'),
        ),
    ]
