Saltar a contenido

Prospecto — clientes.mae_prospectos

Ficha inicial de un posible cliente.

"""``clientes.mae_prospectos``: ficha inicial de un posible cliente."""

import uuid
from datetime import date

from sqlalchemy import (
    BigInteger,
    CheckConstraint,
    Date,
    ForeignKeyConstraint,
    Index,
    String,
    Text,
    UniqueConstraint,
    text,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base
from app.models.mixins import AuditMixin


class Prospecto(AuditMixin, Base):
    __tablename__ = "mae_prospectos"
    __table_args__ = (
        UniqueConstraint("empresa_id", "id", name="uq_mae_prospectos_empresa_id"),
        UniqueConstraint(
            "empresa_id", "cliente_id", name="uq_mae_prospectos_empresa_cliente"
        ),
        CheckConstraint(
            "(cliente_id IS NULL) = (fecha_conversion IS NULL)",
            name="ck_mae_prospectos_conversion_pareja",
        ),
        ForeignKeyConstraint(
            ["empresa_id", "estado_prospecto_id"],
            [
                "clientes.cat_estados_prospecto.empresa_id",
                "clientes.cat_estados_prospecto.id",
            ],
            name="fk_mae_prospectos_estado",
        ),
        ForeignKeyConstraint(
            ["empresa_id", "cliente_id"],
            ["clientes.mae_clientes.empresa_id", "clientes.mae_clientes.id"],
            name="fk_mae_prospectos_cliente",
        ),
        Index("ix_mae_prospectos_empresa_promotor", "empresa_id", "promotor_id"),
        Index("ix_mae_prospectos_empresa_estado", "empresa_id", "estado_prospecto_id"),
        Index("ix_mae_prospectos_empresa_ciudad", "empresa_id", "ciudad_id"),
        {"schema": "clientes"},
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        primary_key=True,
        server_default=text("gen_random_uuid()"),
    )
    empresa_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
    nombre: Mapped[str] = mapped_column(String(200), nullable=False)
    marca: Mapped[str | None] = mapped_column(String(200), nullable=True)
    contacto: Mapped[str | None] = mapped_column(String(150), nullable=True)
    correo: Mapped[str | None] = mapped_column(String(254), nullable=True)
    telefono: Mapped[str | None] = mapped_column(String(50), nullable=True)
    direccion: Mapped[str | None] = mapped_column(Text, nullable=True)
    ciudad_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), nullable=True
    )
    promotor_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), nullable=True
    )
    estado_prospecto_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), nullable=False
    )
    cliente_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), nullable=True
    )
    fecha_conversion: Mapped[date | None] = mapped_column(Date, nullable=True)