--Hash:5A18E47F236443AF08DEEB05ABCF9C1A
SET XACT_ABORT ON
BEGIN TRANSACTION;

GO

SET DATEFORMAT mdy;

--Validar que no existan scripts que no se hayan ejecutado
--//
IF EXISTS
(
	SELECT a.NumVersion + 1 AS HuecoInicio
	FROM ZiurVersiones a
	LEFT JOIN ZiurVersiones b ON a.NumVersion + 1 = b.NumVersion
	WHERE a.NumVersion>830 AND b.NumVersion IS NULL
	AND a.NumVersion + 1 < 967
)
RAISERROR('Error : hay un script que no se ejecuto , debe ejecutarlo manualmente.', 20, -1) with log
--//

--Colocar contenido de script diferencial aqui (Ziur.Database.publish.sql)

GO
/*
 Plantilla de script anterior a la implementación							
--------------------------------------------------------------------------------------
 Este archivo contiene instrucciones de SQL que se ejecutarán antes del script de compilación	
 Use la sintaxis de SQLCMD para incluir un archivo en el script anterior a la implementación			
 Ejemplo:      :r .\miArchivo.sql								
 Use la sintaxis de SQLCMD para hacer referencia a una variable en el script anterior a la implementación		
 Ejemplo:      :setvar TableName miTabla							
               SELECT * FROM [$(TableName)]					
--------------------------------------------------------------------------------------
*/
GO

GO
PRINT N'Altering Table [dbo].[DocElectronicos]...';


GO
ALTER TABLE [dbo].[DocElectronicos] ALTER COLUMN [NroDoc] NVARCHAR (200) NOT NULL;


GO
PRINT N'Altering Table [dbo].[Empresas]...';


GO
ALTER TABLE [dbo].[Empresas] ALTER COLUMN [EmpresaNombre] NVARCHAR (200) NOT NULL;


GO
PRINT N'Refreshing View [dbo].[VClientes]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VClientes]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_00]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_00]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_00]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_00]';


GO
PRINT N'Refreshing View [dbo].[VCruces_Origen]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCruces_Origen]';


GO
PRINT N'Refreshing View [dbo].[VCruces_TodosDocs]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCruces_TodosDocs]';


GO
PRINT N'Refreshing View [dbo].[vLibroAuxiliar_Mov]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[vLibroAuxiliar_Mov]';


GO
PRINT N'Refreshing View [dbo].[vLibroAuxiliar_Saldos_02]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[vLibroAuxiliar_Saldos_02]';


GO
PRINT N'Refreshing View [dbo].[VNomina_NE_Encabezados]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VNomina_NE_Encabezados]';


GO
PRINT N'Refreshing View [dbo].[VNominaContratos]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VNominaContratos]';


GO
PRINT N'Refreshing View [dbo].[VHistoriaClinica_Datos]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VHistoriaClinica_Datos]';


GO
PRINT N'Refreshing View [dbo].[VHistoriaClinica_Email]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VHistoriaClinica_Email]';


GO
PRINT N'Refreshing View [dbo].[VNominaCorte]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VNominaCorte]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc____TotPorCuentas_00]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc____TotPorCuentas_00]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP____TotPorCuentas_00]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP____TotPorCuentas_00]';


GO
PRINT N'Refreshing View [dbo].[VCostoPromedio_Cruces]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCostoPromedio_Cruces]';


GO
PRINT N'Refreshing View [dbo].[VCruces_Agrup]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCruces_Agrup]';


GO
PRINT N'Refreshing View [dbo].[VProduccion]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VProduccion]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc____TotPorCuentas_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc____TotPorCuentas_Fin]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP____TotPorCuentas_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP____TotPorCuentas_Fin]';


GO
PRINT N'Refreshing View [dbo].[VEncabezadosMov]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VEncabezadosMov]';


GO
PRINT N'Refreshing View [dbo].[VMovimientosFactElectronica]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VMovimientosFactElectronica]';


GO
PRINT N'Refreshing View [dbo].[VEncabezados]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VEncabezados]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_01]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_01]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_01]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_01]';


GO
PRINT N'Refreshing View [dbo].[VEncabezadosCont_Det]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VEncabezadosCont_Det]';


GO
PRINT N'Refreshing View [dbo].[VEncabezadosMov_Det]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VEncabezadosMov_Det]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc____TotRetes_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc____TotRetes_Fin]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_01_01]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_01_01]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP____TotRetes_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP____TotRetes_Fin]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_01_01]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_01_01]';


GO
PRINT N'Refreshing View [dbo].[VCierresDocsCont]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCierresDocsCont]';


GO
PRINT N'Refreshing View [dbo].[VCierresDocs]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VCierresDocs]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_02]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_02]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_02]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_02]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_03]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_03]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_03]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_03]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_Fin]';


GO
PRINT N'Refreshing View [dbo].[VContabDoc_FP_Fin]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VContabDoc_FP_Fin]';


GO
PRINT N'Refreshing View [dbo].[VEncabezadoFactElectronica]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[VEncabezadoFactElectronica]';


GO
PRINT N'Altering Procedure [dbo].[GetMediosMagneticos]...';


GO
ALTER PROCEDURE [dbo].[GetMediosMagneticos]
	@IdVersion INT = 0,
	@FechaIni NVARCHAR(10) = '',
	@FechaFin NVARCHAR(10) = '',
	@IdEmpresas NVARCHAR(1000) = '',
	@IdDocsExc NVARCHAR(1000) ='',
	@IdCliInf INT = 0,
	@IdCliExc NVARCHAR(1000) = '',
	@Detallar BIT = 0
AS
BEGIN

DECLARE

@FechaIniT DATETIME,
@FechaFinT DATETIME,
@IdFormato INT,
@CodVersion INT,
@Vigencia INT,
@IdEmpFirst INT,
@TarifaIca NUMERIC(28,6),

@CM_Identif NVARCHAR(100),
@CM_TipoIdent INT,
@CM_RazonSocial NVARCHAR(200),
@CM_LongNombres INT,
@CM_LongRazonSocial INT,
@CM_LongDireccion INT


SET @FechaIniT = CAST(CONVERT(DATETIME, @FechaIni, 103) + ' 00:00:00' AS DATETIME)
SET @FechaFinT = CAST(CONVERT(DATETIME, @FechaFin, 103) + ' 23:59:59' AS DATETIME)

--Recuperar el id del formato
--//
SELECT @IdFormato = IdFormato, @CodVersion = CodVersion FROM MediosMag_Versiones WHERE IdVersion = @IdVersion
--//

--Recuperar los parametros de cuantias menores para medios magneticos
--//
SELECT @CM_TipoIdent = CAST(ValParametro AS INT) FROM PARAMETROS WHERE CodParametro = 78
IF @CM_TipoIdent IS NULL
	SET @CM_TipoIdent = 43

SELECT @CM_Identif = ValParametro FROM PARAMETROS WHERE CodParametro = 79
IF @CM_Identif IS NULL
	SET @CM_Identif = '222222222'

SELECT @CM_RazonSocial = ValParametro FROM PARAMETROS WHERE CodParametro = 80
IF @CM_RazonSocial IS NULL
	SET @CM_RazonSocial = 'CUANTIAS MENORES'

SELECT @CM_LongNombres = CAST(ValParametro AS INT) FROM PARAMETROS WHERE CodParametro = 81
IF @CM_LongNombres IS NULL
	SET @CM_LongNombres = 60

SELECT @CM_LongRazonSocial = CAST(ValParametro AS INT) FROM PARAMETROS WHERE CodParametro = 82
IF @CM_LongRazonSocial IS NULL
	SET @CM_LongRazonSocial = 450

SELECT @CM_LongDireccion = CAST(ValParametro AS INT) FROM PARAMETROS WHERE CodParametro = 83
IF @CM_LongDireccion IS NULL
	SET @CM_LongDireccion = 200

--//

SET @FechaIniT = CAST(CONVERT(DATETIME, @FechaIni, 103) + ' 00:00:00' AS DATETIME)
SET @FechaFinT = CAST(CONVERT(DATETIME, @FechaFin, 103) + ' 23:59:59' AS DATETIME)

--//Tarifa ICA primera Empresa
SET @IdEmpFirst= (SELECT top(1) number FROM iter$simple_intlist_to_tbl(@IdEmpresas))

SELECT @TarifaIca=ValorTarifaIca FROM Empresas AS Emp INNER JOIN Empresas_ActividadesEcon Act ON Emp.EmpresaIdActivEconPrinc=Act.IdActividad
WHERE idEmpresa=@IdEmpFirst


--//Vigencia
set @Vigencia=YEAR(@FechaIniT)

--//Para el Formato 1 y Version 8
IF @IdFormato = 1 AND @CodVersion = 8
BEGIN
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	ABS(ROUND(COALESCE(U.M1_Col1_PagoDeducible, 0.0),0)) AS M1_Col1_PagoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col2_PagoNoDeducible, 0.0),0)) AS M1_Col2_PagoNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col3_IvaDeducible, 0.0),0)) AS M1_Col3_IvaDeducible,
	ABS(ROUND(COALESCE(U.M1_Col4_IvaNoDeducible, 0.0),0)) AS M1_Col4_IvaNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col5_ReteFteAplicada, 0.0),0)) AS M1_Col5_ReteFteAplicada,
	ABS(ROUND(COALESCE(U.M1_Col6_ReteFteAsumida, 0.0),0)) AS M1_Col6_ReteFteAsumida,
	ABS(ROUND(COALESCE(U.M1_Col7_ReteIvaRegComun, 0.0),0)) AS M1_Col7_ReteIvaRegComun,
	ABS(ROUND(COALESCE(U.M1_Col8_ReteIvaRegSimpC, 0.0),0)) AS M1_Col8_ReteIvaRegSimpC,
	ABS(ROUND(COALESCE(U.M1_Col9_ReteFteIvaNoDomic, 0.0),0)) AS M1_Col9_ReteFteIvaNoDomic,
	0.0 AS M1_Col10_ReteFteCREEAplicada, 
	0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M1_Col1_PagoDeducible, 
		SUM(T.PagoNoDeducible) AS M1_Col2_PagoNoDeducible, 
		SUM(T.IvaDeducible) AS M1_Col3_IvaDeducible, 
		SUM(T.IvaNoDeducible) AS M1_Col4_IvaNoDeducible, 
		SUM(T.ReteFteAplicada) AS M1_Col5_ReteFteAplicada, 
		SUM(T.ReteFteAsumida) AS M1_Col6_ReteFteAsumida, 
		SUM(T.ReteIvaRegComun) AS M1_Col7_ReteIvaRegComun, 
		SUM(T.ReteIvaRegSimpC) AS M1_Col8_ReteIvaRegSimpC, 
		SUM(T.ReteFteIvaNoDomic) AS M1_Col9_ReteFteIvaNoDomic
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.PagoDeducible) AS PagoDeducible, 
			SUM(M_Ppal.PagoNoDeducible) AS PagoNoDeducible, 
			SUM(M_Ivas.IvaDeducible) AS IvaDeducible, 
			SUM(M_Ivas.IvaNoDeducible) AS IvaNoDeducible, 
			SUM(M_ReteFte.ReteFteAplicada) AS ReteFteAplicada, 
			SUM(M_ReteFte.ReteFteAsumida) AS ReteFteAsumida, 
			SUM(M_ReteIvaRCom.ReteIvaRegComun) AS ReteIvaRegComun, 
			SUM(M_ReteIvaRSC.ReteIvaRegSimpC) AS ReteIvaRegSimpC, 
			SUM(M_ReteFteNoDom.ReteFteIvaNoDomic) AS ReteFteIvaNoDomic, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFteIvaNoDomic_Fin AS M_ReteFteNoDom ON M_Ppal.IdCliente = M_ReteFteNoDom.IdCliente AND M_Ppal.IdEncab = M_ReteFteNoDom.IdEncab AND M_Ppal.IdAgrup = M_ReteFteNoDom.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND 
			M_Ppal.IdAgrup = M_ReteFte.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegComun_Fin AS M_ReteIvaRCom ON M_Ppal.IdCliente = M_ReteIvaRCom.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRCom.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRCom.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_IvaMayorValor_Fin AS M_Ivas ON M_Ppal.IdCliente = M_Ivas.IdCliente AND M_Ppal.IdEncab = M_Ivas.IdEncab AND M_Ppal.IdAgrup = M_Ivas.IdAgrup AND M_Ppal.IdVersion = M_Ivas.IdVersion LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegSimpC_Fin AS M_ReteIvaRSC ON M_Ppal.IdCliente = M_ReteIvaRSC.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRSC.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRSC.IdAgrup
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	RETURN
END

--//Para el Formato 1 Y Version 9
IF @IdFormato = 1 AND @CodVersion = 9
BEGIN
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	ABS(ROUND(COALESCE(U.M1_Col1_PagoDeducible, 0.0),0)) AS M1_Col1_PagoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col2_PagoNoDeducible, 0.0),0)) AS M1_Col2_PagoNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col3_IvaDeducible, 0.0),0)) AS M1_Col3_IvaDeducible,
	ABS(ROUND(COALESCE(U.M1_Col4_IvaNoDeducible, 0.0),0)) AS M1_Col4_IvaNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col5_ReteFteAplicada, 0.0),0)) AS M1_Col5_ReteFteAplicada,
	ABS(ROUND(COALESCE(U.M1_Col6_ReteFteAsumida, 0.0),0)) AS M1_Col6_ReteFteAsumida,
	ABS(ROUND(COALESCE(U.M1_Col7_ReteIvaRegComun, 0.0),0)) AS M1_Col7_ReteIvaRegComun,
	ABS(ROUND(COALESCE(U.M1_Col8_ReteIvaRegSimpC, 0.0),0)) AS M1_Col8_ReteIvaRegSimpC,
	ABS(ROUND(COALESCE(U.M1_Col9_ReteFteIvaNoDomic, 0.0),0)) AS M1_Col9_ReteFteIvaNoDomic,
	ABS(ROUND(COALESCE(U.M1_Col10_ReteFteCREEAplicada, 0.0),0)) AS M1_Col10_ReteFteCREEAplicada,
	ABS(ROUND(COALESCE(U.M1_Col11_ReteFteCREEAsumida, 0.0),0)) AS M1_Col11_ReteFteCREEAsumida,
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M1_Col1_PagoDeducible, 
		SUM(T.PagoNoDeducible) AS M1_Col2_PagoNoDeducible, 
		SUM(T.IvaDeducible) AS M1_Col3_IvaDeducible, 
		SUM(T.IvaNoDeducible) AS M1_Col4_IvaNoDeducible, 
		SUM(T.ReteFteAplicada) AS M1_Col5_ReteFteAplicada, 
		SUM(T.ReteFteAsumida) AS M1_Col6_ReteFteAsumida, 
		SUM(T.ReteIvaRegComun) AS M1_Col7_ReteIvaRegComun, 
		SUM(T.ReteIvaRegSimpC) AS M1_Col8_ReteIvaRegSimpC, 
		SUM(T.ReteFteIvaNoDomic) AS M1_Col9_ReteFteIvaNoDomic,
		SUM(T.ReteFteCREEAplicada) AS M1_Col10_ReteFteCREEAplicada, 
		SUM(T.ReteFteCREEAsumida) AS M1_Col11_ReteFteCREEAsumida
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.PagoDeducible) AS PagoDeducible, 
			SUM(M_Ppal.PagoNoDeducible) AS PagoNoDeducible, 
			SUM(M_Ivas.IvaDeducible) AS IvaDeducible, 
			SUM(M_Ivas.IvaNoDeducible) AS IvaNoDeducible, 
			SUM(M_ReteFte.ReteFteAplicada) AS ReteFteAplicada, 
			SUM(M_ReteFte.ReteFteAsumida) AS ReteFteAsumida, 
			SUM(M_ReteIvaRCom.ReteIvaRegComun) AS ReteIvaRegComun, 
			SUM(M_ReteIvaRSC.ReteIvaRegSimpC) AS ReteIvaRegSimpC, 
			SUM(M_ReteFteNoDom.ReteFteIvaNoDomic) AS ReteFteIvaNoDomic, 
			SUM(M_ReteFteCREE.ReteFteCREEAplicada) AS ReteFteCREEAplicada, 
			SUM(M_ReteFteCREE.ReteFteCREEAsumida) AS ReteFteCREEAsumida, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFuenteCREE_Fin AS M_ReteFteCREE ON M_Ppal.IdEncab = M_ReteFteCREE.IdEncab AND M_Ppal.IdAgrup = M_ReteFteCREE.IdAgrup AND M_Ppal.IdCliente = M_ReteFteCREE.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFteIvaNoDomic_Fin AS M_ReteFteNoDom ON M_Ppal.IdCliente = M_ReteFteNoDom.IdCliente AND M_Ppal.IdEncab = M_ReteFteNoDom.IdEncab AND M_Ppal.IdAgrup = M_ReteFteNoDom.IdAgrup LEFT OUTER JOIN
            dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegComun_Fin AS M_ReteIvaRCom ON M_Ppal.IdCliente = M_ReteIvaRCom.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRCom.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRCom.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_IvaMayorValor_Fin AS M_Ivas ON M_Ppal.IdCliente = M_Ivas.IdCliente AND M_Ppal.IdEncab = M_Ivas.IdEncab AND M_Ppal.IdAgrup = M_Ivas.IdAgrup AND M_Ppal.IdVersion = M_Ivas.IdVersion LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegSimpC_Fin AS M_ReteIvaRSC ON M_Ppal.IdCliente = M_ReteIvaRSC.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRSC.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRSC.IdAgrup
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	RETURN
END

--//Para el (Formato 1 Y Version 10) Y (Formato 32)
IF (@IdFormato = 1 AND @CodVersion = 10) OR (@IdFormato = 32)
BEGIN
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	CASE WHEN NOT(Cli.DVCliente = '') THEN '' END AS DVCliente,
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	ABS(ROUND(COALESCE(U.M1_Col1_PagoDeducible, 0.0),0)) AS M1_Col1_PagoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col2_PagoNoDeducible, 0.0),0)) AS M1_Col2_PagoNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col3_IvaDeducible, 0.0),0)) AS M1_Col3_IvaDeducible,
	ABS(ROUND(COALESCE(U.M1_Col4_IvaNoDeducible, 0.0),0)) AS M1_Col4_IvaNoDeducible,
	ABS(ROUND(COALESCE(U.M1_Col5_ReteFteAplicada, 0.0),0)) AS M1_Col5_ReteFteAplicada,
	ABS(ROUND(COALESCE(U.M1_Col6_ReteFteAsumida, 0.0),0)) AS M1_Col6_ReteFteAsumida,
	ABS(ROUND(COALESCE(U.M1_Col7_ReteIvaRegComun, 0.0),0)) AS M1_Col7_ReteIvaRegComun,
	ABS(ROUND(COALESCE(U.M1_Col9_ReteFteIvaNoDomic, 0.0),0)) AS M1_Col9_ReteFteIvaNoDomic,
    0.0 AS M1_Col8_ReteIvaRegSimpC, 
	0.0 AS M1_Col10_ReteFteCREEAplicada, 
	0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	ABS(ROUND(COALESCE(U.M32_Col1_PagoOAbonoCuenta, 0.0),0)) AS M32_Col1_PagoOAbonoCuenta,
	ABS(ROUND(COALESCE(U.M32_Col2_IVAMayorValorCosto, 0.0),0)) AS M32_Col2_IVAMayorValorCosto,
	ABS(ROUND(COALESCE(U.M32_Col3_ReteFtePracticadaRenta, 0.0),0)) AS M32_Col3_ReteFtePracticadaRenta,
	ABS(ROUND(COALESCE(U.M32_Col4_ReteFteAsumidaRenta, 0.0),0)) AS M32_Col4_ReteFteAsumidaRenta,
	ABS(ROUND(COALESCE(U.M32_Col5_ReteFtePracticadaRegComun, 0.0),0)) AS M32_Col5_ReteFtePracticadaRegComun,
	ABS(ROUND(COALESCE(U.M32_Col6_ReteFtePracticadaNoDomic, 0.0),0)) AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,	
	0.0 AS M33_Col2_ValorIngresoTercero, 
	0.0 AS M33_Col3_ValorReteTercero, 
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng, 
	''	AS M33_Col6_Apellido1Ing, 
	''	AS M33_Col7_Apellido2Ing, 
	''	AS M33_Col8_Nombre1Ing, 
	''	AS M33_Col9_Nombre2Ing, 
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios, 
	0.0 AS M35_Col2_PagosEmolEclesiast,	
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M1_Col1_PagoDeducible, 
		SUM(T.PagoNoDeducible) AS M1_Col2_PagoNoDeducible, 
		SUM(T.IvaDeducible) AS M1_Col3_IvaDeducible, 
		SUM(T.IvaNoDeducible) AS M1_Col4_IvaNoDeducible, 
		SUM(T.ReteFteAplicada) AS M1_Col5_ReteFteAplicada, 
		SUM(T.ReteFteAsumida) AS M1_Col6_ReteFteAsumida, 
		SUM(T.ReteIvaRegComun) AS M1_Col7_ReteIvaRegComun, 
		SUM(T.ReteFteIvaNoDomic) AS M1_Col9_ReteFteIvaNoDomic,
		SUM(T.PagoDeducible) AS M32_Col1_PagoOAbonoCuenta,
		SUM(T.IvaDeducible) AS M32_Col2_IVAMayorValorCosto,
		SUM(T.ReteFteAplicada) AS M32_Col3_ReteFtePracticadaRenta,
		SUM(T.ReteFteAsumida) AS M32_Col4_ReteFteAsumidaRenta,
		SUM(T.ReteIvaRegComun) AS M32_Col5_ReteFtePracticadaRegComun,
		SUM(T.ReteFteIvaNoDomic) AS M32_Col6_ReteFtePracticadaNoDomic
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.PagoDeducible) AS PagoDeducible, 
			SUM(M_Ppal.PagoNoDeducible) AS PagoNoDeducible, 
			SUM(M_Ivas.IvaDeducible) AS IvaDeducible, 
			SUM(M_Ivas.IvaNoDeducible) AS IvaNoDeducible, 
			SUM(M_ReteFte.ReteFteAplicada) AS ReteFteAplicada, 
			SUM(M_ReteFte.ReteFteAsumida) AS ReteFteAsumida, 
			SUM(M_ReteIvaRCom.ReteIvaRegComun) AS ReteIvaRegComun, 
			SUM(M_ReteFteNoDom.ReteFteIvaNoDomic) AS ReteFteIvaNoDomic,  
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc, @IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFuenteCREE_Fin AS M_ReteFteCREE ON M_Ppal.IdEncab = M_ReteFteCREE.IdEncab AND M_Ppal.IdAgrup = M_ReteFteCREE.IdAgrup AND M_Ppal.IdCliente = M_ReteFteCREE.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFteIvaNoDomic_Fin AS M_ReteFteNoDom ON M_Ppal.IdCliente = M_ReteFteNoDom.IdCliente AND M_Ppal.IdEncab = M_ReteFteNoDom.IdEncab AND M_Ppal.IdAgrup = M_ReteFteNoDom.IdAgrup LEFT OUTER JOIN
            dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegComun_Fin AS M_ReteIvaRCom ON M_Ppal.IdCliente = M_ReteIvaRCom.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRCom.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRCom.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_IvaMayorValor_Fin AS M_Ivas ON M_Ppal.IdCliente = M_Ivas.IdCliente AND M_Ppal.IdEncab = M_Ivas.IdEncab AND M_Ppal.IdAgrup = M_Ivas.IdAgrup AND M_Ppal.IdVersion = M_Ivas.IdVersion LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegSimpC_Fin AS M_ReteIvaRSC ON M_Ppal.IdCliente = M_ReteIvaRSC.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRSC.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRSC.IdAgrup
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	RETURN
END

--//Para el Formato 3
--//
IF @IdFormato = 3 AND @CodVersion = 7 
BEGIN
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	ABS(ROUND(COALESCE(U.M3_Col1_ValorPagoAbono, 0.0),0)) AS M3_Col1_ValorPagoAbono, 
	ABS(ROUND(COALESCE(U.M3_Col2_Retencion, 0.0),0)) AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M3_Col1_ValorPagoAbono, 
		SUM(T.ReteFteAplicada) AS M3_Col2_Retencion
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.RetencionBase) AS PagoDeducible, 
			SUM(COALESCE(M_Ppal.SumRetReal,M_Ppal.PagoDeducible)) AS ReteFteAplicada, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal 
			INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente 
			--INNER JOIN dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup 
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
		HAVING SUM(T.ReteFteAplicada) <> 0

	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	RETURN
END
--//

--//Para el formato 4,5,6,7,11
IF @IdFormato IN (4, 5, 6, 7, 11)
BEGIN 
 
 ;WITH tempMediosCont(IdCuenta,IdEncab,IdAgrup,IdCliente,Fecha,IdEmpresa,IdDocumento,PagoDeducible,PagoNoDeducible,PagoTotal,RetencionBase,SumRetReal)
 AS
 (
	SELECT     
	IdCuenta,Enc_C.IdEncab,ISNULL(Enc_C.IdEncabContAgrup, 0) AS IdAgrup, Enc_C.IdCliente,
	MAX(Enc.Fecha) AS Fecha, MAX(Enc.IdEmpresa) AS IdEmpresa, MAX(Enc.IdDocumento) AS IdDocumento, 
	SUM(CASE WHEN Enc_C.EsDeducible = 0 THEN Enc_C.Debito - Enc_C.Credito ELSE 0 END) AS PagoDeducible, 
	SUM(CASE WHEN Enc_C.EsDeducible = 1 THEN Enc_C.Debito - Enc_C.Credito ELSE 0 END) AS PagoNoDeducible, 
	SUM(Enc_C.Debito - Enc_C.Credito) AS PagoTotal, 
	SUM(CASE WHEN ISNULL(Enc_C.RetencionPorc, 0) > 0 THEN ABS(Enc_C.Debito - Enc_C.Credito) * 100 / Enc_C.RetencionPorc ELSE 0 END) AS RetencionBase, 
	SUM(ISNULL(Enc_C.RetFuenteReal, 0)) AS SumRetReal
	FROM    
	dbo.Encabezados_Cont AS Enc_C INNER JOIN dbo.Encabezados AS Enc ON Enc_C.IdEncab = Enc.IdEncab
	WHERE     
	(Enc.EsAnulado = 0) AND
	DATEADD(dd,DATEDIFF(dd,0,Enc.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
	Enc.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
	Enc.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
	Enc_C.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
	GROUP BY IdCuenta, Enc_C.IdEncab, Enc_C.IdCliente, ISNULL(Enc_C.IdEncabContAgrup, 0)

 ),

 tempMediosPrincipal(IdVersion, IdEncab, IdAgrup, IdCliente, IdClienteAgrupMM, IdConcepto, NumCol, IdAgrupMM,  CodConcepto, PagoDeducible, PagoNoDeducible, PagoTotal, RetencionBase, SumRetReal)
 AS
 (
	SELECT        
	MM_Cuentas.IdVersion, Enc_C.IdEncab,IdAgrup, Enc_C.IdCliente, MM_Cuentas.IdCliente AS IdClienteAgrupMM, MM_Cuentas.IdConcepto, MM_Cuentas.NumCol, MM_Cuentas.IdAgrupMM, MAX(MM_Conc.CodConcepto) AS CodConcepto,
	SUM(PagoDeducible), 
	SUM(PagoNoDeducible), 
	SUM(PagoTotal), 
	SUM(RetencionBase), 
	SUM(SumRetReal)SumRetReal
	FROM            
	dbo.MediosMag_CuentasFormato AS MM_Cuentas INNER JOIN 
	tempMediosCont AS Enc_C ON MM_Cuentas.IdCuenta = Enc_C.IdCuenta  INNER JOIN
	dbo.MediosMag_Conceptos AS MM_Conc ON MM_Cuentas.IdConcepto = MM_Conc.IdConcepto
	GROUP BY MM_Cuentas.IdConcepto, Enc_C.IdEncab, Enc_C.IdCliente,IdAgrup, MM_Cuentas.IdVersion, MM_Cuentas.NumCol, MM_Cuentas.IdCliente, MM_Cuentas.IdAgrupMM
),

tempDir(Idcliente,Direccion,DireccionFormato1,DireccionFormato2,Telefonos,EMail,CodDpto,CodMunicipio,CodigoPais)
AS
(
	SELECT
	tempDirPri.Idcliente,      
	SUBSTRING(Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	[Telefonos] AS Telefonos,
	EMail AS EMail,
	RIGHT('00' + CAST(ISNULL( dbo.Departamentos.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(ISNULL(CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	ISNULL(CAST(CodigoDian AS INT),0) AS CodigoPais
	FROM          
	VClientesDir_01 AS tempDirPri INNER JOIN
	dbo.Clientes_Direcciones ON tempDirPri.IdClienteDirMax = dbo.Clientes_Direcciones.IdClienteDir  LEFT JOIN
	dbo.Clientes_TiposDireccion ON dbo.Clientes_Direcciones.IdTipoDir = dbo.Clientes_TiposDireccion.IdTipoDir LEFT JOIN
	dbo.Ciudades  ON dbo.Ciudades.IdCiudad = dbo.Clientes_Direcciones.IdCiudad  LEFT JOIN
	dbo.Departamentos ON dbo.Departamentos.IdDpto = dbo.Ciudades.IdDpto  LEFT JOIN
	dbo.Paises ON dbo.Departamentos.IdPais = dbo.Paises.IdPais 
)


	SELECT
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	CASE WHEN (@IdFormato = 7 AND @CodVersion = 9) THEN '' ELSE Cli.DVCliente END AS DVCliente,
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(ISNULL(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	ISNULL(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	Direccion,
	DireccionFormato1,
	DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	ISNULL(CodDpto,0)AS CodDpto,
	ISNULL(CodMunicipio,0)AS CodMunicipio,
	ISNULL(CodigoPais,0)AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN ISNULL(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	U.M4_Col1_ValorAcumPago AS M4_Col1_ValorAcumPago,
	U.M4_Col2_ValorDctoTrib AS M4_Col2_ValorDctoTrib,
	U.M5_Col1_ValorImptoDesc AS M5_Col1_ValorImptoDesc, 
	U.M5_Col2_IvaResDev AS M5_Col2_IvaResDev,
	U.M5_Col3_IvaMayorVCG AS M5_Col3_IvaMayorVCG,
	U.M6_Col1_ImptoGenerado AS M6_Col1_ImptoGenerado, 
	U.M6_Col2_IvaRecup AS M6_Col2_IvaRecup,
	U.M6_Col3_ImptoConsumo AS M6_Col3_ImptoConsumo,
	U.M7_Col1_IngBrutOpPropias AS M7_Col1_IngBrutOpPropias, 
	U.M7_Col2_IngBrutConsorcio AS M7_Col2_IngBrutConsorcio, 
	CASE WHEN (@IdFormato = 7 AND @CodVersion = 9) THEN 0 ELSE U.M7_Col3_IngContMandato END AS M7_Col3_IngContMandato, 
	CASE WHEN (@IdFormato = 7 AND @CodVersion = 9) THEN 0 ELSE U.M7_Col4_IngExploracion END AS M7_Col4_IngExploracion,
	CASE WHEN (@IdFormato = 7 AND @CodVersion = 9) THEN 0 ELSE M7_Col5_IngPorFiducia END AS M7_Col5_IngPorFiducia,
	CASE WHEN (@IdFormato = 7 AND @CodVersion = 9) THEN 0 ELSE M7_Col6_IngRecPorTerceros END AS M7_Col6_IngRecPorTerceros,
	U.M7_Col7_IngTercDevRebDctos AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	U.M9_Col1_Saldo AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M4_Col1_ValorAcumPago,
		ABS(ROUND(ISNULL(SUM(T.Col2), 0.0),0)) AS M4_Col2_ValorDctoTrib,
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M5_Col1_ValorImptoDesc,
		ABS(ROUND(ISNULL(SUM(T.Col2), 0.0),0)) AS M5_Col2_IvaResDev,
		ABS(ROUND(ISNULL(SUM(T.Col3), 0.0),0)) AS M5_Col3_IvaMayorVCG,
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M6_Col1_ImptoGenerado, 
		ABS(ROUND(ISNULL(SUM(T.Col2), 0.0),0)) AS M6_Col2_IvaRecup, 
		ABS(ROUND(ISNULL(SUM(T.Col3), 0.0),0)) AS M6_Col3_ImptoConsumo, 
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M7_Col1_IngBrutOpPropias, 
		ABS(ROUND(ISNULL(SUM(T.Col2), 0.0),0)) AS M7_Col2_IngBrutConsorcio, 
		ABS(ROUND(ISNULL(SUM(T.Col3), 0.0),0)) AS M7_Col3_IngContMandato,
		ABS(ROUND(ISNULL(SUM(T.Col4), 0.0),0)) AS M7_Col4_IngExploracion, 
		ABS(ROUND(ISNULL(SUM(T.Col5), 0.0),0)) AS M7_Col5_IngPorFiducia, 
		ABS(ROUND(ISNULL(SUM(T.Col6), 0.0),0)) AS M7_Col6_IngRecPorTerceros, 
		ABS(ROUND(ISNULL(SUM(T.Col7), 0.0),0)) AS M7_Col7_IngTercDevRebDctos,
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M9_Col1_Saldo

		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, M_Ppal.IdCliente, MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor, 
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1, 
			SUM(CASE WHEN M_Ppal.NumCol = 2 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col2, 
			SUM(CASE WHEN M_Ppal.NumCol = 3 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col3, 
			SUM(CASE WHEN M_Ppal.NumCol = 4 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col4, 
			SUM(CASE WHEN M_Ppal.NumCol = 5 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col5, 
			SUM(CASE WHEN M_Ppal.NumCol = 6 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col6, 
			SUM(CASE WHEN M_Ppal.NumCol = 7 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col7
			FROM         
			tempMediosPrincipal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente 
			GROUP BY 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT JOIN tempDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	WHERE 
    U.M7_Col1_IngBrutOpPropias + U.M7_Col2_IngBrutConsorcio + U.M7_Col3_IngContMandato + U.M7_Col4_IngExploracion + U.M7_Col5_IngPorFiducia + U.M7_Col6_IngRecPorTerceros + U.M7_Col7_IngTercDevRebDctos <> 0
	OPTION(RECOMPILE)
	RETURN

END

IF @IdFormato IN(8,9,12)
BEGIN
	---Crear Tabla temporal de saldos de cuantias menores
	--//
	IF OBJECT_ID('tempdb..#ufnMediosMagneticos_CuantiasMenores_Saldos') IS NOT NULL
		DROP TABLE #ufnMediosMagneticos_CuantiasMenores_Saldos;

	SELECT *
	INTO #ufnMediosMagneticos_CuantiasMenores_Saldos
	FROM dbo.ufnMediosMagneticos_CuantiasMenores_Saldos(@FechaFin, @IdEmpresas, @IdDocsExc, @IdVersion);

	CREATE NONCLUSTERED INDEX IX_Cuantias_IdCliente ON #ufnMediosMagneticos_CuantiasMenores_Saldos(IdCliente);
	--//
END

--Para el Formato 8
IF @IdFormato = 8
BEGIN 
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato, 0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	ABS(ROUND(COALESCE(U.M8_Col1_Saldo, 0.0),0)) AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.Col1) AS M8_Col1_Saldo
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, COALESCE(M_Ppal.IdClienteAgrupMM, M_Ppal.IdCliente) AS IdCliente, MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor, SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1
			FROM         
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN #ufnMediosMagneticos_CuantiasMenores_Saldos AS M_Cuantias ON M_Ppal.IdCliente = M_Cuantias.IdCliente 
            WHERE     
            M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) <= @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, COALESCE(M_Ppal.IdClienteAgrupMM, M_Ppal.IdCliente)
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	WHERE 
    U.M8_Col1_Saldo <> 0
    RETURN

END

--Para el Formato 9
IF @IdFormato = 9
BEGIN 
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato, 0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	ABS(ROUND(COALESCE(U.M9_Col1_Saldo, 0.0),0)) AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.Col1) AS M9_Col1_Saldo
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, M_Ppal.IdCliente, MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor, 
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1
			FROM         
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN #ufnMediosMagneticos_CuantiasMenores_Saldos AS M_Cuantias ON M_Ppal.IdCliente = M_Cuantias.IdCliente 
            WHERE     
            M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) <= @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	WHERE 
    U.M9_Col1_Saldo <> 0
    RETURN

END


--Para el Formato 10
IF @IdFormato = 10
BEGIN 
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato, 0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	ABS(ROUND(COALESCE(U.M10_Col1_ValorNominalAccion, 0.0),0)) AS M10_Col1_ValorNominalAccion,
	ABS(ROUND(COALESCE(U.M10_Col2_PorcParti, 0.0),0)) AS M10_Col2_PorcParti,
	ABS(ROUND(COALESCE(U.M10_Col3_PorcPartiDec, 0.0),0)) AS M10_Col3_PorcPartiDec,
	ABS(ROUND(COALESCE(U.M10_Col4_ValorPrimaAccion, 0.0),0)) AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.Col1) AS M10_Col1_ValorNominalAccion,
		SUM(T.Col2)*100*10000 AS M10_Col2_PorcParti,
		4 AS M10_Col3_PorcPartiDec,
		SUM(T.Col3) AS M10_Col4_ValorPrimaAccion
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, M_Ppal.IdCliente, 1 AS EsCuantiaMayor, 
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1,
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END)/
			(
				SELECT SUM(PagoTotal)
				FROM dbo.VMediosMag_Procesos_Principal AS M_Sub
				WHERE 
				M_Sub.IdVersion = @IdVersion AND
				DATEADD(dd, DATEDIFF(dd, 0, M_Sub.Fecha), 0) <= @FechaFinT AND
				M_Sub.IdEmpresa IN (SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
				M_Sub.IdDocumento NOT IN (SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc, DEFAULT)) AND
				M_Sub.IdCliente NOT IN (SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc, DEFAULT)) AND
				M_Sub.NumCol = 1
			) AS Col2,
			SUM(CASE WHEN M_Ppal.NumCol = 2 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col3
			FROM         
			dbo.VMediosMag_Procesos_Principal AS M_Ppal
            WHERE     
            M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd, 0, M_Ppal.Fecha), 0) <= @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN (SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN (SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT))
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	WHERE 
    M10_Col1_ValorNominalAccion + M10_Col4_ValorPrimaAccion <> 0
    RETURN

END


--Para el Formato 12
IF @IdFormato = 12
BEGIN 
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	Cli.DVCliente, 
	SUBSTRING(Cli.Apellido1,1,@CM_LongNombres) AS Apellido1,
	SUBSTRING(Cli.Apellido2,1,@CM_LongNombres) AS Apellido2,
	CASE WHEN NOT(Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21) THEN SUBSTRING(COALESCE(Cli.Nombre1,''),1,@CM_LongNombres) ELSE '' END AS Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE '' END) AS RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	SUBSTRING(Dir.DireccionFormato2,1,@CM_LongDireccion) AS DireccionFormato2,
	'' NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	@TarifaIca AS TarifaIca,
	'' TarifaAplicada,
	'' NombreCompleto,
	'' CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	RIGHT('000' + CAST(COALESCE(Dir.CodMunicipio,0) AS VARCHAR),3) AS CodMunicipio,
	COALESCE(Dir.CodigoDian,0) AS CodigoPais,
	M_Conc.CodConcepto,
	U.IdConcepto,
	CASE WHEN U.IdCliente = 0 THEN COALESCE(@IdCliInf,0) ELSE U.IdCliente END AS IdCliente, 
	U.EsCuantiaMayor, 
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato, 0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	ABS(ROUND(COALESCE(U.M12_Col1_Valor, 0.0),0)) AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.Col1) AS M12_Col1_Valor
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, M_Ppal.IdCliente, MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor, 
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1
			FROM         
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN #ufnMediosMagneticos_CuantiasMenores_Saldos AS M_Cuantias ON M_Ppal.IdCliente = M_Cuantias.IdCliente 
            WHERE     
            M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) <= @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) AND
			M_Ppal.IdCliente IN (SELECT IdCliente FROM Clientes_Asociacion where IdTipo = 9)
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	WHERE 
    U.M12_Col1_Valor <> 0
    RETURN

END

--MEDIOS MAGNÉTICOS
--Artículo 2 (Concep. 1001 - v10)

IF @IdFormato = 19
BEGIN
	SELECT 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_TipoIdent ELSE Cli.IdTipoIdent END AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	'' AS DVCliente,
	'' Apellido1,
	'' Apellido2,
	'' Nombre1,
	'' Nombre2,
	'' RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS  DireccionFormato1,
	'' DireccionFormato2,
	TipoIdent.NomTipoIdent AS  NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	0.0 AS TarifaIca,
	'' TarifaAplicada,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE (COALESCE (Cli.Nombre1, '') + COALESCE (' ' + Cli.Nombre2, '') + COALESCE (' ' +Cli.Apellido1, '')+ COALESCE (' ' + Cli.Apellido2, ''))  END) AS NombreCompleto,
	RIGHT('00000' + CAST(COALESCE(Dir.IdCiudad,0) AS VARCHAR),5) AS CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	'' CodMunicipio,
	0 AS CodigoPais,
	'' CodConcepto,
	0 AS IdConcepto,
	0 AS IdCliente, 
	0 AS  EsCuantiaMayor,	 
	CAST(U.IdConPag AS VARCHAR) AS ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	ABS(ROUND(COALESCE(U.M1_Col1_PagoDeducible, 0.0),0)) AS M1_Col1_PagoDeducible,
	0.0 AS M1_Col2_PagoNoDeducible,
	0.0 AS M1_Col3_IvaDeducible,
	0.0 AS M1_Col4_IvaNoDeducible,
	0.0 AS M1_Col5_ReteFteAplicada,
	0.0 AS M1_Col6_ReteFteAsumida,
	0.0 AS M1_Col7_ReteIvaRegComun,
	0.0 AS M1_Col9_ReteFteIvaNoDomic,
    0.0 AS M1_Col8_ReteIvaRegSimpC,  
	0.0 AS M1_Col10_ReteFteCREEAplicada, 
	0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConPag,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M1_Col1_PagoDeducible
		FROM
		(
			SELECT 
			M_Ppal.IdConPag, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.PagoDeducible) AS PagoDeducible, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFuenteCREE_Fin AS M_ReteFteCREE ON M_Ppal.IdEncab = M_ReteFteCREE.IdEncab AND M_Ppal.IdAgrup = M_ReteFteCREE.IdAgrup AND M_Ppal.IdCliente = M_ReteFteCREE.IdCliente LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteFteIvaNoDomic_Fin AS M_ReteFteNoDom ON M_Ppal.IdCliente = M_ReteFteNoDom.IdCliente AND M_Ppal.IdEncab = M_ReteFteNoDom.IdEncab AND M_Ppal.IdAgrup = M_ReteFteNoDom.IdAgrup LEFT OUTER JOIN
                        dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegComun_Fin AS M_ReteIvaRCom ON M_Ppal.IdCliente = M_ReteIvaRCom.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRCom.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRCom.IdAgrup LEFT OUTER JOIN dbo.VMediosMag_Procesos_IvaMayorValor_Fin AS M_Ivas ON M_Ppal.IdCliente = M_Ivas.IdCliente AND M_Ppal.IdEncab = M_Ivas.IdEncab AND M_Ppal.IdAgrup = M_Ivas.IdAgrup AND M_Ppal.IdVersion = M_Ivas.IdVersion LEFT OUTER JOIN dbo.VMediosMag_Procesos_ReteIvaRegSimpC_Fin AS M_ReteIvaRSC ON M_Ppal.IdCliente = M_ReteIvaRSC.IdCliente AND M_Ppal.IdEncab = M_ReteIvaRSC.IdEncab AND M_Ppal.IdAgrup = M_ReteIvaRSC.IdAgrup
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConPag, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConPag,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
    LEFT OUTER JOIN dbo.Clientes_TiposIdent AS TipoIdent ON TipoIdent.IdTipoIdent=cli.IdTipoIdent
	RETURN
END


--Artículo 3  (1007 - 7)		
IF @IdFormato = 20 
BEGIN 
 

 ;WITH tempMediosCont(IdCuenta,IdEncab,IdAgrup,IdCliente,Fecha,IdEmpresa,IdDocumento,PagoDeducible,PagoNoDeducible,PagoTotal,RetencionBase,SumRetReal)
 AS
 (
  SELECT     IdCuenta,Enc_C.IdEncab,ISNULL(Enc_C.IdEncabContAgrup, 0) AS IdAgrup, Enc_C.IdCliente,
     MAX(Enc.Fecha) AS Fecha, MAX(Enc.IdEmpresa) AS IdEmpresa, MAX(Enc.IdDocumento) AS IdDocumento, 
                         SUM(CASE WHEN Enc_C.EsDeducible = 0 THEN Enc_C.Debito - Enc_C.Credito ELSE 0 END) AS PagoDeducible, SUM(CASE WHEN Enc_C.EsDeducible = 1 THEN Enc_C.Debito - Enc_C.Credito ELSE 0 END) AS PagoNoDeducible, 
                         SUM(Enc_C.Debito - Enc_C.Credito) AS PagoTotal, SUM(CASE WHEN ISNULL(Enc_C.RetencionPorc, 0) > 0 THEN ABS(Enc_C.Debito - Enc_C.Credito) * 100 / Enc_C.RetencionPorc ELSE 0 END) AS RetencionBase, 
                         SUM(ISNULL(Enc_C.RetFuenteReal, 0)) AS SumRetReal
FROM    dbo.Encabezados_Cont AS Enc_C INNER JOIN
         dbo.Encabezados AS Enc ON Enc_C.IdEncab = Enc.IdEncab
  WHERE     
  (Enc.EsAnulado = 0) AND
			DATEADD(dd,DATEDIFF(dd,0,Enc.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			Enc.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			Enc.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			Enc_C.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
GROUP BY IdCuenta, Enc_C.IdEncab, Enc_C.IdCliente, ISNULL(Enc_C.IdEncabContAgrup, 0)

 ),

 tempMediosPrincipal(IdVersion, IdEncab, IdAgrup, IdCliente, IdClienteAgrupMM, IdConcepto, NumCol, IdAgrupMM,  CodConcepto, PagoDeducible, PagoNoDeducible, PagoTotal, RetencionBase, SumRetReal)
 AS
 (
 SELECT        MM_Cuentas.IdVersion, Enc_C.IdEncab,IdAgrup, Enc_C.IdCliente, MM_Cuentas.IdCliente AS IdClienteAgrupMM, MM_Cuentas.IdConcepto, MM_Cuentas.NumCol, MM_Cuentas.IdAgrupMM, MAX(MM_Conc.CodConcepto) AS CodConcepto,
                         SUM(PagoDeducible), SUM(PagoNoDeducible), 
                         SUM(PagoTotal), SUM(RetencionBase), 
                         SUM(SumRetReal)SumRetReal
FROM            dbo.MediosMag_CuentasFormato AS MM_Cuentas INNER JOIN
                         tempMediosCont AS Enc_C ON MM_Cuentas.IdCuenta = Enc_C.IdCuenta  INNER JOIN
                         dbo.MediosMag_Conceptos AS MM_Conc ON MM_Cuentas.IdConcepto = MM_Conc.IdConcepto
    
GROUP BY MM_Cuentas.IdConcepto, Enc_C.IdEncab, Enc_C.IdCliente,IdAgrup, MM_Cuentas.IdVersion, MM_Cuentas.NumCol, MM_Cuentas.IdCliente, MM_Cuentas.IdAgrupMM
)

,

tempDir(Idcliente,Direccion,DireccionFormato1,DireccionFormato2,Telefonos,EMail,CodCiudad,CodDpto,CodMunicipio,CodigoPais)
AS
(
SELECT
tempDirPri.Idcliente,      
	SUBSTRING(Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(DireccionFormato1,1,@CM_LongDireccion) AS DireccionFormato1,
	'' DireccionFormato2,
	[Telefonos] AS Telefonos,
	EMail AS EMail,
	RIGHT('00000' + CAST(ISNULL(dbo.Ciudades.IdCiudad,0) AS VARCHAR),5) AS CodCiudad,
	RIGHT('00' + CAST(ISNULL( dbo.Departamentos.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	'' CodMunicipio,
	0 AS CodigoPais
FROM          
						
                         VClientesDir_01 AS tempDirPri INNER JOIN
                         dbo.Clientes_Direcciones ON tempDirPri.IdClienteDirMax = dbo.Clientes_Direcciones.IdClienteDir  LEFT JOIN
                         dbo.Clientes_TiposDireccion ON dbo.Clientes_Direcciones.IdTipoDir = dbo.Clientes_TiposDireccion.IdTipoDir LEFT JOIN
						 dbo.Ciudades  ON dbo.Ciudades.IdCiudad = dbo.Clientes_Direcciones.IdCiudad  LEFT JOIN
						 dbo.Departamentos ON dbo.Departamentos.IdDpto = dbo.Ciudades.IdDpto  LEFT JOIN
						 dbo.Paises ON dbo.Departamentos.IdPais = dbo.Paises.IdPais 
)

	SELECT 
	0 AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	'' DVCliente,
	'' Apellido1,
	'' Apellido2,
	'' Nombre1,
	SUBSTRING(Cli.Nombre2,1,@CM_LongNombres) AS Nombre2,
	'' RazonSocial,
	Direccion,
	DireccionFormato1,
	DireccionFormato2,
	TipoIdent.NomTipoIdent AS NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	0.0 AS TarifaIca,
	'' TarifaAplicada,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE (COALESCE (Cli.Nombre1, '') + COALESCE (' ' + Cli.Nombre2, '') + COALESCE (' ' +Cli.Apellido1, '')+ COALESCE (' ' + Cli.Apellido2, ''))  END) AS NombreCompleto,
	ISNULL(CodCiudad,0)AS CodCiudad,
	ISNULL(CodDpto,0)AS CodDpto,
	'' CodMunicipio,
	0 AS CodigoPais,
	'' CodConcepto,
	0 AS IdConcepto,
	0 AS IdCliente, 
	0 AS  EsCuantiaMayor,
	'' ConceptoPagoCuenta,
	0.0 AS ValorTotalDevRebDes,
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	0.0 AS M3_Col1_ValorPagoAbono, 0.0 AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 
	0.0 AS M6_Col2_IvaRecup,
	0.0 AS M6_Col3_ImptoConsumo,
	U.M7_Col1_IngBrutOpPropias AS M7_Col1_IngBrutOpPropias, 
	0.0 AS M7_Col2_IngBrutConsorcio, 
	0.0 AS M7_Col3_IngContMandato, 
	0.0 AS M7_Col4_IngExploracion,
	0.0 AS M7_Col5_IngPorFiducia,
	0.0 AS M7_Col6_IngRecPorTerceros,
	U.M7_Col7_IngTercDevRebDctos AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M7_Col1_IngBrutOpPropias, 
		ABS(ROUND(ISNULL(SUM(T.Col2), 0.0),0)) AS M7_Col2_IngBrutConsorcio, 
		ABS(ROUND(ISNULL(SUM(T.Col3), 0.0),0)) AS M7_Col3_IngContMandato,
		ABS(ROUND(ISNULL(SUM(T.Col4), 0.0),0)) AS M7_Col4_IngExploracion, 
		ABS(ROUND(ISNULL(SUM(T.Col5), 0.0),0)) AS M7_Col5_IngPorFiducia, 
		ABS(ROUND(ISNULL(SUM(T.Col6), 0.0),0)) AS M7_Col6_IngRecPorTerceros, 
		ABS(ROUND(ISNULL(SUM(T.Col7), 0.0),0)) AS M7_Col7_IngTercDevRebDctos,
		ABS(ROUND(ISNULL(SUM(T.Col1), 0.0),0)) AS M9_Col1_Saldo

		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, M_Ppal.IdCliente, MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor, 
			SUM(CASE WHEN M_Ppal.NumCol = 1 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col1, SUM(CASE WHEN M_Ppal.NumCol = 2 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col2, SUM(CASE WHEN M_Ppal.NumCol = 3 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col3, SUM(CASE WHEN M_Ppal.NumCol = 4 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col4, SUM(CASE WHEN M_Ppal.NumCol = 5 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col5, SUM(CASE WHEN M_Ppal.NumCol = 6 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col6, SUM(CASE WHEN M_Ppal.NumCol = 7 THEN M_Ppal.PagoTotal ELSE 0 END) AS Col7
			FROM         
			tempMediosPrincipal AS M_Ppal INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT JOIN tempDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	LEFT OUTER JOIN dbo.Clientes_TiposIdent AS TipoIdent ON TipoIdent.IdTipoIdent=cli.IdTipoIdent
	WHERE 
    U.M7_Col1_IngBrutOpPropias + U.M7_Col2_IngBrutConsorcio + U.M7_Col3_IngContMandato + U.M7_Col4_IngExploracion + U.M7_Col5_IngPorFiducia + U.M7_Col6_IngRecPorTerceros + U.M7_Col7_IngTercDevRebDctos <> 0
   OPTION(RECOMPILE)
    RETURN

END


--Artículo 4 (1003 - 3)
IF @IdFormato = 21
BEGIN
	SELECT 
	0 AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	'' DVCliente, 
	'' Apellido1,
	'' Apellido2,
	'' Nombre1,
	'' Nombre2,
	'' RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS  DireccionFormato1,
	'' DireccionFormato2,
	TipoIdent.NomTipoIdent AS NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	0.0 AS TarifaIca,
	'966' AS TarifaAplicada,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE (COALESCE (Cli.Nombre1, '') + COALESCE (' ' + Cli.Nombre2, '') + COALESCE (' ' +Cli.Apellido1, '')+ COALESCE (' ' + Cli.Apellido2, ''))  END) AS NombreCompleto,
	RIGHT('00000' + CAST(COALESCE(Dir.IdCiudad,0) AS VARCHAR),5) AS CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	'' CodMunicipio,
	0 AS  CodigoPais,
	'' CodConcepto,
	0 AS IdConcepto,
	0 AS IdCliente, 
	0 AS  EsCuantiaMayor, 
	'' AS ConceptoPagoCuenta, 
	0.0 AS ValorTotalDevRebDes, 
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	ABS(ROUND(COALESCE(U.M3_Col1_ValorPagoAbono, 0.0),0)) AS M3_Col1_ValorPagoAbono, 
	ABS(ROUND(COALESCE(U.M3_Col2_Retencion, 0.0),0)) AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M3_Col1_ValorPagoAbono, 
		SUM(T.ReteFteAplicada) AS M3_Col2_Retencion
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.RetencionBase) AS PagoDeducible, 
			SUM(COALESCE(M_Ppal.SumRetReal,M_Ppal.PagoDeducible)) AS ReteFteAplicada, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal 
			INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente 
			--INNER JOIN dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup 
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
		HAVING SUM(T.ReteFteAplicada) <> 0

	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	LEFT OUTER JOIN dbo.Clientes_TiposIdent AS TipoIdent ON TipoIdent.IdTipoIdent=cli.IdTipoIdent
	RETURN
END


--Artículo 6 (1003- 3)

IF @IdFormato = 23 
BEGIN
	SELECT 
	0 AS IdTipoIdent, 
	CASE WHEN (@Detallar = 0 AND U.EsCuantiaMayor = 0) THEN @CM_Identif ELSE Cli.Identificacion END AS Identificacion, 
	'' DVCliente, 
	'' Apellido1,
	'' Apellido2,
	'' Nombre1,
	'' Nombre2,
	'' RazonSocial,
	SUBSTRING(Dir.Direccion,1,@CM_LongDireccion) AS Direccion,
	SUBSTRING(Dir.DireccionFormato1,1,@CM_LongDireccion) AS  DireccionFormato1,
	'' DireccionFormato2,
	TipoIdent.NomTipoIdent AS NomTipoIdent,
	dir.[Telefonos] AS Telefonos,
	dir.EMail AS EMail,
	@Vigencia AS Vigencia,
	0.0 AS TarifaIca,
	'966' AS TarifaAplicada,
	COALESCE(CASE WHEN U.EsCuantiaMayor = 0 AND @Detallar = 0 THEN @CM_RazonSocial ELSE NULL END, CASE WHEN Cli.IdTipoIdent = 31 OR Cli.IdTipoIdent = 42 OR Cli.IdTipoIdent = 21 THEN SUBSTRING(Cli.Nombre1,1,@CM_LongRazonSocial) ELSE (COALESCE (Cli.Nombre1, '') + COALESCE (' ' + Cli.Nombre2, '') + COALESCE (' ' +Cli.Apellido1, '')+ COALESCE (' ' + Cli.Apellido2, ''))  END) AS NombreCompleto,
	RIGHT('00000' + CAST(COALESCE(Dir.IdCiudad,0) AS VARCHAR),5) AS CodCiudad,
	RIGHT('00' + CAST(COALESCE(Dir.IdDpto,0) AS VARCHAR),2) AS CodDpto,
	'' CodMunicipio,
	0 AS  CodigoPais,
	'' CodConcepto,
	0 AS IdConcepto,
	0 AS IdCliente, 
	0 AS  EsCuantiaMayor, 
	'' AS ConceptoPagoCuenta, 
	0.0 AS ValorTotalDevRebDes, 
	0.0 AS M1_Col1_PagoDeducible, 0.0 AS M1_Col2_PagoNoDeducible, 0.0 AS M1_Col3_IvaDeducible, 0.0 AS M1_Col4_IvaNoDeducible, 0.0 AS M1_Col5_ReteFteAplicada, 0.0 AS M1_Col6_ReteFteAsumida, 0.0 AS M1_Col7_ReteIvaRegComun, 0.0 AS M1_Col8_ReteIvaRegSimpC, 0.0 AS M1_Col9_ReteFteIvaNoDomic, 0.0 AS M1_Col10_ReteFteCREEAplicada, 0.0 AS M1_Col11_ReteFteCREEAsumida, 
	ABS(ROUND(COALESCE(U.M3_Col1_ValorPagoAbono, 0.0),0)) AS M3_Col1_ValorPagoAbono, 
	ABS(ROUND(COALESCE(U.M3_Col2_Retencion, 0.0),0)) AS M3_Col2_Retencion, 
	0.0 AS M4_Col1_ValorAcumPago, 0.0 AS M4_Col2_ValorDctoTrib,
	0.0 AS M5_Col1_ValorImptoDesc, 0.0 AS M5_Col2_IvaResDev, 0.0 AS M5_Col3_IvaMayorVCG,
	0.0 AS M6_Col1_ImptoGenerado, 0.0 AS M6_Col2_IvaRecup, 0.0 AS M6_Col3_ImptoConsumo,
	0.0 AS M7_Col1_IngBrutOpPropias, 0.0 AS M7_Col2_IngBrutConsorcio, 0.0 AS M7_Col3_IngContMandato,0.0 AS M7_Col4_IngExploracion, 0.0 AS M7_Col5_IngPorFiducia, 0.0 AS M7_Col6_IngRecPorTerceros, 0.0 AS M7_Col7_IngTercDevRebDctos,
	0.0 AS M8_Col1_Saldo,
	0.0 AS M9_Col1_Saldo,
	0.0 AS M10_Col1_ValorNominalAccion, 
	0.0 AS M10_Col2_PorcParti, 
	0.0 AS M10_Col3_PorcPartiDec, 
	0.0 AS M10_Col4_ValorPrimaAccion,
	0.0 AS M11_Col1_Saldo,
	0.0 AS M12_Col1_Valor,
	0.0 AS M15_Col1_Valor,
	0.0 AS M16_Col1_ValorBruto, 
	0.0 AS M18_Col1_ValorBase, 
	0.0 AS M18_Col2_Retencion,
	0.0 AS M19_Col1_ValorBase, 
	0.0 AS M19_Col2_Retencion,
	0.0 AS M32_Col1_PagoOAbonoCuenta,
	0.0 AS M32_Col2_IVAMayorValorCosto,
	0.0 AS M32_Col3_ReteFtePracticadaRenta,
	0.0 AS M32_Col4_ReteFteAsumidaRenta,
	0.0 AS M32_Col5_ReteFtePracticadaRegComun,
	0.0 AS M32_Col6_ReteFtePracticadaNoDomic,
	0.0	AS M33_Col1_ValorOperacion,
	0.0	AS M33_Col2_ValorIngresoTercero,
	0.0	AS M33_Col3_ValorReteTercero,
	0	AS M33_Col4_IdTipoIdentIng, 
	''	AS M33_Col5_IdentificacionIng,
	''	AS M33_Col6_Apellido1Ing,
	''	AS M33_Col7_Apellido2Ing,
	''	AS M33_Col8_Nombre1Ing,
	''	AS M33_Col9_Nombre2Ing,
	''	AS M33_Col10_RazonSocialIng,
	0.0	AS M34_Col1_ValorIngresoNoConst,
	0.0 AS M35_Col1_PagosSalarios,
	0.0 AS M35_Col2_PagosEmolEclesiast,
	0.0 AS M35_Col3_PagosBonosEtc,
	0.0 AS M35_Col4_ExcesoAlimentacion41U,
	0.0 AS M35_Col5_PagosHonorarios,
	0.0 AS M35_Col6_PagosServicios,
	0.0 AS M35_Col7_PagosComisiones,
	0.0 AS M35_Col8_PagosPrestSociales,
	0.0 AS M35_Col9_PagosViaticos,
	0.0 AS M35_Col10_PagosGastosRepre,
	0.0 AS M35_Col11_PagosCompensAsoc,
	0.0 AS M35_Col12_ApoyosEducativos,
	0.0 AS M35_Col13_OtrosPagos,
	0.0 AS M35_Col14_CesantiasPagadas,
	0.0 AS M35_Col15_CesantiasConsFondo,
	0.0 AS M35_Col16_CesantiasRegTrad,
	0.0 AS M35_Col17_PensionesJubilacion,
	0.0 AS M35_Col18_TotalIngBrutosRT,
	0.0 AS M35_Col19_AporteSaludTrab,
	0.0 AS M35_Col20_AportePensTrab,
	0.0 AS M35_Col21_AporteVolRAIS,
	0.0 AS M35_Col22_AportePensVol,
	0.0 AS M35_Col23_AporteCuentaAFC,
	0.0 AS M35_Col24_AporteCuentaAVC,
	0.0 AS M35_Col25_ReteFteRentaTrabajo,
	0.0 AS M35_Col26_IVA_MayorCosto,
	0.0 AS M35_Col27_ReteIVA,
	0.0 AS M35_Col28_AlimentHasta41UVT,
	0.0 AS M35_Col29_IngresoProm6Meses,
	0	AS M35_Col30_IdTipoIdentDep, 
	''	AS M35_Col31_IdentificacionDep,
	''	AS M35_Col32_IdentificacionFid,
	0	AS M35_Col33_IdTipoIdentPartic, 
	''	AS M35_Col34_IdentificacionPartic
	FROM
	(
		SELECT 
		T.IdConcepto,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END AS IdCliente, 
		T.EsCuantiaMayor, 
		SUM(T.PagoDeducible) AS M3_Col1_ValorPagoAbono, 
		SUM(T.ReteFteAplicada) AS M3_Col2_Retencion
		FROM
		(
			SELECT 
			M_Ppal.IdConcepto, 
			M_Ppal.IdCliente, 
			SUM(M_Ppal.RetencionBase) AS PagoDeducible, 
			SUM(COALESCE(M_Ppal.SumRetReal,M_Ppal.PagoDeducible)) AS ReteFteAplicada, 
			MAX(M_Cuantias.EsCuantiaMayor) AS EsCuantiaMayor
			FROM 
			dbo.VMediosMag_Procesos_Principal AS M_Ppal 
			INNER JOIN dbo.ufnMediosMagneticos_CuantiasMenores(@FechaIni, @FechaFin, @IdEmpresas, @IdDocsExc,@IdVersion) AS M_Cuantias ON M_Ppal.IdVersion = M_Cuantias.IdVersion AND M_Ppal.IdCliente = M_Cuantias.IdCliente 
			--INNER JOIN dbo.VMediosMag_Procesos_ReteFuente_Fin AS M_ReteFte ON M_Ppal.IdCliente = M_ReteFte.IdCliente AND M_Ppal.IdEncab = M_ReteFte.IdEncab AND M_Ppal.IdAgrup = M_ReteFte.IdAgrup 
			WHERE
			M_Ppal.IdVersion = @IdVersion AND
			DATEADD(dd,DATEDIFF(dd,0,M_Ppal.Fecha),0) BETWEEN @FechaIniT AND @FechaFinT AND
			M_Ppal.IdEmpresa IN(SELECT number FROM iter$simple_intlist_to_tbl(@IdEmpresas)) AND
			M_Ppal.IdDocumento NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdDocsExc,DEFAULT)) AND
			M_Ppal.IdCliente NOT IN(SELECT nstr FROM iter$charlist_to_tbl(@IdCliExc,DEFAULT)) 
			GROUP BY M_Ppal.IdConcepto, M_Ppal.IdCliente
		) T
		GROUP BY 
		T.IdConcepto,
		T.EsCuantiaMayor,
		CASE WHEN (@Detallar = 0 AND T.EsCuantiaMayor = 0) THEN 0 ELSE T.IdCliente END
		HAVING SUM(T.ReteFteAplicada) <> 0

	) U
	INNER JOIN dbo.MediosMag_Conceptos AS M_Conc ON U.IdConcepto = M_Conc.IdConcepto
	LEFT OUTER JOIN dbo.VClientesDir AS Dir ON CASE WHEN U.IdCliente = 0 THEN @IdCliInf ELSE U.IdCliente END = Dir.IdCliente 
	LEFT OUTER JOIN dbo.Clientes AS Cli ON U.IdCliente = Cli.IdCliente
	LEFT OUTER JOIN dbo.Clientes_TiposIdent AS TipoIdent ON TipoIdent.IdTipoIdent=cli.IdTipoIdent
	RETURN
END
END
GO
PRINT N'Refreshing Function [dbo].[ufnContabilizacion_FormasPago]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ufnContabilizacion_FormasPago]';


GO
PRINT N'Refreshing Function [dbo].[ufnContabilizacion_Normal]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ufnContabilizacion_Normal]';


GO
PRINT N'Refreshing Procedure [dbo].[GenerarImptos]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GenerarImptos]';


GO
PRINT N'Refreshing Procedure [dbo].[ActualizarAudit]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ActualizarAudit]';


GO
PRINT N'Refreshing Procedure [dbo].[ContabilizarDoc_Validar]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ContabilizarDoc_Validar]';


GO
PRINT N'Refreshing Procedure [dbo].[GetArqueoCaja]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetArqueoCaja]';


GO
PRINT N'Refreshing Procedure [dbo].[GetComisiones]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetComisiones]';


GO
PRINT N'Refreshing Procedure [dbo].[GetConsecutivosFaltantes]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetConsecutivosFaltantes]';


GO
PRINT N'Refreshing Procedure [dbo].[GetDocsDiferencias]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetDocsDiferencias]';


GO
PRINT N'Refreshing Procedure [dbo].[GetEstadoCuenta]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetEstadoCuenta]';


GO
PRINT N'Refreshing Procedure [dbo].[GetImptoRetenciones]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetImptoRetenciones]';


GO
PRINT N'Refreshing Procedure [dbo].[GetInformeUGPP]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetInformeUGPP]';


GO
PRINT N'Refreshing Procedure [dbo].[GetInvKardexPorLote]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetInvKardexPorLote]';


GO
PRINT N'Refreshing Procedure [dbo].[GetKardexInventario]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetKardexInventario]';


GO
PRINT N'Refreshing Procedure [dbo].[GetKardexInventarioPorAgrupaciones]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetKardexInventarioPorAgrupaciones]';


GO
PRINT N'Refreshing Procedure [dbo].[GetLibroAuxiliar]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetLibroAuxiliar]';


GO
PRINT N'Refreshing Procedure [dbo].[GetPagoSeguridadSocial]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetPagoSeguridadSocial]';


GO
PRINT N'Refreshing Procedure [dbo].[GetVEncabezados]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetVEncabezados]';


GO
PRINT N'Refreshing Procedure [dbo].[Ziur_CrearClienteDeSistema]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[Ziur_CrearClienteDeSistema]';


GO
PRINT N'Refreshing Procedure [dbo].[ContabilizarDoc]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ContabilizarDoc]';


GO
PRINT N'Refreshing Procedure [dbo].[CalcularCostoVenta]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[CalcularCostoVenta]';


GO
PRINT N'Refreshing Procedure [dbo].[GetDocsSinCruzar]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetDocsSinCruzar]';


GO
PRINT N'Refreshing Procedure [dbo].[GetVentasAgrupadoPorCliVend]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetVentasAgrupadoPorCliVend]';


GO
PRINT N'Refreshing Procedure [dbo].[GetDocumentosCierre]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetDocumentosCierre]';


GO
PRINT N'Refreshing Procedure [dbo].[GetLibroDiario]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetLibroDiario]';


GO
PRINT N'Refreshing Procedure [dbo].[GetNominaElectronica_ZiurOne]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetNominaElectronica_ZiurOne]';


GO
PRINT N'Refreshing Procedure [dbo].[GetNominaItemsProgramados]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetNominaItemsProgramados]';


GO
PRINT N'Refreshing Procedure [dbo].[ComisionesPorDoc]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[ComisionesPorDoc]';


GO
PRINT N'Refreshing Procedure [dbo].[GetVacaciones]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetVacaciones]';


GO
PRINT N'Refreshing Procedure [dbo].[GenerarCrucesCont]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GenerarCrucesCont]';


GO
PRINT N'Refreshing Procedure [dbo].[GetActivosFijosDet]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetActivosFijosDet]';


GO
PRINT N'Refreshing Procedure [dbo].[GetInformeRIPS]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetInformeRIPS]';


GO
PRINT N'Refreshing Procedure [dbo].[GetMovimientosFactElec]...';


GO
EXECUTE sp_refreshsqlmodule N'[dbo].[GetMovimientosFactElec]';


GO
	/*
	Plantilla de script posterior a la implementación							
	--------------------------------------------------------------------------------------
	 Este archivo contiene instrucciones de SQL que se anexarán al script de compilación.		
	 Use la sintaxis de SQLCMD para incluir un archivo en el script posterior a la implementación.			
	 Ejemplo:      :r .\miArchivo.sql								
	 Use la sintaxis de SQLCMD para hacer referencia a una variable en el script posterior a la implementación.		
	 Ejemplo:      :setvar TableName miTabla							
				   SELECT * FROM [$(TableName)]					
	--------------------------------------------------------------------------------------
	*/

	--CREACION DE NUEVOS FORMATOS
	--//
	IF(NOT EXISTS(SELECT IdFormato FROM MediosMag_Formatos WHERE IdFormato = 32))
	INSERT INTO [dbo].[MediosMag_Formatos] ([IdFormato] ,[NomFormato] ,[EsDistritales])
	VALUES(32,'Concepto 1056',0)
GO

	IF(NOT EXISTS(SELECT IdFormato FROM MediosMag_Formatos WHERE IdFormato = 33))
	INSERT INTO [dbo].[MediosMag_Formatos] ([IdFormato] ,[NomFormato] ,[EsDistritales])
	VALUES(33,'Concepto 1647',0)
GO

	IF(NOT EXISTS(SELECT IdFormato FROM MediosMag_Formatos WHERE IdFormato = 34))
	INSERT INTO [dbo].[MediosMag_Formatos] ([IdFormato] ,[NomFormato] ,[EsDistritales])
	VALUES(34,'Concepto 2275',0)
GO

	IF(NOT EXISTS(SELECT IdFormato FROM MediosMag_Formatos WHERE IdFormato = 35))
	INSERT INTO [dbo].[MediosMag_Formatos] ([IdFormato] ,[NomFormato] ,[EsDistritales])
	VALUES(35,'Concepto 2276',0)
GO
	--//

    --CREACION DE NUEVAS VERSIONES
    --//
    IF (NOT EXISTS(SELECT IdVersion FROM MediosMag_Versiones WHERE IdVersion=35))
    INSERT INTO [dbo].[MediosMag_Versiones] ([IdVersion] ,[NomVersion] ,[IdFormato] ,[CodVersion] ,[EsActivo])
    VALUES(35,'Concepto 1056 (V10)', 32, 10, 1)
GO

    IF (NOT EXISTS(SELECT IdVersion FROM MediosMag_Versiones WHERE IdVersion=36))
    INSERT INTO [dbo].[MediosMag_Versiones] ([IdVersion] ,[NomVersion] ,[IdFormato] ,[CodVersion] ,[EsActivo])
    VALUES(36,'Concepto 1647 (V2)', 33, 2, 1)
GO

    IF (NOT EXISTS(SELECT IdVersion FROM MediosMag_Versiones WHERE IdVersion=37))
    INSERT INTO [dbo].[MediosMag_Versiones] ([IdVersion] ,[NomVersion] ,[IdFormato] ,[CodVersion] ,[EsActivo])
    VALUES(37,'Concepto 2275 (V2)', 34, 2, 1)
GO

    IF (NOT EXISTS(SELECT IdVersion FROM MediosMag_Versiones WHERE IdVersion=38))
    INSERT INTO [dbo].[MediosMag_Versiones] ([IdVersion] ,[NomVersion] ,[IdFormato] ,[CodVersion] ,[EsActivo])
    VALUES(38,'Concepto 2276 (V4)', 35, 4, 1)
GO
    --//

	--ACTUALIZAR FORMATOS
	--//
	--formatos anteriores
	UPDATE MediosMag_Formatos SET IdVersion=15 WHERE IdFormato=1
	UPDATE MediosMag_Formatos SET IdVersion=3 WHERE IdFormato=3
	UPDATE MediosMag_Formatos SET IdVersion=32 WHERE IdFormato=4
	UPDATE MediosMag_Formatos SET IdVersion=33 WHERE IdFormato=5
	UPDATE MediosMag_Formatos SET IdVersion=14 WHERE IdFormato=6
	UPDATE MediosMag_Formatos SET IdVersion=17 WHERE IdFormato=7
	UPDATE MediosMag_Formatos SET IdVersion=8 WHERE IdFormato=8
	UPDATE MediosMag_Formatos SET IdVersion=9 WHERE IdFormato=9
	UPDATE MediosMag_Formatos SET IdVersion=34 WHERE IdFormato=10
	UPDATE MediosMag_Formatos SET IdVersion=11 WHERE IdFormato=11
	UPDATE MediosMag_Formatos SET IdVersion=12 WHERE IdFormato=12
	--formatos nuevos
	UPDATE MediosMag_Formatos SET IdVersion=35 WHERE IdFormato=32
	UPDATE MediosMag_Formatos SET IdVersion=36 WHERE IdFormato=33
	UPDATE MediosMag_Formatos SET IdVersion=37 WHERE IdFormato=34
	UPDATE MediosMag_Formatos SET IdVersion=38 WHERE IdFormato=35
	--//

	--CONCEPTO 1056 V10 (IdVersion = 35)
    --//
	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 860)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (860, '5002', 'Honorarios', 'Honorarios', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 861)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (861, '5003', 'Comisiones', 'Comisiones', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 862)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (862, '5004', 'Servicios', 'Servicios', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 863)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (863, '5005', 'Arrendamientos', 'Arrendamientos', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 864)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (864, '5006', 'Intereses y rendimientos financieros causados', 'Intereses y rendimientos financieros causados', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 865)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (865, '5007', 'Compra de activos movibles (E.T.Art 60)', 'Compra de activos movibles (E.T.Art 60)', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 866)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (866, '5008', 'Compra de Activos Fijos (E.T. Art 60)', 'Compra de Activos Fijos (E.T. Art 60)', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 867)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (867, '5010', 'Pagos o Abonos en cuenta por concepto de aportes parafiscales al SENA,', 'Pagos o Abonos en cuenta por concepto de aportes parafiscales al SENA, a las Cajas de Compensación Familiar y al Instituto Colombiano de Bienestar Familiar', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 868)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (868, '5011', 'Pagos o abonos en cuenta efectuado a las empresas promotoras de salud', 'Pagos o abonos en cuenta efectuado a las empresas promotoras de salud EPS y los aportes al Sistema de Riesgos Laborales, incluidos los aportes del trabajador', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 869)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (869, '5012', 'Pagos  o abonos  en  cuenta  por concepto de aportes obligatorios para', 'Pagos  o abonos  en  cuenta  por concepto de aportes obligatorios para pensiones efectuados a los Fondos de Pensiones, incluidos los aportes del trabajador', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 870)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (870, '5013', 'Donaciones en dinero efectuadas a las entidades señaladas en los artíc', 'Donaciones en dinero efectuadas a las entidades señaladas en los artículos 125, 125-4,126-2 y 158-1 del Estatuto Tributario y la establecida en el artículo 16 de la Ley 814 de 2003, y demás que determine la ley', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 871)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (871, '5014', 'Donaciones en activos diferentes a dinero efectuadas a las entidades s', 'Donaciones en activos diferentes a dinero efectuadas a las entidades señaladas en los artículos 125, 125-4,126-2 y 158-1 del Estatuto Tributario y la establecida en el artículo 16 de la Ley 814 de 2003, y demás que determine la ley', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 872)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (872, '5015', 'El valor de los impuestos solicitados como deducción', 'El valor de los impuestos solicitados como deducción', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 873)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (873, '5016', 'Los demás costos y deducciones', 'Los demás costos y deducciones', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 874)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (874, '5018', 'Importe de las primas de reaseguros pagados o abonados en cuenta', 'Importe de las primas de reaseguros pagados o abonados en cuenta', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 875)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (875, '5019', 'El monto de las amortizaciones realizadas', 'El monto de las amortizaciones realizadas', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 876)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (876, '5020', 'Compra de activos fijos reales productivos sobre los cuales solicitó d', 'Compra de activos fijos reales productivos sobre los cuales solicitó deducción, art. 158-3 E.T. El valor acumulado pagado o abonado en cuenta', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 877)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (877, '5023', 'Valor  acumulado  de  los  pagos  o  abonos  en  cuenta  al  exterior', 'Valor  acumulado  de  los  pagos  o  abonos  en  cuenta  al  exterior  por  asistencia  técnica', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 878)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (878, '5024', 'Valor acumulado de los pagos o abonos en cuenta al exterior por marcas', 'Valor acumulado de los pagos o abonos en cuenta al exterior por marcas', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 879)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (879, '5025', 'Valor acumulado de los pagos o abonos en cuenta al exterior por patent', 'Valor acumulado de los pagos o abonos en cuenta al exterior por patentes', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 880)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (880, '5026', 'valor acumulado de los pagos o abonos en cuenta al exterior por regalí', 'valor acumulado de los pagos o abonos en cuenta al exterior por regalías', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 881)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (881, '5027', 'Valor acumulado de los pagos o abonos en cuenta al exterior por servic', 'Valor acumulado de los pagos o abonos en cuenta al exterior por servicios técnicos', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 882)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (882, '5028', 'El valor acumulado de la devolución de pagos o abonos en cuenta y rete', 'El valor acumulado de la devolución de pagos o abonos en cuenta y retenciones correspondientes a operaciones de años anteriores.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 883)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (883, '5029', 'Gastos pagados por anticipado por Compras', 'Gastos pagados por anticipado por Compras', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 884)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (884, '5030', 'Gastos pagados por anticipado por Honorarios', 'Gastos pagados por anticipado por Honorarios', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 885)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (885, '5031', 'Gastos pagados por anticipado por Comisiones', 'Gastos pagados por anticipado por Comisiones', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 886)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (886, '5032', 'Gastos pagados por anticipado por Servicios', 'Gastos pagados por anticipado por Servicios', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 887)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (887, '5033', 'Gastos pagados por anticipado por Arrendamientos', 'Gastos pagados por anticipado por Arrendamientos', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 888)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (888, '5034', 'Gastos pagados por anticipado por intereses y rendimientos financieros', 'Gastos pagados por anticipado por intereses y rendimientos financieros', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 889)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (889, '5035', 'Gastos pagados por anticipado por otros conceptos', 'Gastos pagados por anticipado por otros conceptos', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 890)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (890, '5044', 'El pago por loterías, rifas, apuestas y similares', 'El pago por loterías, rifas, apuestas y similares', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 891)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (891, '5045', 'Retención sobre ingresos de tarjetas débito y crédito', 'Retención sobre ingresos de tarjetas débito y crédito', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 892)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (892, '5046', 'Enajenación de activos fijos de personas naturales ante oficinas de tr', 'Enajenación de activos fijos de personas naturales ante oficinas de tránsito y otras entidades autorizadas', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 893)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (893, '5047', 'Importe de los siniestros por lucro cesante pagados o abonados en cuen', 'Importe de los siniestros por lucro cesante pagados o abonados en cuenta', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 894)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (894, '5048', 'Importe de los siniestros por daño emergente pagados o abonados en cue', 'Importe de los siniestros por daño emergente pagados o abonados en cuenta', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 895)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (895, '5053', 'Retenciones practicas a titulo de timbre', 'Retenciones practicas a titulo de timbre', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 896)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (896, '5054', 'La devolución de retenciones a título de impuesto de timbre, correspon', 'La devolución de retenciones a título de impuesto de timbre, correspondientes a operaciones de años anteriores', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 897)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (897, '5055', 'Viaticos', 'Viaticos', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 898)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (898, '5056', 'Gastos de representacion', 'Gastos de representacion', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 899)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (899, '5058', 'Valor de los Aportes, Tasas y Contribuciones solicitado como deducción', 'Valor de los Aportes, Tasas y Contribuciones solicitado como deducción', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 900)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (900, '5059', 'El  pago  o  abono  en cuenta  realizado  a  cada  uno  de  los  coope', 'El  pago  o  abono  en cuenta  realizado  a  cada  uno  de  los  cooperados,  del  valor  del  Fondo  para revalorización de aportes', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 901)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (901, '5060', 'Redención de inversiones en lo que corresponde al reembolso del capita', 'Redención de inversiones en lo que corresponde al reembolso del capital por titulos de capitalizacion', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 902)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (902, '5061', 'Utilidades Pagadas o abonadas en cuenta, cuando el beneficiario es dif', 'Utilidades Pagadas o abonadas en cuenta, cuando el beneficiario es diferente al fideicomitente', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 903)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (903, '5063', 'Intereses y rendimientos financieros efectivamente pagados', 'Intereses y rendimientos financieros efectivamente pagados', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 904)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (904, '5064', 'Devoluciones de saldos de aportes pensionales pagados', 'Devoluciones de saldos de aportes pensionales pagados', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 905)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (905, '5065', 'Excedentes pensionales de libre disponibilidad componente de capital p', 'Excedentes pensionales de libre disponibilidad componente de capital pagados', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 906)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (906, '5066', 'El valor del impuesto nacional al consumo', 'El valor del impuesto nacional al consumo', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 907)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (907, '5067', 'El valor acumulado de los pagos o abonos en cuenta al exterior por con', 'El valor acumulado de los pagos o abonos en cuenta al exterior por consultoría', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 908)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (908, '5068', 'Participaciones o dividendos pagados o abonados en cuenta en calidad d', 'Participaciones o dividendos pagados o abonados en cuenta en calidad de exigibles correspondientes a 2016 y anteriores, parágrafo 2 artículo 49 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 909)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (909, '5069', 'Participaciones o dividendos pagados o abonados en cuenta en calidad d', 'Participaciones o dividendos pagados o abonados en cuenta en calidad de exigibles correspondientes a 2016 y anteriores, numeral 3 del artículo 49 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 910)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (910, '5070', 'Participaciones o dividendos pagados o abonados en cuenta en calidad d', 'Participaciones o dividendos pagados o abonados en cuenta en calidad de exigibles correspondientes a 2017 y siguientes, parágrafo 2 artículo 49 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 911)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (911, '5071', 'Participaciones o dividendos pagados o abonados en cuenta en calidad d', 'Participaciones o dividendos pagados o abonados en cuenta en calidad de exigibles correspondientes a 2017 y siguientes, numeral 3 del artículo 49 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 912)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (912, '5072', 'El importe de los siniestros por seguros de vida pagados o abonados en', 'El importe de los siniestros por seguros de vida pagados o abonados en cuenta', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 913)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (913, '5073', 'Desembolsos por depósitos judiciales. Se debe identificar al beneficia', 'Desembolsos por depósitos judiciales. Se debe identificar al beneficiario', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 914)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (914, '5074', 'Desembolsos por reintegros de depósitos judiciales. Se debe identifica', 'Desembolsos por reintegros de depósitos judiciales. Se debe identificar al beneficiario', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 915)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (915, '5075', 'Regalías y explotación de la propiedad intelectual', 'Regalías y explotación de la propiedad intelectual', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 916)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (916, '5076', 'Valor de utilidades distribuidas provenientes de diferimiento de ingre', 'Valor de utilidades distribuidas provenientes de diferimiento de ingresos, inciso 4 del art. 23-1 del E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 917)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (917, '5079', 'Intereses por deuda a vinculados en subcapitalización art.118-1 E.T.', 'Intereses por deuda a vinculados en subcapitalización art.118-1 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 918)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (918, '5080', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por servicios audiovisuales digitales (entre otros, de música, videos, películas y juegos de cualquier tipo, así como la radiodifusión de cualquier tipo de evento).Art.437-2 nu', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 919)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (919, '5081', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por servicios prestados a través de plataformas digitales. Art.437-2 num.8 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 920)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (920, '5082', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por suministro de servicios de publicidad online. Art.437-2 num.8 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 921)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (921, '5083', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por suministro de enseñanza o entrenamiento a distancia. Art.437-2 num.8 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 922)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (922, '5084', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por suministro de derechos de uso o explotación de intangibles. Art.437-2 num.8 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 923)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (923, '5085', 'El valor acumulado de los pagos o abonos efectuados a proveedores del', 'El valor acumulado de los pagos o abonos efectuados a proveedores del exterior por Otros servicios electrónicos o digitales con destino a usuarios ubicados en Colombia. Art.437-2 num.8 E.T.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 924)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (924, '5086', 'Valor retención en la fuente trasladada a terceros por participaciones', 'Valor retención en la fuente trasladada a terceros por participaciones o dividendos recibidos de sociedades nacionales E.T. art. 242-1 par.1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 925)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (925, '5087', 'Valor de los puntos premio redimidos en el período que afectan el gast', 'Valor de los puntos premio redimidos en el período que afectan el gasto directamente, procedentes de programas de fidelización.', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 926)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (926, '5088', 'Costos y gastos por diferencia en cambio.Se debe repoortar con el NIT', 'Costos y gastos por diferencia en cambio.Se debe repoortar con el NIT del informante', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 927)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (927, '5089', 'Valor Compra de acciones de sociedades que no cotizan en bolsa', 'Valor Compra de acciones de sociedades que no cotizan en bolsa', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 928)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (928, '5090', 'Valor Donación de acciones de sociedades que no cotizan en bolsa', 'Valor Donación de acciones de sociedades que no cotizan en bolsa', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 929)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (929, '5091', 'Valor cesión de acciones de sociedades que no cotizan en bolsa', 'Valor cesión de acciones de sociedades que no cotizan en bolsa', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 930)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (930, '5092', 'Valor del pago que constituye ingreso en especie para el beneficio Art', 'Valor del pago que constituye ingreso en especie para el beneficio Art 29-1E. T', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 931)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (931, '5093', 'Donaciones de bebidas ultraprocesadas y azucaradas a los bancos de ali', 'Donaciones de bebidas ultraprocesadas y azucaradas a los bancos de alimentos por los responsables del impuesto a estos bienes ET art 513-1, par 5 adicionado por el art 54 L 2277/2022', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 932)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (932, '5094', 'Donaciones de productos comestibles ultraprocesados industrialmente y/', 'Donaciones de productos comestibles ultraprocesados industrialmente y/o con alto contenido de azucares añadidos, sodio o grasas saturadas a los bancos de alimentos por los responsables del impuesto a estos bienes ET art 513-1, par 5 adicionado por el art', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 933)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (933, '5095', 'Pago por concepto de regalías pagadas a la Nación u otros entes territ', 'Pago por concepto de regalías pagadas a la Nación u otros entes territoriales por la explotación de hidrocarburos por entes diferentes a los organismos descentralizados. ET art 115 par 1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 934)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (934, '5096', 'Pago por concepto de regalías pagadas a la Nación u otros entes territ', 'Pago por concepto de regalías pagadas a la Nación u otros entes territoriales por la explotación de gas por entes diferentes a los organismos descentralizados. ET art 115 par 1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 935)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (935, '5097', 'Pago por concepto de regalías pagadas a la nación u otros entes territ', 'Pago por concepto de regalías pagadas a la nación u otros entes territoriales por la explotación de carbón por entes diferentes a los organismos descentralizados. ET art 115 par 1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 936)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (936, '5098', 'Pago por concepto de regalías pagadas a la Nación u otros entes territ', 'Pago por concepto de regalías pagadas a la Nación u otros entes territoriales por la explotación de otros minerales y piedras preciosas por entes diferentes a los organismos descentralizados. ET art 115 par 1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 937)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (937, '5099', 'Pago por concepto de regalías pagadas a la Nación u otros entes territ', 'Pago por concepto de regalías pagadas a la Nación u otros entes territoriales por la explotación de sal y materiales de construcción por entes diferentes a los organismos descentralizados. ET art 115 par 1', 32, 1, 0);

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 938)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (938, '5100', 'Valor del Pago o abono en cuenta por prima en colocación de acciones y', 'Valor del Pago o abono en cuenta por prima en colocación de acciones y otros conceptos patrimoniales, sin incluir los reembolsos de aportes.', 32, 1, 0);
	--//

	--CONCEPTO 1647 V2 (IdVersion = 36)
    --//
	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 939)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (939, '4070', 'Ingresos recibidos para terceros', 'Ingresos recibidos para terceros', 33, 1, 0);
	--//

	--CONCEPTO 2275 V2 (IdVersion = 37)
    --//
	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 940)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (940, '8001', 'Ingresos no constitutivos por dividendos y participaciones. E.T., art.', 'Ingresos no constitutivos por dividendos y participaciones. E.T., art. 48. (Modificado. L. 1819/2016, art. 2).', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 941)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (941, '8002', 'Ingresos no constitutivos por componente inflacionario de los rendimie', 'Ingresos no constitutivos por componente inflacionario de los rendimientos financieros. E.T. art. 38 al 40.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 942)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (942, '8005', 'Ingresos no constitutivos por la utilidad en enajenación de acciones.', 'Ingresos no constitutivos por la utilidad en enajenación de acciones. E.T., art. 36-1, incisos 2 y 3.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 943)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (943, '8006', 'Ingresos no constitutivos por utilidades provenientes de la negociació', 'Ingresos no constitutivos por utilidades provenientes de la negociación de derivados. E.T., art. 36-1, inciso 4.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 944)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (944, '8008', 'Ingresos no constitutivos por las indemnizaciones en virtud de seguros', 'Ingresos no constitutivos por las indemnizaciones en virtud de seguros de daño. E.T., art. 45.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 945)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (945, '8009', 'Ingresos no constitutivos por las indemnizaciones por destrucción o re', 'Ingresos no constitutivos por las indemnizaciones por destrucción o renovación de cultivos, y por control de plagas. E.T., art. 46-1.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 946)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (946, '8010', 'Ingresos no constitutivos por los aportes de entidades estatales, sobr', 'Ingresos no constitutivos por los aportes de entidades estatales, sobretasas e impuestos para financiamiento de sistemas de servicio público de transporte masivo de pasajeros. E.T., art. 53.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 947)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (947, '8011', 'Ingresos no constitutivos percibidos por las organizaciones regionales', 'Ingresos no constitutivos percibidos por las organizaciones regionales de televisión y audiovisuales provenientes de la Comisión Nacional de Televisión. L. 488/98, art. 40.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 948)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (948, '8013', 'Ingresos no constitutivos por la liberación de la reserva de que trata', 'Ingresos no constitutivos por la liberación de la reserva de que trata el numeral 12 del artículo Art. 290 E.T., Reserva de que trataba el 130 E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 949)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (949, '8014', 'Ingresos no constitutivos provenientes del Incentivo a la Capitalizaci', 'Ingresos no constitutivos provenientes del Incentivo a la Capitalización Rural, (ICR). E.T., art. 52.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 950)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (950, '8016', 'Ingresos no constitutivos por la retribución como recompensa. E.T., ar', 'Ingresos no constitutivos por la retribución como recompensa. E.T., art. 42.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 951)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (951, '8017', 'Ingresos no constitutivos por la utilidad en la enajenación voluntaria', 'Ingresos no constitutivos por la utilidad en la enajenación voluntaria de bienes expropiados. L. 388/97, art. 67, par. 2', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 952)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (952, '8019', 'Ingresos no constitutivos por aportes al sistema general de pensiones.', 'Ingresos no constitutivos por aportes al sistema general de pensiones. Art. 55 E.T. adicionado L. 1819/2016, art. 13)', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 953)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (953, '8025', 'Ingresos no constitutivos por distribución de utilidades por liquidaci', 'Ingresos no constitutivos por distribución de utilidades por liquidación de sociedades limitadas. E.T., art. 51.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 954)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (954, '8026', 'Ingresos no constitutivos por donaciones recibidas para partidos, movi', 'Ingresos no constitutivos por donaciones recibidas para partidos, movimientos y campañas políticas. E.T., Art. 47-1.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 955)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (955, '8028', 'Ingresos no constitutivos por la utilidad en procesos de capitalizació', 'Ingresos no constitutivos por la utilidad en procesos de capitalización. L. 789/2002, art. 44.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 956)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (956, '8029', 'Ingresos no constitutivos recibidos por el contribuyente para ser dest', 'Ingresos no constitutivos recibidos por el contribuyente para ser destinados al desarrollo de proyectos calificados como de carácter científico, tecnológico o de inversión. Art. 57-2 E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 957)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (957, '8030', 'Ingresos no constitutivos recursos administrados por Fogafin. E.T., ar', 'Ingresos no constitutivos recursos administrados por Fogafin. E.T., art. 19-3, inciso 1.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 958)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (958, '8032', 'Ingresos no constitutivos por gananciales. E.T., art. 47.', 'Ingresos no constitutivos por gananciales. E.T., art. 47.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 959)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (959, '8033', 'Ingresos no constitutivos por capitalización de utilidades en ajustes', 'Ingresos no constitutivos por capitalización de utilidades en ajustes por inflación o componente inflacionario. Art. 50 E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 960)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (960, '8034', 'Ingresos no constitutivos remuneración labores de carácter científico,', 'Ingresos no constitutivos remuneración labores de carácter científico, tecnológico o innovación. E.T., art. 57 -2.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 961)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (961, '8035', 'Ingresos no constitutivos por apoyos económicos entregados como capita', 'Ingresos no constitutivos por apoyos económicos entregados como capital semilla. L. 1429/10, art. 16.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 962)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (962, '8036', 'Ingresos no constitutivos por recursos recibidos por aportes de la nac', 'Ingresos no constitutivos por recursos recibidos por aportes de la nación a entidades públicas en liquidación. L. 633/2000, art. 77.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 963)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (963, '8037', 'Ingresos no constitutivos por componente inflacionario o mantenimiento', 'Ingresos no constitutivos por componente inflacionario o mantenimiento de valor de títulos emitidos en proceso de titularización de cartera hipotecaria. L. 546/1999, art. 16, inciso 4.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 964)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (964, '8041', 'Ingresos no constitutivos por enajenación de inmuebles. L 9/1989, art.', 'Ingresos no constitutivos por enajenación de inmuebles. L 9/1989, art. 15, modificado por la L. 3 de 1991, art. 35.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 965)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (965, '8042', 'Ingresos no constitutivos por dividendos y beneficios distribuidos por', 'Ingresos no constitutivos por dividendos y beneficios distribuidos por la ECE. Art. 893 E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 966)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (966, '8043', 'Ingresos no constitutivos por rentas o ganancias ocasionales por enaje', 'Ingresos no constitutivos por rentas o ganancias ocasionales por enajenación de acciones o participaciones en la ECE. Inc. 2, Art. 893 E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 967)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (967, '8044', 'Ingresos no constitutivos por Certificados de Incentivo Forestal. L. 1', 'Ingresos no constitutivos por Certificados de Incentivo Forestal. L. 139/1994, art. 8, literal c).', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 968)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (968, '8045', 'Ingresos no constitutivos por aportes obligatorios al sistema general', 'Ingresos no constitutivos por aportes obligatorios al sistema general de salud. Art. 56 E.T. (Adicionado, L. 1819/2016, art. 14)', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 969)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (969, '8046', 'Ingresos no constitutivos por premios obtenidos en virtud del Premio F', 'Ingresos no constitutivos por premios obtenidos en virtud del Premio Fiscal que trata el art. 618-1 del E.T.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 970)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (970, '8047', 'Ingresos no constitutivos por contraprestación por la producción de ob', 'Ingresos no constitutivos por contraprestación por la producción de obras cinematográficas. L. 1556/2012, art. 9 y D.R. 437/2013, art. 8', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 971)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (971, '8048', 'Ingresos no constitutivos por donaciones Protocolo Montreal.L.488/1998', 'Ingresos no constitutivos por donaciones Protocolo Montreal.L.488/1998, art. 32.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 972)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (972, '8049', 'Ingresos no constitutivos por apoyos económicos entregados por el Esta', 'Ingresos no constitutivos por apoyos económicos entregados por el Estado o financiados con recursos públicos. Art. 46, modificado por L. 1819/2016, art. 11.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 973)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (973, '8050', 'Ingreso no constitutivo por utilidades repartidas a través de acciones', 'Ingreso no constitutivo por utilidades repartidas a través de acciones a trabajadores de sociedades de Beneficio de Interés Colectivo – BIC Art. 44 Ley 789 de 2002.', 34, 1, 0)

	IF NOT EXISTS (SELECT 1 FROM MediosMag_Conceptos WHERE IdConcepto = 974)
	INSERT INTO MediosMag_Conceptos (IdConcepto, CodConcepto, NomConcepto, Nota, IdFormato, EsActivo, ValorBase)
	VALUES (974, '8051', 'Ingresos no constitutivos por aplicación de algún convenio para evitar', 'Ingresos no constitutivos por aplicación de algún convenio para evitar la doble tributación', 34, 1, 0)
	--//


	--CONCEPTO AGRUPACION DE -- 1056 V10 (IdVersion = 35)
	--//
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 860)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 860, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 861)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 861, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 862)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 862, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 863)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 863, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 864)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 864, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 865)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 865, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 866)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 866, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 867)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 867, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 868)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 868, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 869)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 869, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 870)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 870, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 871)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 871, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 872)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 872, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 873)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 873, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 874)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 874, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 875)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 875, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 876)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 876, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 877)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 877, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 878)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 878, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 879)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 879, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 880)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 880, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 881)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 881, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 882)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 882, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 883)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 883, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 884)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 884, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 885)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 885, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 886)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 886, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 887)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 887, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 888)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 888, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 889)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 889, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 890)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 890, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 891)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 891, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 892)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 892, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 893)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 893, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 894)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 894, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 895)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 895, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 896)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 896, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 897)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 897, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 898)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 898, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 899)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 899, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 900)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 900, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 901)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 901, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 902)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 902, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 903)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 903, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 904)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 904, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 905)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 905, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 906)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 906, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 907)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 907, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 908)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 908, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 909)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 909, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 910)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 910, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 911)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 911, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 912)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 912, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 913)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 913, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 914)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 914, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 915)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 915, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 916)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 916, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 917)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 917, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 918)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 918, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 919)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 919, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 920)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 920, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 921)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 921, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 922)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 922, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 923)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 923, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 924)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 924, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 925)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 925, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 926)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 926, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 927)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 927, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 928)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 928, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 929)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 929, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 930)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 930, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 931)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 931, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 932)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 932, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 933)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 933, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 934)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 934, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 935)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 935, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 936)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 936, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 937)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 937, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 35 AND IdConcepto = 938)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (35, 938, 0, 0)
	--//

	--CONCEPTO AGRUPACION DE -- 1047 V2 (IdVersion = 36)
	--//
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 36 AND IdConcepto = 939)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (36, 939, 0, 0)
	--//

	--CONCEPTO AGRUPACION DE -- 2275 V2 (IdVersion = 37)
	--//
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 940)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 940, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 941)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 941, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 942)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 942, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 943)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 943, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 944)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 944, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 945)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 945, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 946)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 946, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 947)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 947, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 948)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 948, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 949)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 949, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 950)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 950, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 951)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 951, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 952)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 952, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 953)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 953, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 954)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 954, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 955)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 955, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 956)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 956, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 957)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 957, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 958)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 958, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 959)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 959, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 960)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 960, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 961)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 961, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 962)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 962, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 963)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 963, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 964)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 964, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 965)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 965, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 966)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 966, 0, 0)

	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 967)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 967, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 968)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 968, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 969)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 969, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 970)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 970, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 971)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 971, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 972)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 972, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 973)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 973, 0, 0)
	
	IF NOT EXISTS (SELECT * FROM MediosMag_ConceptosAgrupacion WHERE IdVersion = 37 AND IdConcepto = 974)
	INSERT INTO MediosMag_ConceptosAgrupacion (IdVersion, IdConcepto, IdAgrup, IdAgrupPers)
	VALUES (37, 974, 0, 0)
	--//

GO

GO
PRINT N'Update complete.';


GO

--Actualizar datos de la version
INSERT INTO ZiurVersiones (IdVersion, NumVersion, Descripcion)
VALUES (377,967,'Radian y api rest')
GO

COMMIT
GO
