ALTER PROCEDURE [dbo].[sp_venCentralizacion_PRIXUS]
(@fecini VARCHAR(10), 
 @fecfin VARCHAR(10),
 @unidad int = 0,
 @correIni INT = 0,
 @correFin Int = 99999999,
 @tipoDocto INT = 0
)
AS

     --set nocount on
     --DROP TABLE #TEMP_COMPROX
     --DROP TABLE #TEMP_COMPRO3

DECLARE @tipoConta INT;
DECLARE @ctaclientes INT;
DECLARE @ctaiva INT;
DECLARE @ctadescuentos INT;
DECLARE @ctaimpadic INT;
DECLARE @ctaexento INT;
DECLARE @ctaafecto INT;
DECLARE @ctaclientesexport INT;
DECLARE @periodoventa INT;
DECLARE @ctaexentoport INT;
DECLARE @ctafacturacompra INT;
DECLARE @ctaclientesBoleta INT;
DECLARE @ctaafectoBoleta INT;
DECLARE @poranalisis INT= 0;
DECLARE @cuentacosto INT= 0;
DECLARE @cuentaexistencia INT= 0;
DECLARE @centraCondicionPago INT = 0;
DECLARE @porUniNeg BIT = 0;


SELECT  @tipoConta = ctaafecto, 
        @periodoventa = año, 
        @ctaclientes = ctaclientes, 
        @ctaiva = ctaiva, 
        @ctadescuentos = ctadescuentos, 
        @ctaimpadic = ctaimpadic, 
        @ctaexento = ctaexento, 
        @ctaafecto = ctaafecto, 
        @ctaclientesexport = ctaclientesexport, 
        @ctaexentoport = ctaexentoexport, 
        @ctafacturacompra = ctaivaretenido, 
        @ctaclientesBoleta = ctaclientesBoleta, 
        @ctaafectoBoleta = ctaafectoBoleta, 
        @poranalisis = centraporanalisis, 
        @cuentacosto = CUENTAcosto, 
        @cuentaexistencia = CUENTAexistencia,
		@centraCondicionPago = centralizacionCondicionPago,
		@porUniNeg = centralizacionUnidadNegocio
FROM venParametros;

--DROP TABLE #TEMP_COMPRO

SELECT  1 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN venEncabezado.codsii IN(101, 110)
			THEN @ctaclientesexport
			WHEN venEncabezado.codsii IN(35, 39, 48) AND  @centraCondicionPago = 0
			THEN @ctaclientesBoleta
			WHEN venEncabezado.codsii IN(35, 39, 48) AND  @centraCondicionPago = 1 
			THEN
			(
				SELECT TOP 1 cuentacontable
				FROM venCondicionPago
				WHERE codpag = venEncabezado.codpag
			)
			WHEN @centraCondicionPago = 1
			THEN
			(
				SELECT TOP 1 cuentacontable
				FROM venCondicionPago
				WHERE codpag = venEncabezado.codpag
			)
		ELSE @ctaclientes
		END AS cuenta, 
		'' AS descripcion, 
		venEncabezado.total AS debe, 
		0 AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem as item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
INTO #TEMP_COMPRO
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.total > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  2 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE 
			WHEN @porUniNeg = 1
			THEN conUnidadNegocio.ctacontable
			ELSE
				CASE
					WHEN @ctadescuentos > 0
					THEN @ctadescuentos 
										--(
										----CASE WHEN @poranalisis = 0 THEN @ctadescuentos
										----ELSE dbo.cuentaanalisis(codana) 				   
										----END
										--)
					ELSE  --SIN CUENTA DESCUENTO
					(CASE
						WHEN @poranalisis = 0
						THEN(CASE
								WHEN
								(
									SELECT COUNT(codsii)
									FROM venDocumentos D
									WHERE D.codsii = venencabezado.codsii
									AND D.afecto = 'F'
								) > 0
								THEN @ctaexento
								ELSE 
								( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
									FROM venCondicionPago
									WHERE codpag = venEncabezado.codpag)
								ELSE
								@ctaafecto
								END)
							END)
						ELSE dbo.cuentaanalisis(codana)
					 END)
				END
		END AS cuenta, 
		'' AS descripcion, 
		venEncabezado.descuentos AS debe, 
		0 AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		codItem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		venEncabezado.monpar, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado  INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.descuentos > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  2 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE 
			WHEN @porUniNeg = 1
			THEN conUnidadNegocio.ctacontable
			ELSE
				CASE
					WHEN @ctadescuentos > 0
					THEN @ctadescuentos
					ELSE  --SIN CUENTA DESCUENTO
					(CASE
						WHEN @poranalisis = 0
						THEN(CASE
								WHEN
								(
									SELECT COUNT(codsii)
									FROM venDocumentos D
									WHERE D.codsii = venencabezado.codsii
									AND D.afecto = 'F'
								) > 0
								THEN @ctaexento
								ELSE ( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
									FROM venCondicionPago
									WHERE codpag = venEncabezado.codpag)
								ELSE
								@ctaafecto
								END)
							END)
						ELSE dbo.cuentaanalisis(codana)
					END)
				END
		END AS cuenta, 
		'' AS descripcion,
		0 AS debe, 
		venEncabezado.descuentos AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado  INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen
WHERE(venEncabezado.numcom = 0)
    AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.descuentos > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  3 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
		WHEN @porUniNeg = 1
		THEN conUnidadNegocio.ctacontable
		ELSE (CASE
				WHEN @poranalisis = 1
				THEN dbo.cuentaanalisis(codana)
				ELSE (CASE
						WHEN venEncabezado.codsii IN(101, 110)
						THEN @ctaexentoport
						ELSE @ctaexento
						END)
				END)
		END AS cuenta, 
		'' AS descripcion, 
		0 AS debe, 
		venEncabezado.exento + venEncabezado.neto AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado  INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen

WHERE(venEncabezado.numcom = 0)
    AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.exento > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  5 AS n, 
		0 AS tipo_ingreso, 
		MIN(MONTH(venEncabezado.fecdoc)) AS mes, 
		MIN(YEAR(venEncabezado.fecdoc)) AS periodo, 
		0 AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		0 AS tipo_documento, 
		@Ctaiva AS cuenta, 
		'' AS descripcion, 
		0 AS debe, 
		CONVERT(DECIMAL(18,4),SUM(venEncabezado.iva)) AS haber, 
		0 AS numlin, 
		0 AS ctacte, 
		coditem AS item, 
		0 AS tipo_docto, 
		0 AS num_docto, 
		MIN(venEncabezado.fecven) AS vencimiento, 
		0 AS analisis,
		@unidad AS uni_negocio, 
		0 AS moneda, 
		0 AS tasa_cambio, 
		'IVA' AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.grudoc = 10)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.iva > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)
GROUP BY coditem
HAVING(MIN(MONTH(venEncabezado.fecdoc)) IS NOT NULL)

UNION ALL

SELECT  6 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE 
			WHEN @porUniNeg = 1
			THEN conUnidadNegocio.ctacontable
			ELSE
				CASE
					WHEN @poranalisis = 1
					THEN dbo.cuentaanalisis(codana)
					ELSE CASE
							WHEN venEncabezado.codsii IN(35, 39, 48)
							THEN @ctaafectoBoleta
							ELSE ( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
									FROM venCondicionPago
									WHERE codpag = venEncabezado.codpag)
								ELSE
								@ctaafecto
							END)
						 END
			 END
		END AS cuenta, 
		'' AS descripcion, 
		0 AS debe, 
		venEncabezado.descuentos AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado  INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.descuentos > 0)
AND (venEncabezado.neto >= venEncabezado.exento)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  7 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN @porUniNeg = 1
			THEN conUnidadNegocio.ctacontable
			ELSE
				CASE
					WHEN @poranalisis = 1
					THEN dbo.cuentaanalisis(codana)
					ELSE @ctaexento
					END
		END AS cuenta, 
		'' AS descripcion, 
		0 AS debe, 
		venEncabezado.descuentos AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen  
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.descuentos > 0)
AND (venEncabezado.exento > venEncabezado.neto)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  7 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN @porUniNeg = 1
			THEN conUnidadNegocio.ctacontable
			ELSE
				CASE
					WHEN @poranalisis = 1
					THEN dbo.cuentaanalisis(codana)
					ELSE(CASE
							WHEN
							(
								SELECT COUNT(codsii)
								FROM venDocumentos D
								WHERE D.codsii = venencabezado.codsii
								AND D.afecto = 'F'
							) > 0
							THEN @ctaexento
							ELSE ( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
									FROM venCondicionPago
									WHERE codpag = venEncabezado.codpag)
								ELSE
								@ctaafecto
								END)
						END)
				END
		END AS cuenta, 
		'' AS descripcion, 
		venEncabezado.descuentos AS debe, 
		0 AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado INNER JOIN
	 conUnidadNegocio ON conUnidadNegocio.idUniNeg = venEncabezado.codcen  
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.descuentos > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  5 AS n, 
        venEncabezado.coddoc AS tipo_ingreso, 
        MONTH(venEncabezado.fecdoc) AS mes, 
        YEAR(venEncabezado.fecdoc) AS periodo, 
        venEncabezado.numinv AS correlativo, 
        0 AS linea, 
        0 AS concepto, 
        venEncabezado.codsii AS tipo_documento, 
        @ctaimpadic AS cuenta, 
        '' AS descripcion, 
        0 AS debe, 
        venEncabezado.totimpadi AS haber, 
        0 AS numlin, 
        venEncabezado.rutcte AS ctacte, 
        coditem AS item, 
        venEncabezado.codsii AS tipo_docto, 
        venEncabezado.numinv AS num_docto, 
        venEncabezado.fecven AS vencimiento, 
        venEncabezado.codana AS analisis, 
        venEncabezado.codcen AS uni_negocio, 
        venEncabezado.codmon AS moneda, 
        CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
        venEncabezado.glosa AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.totimpadi > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  5 AS n, 
        venEncabezado.coddoc AS tipo_ingreso, 
        MONTH(venEncabezado.fecdoc) AS mes, 
        YEAR(venEncabezado.fecdoc) AS periodo, 
        venEncabezado.numinv AS correlativo, 
        0 AS linea, 
        0 AS concepto, 
        venEncabezado.codsii AS tipo_documento, 
        @ctaimpadic AS cuenta, 
        '' AS descripcion, 
        venEncabezado.totimpadi AS debe, 
        0 AS haber, 
        0 AS numlin, 
        venEncabezado.rutcte AS ctacte, 
        coditem AS item, 
        venEncabezado.codsii AS tipo_docto, 
        venEncabezado.numinv AS num_docto, 
        venEncabezado.fecven AS vencimiento, 
        venEncabezado.codana AS analisis, 
        venEncabezado.codcen AS uni_negocio, 
        venEncabezado.codmon AS moneda, 
        CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
        venEncabezado.glosa AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.totimpadi > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT  8 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN @centraCondicionPago = 1
			THEN
				(
					SELECT TOP 1 cuentacontable
					FROM venCondicionPago
					WHERE codpag = venEncabezado.codpag
				)
			WHEN venEncabezado.codsii IN(35, 39, 48) THEN @ctaclientesBoleta
			WHEN venEncabezado.codsii = 61 AND venEncabezado.tipref1=39 THEN @ctaclientesBoleta
			ELSE @ctaclientes
		END AS cuenta, 
		'' AS descripcion, 
		0 AS debe, 
		venEncabezado.total AS haber, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.total > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

UNION ALL

SELECT 3 AS n, 
            0 AS tipo_ingreso, 
            MIN(MONTH(venEncabezado.fecdoc)) AS mes, 
            MIN(YEAR(venEncabezado.fecdoc)) AS periodo, 
            0 AS correlativo, 
            0 AS linea, 
            0 AS concepto, 
            0 AS tipo_documento, 
            @ctaiva AS cuenta, 
            '' AS descripcion, 
            CONVERT(DECIMAL(18,4),SUM(venEncabezado.iva)) AS debe, 
            0 AS haber, 
            0 AS numlin, 
            0 AS ctacte, 
            coditem AS item, 
            0 AS tipo_docto, 
            0 AS num_docto, 
            MIN(venEncabezado.fecven) AS vencimiento, 
            0 AS analisis,
            @unidad AS uni_negocio, 
            0 AS moneda, 
            0 AS tasa_cambio, 
            'IVA' AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.grudoc = 10)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.iva > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)
GROUP BY coditem
HAVING(MIN(MONTH(venEncabezado.fecdoc)) IS NOT NULL)

UNION ALL

SELECT  5 AS n, 
        0 AS tipo_ingreso, 
        MIN(MONTH(venEncabezado.fecdoc)) AS mes, 
        MIN(YEAR(venEncabezado.fecdoc)) AS periodo, 
        0 AS correlativo, 
        0 AS linea, 
        0 AS concepto, 
        0 AS tipo_documento, 
        @ctafacturacompra AS cuenta, 
        '' AS descripcion, 
        CONVERT(DECIMAL(18,4),SUM(venEncabezado.neto * (ivaret / 100.000))) AS debe, 
        0 AS haber, 
        0 AS numlin, 
        0 AS ctacte, 
        coditem AS item, 
        0 AS tipo_docto, 
        0 AS num_docto, 
        MIN(venEncabezado.fecven) AS vencimiento, 
        0 AS analisis, 
        0 AS uni_negocio, 
        0 AS moneda, 
        0 AS tasa_cambio, 
        'IVA RETENIDO FACTURA' AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.grudoc = 10)
AND (venEncabezado.codsii IN(45, 46))
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (ivaret > 0
AND neto > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)
GROUP BY coditem
HAVING(MIN(MONTH(venEncabezado.fecdoc)) IS NOT NULL)

UNION ALL

SELECT  14 AS n, 
        0 AS tipo_ingreso, 
        MIN(MONTH(venEncabezado.fecdoc)) AS mes, 
        MIN(YEAR(venEncabezado.fecdoc)) AS periodo, 
        0 AS correlativo, 
        0 AS linea, 
        0 AS concepto, 
        0 AS tipo_documento, 
        @ctafacturacompra AS cuenta, 
        '' AS descripcion, 
        0 AS debe, 
        CONVERT(DECIMAL(18,4),SUM(venEncabezado.neto * (ivaret / 100.000))) AS haber, 
        0 AS numlin, 
        0 AS ctacte, 
        coditem AS item, 
        0 AS tipo_docto, 
        0 AS num_docto, 
        MIN(venEncabezado.fecven) AS vencimiento, 
        0 AS analisis, 
        0 AS uni_negocio, 
        0 AS moneda, 
        0 AS tasa_cambio, 
        'IVA RETENIDO NC' AS glosa_linea
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.tipref1 IN(45, 46))
AND (ivaret > 0
AND neto > 0)
AND esCompra = 0
AND (venencabezado.codcen =@unidad or @unidad=0)
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)
GROUP BY coditem
HAVING(MIN(MONTH(venEncabezado.fecdoc)) IS NOT NULL)
ORDER BY correlativo, n, haber;





--DROP TABLE #NC

SELECT  20 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN tipref1 IN(101, 110)
			THEN @ctaclientesexport
			WHEN tipref1 IN(35, 39, 48)
			THEN @ctaclientesBoleta
			WHEN @centraCondicionPago = 1
			THEN
		(
		SELECT TOP 1 cuentacontable
		FROM venCondicionPago
		WHERE codpag = venEncabezado.codpag
		)
			ELSE @ctaclientes
		END AS cuenta, 
		'' AS descripcion, 
		venEncabezado.total, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem AS item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea, 
		docref1, 
		tipref1
INTO #NC
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii IN(60, 61, 106, 112))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.total > 0)
AND (venencabezado.codcen =@unidad or @unidad=0)
AND esCompra = 0
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0);


--DROP TABLE #FACTURANOTACREDITO

SELECT  n, 
		tipo_ingreso, 
		mes, 
		periodo, 
		correlativo, 
		linea, 
		0 AS concepto, 
		tipo_documento, 
		cuenta, 
		descripcion, 
		total AS debe, 
		0 AS haber, 
		0 AS numlin, 
		ctacte, 
		item, 
		tipo_docto, 
		num_docto, 
		vencimiento, 
		analisis, 
		uni_negocio, 
		moneda, 
		tasa_cambio, 
		glosa_linea, 
		docref1 AS ORDEN
INTO #FACTURANOTACREDITO
FROM #NC

UNION ALL

SELECT  n, 
		tipo_ingreso, 
		mes, 
		periodo, 
		docref1 AS correlativo, 
		linea, 
		0 AS concepto, 
		TIPREF1 AS tipo_documento, 
		cuenta, 
		descripcion, 
		0 AS debe, 
		total AS haber, 
		0 AS numlin, 
		ctacte, 
		item, 
		TIPREF1 AS tipo_docto, 
		docref1 AS num_docto, 
		vencimiento, 
		analisis, 
		uni_negocio, 
		moneda, 
		tasa_cambio, 
		glosa_linea, 
		docref1 AS ORDEN
FROM #NC
WHERE(#NC.tipref1 NOT IN(55, 56, 60, 61, 104, 111, 106, 112));


--DROP TABLE #ND

SELECT  21 AS n, 
		venEncabezado.coddoc AS tipo_ingreso, 
		MONTH(venEncabezado.fecdoc) AS mes, 
		YEAR(venEncabezado.fecdoc) AS periodo, 
		venEncabezado.numinv AS correlativo, 
		0 AS linea, 
		0 AS concepto, 
		venEncabezado.codsii AS tipo_documento,
		CASE
			WHEN tipref1 IN(101, 110)
			THEN @ctaclientesexport
			WHEN tipref1 IN(35, 39, 48)
			THEN @ctaclientesBoleta
			WHEN @centraCondicionPago = 1
			THEN
		(
		SELECT TOP 1 cuentacontable
		FROM venCondicionPago
		WHERE codpag = venEncabezado.codpag
		)
			ELSE @ctaclientes
		END AS cuenta, 
		'' AS descripcion, 
		venEncabezado.total, 
		0 AS numlin, 
		venEncabezado.rutcte AS ctacte, 
		coditem as item, 
		venEncabezado.codsii AS tipo_docto, 
		venEncabezado.numinv AS num_docto, 
		venEncabezado.fecven AS vencimiento, 
		venEncabezado.codana AS analisis, 
		venEncabezado.codcen AS uni_negocio, 
		venEncabezado.codmon AS moneda, 
		CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
		venEncabezado.glosa AS glosa_linea, 
		docref1, 
		tipref1
INTO #ND
FROM venEncabezado
WHERE(venEncabezado.numcom = 0)
AND (venEncabezado.codsii IN(104, 111, 55, 56))
AND (venEncabezado.grudoc = 10)
AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
AND (venEncabezado.total > 0)
AND (venencabezado.codcen =@unidad or @unidad=0)
AND esCompra = 0
AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0);


--DROP TABLE #FACTURANOTADEBITO

SELECT  n, 
        tipo_ingreso, 
        mes, 
        periodo, 
        correlativo, 
        linea, 
        0 AS concepto, 
        tipo_documento, 
        cuenta, 
        descripcion, 
        0 AS debe, 
        total AS haber, 
        0 AS numlin, 
        ctacte, 
        item, 
        tipo_docto, 
        num_docto, 
        vencimiento, 
        analisis, 
        uni_negocio, 
        moneda, 
        tasa_cambio, 
        glosa_linea, 
        docref1 AS ORDEN
INTO #FACTURANOTADEBITO
FROM #ND

UNION ALL

SELECT  n, 
        tipo_ingreso, 
        mes, 
        periodo, 
        docref1 AS correlativo, 
        linea, 
        0 AS concepto, 
        tipref1 AS tipo_documento, 
        cuenta, 
        descripcion, 
        total AS debe, 
        0 AS haber, 
        0 AS numlin, 
        ctacte, 
        item, 
        tipref1 AS tipo_docto, 
        docref1 AS num_docto, 
        vencimiento, 
        analisis, 
        uni_negocio, 
        moneda, 
        tasa_cambio, 
        glosa_linea, 
        docref1 AS ORDEN
FROM #ND
WHERE(#ND.tipref1 NOT IN(55, 56, 60, 61, 104, 111, 106, 112));


--DROP TABLE #NOTACREDEBREF

SELECT  70 AS n, 
        tipo_ingreso, 
        ENC.mes, 
        ENC.periodo, 
        correlativo, 
        linea, 
        0 AS concepto, 
        venEncabezado.tipref1 AS tipo_documento, 
        cuenta, 
        descripcion,
        CASE
            WHEN venEncabezado.codsii IN(104, 111, 55, 56)
            THEN ENC.total
            ELSE 0
        END AS debe,
        CASE
            WHEN venEncabezado.codsii IN(60, 61, 106, 112)
            THEN ENC.total
            ELSE 0
        END AS haber, 
        0 AS numlin, 
        ctacte, 
        venEncabezado.coditem AS item, 
        venEncabezado.tipref1 AS tipo_docto, 
        venEncabezado.docref1 AS num_docto, 
        vencimiento, 
        analisis, 
        uni_negocio, 
        moneda, 
        tasa_cambio, 
        CONVERT(VARCHAR, ENC.CORRELATIVO) + '->' + CONVERT(VARCHAR, ENC.docref1) AS glosa_linea, 
        ENC.docref1 AS ORDEN
INTO #NOTACREDEBREF
FROM venEncabezado INNER JOIN 
	 #NC AS ENC ON venEncabezado.rutcte = ENC.ctacte
     AND venEncabezado.CODSII = ENC.tipref1
     AND venEncabezado.numinv = ENC.docref1
WHERE venEncabezado.grudoc = 10
AND (venEncabezado.total > 0)
AND venEncabezado.codsii IN(55, 56, 60, 61, 104, 111, 106, 112)
AND esCompra = 0

UNION ALL

SELECT  71 AS n, 
        tipo_ingreso, 
        ENC.mes, 
        ENC.periodo, 
        correlativo, 
        linea, 
        0 AS concepto, 
        venEncabezado.tipref1 AS tipo_documento, 
        cuenta, 
        descripcion,
        CASE
            WHEN venEncabezado.codsii NOT IN(104, 111, 55, 56)
            THEN ENC.total
            ELSE 0
        END AS debe,
        CASE
            WHEN venEncabezado.codsii NOT IN(60, 61, 106, 112)
            THEN ENC.total
            ELSE 0
        END AS haber, 
        0 AS numlin, 
        ctacte, 
        item, 
        venEncabezado.tipref1 AS tipo_docto, 
        venEncabezado.docref1 AS num_docto, 
        vencimiento, 
        analisis, 
        uni_negocio, 
        moneda, 
        tasa_cambio, 
        CONVERT(VARCHAR, ENC.CORRELATIVO) + '->' + CONVERT(VARCHAR, ENC.docref1) AS glosa_linea, 
        ENC.docref1 AS ORDEN
FROM venEncabezado INNER JOIN 
	 #ND AS ENC ON venEncabezado.rutcte = ENC.ctacte
     AND venEncabezado.CODSII = ENC.tipref1
     AND venEncabezado.numinv = ENC.docref1
WHERE venEncabezado.grudoc = 10
AND (venEncabezado.total > 0)
AND venEncabezado.codsii IN(55, 56, 60, 61, 104, 111, 106, 112)
AND esCompra = 0;

     IF @tipoConta > 0
         BEGIN
             --	DOCUMENTOS DISTINTOS A NOTAS DE CREDITO
             --	NETO AL HABER

             --DROP TABLE #TEMP_COMPRO_PARAMETROS

			SELECT  44 AS n, 
					venEncabezado.coddoc AS tipo_ingreso, 
					MONTH(venEncabezado.fecdoc) AS mes, 
					YEAR(venEncabezado.fecdoc) AS periodo, 
					venEncabezado.numinv AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					venEncabezado.codsii AS tipo_documento,
					CASE
						WHEN @poranalisis = 1
						THEN dbo.cuentaanalisis(codana)
						ELSE CASE
								WHEN venEncabezado.codsii IN(35, 39, 48) AND @centraCondicionPago = 0
								THEN @ctaafectoBoleta
								WHEN venEncabezado.codsii IN(35, 39, 48) AND @centraCondicionPago = 1
								THEN
									(SELECT TOP 1 cuentacontable2
									FROM venCondicionPago
									WHERE codpag = venEncabezado.codpag)
										 
									
								ELSE ( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
										FROM venCondicionPago
										WHERE codpag = venEncabezado.codpag)
										ELSE @ctaafecto
									END)
							 END
					END AS cuenta, 
					'' AS descripcion, 
					0 AS debe, 
					venEncabezado.neto AS haber, 
					0 AS numlin, 
					venEncabezado.rutcte AS ctacte, 
					coditem, 
					venEncabezado.codsii AS tipo_docto, 
					venEncabezado.numinv AS num_docto, 
					venEncabezado.fecven AS vencimiento, 
					venEncabezado.codana AS analisis, 
					venEncabezado.codcen AS uni_negocio, 
					venEncabezado.codmon AS moneda, 
					CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
					CONVERT(VARCHAR, venEncabezado.codsii) + '/' + CONVERT(VARCHAR, venEncabezado.numinv) AS glosa_linea
			INTO #TEMP_COMPRO_PARAMETROS
			FROM venEncabezado
			WHERE(venEncabezado.numcom = 0)
			AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
			AND (venEncabezado.grudoc = 10)
			AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
			AND (venEncabezado.neto > 0)
			AND esCompra = 0
			AND (venencabezado.codcen =@unidad or @unidad=0)
			AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
			AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)

			UNION ALL

			SELECT  6 AS n, 
					venEncabezado.coddoc AS tipo_ingreso, 
					MONTH(venEncabezado.fecdoc) AS mes, 
					YEAR(venEncabezado.fecdoc) AS periodo, 
					venEncabezado.numinv AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					venEncabezado.codsii AS tipo_documento,
					CASE
						WHEN @poranalisis = 1
						THEN dbo.cuentaanalisis(codana)
						ELSE CASE
								WHEN venEncabezado.codsii IN(35, 39, 48)
								THEN @ctaafectoBoleta
								ELSE ( CASE WHEN @centraCondicionPago = 1 THEN
									(SELECT TOP 1 cuentacontable2
										FROM venCondicionPago
										WHERE codpag = venEncabezado.codpag)
										ELSE @ctaafecto
									END)
							 END
					END AS cuenta, 
					'' AS descripcion, 
					(venEncabezado.neto + venEncabezado.exento) AS debe, 
					0 AS haber, 
					0 AS numlin, 
					venEncabezado.rutcte AS ctacte, 
					coditem AS item, 
					venEncabezado.codsii AS tipo_docto, 
					venEncabezado.numinv AS num_docto, 
					venEncabezado.fecven AS vencimiento, 
					venEncabezado.codana AS analisis, 
					venEncabezado.codcen AS uni_negocio, 
					venEncabezado.codmon AS moneda, 
					CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
					venEncabezado.glosa AS glosa_linea
			FROM venEncabezado
			WHERE(venEncabezado.numcom = 0)
			AND (venEncabezado.codsii IN(60, 61, 106, 112))
			AND (venEncabezado.grudoc = 10)
			AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
			AND ((venEncabezado.neto + venEncabezado.exento) > 0)
			AND (venencabezado.codcen =@unidad or @unidad=0)
			AND esCompra = 0
			AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
			AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0);



			--DROP TABLE #COSTOV

			SELECT  30 AS n, 
					0 AS tipo_ingreso, 
					MONTH(E.fecdoc) AS mes, 
					YEAR(E.fecdoc) AS periodo, 
					0 AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					0 AS tipo_documento, 
					@cuentacosto AS cuenta, 
					'' AS descripcion, 
					CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS debe, 
					0 AS haber, 
					0 AS numlin, 
					0 AS ctacte, 
					coditem AS item, 
					0 AS tipo_docto, 
					0 AS num_docto, 
					'' AS vencimiento, 
					E.codana AS analisis, 
					E.codcen AS uni_negocio, 
					E.codmon AS moneda, 
					E.monpar AS tasa_cambio, 
					E.glosa AS glosa_linea
			INTO #COSTOV
			FROM venEncabezado AS E INNER JOIN 
				 venDetalle AS D ON E.coddoc = D.coddoc
				 AND E.grudoc = D.grudoc
				 AND E.numinv = D.numinv
			WHERE(E.numcom = 0)
			AND (E.grudoc = 10)
			AND (@cuentacosto > 0)
			AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
			AND (codsii NOT IN(60, 61, 106, 112))
			AND (codsii NOT IN(104, 111, 55, 56)
			OR (codsii IN(104, 111, 55, 56)
			AND E.motivo IN(1, 4)))
			AND E.esCompra = 0
			AND (E.codcen =@unidad or @unidad=0)
			AND (E.numinv BETWEEN @correIni AND @correFin)
			AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
			GROUP BY MONTH(E.fecdoc), YEAR(E.fecdoc), E.codana, E.codcen, E.codmon, E.monpar, E.glosa, coditem;



			--DROP TABLE #EXISTENCIAV

			SELECT  31 AS n, 
					0 AS tipo_ingreso, 
					MONTH(E.fecdoc) AS mes, 
					YEAR(E.fecdoc) AS periodo, 
					0 AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					0 AS tipo_documento, 
					@cuentaEXISTENCIA AS cuenta, 
					'' AS descripcion, 
					0 AS debe, 
					CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS haber, 
					0 AS numlin, 
					0 AS ctacte, 
					coditem AS item, 
					0 AS tipo_docto, 
					0 AS num_docto, 
					'' AS vencimiento, 
					E.codana AS analisis, 
					E.codcen AS uni_negocio, 
					E.codmon AS moneda, 
					E.monpar AS tasa_cambio, 
					E.glosa AS glosa_linea
			INTO #EXISTENCIAV
			FROM venEncabezado AS E INNER JOIN 
				 venDetalle AS D ON E.coddoc = D.coddoc
				 AND E.grudoc = D.grudoc
				 AND E.numinv = D.numinv
			WHERE(E.numcom = 0)
			AND (E.grudoc = 10)
			AND @cuentaEXISTENCIA > 0
			AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
			AND (codsii NOT IN(60, 61, 106, 112))
			AND (codsii NOT IN(104, 111, 55, 56)
			OR (codsii IN(104, 111, 55, 56)
			AND E.motivo IN(1, 4)))
			AND E.esCompra = 0
			AND (E.codcen =@unidad or @unidad=0)
			AND (E.numinv BETWEEN @correIni AND @correFin)
			AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
			GROUP BY MONTH(E.fecdoc), YEAR(E.fecdoc), E.codana, E.codcen, E.codmon, E.monpar, E.glosa, coditem;




			--DROP TABLE #COSTONC

			SELECT  32 AS n, 
					0 AS tipo_ingreso, 
					MONTH(E.fecdoc) AS mes, 
					YEAR(E.fecdoc) AS periodo, 
					0 AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					0 AS tipo_documento, 
					@cuentacosto AS cuenta, 
					'' AS descripcion, 
					0 AS debe, 
					CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS haber, 
					0 AS numlin, 
					0 AS ctacte, 
					coditem AS item, 
					0 AS tipo_docto, 
					0 AS num_docto, 
					'' AS vencimiento, 
					E.codana AS analisis, 
					E.codcen AS uni_negocio, 
					E.codmon AS moneda, 
					E.monpar AS tasa_cambio, 
					E.glosa AS glosa_linea
			INTO #COSTONC
			FROM venEncabezado AS E INNER JOIN 
				 venDetalle AS D ON E.coddoc = D.coddoc
				 AND E.grudoc = D.grudoc
				 AND E.numinv = D.numinv
			WHERE(E.numcom = 0)
			AND (E.grudoc = 10)
			AND (@cuentacosto > 0)
			AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
			AND (E.codsii IN(60, 61, 106, 112)
			AND motivo IN(1, 4))
			AND E.esCompra = 0
			AND (E.codcen =@unidad or @unidad=0)
			AND (E.numinv BETWEEN @correIni AND @correFin)
			AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
			GROUP BY MONTH(E.fecdoc), YEAR(E.fecdoc), E.codana, E.codcen, E.codmon, E.monpar, E.glosa, coditem;




			--DROP TABLE #EXISTENCIANC

			SELECT  33 AS n, 
					0 AS tipo_ingreso, 
					MONTH(E.fecdoc) AS mes, 
					YEAR(E.fecdoc) AS periodo, 
					0 AS correlativo, 
					0 AS linea, 
					0 AS concepto, 
					0 AS tipo_documento, 
					@cuentaEXISTENCIA AS cuenta, 
					'' AS descripcion, 
					CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS debe, 
					0 AS haber, 
					0 AS numlin, 
					0 AS ctacte, 
					coditem AS item, 
					0 AS tipo_docto, 
					0 AS num_docto, 
					'' AS vencimiento, 
					E.codana AS analisis, 
					E.codcen AS uni_negocio, 
					E.codmon AS moneda, 
					E.monpar AS tasa_cambio, 
					E.glosa AS glosa_linea
			INTO #EXISTENCIANC
			FROM venEncabezado AS E INNER JOIN 
				 venDetalle AS D ON E.coddoc = D.coddoc
				 AND E.grudoc = D.grudoc
				 AND E.numinv = D.numinv
			WHERE(E.numcom = 0)
			AND (E.grudoc = 10)
			AND @cuentaEXISTENCIA > 0
			AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
			AND (E.codsii IN(60, 61, 106, 112)
			AND motivo IN(1, 4))
			AND E.esCompra = 0
			AND (E.codcen =@unidad or @unidad=0)
			AND (E.numinv BETWEEN @correIni AND @correFin)
			AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
			GROUP BY MONTH(E.fecdoc), 
			YEAR(E.fecdoc), E.codana, E.codcen, E.codmon, E.monpar, E.glosa, coditem;




			--DROP TABLE #TEMP_COMPRO3

			SELECT *, 
				   0 AS ORDEN
			INTO #TEMP_COMPRO3
			FROM #TEMP_COMPRO

			UNION ALL

			SELECT *, 
				0 AS ORDEN
			FROM #TEMP_COMPRO_PARAMETROS

			UNION ALL

			SELECT *
			FROM #FACTURANOTACREDITO

			UNION ALL

			SELECT *
			FROM #FACTURANOTADEBITO

			UNION ALL

			SELECT *, 
				3 AS ORDEN
			FROM #COSTOV

			UNION ALL

			SELECT *, 
				3 AS ORDEN
			FROM #EXISTENCIAV

			UNION ALL

			SELECT *, 
				3 AS ORDEN
			FROM #COSTONC

			UNION ALL

			SELECT *, 
				3 AS ORDEN
			FROM #EXISTENCIANC

			UNION ALL

			SELECT *
			FROM #NOTACREDEBREF
			ORDER BY n;



			--DROP TABLE #TODOCOMPRO

			SELECT  TEMP_COMPRO.n, 
					TEMP_COMPRO.tipo_ingreso, 
					TEMP_COMPRO.mes, 
					TEMP_COMPRO.periodo, 
					TEMP_COMPRO.correlativo, 
					TEMP_COMPRO.linea, 
					TEMP_COMPRO.concepto, 
					TEMP_COMPRO.tipo_documento, 
					TEMP_COMPRO.cuenta, 
					conPlanCuentas.descripcion, 
					CAST(TEMP_COMPRO.debe AS DECIMAL(18,4)) AS debe, 
					CAST(TEMP_COMPRO.haber AS DECIMAL(18,4)) AS haber, 
					TEMP_COMPRO.numlin,
					CASE
						WHEN conPlanCuentas.marcaCtacte = 1
						THEN TEMP_COMPRO.ctacte
						ELSE 0
					END AS ctacte,
					CASE
						WHEN conPlanCuentas.marcaItem = 1
								AND TEMP_COMPRO.analisis > 0
						THEN item
						ELSE 0
					END AS item,
					CASE
						WHEN conPlanCuentas.marcaDocumento = 1
						THEN TEMP_COMPRO.tipo_docto
						ELSE 0
					END AS tipo_docto,
					CASE
						WHEN conPlanCuentas.marcaDocumento = 1
						THEN TEMP_COMPRO.num_docto
						ELSE 0
					END AS num_docto,
					CASE
						WHEN conPlanCuentas.marcaDocumento = 1
						THEN TEMP_COMPRO.vencimiento
						ELSE '01-01-1900'
					END AS vencimiento,
					CASE
						WHEN conPlanCuentas.marcaAnalisis = 1
						THEN TEMP_COMPRO.analisis
						ELSE 0
					END AS analisis,
					CASE
						WHEN conPlanCuentas.marcaMoneda = 1
						THEN TEMP_COMPRO.moneda
						ELSE 0
					END AS moneda,
					CASE
						WHEN conPlanCuentas.marcaMoneda = 1
						THEN TEMP_COMPRO.tasa_cambio
						ELSE 0
					END AS tasa_cambio,
					CASE
						WHEN conPlanCuentas.marcaNegocio = 1
						THEN TEMP_COMPRO.uni_negocio
						ELSE 0
					END AS uni_negocio,
					CASE
						WHEN conPlanCuentas.marcaDocumento = 1
						THEN TEMP_COMPRO.glosa_linea
						ELSE ''
					END AS glosa_linea,
					CASE
						WHEN debe > 0
						THEN 0
						ELSE 1
					END AS orden
			INTO #TODOCOMPRO
			FROM #TEMP_COMPRO3 TEMP_COMPRO LEFT OUTER JOIN 
			conPlanCuentas ON TEMP_COMPRO.periodo = conPlanCuentas.periodo
			AND TEMP_COMPRO.cuenta = conPlanCuentas.cuenta;




			SELECT  n, 
					tipo_ingreso, 
					mes, 
					periodo, 
					linea, 
					concepto, 
					tipo_documento, 
					cuenta, 
					descripcion, 
					CONVERT(DECIMAL(18,4),SUM(debe)) AS debe, 
					CONVERT(DECIMAL(18,4),SUM(haber)) AS haber, 
					numlin, 
					vencimiento, 
					analisis, 
					moneda, 
					tasa_cambio, 
					uni_negocio, 
					glosa_linea, 
					num_docto, 
					item, 
					ctacte, 
					tipo_docto
			FROM #TODOCOMPRO
			GROUP BY n, 
			tipo_ingreso, 
			mes, 
			periodo, 
			linea, 
			concepto, 
			tipo_documento, 
			cuenta, 
			descripcion, 
			numlin, 
			vencimiento, 
			analisis, 
			moneda, 
			tasa_cambio, 
			uni_negocio, 
			glosa_linea, 
			num_docto, 
			item, 
			ctacte, 
			tipo_docto, 
			orden
			ORDER BY orden, cuenta;
     END;
         ELSE
         BEGIN -- CONTABILIZACION POR SUBFAMILIA
             --	DOCUMENTOS DISTINTOS A NOTAS DE CREDITO
             --	NETO AL HABER
             SELECT 4 AS n, 
                    venEncabezado.coddoc AS tipo_ingreso, 
                    MONTH(venEncabezado.fecdoc) AS mes, 
                    YEAR(venEncabezado.fecdoc) AS periodo, 
                    venEncabezado.numinv AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    venEncabezado.codsii AS tipo_documento, 
                    invSubFamilia.ctacontable AS cuenta, 
                    conPlanCuentas.descripcion, 
                    0 AS debe, 
                    (venDetalle.pretot * 1) AS haber, 
                    0 AS numlin, 
                    venEncabezado.rutcte AS ctacte, 
                    coditem AS item, 
                    venEncabezado.codsii AS tipo_docto, 
                    venEncabezado.numinv AS num_docto, 
                    venEncabezado.fecven AS vencimiento, 
                    venEncabezado.codana AS analisis, 
                    venEncabezado.codcen AS uni_negocio, 
                    venEncabezado.codmon AS moneda, 
                    CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
                    venEncabezado.glosa AS glosa_linea
             INTO #TEMP_COMPRO_SUBFAMILIA
             FROM venDetalle
                  INNER JOIN venEncabezado ON venDetalle.coddoc = venEncabezado.coddoc
                                              AND venDetalle.grudoc = venEncabezado.grudoc
                                              AND venDetalle.numinv = venEncabezado.numinv
                  INNER JOIN invSubFamilia ON venDetalle.codfam = invSubFamilia.codfam
                                              AND venDetalle.codsub = invSubFamilia.codsub
                  INNER JOIN conPlanCuentas ON invSubFamilia.ctacontable = conPlanCuentas.cuenta
                                               AND YEAR(venDetalle.fecdoc) = conPlanCuentas.periodo
             WHERE(venEncabezado.numcom = 0)
                  AND (venEncabezado.codsii NOT IN(60, 61, 106, 112))
                  AND (venEncabezado.grudoc = 10)
                  AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (venEncabezado.neto <> 0
                       OR venEncabezado.exento <> 0)
					   AND venEncabezado.esCompra = 0
					   AND (venencabezado.codcen =@unidad or @unidad=0)
				 AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
				 AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0)
             UNION ALL

             --	NOTAS DE CREDITO
             --	NETO AL DEBE
             SELECT 9 AS n, 
                    venEncabezado.coddoc AS tipo_ingreso, 
                    MONTH(venEncabezado.fecdoc) AS mes, 
                    YEAR(venEncabezado.fecdoc) AS periodo, 
                    venEncabezado.numinv AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    venEncabezado.codsii AS tipo_documento, 
                    invSubFamilia.ctacontable AS cuenta, 
                    conPlanCuentas.descripcion, 
                    (venDetalle.pretot * 1) AS debe, 
                    0 AS haber, 
                    0 AS numlin, 
                    venEncabezado.rutcte AS ctacte, 
                    coditem AS item, 
                    venEncabezado.codsii AS tipo_docto, 
                    venEncabezado.numinv AS num_docto, 
                    venEncabezado.fecven AS vencimiento, 
                    venEncabezado.codana AS analisis, 
                    venEncabezado.codcen AS uni_negocio, 
                    venEncabezado.codmon AS moneda, 
                    CASE venEncabezado.monpar WHEN 0 THEN 1 END AS tasa_cambio, 
                    venEncabezado.glosa AS glosa_linea
             FROM venDetalle
                  INNER JOIN venEncabezado ON venDetalle.coddoc = venEncabezado.coddoc
                                              AND venDetalle.grudoc = venEncabezado.grudoc
                                              AND venDetalle.numinv = venEncabezado.numinv
                  INNER JOIN invSubFamilia ON venDetalle.codfam = invSubFamilia.codfam
                                              AND venDetalle.codsub = invSubFamilia.codsub
                  INNER JOIN conPlanCuentas ON invSubFamilia.ctacontable = conPlanCuentas.cuenta
                                               AND YEAR(venDetalle.fecdoc) = conPlanCuentas.periodo
             WHERE(venEncabezado.numcom = 0)
                  AND (venEncabezado.codsii IN(60, 61, 106, 112))
                  AND (venEncabezado.grudoc = 10)
                  AND (CONVERT(DATE, venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (venEncabezado.neto <> 0
                       OR venEncabezado.exento <> 0)
					   AND (venencabezado.codcen =@unidad or @unidad=0)
					   AND venEncabezado.esCompra = 0
				  AND (venEncabezado.numinv BETWEEN @correIni AND @correFin)
				  AND (venEncabezado.coddoc = @tipoDocto OR @tipoDocto = 0);

             --SELECT * FROM #TEMP_COMPRO_SUBFAMILIA
             ---CUENTAS POR COSTO

             SELECT 30 AS n, 
                    0 AS tipo_ingreso, 
                    MONTH(E.fecdoc) AS mes, 
                    YEAR(E.fecdoc) AS periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    @cuentacosto AS cuenta, 
                    '' AS descripcion, 
                    CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS debe, 
                    0 AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    coditem AS item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    E.codana AS analisis, 
                    E.codcen AS uni_negocio, 
                    E.codmon AS moneda, 
                    E.monpar AS tasa_cambio, 
                    E.glosa AS glosa_linea
             INTO #COSTOVSUBFAMILIA
             FROM venEncabezado AS E
                  INNER JOIN venDetalle AS D ON E.coddoc = D.coddoc
                                                AND E.grudoc = D.grudoc
                                                AND E.numinv = D.numinv
             WHERE(E.numcom = 0)
                  AND (E.grudoc = 10)
                  AND (@cuentacosto > 0)
                  AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (codsii NOT IN(60, 61, 106, 112))
                  AND (codsii NOT IN(104, 111, 55, 56)
             OR (codsii IN(104, 111, 55, 56)
             AND E.motivo IN(1, 4)))
			 AND E.esCompra = 0
			 AND (E.codcen =@unidad or @unidad=0)
			 AND (E.numinv BETWEEN @correIni AND @correFin)
			 AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
             GROUP BY MONTH(E.fecdoc), 
                      YEAR(E.fecdoc), 
                      E.codana, 
                      E.codcen, 
                      E.codmon, 
                      E.monpar, 
                      E.glosa,
					  coditem;
             SELECT 32 AS n, 
                    0 AS tipo_ingreso, 
                    MONTH(E.fecdoc) AS mes, 
                    YEAR(E.fecdoc) AS periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    @cuentacosto AS cuenta, 
                    '' AS descripcion, 
                    0 AS debe, 
                    CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    coditem AS item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    E.codana AS analisis, 
                    E.codcen AS uni_negocio, 
                    E.codmon AS moneda, 
                    E.monpar AS tasa_cambio, 
                    E.glosa AS glosa_linea
             INTO #COSTONCSUBFAMILIA
             FROM venEncabezado AS E
                  INNER JOIN venDetalle AS D ON E.coddoc = D.coddoc
                                                AND E.grudoc = D.grudoc
                                                AND E.numinv = D.numinv
             WHERE(E.numcom = 0)
                  AND (E.grudoc = 10)
                  AND (@cuentacosto > 0)
                  AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (E.codsii IN(60, 61, 106, 112)
             AND motivo IN(1, 4))
			 AND E.esCompra = 0
			 AND (E.codcen =@unidad or @unidad=0)
			 AND (E.numinv BETWEEN @correIni AND @correFin)
			 AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
             GROUP BY MONTH(E.fecdoc), 
                      YEAR(E.fecdoc), 
                      E.codana, 
                      E.codcen, 
                      E.codmon, 
                      E.monpar, 
                      E.glosa,
					  coditem;
             SELECT 31 AS n, 
                    0 AS tipo_ingreso, 
                    MONTH(E.fecdoc) AS mes, 
                    YEAR(E.fecdoc) AS periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    @cuentaEXISTENCIA AS cuenta, 
                    '' AS descripcion, 
                    0 AS debe, 
                    CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    coditem AS item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    E.codana AS analisis, 
                    E.codcen AS uni_negocio, 
                    E.codmon AS moneda, 
                    E.monpar AS tasa_cambio, 
                    E.glosa AS glosa_linea
             INTO #EXISTENCIAVSUBFAMILIA
             FROM venEncabezado AS E
                  INNER JOIN venDetalle AS D ON E.coddoc = D.coddoc
                                                AND E.grudoc = D.grudoc
                                                AND E.numinv = D.numinv
             WHERE(E.numcom = 0)
                  AND (E.grudoc = 10)
                  AND @cuentaEXISTENCIA > 0
                  AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (codsii NOT IN(60, 61, 106, 112))
                  AND (codsii NOT IN(104, 111, 55, 56)
             OR (codsii IN(104, 111, 55, 56)
             AND E.motivo IN(1, 4)))
			 AND E.esCompra = 0
			 AND (E.codcen =@unidad or @unidad=0)
			 AND (E.numinv BETWEEN @correIni AND @correFin)
			 AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
             GROUP BY MONTH(E.fecdoc), 
                      YEAR(E.fecdoc), 
                      E.codana, 
                      E.codcen, 
                      E.codmon, 
                      E.monpar, 
                      E.glosa,
					  coditem;
             SELECT 33 AS n, 
                    0 AS tipo_ingreso, 
                    MONTH(E.fecdoc) AS mes, 
                    YEAR(E.fecdoc) AS periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    @cuentaEXISTENCIA AS cuenta, 
                    '' AS descripcion, 
                    CONVERT(DECIMAL(18,4),SUM(D.costo * d.cantidad)) AS debe, 
                    0 AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    coditem AS item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    E.codana AS analisis, 
                    E.codcen AS uni_negocio, 
                    E.codmon AS moneda, 
                    E.monpar AS tasa_cambio, 
                    E.glosa AS glosa_linea
             INTO #EXISTENCIANCSUBFAMILIA
             FROM venEncabezado AS E
                  INNER JOIN venDetalle AS D ON E.coddoc = D.coddoc
                                                AND E.grudoc = D.grudoc
                                                AND E.numinv = D.numinv
             WHERE(E.numcom = 0)
                  AND (E.grudoc = 10)
                  AND @cuentaEXISTENCIA > 0
                  AND (CONVERT(DATE, E.fecdoc) BETWEEN @fecini AND @fecfin)
                  AND (E.codsii IN(60, 61, 106, 112)
             AND motivo IN(1, 4))
			 AND E.esCompra = 0
			 AND (E.codcen =@unidad or @unidad=0)
			 AND (E.numinv BETWEEN @correIni AND @correFin)
			 AND (E.codsii = @tipoDocto OR @tipoDocto = 0)
             GROUP BY MONTH(E.fecdoc), 
                      YEAR(E.fecdoc), 
                      E.codana, 
                      E.codcen, 
                      E.codmon, 
                      E.monpar, 
                      E.glosa,
					  coditem;

             --SELECT *  FROM #COSTOVSUBFAMILIA
             --SELECT *  FROM #EXISTENCIAVSUBFAMILIA
             --SELECT * FROM #COSTONCSUBFAMILIA
             --SELECT * FROM #EXISTENCIANCSUBFAMILIA

             SELECT 0 AS n, 
                    0 AS tipo_ingreso, 
                    mes, 
                    periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    cuenta, 
                    '' AS descripcion, 
                    CONVERT(DECIMAL(18,4),SUM(debe)) AS debe, 
                    0 AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    0 AS analisis, 
                    uni_negocio, 
                    0 AS moneda, 
                    0 AS tasa_cambio, 
                    '' AS glosa_linea
             INTO #COSTOEXISTENCIA
             FROM #COSTOVSUBFAMILIA
             GROUP BY cuenta, 
                      mes, 
                      periodo, 
                      item, 
                      uni_negocio
             UNION ALL
             SELECT 0 AS n, 
                    0 AS tipo_ingreso, 
                    mes, 
                    periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    cuenta, 
                    '' AS descripcion, 
                    0 AS debe, 
                    CONVERT(DECIMAL(18,4),SUM(haber)) AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    0 AS analisis, 
                    uni_negocio, 
                    0 AS moneda, 
                    0 AS tasa_cambio, 
                    '' AS glosa_linea
             FROM #COSTONCSUBFAMILIA
             GROUP BY cuenta, 
                      mes, 
                      periodo, 
                      item, 
                      uni_negocio
             UNION ALL
             SELECT 0 AS n, 
                    0 AS tipo_ingreso, 
                    mes, 
                    periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    cuenta, 
                    '' AS descripcion, 
                    0 AS debe, 
                    CONVERT(DECIMAL(18,4),SUM(haber)) AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    0 AS analisis, 
                    uni_negocio, 
                    0 AS moneda, 
                    0 AS tasa_cambio, 
                    '' AS glosa_linea
             FROM #EXISTENCIAVSUBFAMILIA
             GROUP BY cuenta, 
                      mes, 
                      periodo, 
                      item, 
                      uni_negocio
             UNION ALL
             SELECT 0 n, 
                    0 AS tipo_ingreso, 
                    mes, 
                    periodo, 
                    0 AS correlativo, 
                    0 AS linea, 
                    0 AS concepto, 
                    0 AS tipo_documento, 
                    cuenta, 
                    '' AS descripcion, 
                    CONVERT(DECIMAL(18,4),SUM(debe)) AS debe, 
                    0 AS haber, 
                    0 AS numlin, 
                    0 AS ctacte, 
                    item, 
                    0 AS tipo_docto, 
                    0 AS num_docto, 
                    '' AS vencimiento, 
                    0 AS analisis, 
                    uni_negocio, 
                    0 AS moneda, 
                    0 AS tasa_cambio, 
                    '' AS glosa_linea
             FROM #EXISTENCIANCSUBFAMILIA
             GROUP BY cuenta, 
                      mes, 
                      periodo, 
                      item, 
                      uni_negocio;
             SELECT *, 
                    1 AS ORDEN
             INTO #TEMP_COMPRO2
             FROM #TEMP_COMPRO
             UNION ALL
             SELECT *, 
                    1 AS ORDEN
             FROM #TEMP_COMPRO_SUBFAMILIA
             UNION ALL
             SELECT *, 
                    3 AS ORDEN
             FROM #COSTOEXISTENCIA;
             SELECT TEMP_COMPRO.n, 
                    TEMP_COMPRO.tipo_ingreso, 
                    TEMP_COMPRO.mes, 
                    TEMP_COMPRO.periodo, 
                    TEMP_COMPRO.correlativo, 
                    TEMP_COMPRO.linea, 
                    TEMP_COMPRO.concepto, 
                    TEMP_COMPRO.tipo_documento, 
                    TEMP_COMPRO.cuenta, 
                    conPlanCuentas.descripcion, 
                    CAST(TEMP_COMPRO.debe AS DECIMAL(18,4)) AS debe, 
                    CAST(TEMP_COMPRO.haber AS DECIMAL(18,4)) AS haber, 
                    TEMP_COMPRO.numlin,
                    CASE
                        WHEN conPlanCuentas.marcaCtacte = 1
                        THEN TEMP_COMPRO.ctacte
                        ELSE 0
                    END AS ctacte,
                    CASE
                        WHEN conPlanCuentas.marcaItem = 1
                             AND TEMP_COMPRO.analisis > 0
                        THEN
             (
                 SELECT item
                 FROM conAnalisis AS A
                 WHERE(tipoAnalisis = 1)
                      AND (codigoAnalisis = TEMP_COMPRO.analisis)
             )
                        ELSE 0
                    END AS item,
                    CASE
                        WHEN conPlanCuentas.marcaDocumento = 1
                        THEN TEMP_COMPRO.tipo_docto
                        ELSE 0
                    END AS tipo_docto,
                    CASE
                        WHEN conPlanCuentas.marcaDocumento = 1
                        THEN TEMP_COMPRO.num_docto
                        ELSE 0
                    END AS num_docto,
                    CASE
                        WHEN conPlanCuentas.marcaDocumento = 1
                        THEN CAST(TEMP_COMPRO.vencimiento AS DATE)
                        ELSE '01-01-1900'
                    END AS vencimiento,
                    CASE
                        WHEN conPlanCuentas.marcaAnalisis = 1
                        THEN TEMP_COMPRO.analisis
                        ELSE 0
                    END AS analisis,
                    CASE
                        WHEN conPlanCuentas.marcaMoneda = 1
                        THEN TEMP_COMPRO.moneda
                        ELSE 0
                    END AS moneda,
                    CASE
                        WHEN conPlanCuentas.marcaMoneda = 1
                        THEN TEMP_COMPRO.tasa_cambio
                        ELSE 0
                    END AS tasa_cambio,
                    CASE
                        WHEN conPlanCuentas.marcaNegocio = 1
                        THEN TEMP_COMPRO.uni_negocio
                        ELSE 0
                    END AS uni_negocio, 
                    TEMP_COMPRO.glosa_linea
             FROM #TEMP_COMPRO2 TEMP_COMPRO
                  INNER JOIN conPlanCuentas ON TEMP_COMPRO.periodo = conPlanCuentas.periodo
                                               AND TEMP_COMPRO.cuenta = conPlanCuentas.cuenta

             ORDER BY orden;
			 
             --SELECT * FROM TEMP_COMPRO2

     END;