Files
MVE/mve_incrementales_decrementales.sql
2025-10-21 11:41:30 -06:00

90 lines
3.5 KiB
SQL

-- Tabla para almacenar datos de Manifestación de Valor por factura
CREATE TABLE mve_facturas_datos (
id INT IDENTITY(1,1) PRIMARY KEY,
id_pedimento INT NOT NULL,
id_factura INT NOT NULL,
numero_factura NVARCHAR(100),
-- Campos Art. 65 - Incrementables
art65_fecha_transporte DATE NULL,
art65_importe_transporte DECIMAL(15,2) NULL,
art65_fecha_descuentos DATE NULL,
art65_importe_descuentos DECIMAL(15,2) NULL,
art65_fecha_posteriores DATE NULL,
art65_importe_posteriores DECIMAL(15,2) NULL,
art65_fecha_contribuciones DATE NULL,
art65_importe_contribuciones DECIMAL(15,2) NULL,
art65_fecha_pagos_vendedor DATE NULL,
art65_importe_pagos_vendedor DECIMAL(15,2) NULL,
-- Campos Art. 66 - Decrementables
art66_fecha_comisiones DATE NULL,
art66_importe_comisiones DECIMAL(15,2) NULL,
art66_cargo_comisiones NVARCHAR(10) CHECK (art66_cargo_comisiones IN ('Si', 'No')) NULL,
art66_fecha_envases DATE NULL,
art66_importe_envases DECIMAL(15,2) NULL,
art66_cargo_envases NVARCHAR(10) CHECK (art66_cargo_envases IN ('Si', 'No')) NULL,
art66_fecha_embalaje DATE NULL,
art66_importe_embalaje DECIMAL(15,2) NULL,
art66_cargo_embalaje NVARCHAR(10) CHECK (art66_cargo_embalaje IN ('Si', 'No')) NULL,
art66_fecha_transporte_dec DATE NULL,
art66_importe_transporte_dec DECIMAL(15,2) NULL,
art66_cargo_transporte_dec NVARCHAR(10) CHECK (art66_cargo_transporte_dec IN ('Si', 'No')) NULL,
art66_fecha_ingenieria DATE NULL,
art66_importe_ingenieria DECIMAL(15,2) NULL,
art66_cargo_ingenieria NVARCHAR(10) CHECK (art66_cargo_ingenieria IN ('Si', 'No')) NULL,
art66_fecha_regalias DATE NULL,
art66_importe_regalias DECIMAL(15,2) NULL,
art66_cargo_regalias NVARCHAR(10) CHECK (art66_cargo_regalias IN ('Si', 'No')) NULL,
art66_fecha_producto DATE NULL,
art66_importe_producto DECIMAL(15,2) NULL,
art66_cargo_producto NVARCHAR(10) CHECK (art66_cargo_producto IN ('Si', 'No')) NULL,
-- Campos de control
fecha_creacion DATETIME2 DEFAULT GETDATE(),
fecha_actualizacion DATETIME2 DEFAULT GETDATE(),
usuario_creacion NVARCHAR(100),
-- Índices y restricciones
CONSTRAINT UQ_mve_facturas_datos_pedimento_factura UNIQUE (id_pedimento, id_factura)
);
-- Crear índices separadamente
CREATE INDEX IX_mve_facturas_datos_pedimento ON mve_facturas_datos (id_pedimento);
CREATE INDEX IX_mve_facturas_datos_factura ON mve_facturas_datos (id_factura);
-- Tabla para el historial de solicitudes MVE
CREATE TABLE mve_solicitudes (
id INT IDENTITY(1,1) PRIMARY KEY,
id_pedimento INT NOT NULL,
numero_pedimento NVARCHAR(50),
estado NVARCHAR(20) CHECK (estado IN ('Pendiente', 'En_Proceso', 'Completada', 'Rechazada')) DEFAULT 'Pendiente',
fecha_solicitud DATETIME2 DEFAULT GETDATE(),
fecha_respuesta DATETIME2 NULL,
observaciones NTEXT,
usuario_solicitud NVARCHAR(100)
);
-- Crear índices para mve_solicitudes
CREATE INDEX IX_mve_solicitudes_pedimento ON mve_solicitudes (id_pedimento);
CREATE INDEX IX_mve_solicitudes_estado ON mve_solicitudes (estado);
CREATE INDEX IX_mve_solicitudes_fecha_solicitud ON mve_solicitudes (fecha_solicitud);
-- Crear trigger para actualizar fecha_actualizacion automáticamente
CREATE TRIGGER TR_mve_facturas_datos_update
ON mve_facturas_datos
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE mve_facturas_datos
SET fecha_actualizacion = GETDATE()
FROM mve_facturas_datos m
INNER JOIN inserted i ON m.id = i.id;
END;