""" Données de RÉFÉRENCE (barèmes + catégories complètes Indy + groupe Gestionnaire). Idempotent : sûr à relancer en production. Appelé par l'entrypoint au démarrage. Usage : python manage.py seed_reference """ from decimal import Decimal from django.core.management.base import BaseCommand from django.contrib.auth.models import Group, Permission from compta.models import Bareme, Categorie, Activite from compta.categories_indy import CATALOGUE BAREMES_2026 = [ (Activite.VENTE, Decimal("12.30"), Decimal("71"), Decimal("1.0"), Decimal("203100"), Decimal("85000"), Decimal("93500")), (Activite.SERVICE_BIC, Decimal("21.20"), Decimal("50"), Decimal("1.7"), Decimal("83600"), Decimal("37500"), Decimal("41250")), (Activite.SERVICE_BNC, Decimal("25.60"), Decimal("34"), Decimal("2.2"), Decimal("83600"), Decimal("37500"), Decimal("41250")), ] GROUPE_NOM = "Gestionnaire entreprise" PERMS_COMPLETES = ["client", "facture", "lignefacture", "depense", "paiement"] PERMS_CHANGE = ["entreprise"] PERMS_VUE = ["categorie", "bareme"] def charger_groupes(): g, _ = Group.objects.get_or_create(name=GROUPE_NOM) perms = [] for model in PERMS_COMPLETES: for action in ("add", "change", "delete", "view"): perms += list(Permission.objects.filter( content_type__app_label="compta", codename=f"{action}_{model}")) for model in PERMS_CHANGE: for action in ("view", "change"): perms += list(Permission.objects.filter( content_type__app_label="compta", codename=f"{action}_{model}")) for model in PERMS_VUE: perms += list(Permission.objects.filter( content_type__app_label="compta", codename=f"view_{model}")) g.permissions.set(perms) return g def charger_reference(stdout=None, style=None): for act, urssaf, ab, vl, sca, tb, tm in BAREMES_2026: Bareme.objects.update_or_create( activite=act, annee=2026, defaults=dict(taux_urssaf=urssaf, abattement_fiscal=ab, taux_vl=vl, seuil_ca=sca, seuil_tva_base=tb, seuil_tva_majore=tm)) cat = {} for i, (nom, usage, excl, compte) in enumerate(CATALOGUE): c, _ = Categorie.objects.update_or_create( nom=nom, defaults=dict(usage=usage, exclure_calculs=excl, compte_pcg=compte, ordre=i)) cat[nom] = c g = charger_groupes() if stdout and style: stdout.write(style.SUCCESS( f"Référence : {len(BAREMES_2026)} barèmes, {len(cat)} catégories, " f"groupe « {g.name} » ({g.permissions.count()} permissions).")) return cat class Command(BaseCommand): help = "Charge barèmes, catégories (référentiel Indy) et le groupe Gestionnaire." def handle(self, *args, **options): charger_reference(self.stdout, self.style)