# Generated by Django 5.2.9 on 2026-02-12 13:25
from decimal import Decimal

import django.contrib.postgres.constraints
import django.contrib.postgres.fields.ranges
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('currency', '0002_alter_currencyfxrates_unique_together_and_more'),
        ('directory', '0015_alter_emailcontact_address_and_more'),
        ('wbaccounting', '0014_bookingentrytype_remove_bookingentry_title_and_more'),
        ('wbcrm', '0020_alter_product_unique_together_product_unique_product'),
    ]

    operations = [
        migrations.AlterField(
            model_name='bookingentry',
            name='backlinks',
            field=models.JSONField(blank=True, default=dict),
        ),
        migrations.AlterField(
            model_name='bookingentry',
            name='parameters',
            field=models.JSONField(blank=True, default=dict),
        ),
        migrations.AlterField(
            model_name='bookingentry',
            name='period',
            field=django.contrib.postgres.fields.ranges.DateRangeField(default=None, verbose_name='Period'),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='bookingentry',
            name='key',
            field=models.CharField(default=None, max_length=255, verbose_name='Key'),
            preserve_default=False,
        ),
        migrations.RemoveField(
            model_name='invoice',
            name='gross_value',
        ),
        migrations.RemoveField(
            model_name='invoice',
            name='net_value',
        ),
        migrations.RemoveField(
            model_name='bookingentry',
            name='invoice_gross_value',
        ),
        migrations.RemoveField(
            model_name='bookingentry',
            name='invoice_net_value',
        ),
        migrations.AddField(
            model_name='bookingentry',
            name='invoice_gross_value',
            field=models.GeneratedField(db_persist=True, expression=django.db.models.expressions.CombinedExpression(
                models.F('invoice_fx_rate'), '*', models.F('gross_value')),
                                        output_field=models.DecimalField(blank=True, decimal_places=4, max_digits=16,
                                                                         null=True)),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='bookingentry',
            name='invoice_net_value',
            field=models.GeneratedField(db_persist=True, expression=django.db.models.expressions.CombinedExpression(models.F('invoice_fx_rate'), '*', django.db.models.expressions.CombinedExpression(models.F('gross_value'), '/', django.db.models.expressions.CombinedExpression(models.Value(Decimal('1')), '+', models.F('vat')))), output_field=models.DecimalField(blank=True, decimal_places=4, max_digits=16, null=True)),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='bookingentry',
            name='type',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='booking_entries', to='wbaccounting.bookingentrytype',
                                    verbose_name='Type'),
        ),
        migrations.AlterField(
            model_name='bookingentry',
            name='gross_value',
            field=models.DecimalField(decimal_places=4, default=Decimal('0'), max_digits=16,
                                      verbose_name='Gross Value'),
        ),
        migrations.RemoveField(
            model_name='bookingentry',
            name='net_value',
        ),
        migrations.AddField(
            model_name='bookingentry',
            name='net_value',
            field=models.GeneratedField(db_persist=True, expression=django.db.models.expressions.CombinedExpression(models.F('gross_value'), '/', django.db.models.expressions.CombinedExpression(models.Value(Decimal('1')), '+', models.F('vat'))), output_field=models.DecimalField(decimal_places=4, max_digits=16)),
        ),
        migrations.AddIndex(
            model_name='bookingentry',
            index=models.Index(fields=['counterparty', 'type', 'product', 'account', 'period'],
                               name='wbaccountin_counter_565eaa_idx'),
        ),
        migrations.AddIndex(
            model_name='bookingentry',
            index=models.Index(fields=['counterparty', 'type', 'product', 'account', 'period', 'invoice'],
                               name='wbaccountin_counter_70bf5b_idx'),
        ),
        migrations.AddConstraint(
            model_name='bookingentry',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(
                condition=models.Q(('invoice__isnull', True), ('account__isnull', False)),
                expressions=[('period', '&&'), ('counterparty', '='), ('key', '='), ('account', '=')],
                name='exclude_overlapping_booking_entry'),
        ),
        migrations.AddConstraint(
            model_name='bookingentry',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(
                condition=models.Q(('invoice__isnull', True), ('account__isnull', True)),
                expressions=[('period', '&&'), ('counterparty', '='), ('key', '=')],
                name='exclude_overlapping_booking_entry_no_account'),
        ),
        migrations.AddConstraint(
            model_name='bookingentry',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(
                condition=models.Q(('invoice__isnull', False), ('account__isnull', False)),
                expressions=[('period', '&&'), ('counterparty', '='), ('key', '='), ('account', '='), ('invoice', '=')],
                name='exclude_overlapping_invoiced_booking_entry'),
        ),
        migrations.AddConstraint(
            model_name='bookingentry',
            constraint=django.contrib.postgres.constraints.ExclusionConstraint(
                condition=models.Q(('invoice__isnull', False), ('account__isnull', True)),
                expressions=[('period', '&&'), ('counterparty', '='), ('key', '='), ('invoice', '=')],
                name='exclude_overlapping_invoiced_booking_entry_no_account'),
        ),

    ]
