# Generated by Django 5.2.18 on 2026-11-30 01:04

import apps.core.ids
import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("agencies", "0001_initial"),
        ("finance", "0003_alter_financialevent_payload"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Compte",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=apps.core.ids.uuid7,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "numero",
                    models.CharField(max_length=12, unique=True, verbose_name="numéro"),
                ),
                ("intitule", models.CharField(max_length=160, verbose_name="intitulé")),
                ("actif", models.BooleanField(default=True, verbose_name="actif")),
                (
                    "note",
                    models.CharField(blank=True, max_length=255, verbose_name="note"),
                ),
                ("cree_le", models.DateTimeField(default=django.utils.timezone.now)),
            ],
            options={
                "verbose_name": "compte",
                "ordering": ["numero"],
            },
        ),
        migrations.CreateModel(
            name="Journal",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=apps.core.ids.uuid7,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("code", models.CharField(max_length=6, unique=True)),
                ("intitule", models.CharField(max_length=120, verbose_name="intitulé")),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("ACHATS", "Achats et charges"),
                            ("RECETTES", "Recettes et produits"),
                            ("TRESORERIE", "Trésorerie"),
                            ("OD", "Opérations diverses"),
                            ("AN", "À-nouveaux"),
                        ],
                        default="OD",
                        max_length=12,
                    ),
                ),
                ("actif", models.BooleanField(default=True)),
                (
                    "compte_tresorerie",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="journaux",
                        to="accounting.compte",
                        verbose_name="compte de trésorerie",
                    ),
                ),
            ],
            options={
                "verbose_name": "journal",
                "ordering": ["code"],
            },
        ),
        migrations.CreateModel(
            name="Ecriture",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=apps.core.ids.uuid7,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "deleted_at",
                    models.DateTimeField(blank=True, db_index=True, null=True),
                ),
                ("deletion_reason", models.CharField(blank=True, max_length=255)),
                ("origin_node", models.UUIDField(blank=True, db_index=True, null=True)),
                ("version", models.PositiveIntegerField(default=1)),
                (
                    "sync_state",
                    models.CharField(
                        choices=[
                            ("LOCAL_PENDING", "Local, en attente"),
                            ("SYNCED", "Synchronisé"),
                            ("CONFLICT", "Conflit"),
                        ],
                        default="LOCAL_PENDING",
                        max_length=16,
                    ),
                ),
                ("last_synced_at", models.DateTimeField(blank=True, null=True)),
                (
                    "numero",
                    models.CharField(blank=True, max_length=40, verbose_name="numéro"),
                ),
                (
                    "date_piece",
                    models.DateField(
                        default=django.utils.timezone.localdate, verbose_name="date"
                    ),
                ),
                ("libelle", models.CharField(max_length=200, verbose_name="libellé")),
                (
                    "piece",
                    models.CharField(
                        blank=True, max_length=80, verbose_name="référence de la pièce"
                    ),
                ),
                (
                    "statut",
                    models.CharField(
                        choices=[
                            ("BROUILLARD", "Brouillard"),
                            ("VALIDEE", "Validée"),
                            ("ANNULEE", "Annulée"),
                        ],
                        default="BROUILLARD",
                        max_length=12,
                    ),
                ),
                (
                    "origine",
                    models.CharField(
                        choices=[
                            ("MANUELLE", "Saisie manuelle"),
                            ("AUTO", "Générée depuis les finances"),
                            ("CONTREPASSATION", "Contre-passation"),
                            ("A_NOUVEAUX", "À-nouveaux"),
                        ],
                        default="MANUELLE",
                        max_length=16,
                    ),
                ),
                ("source_type", models.CharField(blank=True, max_length=40)),
                ("source_id", models.UUIDField(blank=True, null=True)),
                ("source_role", models.CharField(blank=True, max_length=16)),
                ("total", models.BigIntegerField(default=0)),
                ("validee_le", models.DateTimeField(blank=True, null=True)),
                (
                    "agency",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="ecritures",
                        to="agencies.agency",
                    ),
                ),
                (
                    "contrepassation_de",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="contrepassations",
                        to="accounting.ecriture",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "deleted_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "fiscal_year",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="ecritures",
                        to="finance.fiscalyear",
                        verbose_name="exercice",
                    ),
                ),
                (
                    "updated_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "validee_par",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "journal",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="ecritures",
                        to="accounting.journal",
                    ),
                ),
            ],
            options={
                "verbose_name": "écriture comptable",
                "verbose_name_plural": "écritures comptables",
                "ordering": ["-date_piece", "-created_at"],
                "permissions": [
                    ("valider_ecriture", "Peut valider des écritures comptables"),
                    ("cloturer_exercice", "Peut clôturer un exercice comptable"),
                    ("exporter_comptabilite", "Peut exporter la comptabilité"),
                ],
            },
        ),
        migrations.CreateModel(
            name="Correspondance",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=apps.core.ids.uuid7,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                (
                    "type",
                    models.CharField(
                        choices=[
                            ("CHARGE", "Catégorie budgétaire → compte de charge"),
                            ("PRODUIT", "Source de financement → compte de produit"),
                            ("TRESORERIE", "Mode de paiement → trésorerie"),
                            ("TIERS", "Compte de tiers"),
                        ],
                        max_length=12,
                    ),
                ),
                ("cle", models.CharField(max_length=40, verbose_name="clé")),
                (
                    "libelle",
                    models.CharField(
                        blank=True, max_length=160, verbose_name="libellé"
                    ),
                ),
                (
                    "compte",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="correspondances",
                        to="accounting.compte",
                    ),
                ),
                (
                    "journal",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="+",
                        to="accounting.journal",
                    ),
                ),
            ],
            options={
                "verbose_name": "règle de comptabilisation",
                "verbose_name_plural": "règles de comptabilisation",
                "ordering": ["type", "cle"],
            },
        ),
        migrations.CreateModel(
            name="LigneEcriture",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=apps.core.ids.uuid7,
                        editable=False,
                        primary_key=True,
                        serialize=False,
                    ),
                ),
                ("ordre", models.PositiveIntegerField(default=0)),
                (
                    "libelle",
                    models.CharField(
                        blank=True, max_length=200, verbose_name="libellé"
                    ),
                ),
                ("tiers", models.CharField(blank=True, max_length=160)),
                ("debit", models.BigIntegerField(default=0, verbose_name="débit")),
                ("credit", models.BigIntegerField(default=0, verbose_name="crédit")),
                (
                    "activity",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="finance.activity",
                        verbose_name="activité",
                    ),
                ),
                (
                    "compte",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="lignes",
                        to="accounting.compte",
                    ),
                ),
                (
                    "ecriture",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="lignes",
                        to="accounting.ecriture",
                    ),
                ),
                (
                    "funding_source",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="finance.fundingsource",
                        verbose_name="source de financement",
                    ),
                ),
            ],
            options={
                "verbose_name": "ligne d'écriture",
                "ordering": ["ordre", "id"],
            },
        ),
        migrations.AddIndex(
            model_name="ecriture",
            index=models.Index(
                fields=["agency", "fiscal_year", "statut"],
                name="accounting__agency__554438_idx",
            ),
        ),
        migrations.AddIndex(
            model_name="ecriture",
            index=models.Index(
                fields=["source_type", "source_id"],
                name="accounting__source__12c7b1_idx",
            ),
        ),
        migrations.AddConstraint(
            model_name="ecriture",
            constraint=models.UniqueConstraint(
                condition=models.Q(("source_id__isnull", False)),
                fields=("source_type", "source_id", "source_role"),
                name="uniq_ecriture_source",
            ),
        ),
        migrations.AddConstraint(
            model_name="ecriture",
            constraint=models.UniqueConstraint(
                condition=models.Q(("numero", ""), _negated=True),
                fields=("agency", "numero"),
                name="uniq_ecriture_numero",
            ),
        ),
        migrations.AddConstraint(
            model_name="correspondance",
            constraint=models.UniqueConstraint(
                fields=("type", "cle"), name="uniq_correspondance"
            ),
        ),
        migrations.AddConstraint(
            model_name="ligneecriture",
            constraint=models.CheckConstraint(
                condition=models.Q(("credit__gte", 0), ("debit__gte", 0)),
                name="ligne_montants_positifs",
            ),
        ),
        migrations.AddConstraint(
            model_name="ligneecriture",
            constraint=models.CheckConstraint(
                condition=models.Q(("debit__gt", 0), ("credit__gt", 0), _negated=True),
                name="ligne_debit_ou_credit",
            ),
        ),
    ]
