Especificación Técnica: trx_pedidos_planificaciones¶
Esquema: fuerza_ventas
Base de Datos: crm
Tabla Legacy Origen: c072t_planificacion_pedido
Propósito: Cronograma de entregas parciales y despachos escalonados vinculados a una orden de pedido.
1. Justificación y Mejoras de Arquitectura¶
- Soporte Flexible de Entregas Parciales: Permite desglosar las entregas comprometidas en múltiples despachos sucesivos (
numero_entrega) con trazabilidad de fechas pactadas (fecha_programada_entrega) y estado de cada despacho (estado_planificacion). Las cantidades por producto se controlan entrx_pedidos_detalle(cantidad_aprobada,cantidad_despachada). - Integridad Referencial Compuesta Multi-Tenant: Define la clave foránea compuesta
(empresa_id, pedido_id)haciafuerza_ventas.trx_pedidos(empresa_id, id)con borrado en cascada (ON DELETE CASCADE). La planificación es por pedido, no por línea de producto. - Aislamiento Multi-Tenant (RLS): Integra
empresa_id BIGINT NOT NULLcon políticas RLS obligatorias (USINGyWITH CHECK). - Control de Secuencia por Entrega: Restricción
UNIQUE (empresa_id, pedido_id, numero_entrega)que previene duplicados en la numeración de los despachos parciales. - Trazabilidad y Auditoría: Auditoría integral (
created_at,updated_at,deleted_at,created_by,updated_by) con auditoría integral.
2. Definiciones de Implementación¶
Table fuerza_ventas.trx_pedidos_planificaciones {
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)']
pedido_id uuid [not null, note: 'Cabecera de la orden de pedido asociada']
numero_entrega int [not null, default: `1`, note: 'Número de entrega parcial (1, 2, 3...)']
fecha_programada_entrega date [not null, note: 'Fecha planificada para la entrega parcial']
estado_planificacion varchar(30) [not null, default: `'PROGRAMADO'`, note: 'PROGRAMADO, EN_RUTA, ENTREGADO_TOTAL, ENTREGADO_PARCIAL, CANCELADO']
// 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_trx_pedidos_planif_empresa_id']
(empresa_id, pedido_id, numero_entrega) [unique, name: 'uq_trx_pedidos_planif_empresa_entrega']
(empresa_id, is_activo) [name: 'ix_trx_pedidos_planif_empresa_activo']
(empresa_id, pedido_id) [name: 'ix_trx_pedidos_planif_empresa_pedido']
(empresa_id, fecha_programada_entrega) [name: 'ix_trx_pedidos_planif_empresa_fecha']
(empresa_id, estado_planificacion) [name: 'ix_trx_pedidos_planif_empresa_estado']
}
}
Ref: fuerza_ventas.trx_pedidos_planificaciones.(empresa_id, pedido_id) > fuerza_ventas.trx_pedidos.(empresa_id, id) [delete: cascade]
CREATE TABLE IF NOT EXISTS fuerza_ventas.trx_pedidos_planificaciones (
id UUID NOT NULL DEFAULT gen_random_uuid(),
empresa_id BIGINT NOT NULL,
pedido_id UUID NOT NULL,
numero_entrega INT NOT NULL DEFAULT 1,
fecha_programada_entrega DATE NOT NULL,
estado_planificacion VARCHAR(30) NOT NULL DEFAULT 'PROGRAMADO',
-- 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_trx_pedidos_planificaciones
PRIMARY KEY (id),
CONSTRAINT uq_trx_pedidos_planif_empresa_id
UNIQUE (empresa_id, id),
CONSTRAINT uq_trx_pedidos_planif_empresa_entrega
UNIQUE (empresa_id, pedido_id, numero_entrega),
CONSTRAINT fk_trx_pedidos_planif_pedido
FOREIGN KEY (empresa_id, pedido_id)
REFERENCES fuerza_ventas.trx_pedidos (empresa_id, id)
ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS ix_trx_pedidos_planif_empresa_activo
ON fuerza_ventas.trx_pedidos_planificaciones (empresa_id, is_activo);
CREATE INDEX IF NOT EXISTS ix_trx_pedidos_planif_empresa_pedido
ON fuerza_ventas.trx_pedidos_planificaciones (empresa_id, pedido_id);
CREATE INDEX IF NOT EXISTS ix_trx_pedidos_planif_empresa_fecha
ON fuerza_ventas.trx_pedidos_planificaciones (empresa_id, fecha_programada_entrega);
CREATE INDEX IF NOT EXISTS ix_trx_pedidos_planif_empresa_estado
ON fuerza_ventas.trx_pedidos_planificaciones (empresa_id, estado_planificacion);
COMMENT ON TABLE fuerza_ventas.trx_pedidos_planificaciones IS
'Planificaciones y Entregas Parciales de Pedidos: Cronograma de despachos parciales escalonados vinculados a una orden de pedido.';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.id IS
'Identificador único UUID';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.empresa_id IS
'Identificador BIGINT de la empresa en core.identidad (RLS)';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.pedido_id IS
'Cabecera de la orden de pedido asociada';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.numero_entrega IS
'Número de entrega parcial (1, 2, 3...)';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.fecha_programada_entrega IS
'Fecha planificada para la entrega parcial';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.estado_planificacion IS
'PROGRAMADO, EN_RUTA, ENTREGADO_TOTAL, ENTREGADO_PARCIAL, CANCELADO';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.is_activo IS
'Estado lógico del registro';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.created_at IS
'Fecha de creación';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.updated_at IS
'Fecha de última actualización';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.deleted_at IS
'Fecha de eliminación lógica';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.created_by IS
'Usuario creador';
COMMENT ON COLUMN fuerza_ventas.trx_pedidos_planificaciones.updated_by IS
'Usuario modificador';
ALTER TABLE fuerza_ventas.trx_pedidos_planificaciones
ENABLE ROW LEVEL SECURITY;
CREATE POLICY rls_trx_pedidos_planif_empresa
ON fuerza_ventas.trx_pedidos_planificaciones
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
);