IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'dbo.CPJ_BalanceCC_Rpt') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE dbo.CPJ_BalanceCC_Rpt
END
GO

CREATE PROCEDURE [dbo].[CPJ_BalanceCC_Rpt]	@pJson VARCHAR(8000)
AS
SET NOCOUNT ON

/*Variables recibidas por parametro a traves del JSON*/
DECLARE @pModoDebug			    	TINYINT
DECLARE @pId_Usuario			    SMALLINT
DECLARE @pFecha_Desde			    DATETIME
DECLARE @pFecha_Hasta			    DATETIME
DECLARE @pId_Centro_Costo			CHAR(10)

SET @pModoDebug					= ISNULL(CONVERT(TINYINT, dbo.CF_JSON_VALUE(@pJson,'ModoDebug')), 0)
SET @pId_Usuario				= ISNULL(CONVERT(SMALLINT, dbo.CF_JSON_VALUE(@pJson,'Id_Usuario')), 0)
SET @pFecha_Desde				= ISNULL(CONVERT(DATETIME, dbo.CF_JSON_VALUE(@pJson,'Fecha_Desde')), '19000101')
SET @pFecha_Hasta				= ISNULL(CONVERT(DATETIME, dbo.CF_JSON_VALUE(@pJson,'Fecha_Hasta')), '19000101')
SET @pId_Centro_Costo			= ISNULL(CONVERT(CHAR(10), dbo.CF_JSON_VALUE(@pJson,'Id_Centro_Costo')), 0)
/*FIN Variables recibidas por parametro a traves del JSON*/

/*Variables Locales*/
DECLARE @SqlStr						NVARCHAR(MAX)
DECLARE @Id_Trans					INT
DECLARE @Nro_Cta_Contable			VARCHAR(20)
DECLARE @Debe						DECIMAL(12,2)
DECLARE @Haber						DECIMAL(12,2)
DECLARE @Fecha						DATETIME
DECLARE @Descripcion				VARCHAR(80)
DECLARE @Centro_Costo				VARCHAR(200)
DECLARE @Centro_Costo_Padre1		VARCHAR(200)
DECLARE @Centro_Costo_Padre2		VARCHAR(200)
DECLARE @Destinatario				VARCHAR(100)
DECLARE @Observacion_Orig			VARCHAR(8000)
DECLARE @Observacion_Dest			VARCHAR(8000)
DECLARE @Importe					DECIMAL(12,2)
DECLARE @Imp_Total_Fac				DECIMAL(12,2)
DECLARE @Imp_Aplicado				DECIMAL(12,2)
DECLARE @Imp_Fac					DECIMAL(12,2)
DECLARE @Coef_Aplic					DECIMAL(12,2)
DECLARE @Id_Trans_CCC				INT
DECLARE @Id_Centro_Costo_Padre1		CHAR(10)
DECLARE @Id_Centro_Costo_Padre2		CHAR(10)
/*FIN Variables Locales*/

BEGIN TRY
	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Ingreso a Stored Procedure: CPJ_BalanceCC_Rpt'
		PRINT 'Parametros: '
		PRINT '@pId_Usuario: ' + CONVERT(VARCHAR(10), @pId_Usuario) + '. @pFecha_Desde: ' + CONVERT(VARCHAR(10), @pFecha_Desde, 112) + '. @pFecha_Hasta: ' + CONVERT(VARCHAR(10), @pFecha_Hasta, 112) + '. @pId_Centro_Costo: ' + CONVERT(VARCHAR(10), @pId_Centro_Costo)
	END

	/*=============================================================================================================================*/
	/*=============================================================================================================================*/

	IF(@pId_Usuario = 0)
	BEGIN
		SET @pId_Usuario = -1
	END
	
	IF(@pFecha_Desde = '19000101')								RAISERROR ('Fecha_Desde can not be blank.', 16,  1)
	IF(@pFecha_Hasta = '19000101')								RAISERROR ('Fecha_Hasta can not be blank.', 16,  1) 
	IF(@pFecha_Desde >= @pFecha_Hasta)							RAISERROR ('Fecha_Desde can not be bigger or equal than Fecha_Hasta.', 16,  1) 
	IF(DATEDIFF(YEAR, @pFecha_Desde, @pFecha_Hasta) > 0)		RAISERROR ('Diference between Fecha_Desde and Fecha_Hasta can not be bigger than a year.', 16,  1) 	
	IF(@pId_Centro_Costo = '')									RAISERROR ('ID_Centro_Costo can not be blank.', 16,  1)
	
	/*=============================================================================================================================*/

	SELECT
	@Centro_Costo = ISNULL(Descripcion, ''),
	@Id_Centro_Costo_Padre1 = ISNULL(Id_Centro_Costo_Padre, '')
	FROM Centro_Costos
	WHERE
	Id_Centro_Costo = @pId_Centro_Costo

	SELECT
	@Centro_Costo_Padre1 = ISNULL(Descripcion, ''),
	@Id_Centro_Costo_Padre2 = ISNULL(Id_Centro_Costo_Padre, '')
	FROM Centro_Costos
	WHERE
	Id_Centro_Costo = @Id_Centro_Costo_Padre1

	SELECT
	@Centro_Costo_Padre2 = ISNULL(Descripcion, '')
	FROM Centro_Costos
	WHERE
	Id_Centro_Costo = @Id_Centro_Costo_Padre2

	/*=============================================================================================================================*/
	
	CREATE TABLE #Rpt_BalanceCC
	(
	Id_Trans_O						INT,
	Id_Trans_D						INT,
	Nro_Cta_Contable				VARCHAR(20),
	Debe							DECIMAL(12,2),
	Haber							DECIMAL(12,2),
	Saldo							DECIMAL(12,2),
	Fecha							DATETIME,
	Cta_Contable					VARCHAR(80),
	Id_Centro_Costo					VARCHAR(10),
	Centro_Costo					VARCHAR(200),
	Centro_Costo2					VARCHAR(200),
	Centro_Costo3					VARCHAR(200),
	Destinatario					VARCHAR(100),
	Observacion_Orig				VARCHAR(8000),
	Observacion_Dest				VARCHAR(8000)
	)

	/*=============================================================================================================================*/
	/*=============================================================================================================================*/
	
	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Recibos Varios'
	END

	CREATE TABLE #Rpt_BalanceCC_Recibos_Varios
	(
	Id_Trans						INT,
	Nro_Cta_Contable				VARCHAR(20),
	Debe							DECIMAL(12,2),
	Haber							DECIMAL(12,2),
	Fecha							DATETIME,
	Descripcion						VARCHAR(80),
	Destinatario					VARCHAR(100),
	Observacion_Orig				VARCHAR(8000),
	Observacion_Dest				VARCHAR(8000)
	)

	/*=============================================================================================================================*/

	SET @SqlStr =	'INSERT INTO #Rpt_BalanceCC_Recibos_Varios (Id_Trans, Nro_Cta_Contable, Debe, Haber, Fecha, Descripcion, Destinatario, Observacion_Orig, Observacion_Dest) '
	
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr))							+ CHAR(13) + 
					'SELECT '										+ CHAR(13) + 
					'C.Id_Trans, '									+ CHAR(13) + 
					'O.Nro_Cta_Contable, '							+ CHAR(13) + 
					'C.Debe, '										+ CHAR(13) + 
					'C.Haber, '										+ CHAR(13) + 
					'E.Fecha, '										+ CHAR(13) + 
					'O.Descripcion AS Cta_Contable, '				+ CHAR(13) + 
					'ISNULL(E.Destinatario, '''') AS Destinatario, '	+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), E.Observacion), '''') AS Observacion_Orig, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), E.Observacion), '''') AS Observacion_Dest '+ CHAR(13) +
					'FROM Cbtes_Ctas_Contables C, Cbtes E,Cbtes_Tipos T,Pla_Ctas_Cuentas_Contables O ' + CHAR(13) + 
					'WHERE '										+ CHAR(13) + 
					'C.Id_Trans = E.Id_Trans '						+ CHAR(13) + 
					'AND E.Id_Tipo_Cbte = T.Id_Tipo_Cbte '			+ CHAR(13) + 					
					'AND ISNULL(E.Id_Cliente, 0) = 0 '				+ CHAR(13) + 
					'AND O.Id_Cta_Contable = C.Id_Cta_Contable '	+ CHAR(13) +
					'AND E.Id_Estado_Cbte NOT IN (0,3) '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND E.Fecha >= ''' + CONVERT(VARCHAR(10), @pFecha_Desde, 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND E.Fecha < ''' + CONVERT(VARCHAR(10), DATEADD(DAY, 1, @pFecha_Hasta), 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND C.Id_Centro_Costo = ''' + @pId_Centro_Costo + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND T.Compromete_Recibos IN (1,-1)'

	IF(@pModoDebug = 1)
	BEGIN
		PRINT ''
		PRINT '@SqlStr (Recibos Varios): '
		PRINT @SqlStr
		PRINT ''
	END

	EXECUTE sp_executesql @SqlStr

	/*=============================================================================================================================*/

	DECLARE cur_BalanceCC_Recibos_Varios CURSOR FOR
	SELECT 
	Id_Trans,
	Nro_Cta_Contable, 
	Debe, 
	Haber, 
	Fecha, 
	Descripcion,
	Destinatario,
	Observacion_Orig,
	Observacion_Dest
	FROM #Rpt_BalanceCC_Recibos_Varios

	OPEN cur_BalanceCC_Recibos_Varios
	FETCH NEXT FROM cur_BalanceCC_Recibos_Varios INTO @Id_Trans, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	WHILE @@FETCH_STATUS = 0
	BEGIN

		INSERT INTO #Rpt_BalanceCC
		(Id_Trans_O,
		Id_Trans_D,
		Nro_Cta_Contable,
		Debe,
		Haber,
		Saldo,
		Fecha,
		Cta_Contable,
		Id_Centro_Costo,
		Centro_Costo,
		Centro_Costo2,
		Centro_Costo3,
		Destinatario,
		Observacion_Orig,
		Observacion_Dest)
		VALUES
		(@Id_Trans,
		0,
		@Nro_Cta_Contable,
		@Debe,
		@Haber,
		0,
		@Fecha,
		@Descripcion,
		@pId_Centro_Costo,
		@Centro_Costo,
		@Centro_Costo_Padre1,
		@Centro_Costo_Padre2,
		@Destinatario,
		@Observacion_Orig,
		@Observacion_Dest)

		FETCH NEXT FROM cur_BalanceCC_Recibos_Varios INTO @Id_Trans, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	END
	CLOSE cur_BalanceCC_Recibos_Varios
	DEALLOCATE cur_BalanceCC_Recibos_Varios
	
	/*=============================================================================================================================*/
	/*=============================================================================================================================*/

	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Recibos Clientes'
	END

	CREATE TABLE #Rpt_BalanceCC_Recibos_Clientes
	(
	Id_Trans_Aplica					INT,
	Importe							DECIMAL(12,2),
	Imp_Total_Fac					DECIMAL(12,2),
	Id_Trans_CCC					INT,
	Nro_Cta_Contable				VARCHAR(20),
	Debe							DECIMAL(12,2),
	Haber							DECIMAL(12,2),
	Fecha_Rec						DATETIME,
	Descripcion						VARCHAR(80),
	Destinatario					VARCHAR(100),
	Observacion_Orig				VARCHAR(8000),
	Observacion_Dest				VARCHAR(8000)
	)
	
	/*=============================================================================================================================*/

	SET @SqlStr =	'INSERT INTO #Rpt_BalanceCC_Recibos_Clientes (Id_Trans_Aplica, Importe, Imp_Total_Fac, Id_Trans_CCC, Nro_Cta_Contable, Debe, Haber, Fecha_Rec, Descripcion, Destinatario, Observacion_Orig, Observacion_Dest) '
	
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr))							+ CHAR(13) + 
					'SELECT '										+ CHAR(13) + 
					'A.Id_Trans, '									+ CHAR(13) + 
					'A.Importe, '									+ CHAR(13) + 
					'Fac.Imp_Total, '								+ CHAR(13) + 
					'C.Id_Trans, '									+ CHAR(13) + 
					'O.Nro_Cta_Contable, '							+ CHAR(13) + 
					'C.Debe, '										+ CHAR(13) + 
					'C.Haber, '										+ CHAR(13) + 
					'Rec.Fecha, '									+ CHAR(13) + 
					'O.Descripcion, '								+ CHAR(13) + 
					'ISNULL(Fac.Destinatario, '''') AS Destinatario, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), Fac.Observacion), '''') AS Observacion_Orig, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), Rec.Observacion), '''') AS Observacion_Dest '+ CHAR(13) +
					'FROM Cbtes_Aplica A, Cbtes_Ctas_Contables C, Cbtes Rec,Cbtes Fac,Cbtes_Tipos T,Pla_Ctas_Cuentas_Contables O ' + CHAR(13) + 
					'WHERE '										+ CHAR(13) + 
					'C.id_trans = A.id_trans '						+ CHAR(13) + 
					'AND A.id_trans_D = Rec.id_trans '				+ CHAR(13) + 
					'AND Fac.id_trans = C.id_trans '				+ CHAR(13) + 
					'AND Rec.id_Tipo_Cbte = T.Id_Tipo_Cbte '		+ CHAR(13) + 
					'AND O.Id_Cta_Contable = C.Id_Cta_Contable '	+ CHAR(13) + 
					'AND Rec.Id_Cliente > 0 '						+ CHAR(13) + 
					'AND Rec.Id_Estado_Cbte NOT IN (0,3) '			+ CHAR(13) + 
					'AND Fac.Id_Estado_Cbte NOT IN (0,3) ' 


	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND Rec.Fecha >= ''' + CONVERT(VARCHAR(10), @pFecha_Desde, 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND Rec.Fecha < ''' + CONVERT(VARCHAR(10), DATEADD(DAY, 1, @pFecha_Hasta), 112) + ''' '

	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND C.Id_Centro_Costo = ''' + @pId_Centro_Costo + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND T.Compromete_Recibos IN (1,-1)'

	IF(@pModoDebug = 1)
	BEGIN
		PRINT ''
		PRINT '@SqlStr (Recibos Clientes): '
		PRINT @SqlStr
		PRINT ''
	END

	EXECUTE sp_executesql @SqlStr

	/*=============================================================================================================================*/

	DECLARE cur_BalanceCC_Recibos_Clientes CURSOR FOR
	SELECT 
	Id_Trans_Aplica,
	Id_Trans_CCC,
	Importe,
	Imp_Total_Fac,
	Nro_Cta_Contable, 
	Debe, 
	Haber, 
	Fecha_Rec, 
	Descripcion,
	Destinatario,
	Observacion_Orig,
	Observacion_Dest
	FROM #Rpt_BalanceCC_Recibos_Clientes

	OPEN cur_BalanceCC_Recibos_Clientes
	FETCH NEXT FROM cur_BalanceCC_Recibos_Clientes INTO @Id_Trans, @Id_Trans_CCC, @Importe, @Imp_Total_Fac, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	WHILE @@FETCH_STATUS = 0
	BEGIN

		SET @Imp_Aplicado	= @Importe
		SET @Imp_Fac		= @Imp_Total_Fac
		SET @Coef_Aplic		= @Imp_Aplicado / @Imp_Fac

		INSERT INTO #Rpt_BalanceCC
		(Id_Trans_O,
		Id_Trans_D,
		Nro_Cta_Contable,
		Debe,
		Haber,
		Saldo,
		Fecha,
		Cta_Contable,
		Id_Centro_Costo,
		Centro_Costo,
		Centro_Costo2,
		Centro_Costo3,
		Destinatario,
		Observacion_Orig,
		Observacion_Dest)
		VALUES
		(@Id_Trans_CCC,
		@Id_Trans,
		@Nro_Cta_Contable,
		@Debe * @Coef_Aplic,
		@Haber * @Coef_Aplic,
		0,
		@Fecha,
		@Descripcion,
		@pId_Centro_Costo,
		@Centro_Costo,
		@Centro_Costo_Padre1,
		@Centro_Costo_Padre2,
		@Destinatario,
		@Observacion_Orig,
		@Observacion_Dest)

		FETCH NEXT FROM cur_BalanceCC_Recibos_Clientes INTO @Id_Trans, @Id_Trans_CCC, @Importe, @Imp_Total_Fac, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	END
	CLOSE cur_BalanceCC_Recibos_Clientes
	DEALLOCATE cur_BalanceCC_Recibos_Clientes

	/*=============================================================================================================================*/
	/*=============================================================================================================================*/

	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Recibos OP Varias'
	END

	CREATE TABLE #Rpt_BalanceCC_OP_Varias
	(
	Id_Trans						INT,
	Nro_Cta_Contable				VARCHAR(20),
	Debe							DECIMAL(12,2),
	Haber							DECIMAL(12,2),
	Fecha							DATETIME,
	Descripcion						VARCHAR(80),
	Destinatario					VARCHAR(100),
	Observacion_Orig				VARCHAR(8000),
	Observacion_Dest				VARCHAR(8000)
	)
	
	/*=============================================================================================================================*/

	SET @SqlStr =	'INSERT INTO #Rpt_BalanceCC_OP_Varias (Id_Trans, Nro_Cta_Contable, Debe, Haber, Fecha, Descripcion, Destinatario, Observacion_Orig, Observacion_Dest) '

	SET @SqlStr =	RTRIM(LTRIM(@SqlStr))							+ CHAR(13) + 
					'SELECT '										+ CHAR(13) + 
					'C.Id_Trans, '									+ CHAR(13) + 
					'O.Nro_Cta_Contable, '							+ CHAR(13) + 
					'C.Debe, '										+ CHAR(13) + 
					'C.Haber, '										+ CHAR(13) + 
					'E.Fecha, '										+ CHAR(13) + 
					'O.Descripcion AS Cta_Contable, '				+ CHAR(13) + 
					'ISNULL(E.Destinatario, '''') AS Destinatario, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), E.Observacion), '''') AS Observacion_Orig, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), E.Observacion), '''') AS Observacion_Dest '+ CHAR(13) +
					'FROM Cbtes_Ctas_Contables C, Cbtes E,Cbtes_Tipos T,Pla_Ctas_Cuentas_Contables O ' + CHAR(13) + 
					'WHERE '										+ CHAR(13) + 
					'C.Id_Trans = E.Id_Trans '						+ CHAR(13) + 
					'AND E.Id_Tipo_Cbte = T.Id_Tipo_Cbte '			+ CHAR(13) + 	
					'AND O.Id_Cta_Contable = C.Id_Cta_Contable '	+ CHAR(13) +
					'AND ISNULL(E.Id_Proveedor, 0) = 0 '			+ CHAR(13) + 					
					'AND E.Id_Estado_Cbte NOT IN (0,3) '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND E.Fecha >= ''' + CONVERT(VARCHAR(10), @pFecha_Desde, 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND E.Fecha < ''' + CONVERT(VARCHAR(10), DATEADD(DAY, 1, @pFecha_Hasta), 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND C.Id_Centro_Costo = ''' + @pId_Centro_Costo + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND T.Compromete_Pagos IN (1,-1)'

	IF(@pModoDebug = 1)
	BEGIN
		PRINT ''
		PRINT '@SqlStr (Orden Pagos Varias): '
		PRINT @SqlStr
		PRINT ''
	END

	EXECUTE sp_executesql @SqlStr

	/*=============================================================================================================================*/

	DECLARE cur_BalanceCC_OP_Varias CURSOR FOR
	SELECT 
	Id_Trans,
	Nro_Cta_Contable, 
	Debe, 
	Haber, 
	Fecha, 
	Descripcion,
	Destinatario,
	Observacion_Orig,
	Observacion_Dest
	FROM #Rpt_BalanceCC_OP_Varias

	OPEN cur_BalanceCC_OP_Varias
	FETCH NEXT FROM cur_BalanceCC_OP_Varias INTO @Id_Trans, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	WHILE @@FETCH_STATUS = 0
	BEGIN

		INSERT INTO #Rpt_BalanceCC
		(Id_Trans_O,
		Id_Trans_D,
		Nro_Cta_Contable,
		Debe,
		Haber,
		Saldo,
		Fecha,
		Cta_Contable,
		Id_Centro_Costo,
		Centro_Costo,
		Centro_Costo2,
		Centro_Costo3,
		Destinatario,
		Observacion_Orig,
		Observacion_Dest)
		VALUES
		(@Id_Trans,
		0,
		@Nro_Cta_Contable,
		@Debe,
		@Haber,
		0,
		@Fecha,
		@Descripcion,
		@pId_Centro_Costo,
		@Centro_Costo,
		@Centro_Costo_Padre1,
		@Centro_Costo_Padre2,
		@Destinatario,
		@Observacion_Orig,
		@Observacion_Dest)		

		FETCH NEXT FROM cur_BalanceCC_OP_Varias INTO @Id_Trans, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	END
	CLOSE cur_BalanceCC_OP_Varias
	DEALLOCATE cur_BalanceCC_OP_Varias

	/*=============================================================================================================================*/
	/*=============================================================================================================================*/

	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'OP Proveedores'
	END

	CREATE TABLE #Rpt_BalanceCC_OP_Proveedores
	(
	Id_Trans_Aplica					INT,
	Importe							DECIMAL(12,2),
	Imp_Total_Fac					DECIMAL(12,2),
	Id_Trans_CCC					INT,
	Nro_Cta_Contable				VARCHAR(20),
	Debe							DECIMAL(12,2),
	Haber							DECIMAL(12,2),
	Fecha_OP						DATETIME,
	Descripcion						VARCHAR(80),
	Destinatario					VARCHAR(100),
	Observacion_Orig				VARCHAR(8000),
	Observacion_Dest				VARCHAR(8000)
	)
	
	/*=============================================================================================================================*/

	SET @SqlStr =	'INSERT INTO #Rpt_BalanceCC_OP_Proveedores (Id_Trans_Aplica, Importe, Imp_Total_Fac, Id_Trans_CCC, Nro_Cta_Contable, Debe, Haber, Fecha_OP, Descripcion, Destinatario, Observacion_Orig, Observacion_Dest) '
	
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr))							+ CHAR(13) + 
					'SELECT '										+ CHAR(13) + 
					'A.Id_Trans, '									+ CHAR(13) + 
					'A.Importe, '									+ CHAR(13) + 
					'Fac.Imp_Total, '								+ CHAR(13) + 
					'OP.Id_Trans, '									+ CHAR(13) + 
					'O.Nro_Cta_Contable, '							+ CHAR(13) + 
					'C.Debe, '										+ CHAR(13) + 
					'C.Haber, '										+ CHAR(13) + 
					'OP.Fecha, '									+ CHAR(13) + 
					'O.Descripcion, '								+ CHAR(13) + 
					'ISNULL(Fac.Destinatario, '''') AS Destinatario, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), Fac.Observacion), '''') AS Observacion_Orig, '+ CHAR(13) + 
					'ISNULL(CONVERT(VARCHAR(8000), OP.Observacion), '''') AS Observacion_Dest '+ CHAR(13) +
					'FROM Cbtes_Aplica A,Cbtes_Ctas_Contables C, Cbtes OP,Cbtes Fac,Cbtes_Tipos T,Pla_Ctas_Cuentas_Contables O ' + CHAR(13) + 
					'WHERE '										+ CHAR(13) + 
					'C.id_trans = A.id_trans '						+ CHAR(13) + 
					'AND A.id_trans_D = OP.id_trans '				+ CHAR(13) + 
					'AND Fac.id_trans = C.id_trans '				+ CHAR(13) + 
					'AND OP.id_Tipo_Cbte = T.Id_Tipo_Cbte '			+ CHAR(13) + 
					'AND O.Id_Cta_Contable = C.Id_Cta_Contable '	+ CHAR(13) + 
					'AND OP.Id_Proveedor > 0 '						+ CHAR(13) + 
					'AND OP.Id_Estado_Cbte NOT IN (0,3) '			+ CHAR(13) + 
					'AND Fac.Id_Estado_Cbte NOT IN (0,3) ' 


	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND OP.Fecha >= ''' + CONVERT(VARCHAR(10), @pFecha_Desde, 112) + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND OP.Fecha < ''' + CONVERT(VARCHAR(10), DATEADD(DAY, 1, @pFecha_Hasta), 112) + ''' '

	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND C.Id_Centro_Costo = ''' + @pId_Centro_Costo + ''' '
					
	SET @SqlStr =	RTRIM(LTRIM(@SqlStr)) + CHAR(13) + 
					'AND T.Compromete_Pagos IN (1,-1)'

	IF(@pModoDebug = 1)
	BEGIN
		PRINT ''
		PRINT '@SqlStr (OP Proveedores): '
		PRINT @SqlStr
		PRINT ''
	END

	EXECUTE sp_executesql @SqlStr

	/*=============================================================================================================================*/

	DECLARE cur_BalanceCC_OP_Proveedores CURSOR FOR
	SELECT 
	Id_Trans_Aplica,
	Id_Trans_CCC,
	Importe,
	Imp_Total_Fac,
	Nro_Cta_Contable, 
	Debe, 
	Haber, 
	Fecha_OP, 
	Descripcion,
	Destinatario,
	Observacion_Orig,
	Observacion_Dest	
	FROM #Rpt_BalanceCC_OP_Proveedores

	OPEN cur_BalanceCC_OP_Proveedores
	FETCH NEXT FROM cur_BalanceCC_OP_Proveedores INTO @Id_Trans, @Id_Trans_CCC, @Importe, @Imp_Total_Fac, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	WHILE @@FETCH_STATUS = 0
	BEGIN

		SET @Imp_Aplicado	= @Importe
		SET @Imp_Fac		= @Imp_Total_Fac
		SET @Coef_Aplic		= @Imp_Aplicado / @Imp_Fac

		INSERT INTO #Rpt_BalanceCC
		(Id_Trans_O,
		Id_Trans_D,
		Nro_Cta_Contable,
		Debe,
		Haber,
		Saldo,
		Fecha,
		Cta_Contable,
		Id_Centro_Costo,
		Centro_Costo,
		Centro_Costo2,
		Centro_Costo3,
		Destinatario,
		Observacion_Orig,
		Observacion_Dest)
		VALUES
		(@Id_Trans_CCC,
		@Id_Trans,
		@Nro_Cta_Contable,
		@Debe * @Coef_Aplic,
		@Haber * @Coef_Aplic,
		0,
		@Fecha,
		@Descripcion,
		@pId_Centro_Costo,
		@Centro_Costo,
		@Centro_Costo_Padre1,
		@Centro_Costo_Padre2,
		@Destinatario,
		@Observacion_Orig,
		@Observacion_Dest)

		FETCH NEXT FROM cur_BalanceCC_OP_Proveedores INTO @Id_Trans, @Id_Trans_CCC, @Importe, @Imp_Total_Fac, @Nro_Cta_Contable, @Debe, @Haber, @Fecha, @Descripcion, @Destinatario, @Observacion_Orig, @Observacion_Dest
	END
	CLOSE cur_BalanceCC_OP_Proveedores
	DEALLOCATE cur_BalanceCC_OP_Proveedores

	/*=============================================================================================================================*/
	/*=============================================================================================================================*/

	SELECT
	Centro_Costo AS CC1, 
	Centro_Costo2 AS CC2,
	Centro_Costo3 AS CC3,
	Nro_Cta_Contable AS Nro_Cta, 
	Cta_Contable AS Cta,
	Id_Trans_O AS Trans_Origen,
	CONVERT(DATE, Fecha) AS Fecha,
	dbo.CF_Cbte_Descrip(Id_Trans_O) AS Cbte,
	Destinatario AS Destinatario,
	Observacion_Orig AS Obs,
	Id_Trans_D AS Trans_Destino,
	'' AS Fecha_Destino,
	dbo.CF_Cbte_Descrip(Id_Trans_D) AS Cbte_Destino,
	Observacion_Dest AS Obs_Destino,
	Debe,
	Haber,
	Saldo
	FROM #Rpt_BalanceCC
	ORDER BY Nro_Cta_Contable, Fecha

	/*=============================================================================================================================*/

	DROP TABLE #Rpt_BalanceCC_Recibos_Varios
	DROP TABLE #Rpt_BalanceCC_Recibos_Clientes
	DROP TABLE #Rpt_BalanceCC_OP_Varias
	DROP TABLE #Rpt_BalanceCC_OP_Proveedores
	DROP TABLE #Rpt_BalanceCC
	
	/*=============================================================================================================================*/

	IF(@pModoDebug = 1)
	BEGIN
		PRINT 'Fin Stored Procedure: CPJ_BalanceCC_Rpt'
	END
END TRY  


BEGIN CATCH  
    EXECUTE usp_GetErrorInfo

	RETURN 9999
END CATCH

RETURN 0

/*
EXEC dbo.CPJ_BalanceCC_Rpt '{
							"ModoDebug":"1",
							"Id_Usuario":"-1",
							"Fecha_Desde":"20210101",
							"Fecha_Hasta":"20211231",
							"Id_Centro_Costo":"1"
							}'
*/