from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
    initial = True
    dependencies = [
        ("G_Affectation", "0002_mentions_points_de_vente"),
        ("facturation", "0061_alter_produit_prix_vente"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]
    operations = [
        migrations.CreateModel(
            name="ConfigurationTaux",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("mode", models.CharField(choices=[("GLOBAL", "Un taux unique pour tous les points de vente"), ("PAR_CIBLE", "Des taux par mention ou point de vente")], default="GLOBAL", max_length=20)),
                ("taux_global", models.DecimalField(decimal_places=2, default=2800, max_digits=18, verbose_name="Taux global CDF pour 1 USD")),
                ("modifie_le", models.DateTimeField(auto_now=True)),
                ("modifie_par", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="+", to=settings.AUTH_USER_MODEL, verbose_name="Modifiée par")),
            ],
            options={"verbose_name": "Configuration des taux", "verbose_name_plural": "Configuration des taux"},
        ),
        migrations.CreateModel(
            name="HistoriqueTaux",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("action", models.CharField(choices=[("CONFIGURATION", "Configuration globale"), ("CREATION", "Création"), ("MODIFICATION", "Modification"), ("SUPPRESSION", "Suppression")], max_length=20)),
                ("cible_type", models.CharField(blank=True, max_length=20)),
                ("cible_id", models.PositiveBigIntegerField(blank=True, null=True)),
                ("cible_libelle", models.CharField(max_length=255)),
                ("ancien_taux", models.DecimalField(blank=True, decimal_places=2, max_digits=18, null=True)),
                ("nouveau_taux", models.DecimalField(blank=True, decimal_places=2, max_digits=18, null=True)),
                ("ancien_mode", models.CharField(blank=True, max_length=20)),
                ("nouveau_mode", models.CharField(blank=True, max_length=20)),
                ("cree_le", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("utilisateur", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="+", to=settings.AUTH_USER_MODEL)),
            ],
            options={"verbose_name": "Historique de taux", "verbose_name_plural": "Historique des taux", "ordering": ("-cree_le", "-pk")},
        ),
        migrations.CreateModel(
            name="RegleTaux",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("taux", models.DecimalField(decimal_places=2, max_digits=18, verbose_name="Taux CDF pour 1 USD")),
                ("actif", models.BooleanField(default=True, verbose_name="Actif")),
                ("cree_le", models.DateTimeField(auto_now_add=True)),
                ("modifie_le", models.DateTimeField(auto_now=True)),
                ("cree_par", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="+", to=settings.AUTH_USER_MODEL, verbose_name="Créée par")),
                ("mention", models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name="regle_taux", to="G_Affectation.mentionpointdevente", verbose_name="Mention")),
                ("modifie_par", models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name="+", to=settings.AUTH_USER_MODEL, verbose_name="Modifiée par")),
                ("point_de_vente", models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name="regle_taux", to="facturation.pointdevente", verbose_name="Point de vente")),
            ],
            options={"verbose_name": "Règle de taux", "verbose_name_plural": "Règles de taux", "ordering": ("mention__nom", "point_de_vente__nom", "pk")},
        ),
        migrations.AddConstraint(
            model_name="regletaux",
            constraint=models.CheckConstraint(condition=(models.Q(("mention__isnull", True), ("point_de_vente__isnull", False)) | models.Q(("mention__isnull", False), ("point_de_vente__isnull", True))), name="g_taux_cible_unique"),
        ),
        migrations.AddConstraint(
            model_name="regletaux",
            constraint=models.CheckConstraint(condition=models.Q(("taux__gt", 0)), name="g_taux_taux_positif"),
        ),
    ]
