Saltar a contenido

MaeMetasPromotores — fuerza_ventas.mae_metas_promotores

Metas y Cuotas Comerciales de Promotores.

import uuid
from datetime import datetime
from decimal import Decimal
from typing import Optional

from sqlalchemy import (
    BigInteger,
    Boolean,
    CheckConstraint,
    ForeignKeyConstraint,
    Index,
    Integer,
    Numeric,
    UniqueConstraint,
    text,
)
from sqlalchemy.dialects.postgresql import TIMESTAMP, UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class MaeMetasPromotores(Base):
    """
    Metas y Cuotas Comerciales de Promotores.

    Objetivos mensuales o periódicos de ventas, cobertura de clientes y
    recaudación asignados a los promotores.

    Legacy:
        i176t_meta_promotor
    """

    __tablename__ = "mae_metas_promotores"

    __table_args__ = (
        UniqueConstraint(
            "empresa_id",
            "id",
            name="uq_mae_metas_promotores_empresa_id",
        ),
        UniqueConstraint(
            "empresa_id",
            "promotor_id",
            "anio",
            "mes",
            name="uq_mae_metas_promotores_periodo",
        ),
        CheckConstraint(
            "mes BETWEEN 1 AND 12",
            name="ck_mae_metas_promotores_mes",
        ),
        ForeignKeyConstraint(
            ["empresa_id", "promotor_id"],
            ["fuerza_ventas.mae_promotores.empresa_id", "fuerza_ventas.mae_promotores.id"],
            ondelete="CASCADE",
            name="fk_mae_metas_promotores_promotor",
        ),
        Index(
            "ix_mae_metas_promotores_empresa_activo",
            "empresa_id",
            "is_activo",
        ),
        Index(
            "ix_mae_metas_promotores_empresa_periodo",
            "empresa_id",
            "anio",
            "mes",
        ),
        Index(
            "ix_mae_metas_promotores_empresa_promotor",
            "empresa_id",
            "promotor_id",
        ),
        {
            "schema": "fuerza_ventas",
            "comment": "Metas y Cuotas Comerciales de Promotores",
        },
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        primary_key=True,
        server_default=text("gen_random_uuid()"),
        comment="Identificador único UUID",
    )

    empresa_id: Mapped[int] = mapped_column(
        BigInteger,
        nullable=False,
        comment="Identificador BIGINT de la empresa en core.identidad (RLS)",
    )

    promotor_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        nullable=False,
        comment="Promotor comercial asignado",
    )

    anio: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        comment="Año de la meta (p. ej. 2026)",
    )

    mes: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        comment="Mes de la meta (1-12)",
    )

    meta_monto_ventas: Mapped[Decimal] = mapped_column(
        Numeric(12, 2),
        nullable=False,
        server_default=text("0.00"),
        comment="Meta financiera de facturación",
    )

    meta_cobertura_clientes: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        server_default=text("0"),
        comment="Meta de cantidad de clientes compradores únicos",
    )

    meta_visitas_efectivas: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        server_default=text("0"),
        comment="Meta de visitas efectivas con transacción",
    )

    meta_monto_cobranza: Mapped[Decimal] = mapped_column(
        Numeric(12, 2),
        nullable=False,
        server_default=text("0.00"),
        comment="Meta de recuperación y cobro de cartera",
    )

    is_activo: Mapped[bool] = mapped_column(
        Boolean,
        nullable=False,
        server_default=text("true"),
        comment="Estado lógico del registro",
    )

    created_at: Mapped[datetime] = mapped_column(
        TIMESTAMP(timezone=True),
        nullable=False,
        server_default=text("now()"),
        comment="Fecha de creación",
    )

    updated_at: Mapped[datetime] = mapped_column(
        TIMESTAMP(timezone=True),
        nullable=False,
        server_default=text("now()"),
        comment="Fecha de última actualización",
    )

    deleted_at: Mapped[Optional[datetime]] = mapped_column(
        TIMESTAMP(timezone=True),
        nullable=True,
        comment="Fecha de eliminación lógica",
    )

    created_by: Mapped[Optional[uuid.UUID]] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
        comment="Usuario creador",
    )

    updated_by: Mapped[Optional[uuid.UUID]] = mapped_column(
        UUID(as_uuid=True),
        nullable=True,
        comment="Usuario modificador",
    )