Saltar a contenido

Especificación Técnica: cat_modalidades_pago_comision

Esquema: fuerza_ventas
Base de Datos: crm
Servicio: svc-fuerza-ventas
Tabla Legacy Origen: i127t.tx_modalidad_pago_comision
Propósito: modalidad de pago de comisión del promotor (antes ENUM): CONTADO / CRÉDITO


1. Justificación y Mejoras de Arquitectura

  • Normalización Relacional Rigurosa: Aplica diseño normalizado (1FN a BCNF), eliminando grupos repetidos de booleanos, desnormalizaciones innecesarias y redundancias del sistema legacy.
  • Aislamiento Multi-Tenant (RLS): Integra empresa_id BIGINT NOT NULL indexado, gobernado por políticas PostgreSQL Row-Level Security (USING y WITH CHECK) para garantizar la total separación de datos por tenant.
  • Integridad Referencial Compuesta: Todas las claves foráneas internas hacia tablas con tenencia son compuestas (empresa_id, <fk_id>), asegurando la integridad física en PostgreSQL.
  • Trazabilidad y Auditoría: Auditoría estándar (created_at, updated_at, deleted_at, created_by, updated_by) con auditoría integral.

2. Definiciones de Implementación

Table fuerza_ventas.cat_modalidades_pago_comision {
    id                      uuid          [pk, default: `gen_random_uuid()`, note: 'Identificador único UUID']
    empresa_id                  bigint        [not null, note: 'Identificador BIGINT de la empresa en core.identidad (RLS)']
    codigo                  varchar(20)   [not null, note: 'Campo codigo']
    nombre                  varchar(40)   [not null, note: 'Campo nombre']

    // Auditoría General
    is_activo               boolean       [not null, default: `true`, note: 'Estado lógico del registro']
    created_at              timestamptz   [not null, default: `now()`, note: 'Fecha de creación']
    updated_at              timestamptz   [not null, default: `now()`, note: 'Fecha de última actualización']
    deleted_at              timestamptz   [note: 'Fecha de eliminación lógica']
    created_by              uuid          [note: 'Usuario creador']
    updated_by              uuid          [note: 'Usuario modificador']

    Indexes {
        (empresa_id, id) [unique, name: 'uq_cat_modalidades_pago_comision_empresa_id']
        (empresa_id, codigo) [unique, name: 'uq_cat_modalidades_pago_comision_empresa_codigo']
        (empresa_id, is_activo) [name: 'ix_cat_modalidades_pago_comision_empresa_activo']
    }
}
CREATE TABLE IF NOT EXISTS fuerza_ventas.cat_modalidades_pago_comision (
    id UUID NOT NULL DEFAULT gen_random_uuid(),
    empresa_id BIGINT NOT NULL,
    codigo VARCHAR(20) NOT NULL,
    nombre VARCHAR(40) NOT NULL,

    -- Auditoría General
    is_activo BOOLEAN NOT NULL DEFAULT true,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    deleted_at TIMESTAMPTZ,
    created_by UUID,
    updated_by UUID,

    CONSTRAINT pk_cat_modalidades_pago_comision PRIMARY KEY (id),
    CONSTRAINT uq_cat_modalidades_pago_comision_empresa_id UNIQUE (empresa_id, id),
    CONSTRAINT uq_cat_modalidades_pago_comision_empresa_codigo UNIQUE (empresa_id, codigo)
);

CREATE INDEX IF NOT EXISTS ix_cat_modalidades_pago_comision_empresa_activo ON fuerza_ventas.cat_modalidades_pago_comision (empresa_id, is_activo);

ALTER TABLE fuerza_ventas.cat_modalidades_pago_comision ENABLE ROW LEVEL SECURITY;
CREATE POLICY rls_cat_modalidades_pago_comision_tenant_isolation ON fuerza_ventas.cat_modalidades_pago_comision
    FOR ALL
    USING (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT)
    WITH CHECK (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT);