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

CREATE procedure dbo.CP_Asientos_AjuInflacion @pTipo CHAR(1),@pId_Moneda CHAR(3), @pFDesde datetime,@pFHasta datetime,@pId_Plan_Cta INT,@pAlias INT
/*

@Tipo = 'C'   --Coeficientes
@Tipo = 'F'   --Coeficientes A Aplicar
@Tipo = 'H'   --Valores Historicos
@Tipo = 'A'   --Valores Actuales
@Tipo = 'D'   --Diferencia

*/
AS

IF @pFDesde > @pFHasta 
	RETURN 1

IF DATEDIFF(MM,@pFDesde,@pFHasta) > 12  
	SET @pFHasta = DBO.CF_EOMonth(DATEADD(MM,12,@pFDesde))


DECLARE @Id_Cta_Contable INT
		,@Nro_Cta_Contable VARCHAR(20)
		,@Descripcion VARCHAR(80)
		,@Alias INT
		,@Saldo DECIMAL(16,2)
		,@Mes00	DECIMAL(18,5)
		,@Mes01	DECIMAL(18,5)
		,@Mes02	DECIMAL(18,5)
		,@Mes03	DECIMAL(18,5)
		,@Mes04	DECIMAL(18,5)
		,@Mes05	DECIMAL(18,5)
		,@Mes06	DECIMAL(18,5)
		,@Mes07	DECIMAL(18,5)
		,@Mes08	DECIMAL(18,5)
		,@Mes09	DECIMAL(18,5)
		,@Mes10	DECIMAL(18,5)
		,@Mes11	DECIMAL(18,5)
		,@Mes12	DECIMAL(18,5)
		,@Id_Centro_Costo CHAR(10)
		,@Descripcion_CC VARCHAR(200)
		,@CoefActual DECIMAL(18,5) 
		,@AliasCtaAju INT
		

DECLARE @CoefMes00	DECIMAL(18,5) 
		,@CoefMes01	DECIMAL(18,5) 
		,@CoefMes02	DECIMAL(18,5) 
		,@CoefMes03	DECIMAL(18,5) 
		,@CoefMes04	DECIMAL(18,5) 
		,@CoefMes05	DECIMAL(18,5) 
		,@CoefMes06	DECIMAL(18,5) 
		,@CoefMes07	DECIMAL(18,5) 
		,@CoefMes08	DECIMAL(18,5) 
		,@CoefMes09	DECIMAL(18,5) 
		,@CoefMes10	DECIMAL(18,5) 
		,@CoefMes11	DECIMAL(18,5) 
		,@CoefMes12	DECIMAL(18,5) 
	

DECLARE	 @CoefAplicMes00	DECIMAL(18,5) 
		,@CoefAplicMes01	DECIMAL(18,5) 
		,@CoefAplicMes02	DECIMAL(18,5) 
		,@CoefAplicMes03	DECIMAL(18,5) 
		,@CoefAplicMes04	DECIMAL(18,5) 
		,@CoefAplicMes05	DECIMAL(18,5) 
		,@CoefAplicMes06	DECIMAL(18,5) 
		,@CoefAplicMes07	DECIMAL(18,5) 
		,@CoefAplicMes08	DECIMAL(18,5) 
		,@CoefAplicMes09	DECIMAL(18,5) 
		,@CoefAplicMes10	DECIMAL(18,5) 
		,@CoefAplicMes11	DECIMAL(18,5) 
		,@CoefAplicMes12	DECIMAL(18,5)

CREATE TABLE #ValorH (
	Id_Cta_Contable INT,
	Nro_Cta_Contable VARCHAR(20),
	Descripcion VARCHAR(80),
	Alias SMALLINT,
	Id_Centro_Costo CHAR(10),
    Descripcion_CC VARCHAR(200),
	Mes00	DECIMAL(18,5) DEFAULT(0),
	Mes01	DECIMAL(18,5) DEFAULT(0),
	Mes02	DECIMAL(18,5) DEFAULT(0),
	Mes03	DECIMAL(18,5) DEFAULT(0),
	Mes04	DECIMAL(18,5) DEFAULT(0),
	Mes05	DECIMAL(18,5) DEFAULT(0),
	Mes06	DECIMAL(18,5) DEFAULT(0),
	Mes07	DECIMAL(18,5) DEFAULT(0),
	Mes08	DECIMAL(18,5) DEFAULT(0),
	Mes09	DECIMAL(18,5) DEFAULT(0),
	Mes10	DECIMAL(18,5) DEFAULT(0),
	Mes11	DECIMAL(18,5) DEFAULT(0),
	Mes12	DECIMAL(18,5) DEFAULT(0),
	Total	DECIMAL(18,5) DEFAULT(0),
	AliasCtaAju INT
	)


CREATE TABLE #Resultado (
	Tipo Char(1),
	Id_Cta_Contable INT,
	Nro_Cta_Contable VARCHAR(20),
	Descripcion VARCHAR(80),
	Alias SMALLINT,
	Id_Centro_Costo CHAR(10),
    Descripcion_CC VARCHAR(200),
	Mes00   DECIMAL(18,5) DEFAULT(0),
	Mes01	DECIMAL(18,5) DEFAULT(0),
	Mes02	DECIMAL(18,5) DEFAULT(0),
	Mes03	DECIMAL(18,5) DEFAULT(0),
	Mes04	DECIMAL(18,5) DEFAULT(0),
	Mes05	DECIMAL(18,5) DEFAULT(0),
	Mes06	DECIMAL(18,5) DEFAULT(0),
	Mes07	DECIMAL(18,5) DEFAULT(0),
	Mes08	DECIMAL(18,5) DEFAULT(0),
	Mes09	DECIMAL(18,5) DEFAULT(0),
	Mes10	DECIMAL(18,5) DEFAULT(0),
	Mes11	DECIMAL(18,5) DEFAULT(0),
	Mes12	DECIMAL(18,5) DEFAULT(0),
	Total	DECIMAL(18,5) DEFAULT(0),
    AliasCtaAju INT
	)


IF UPPER(@pTipo) IN ('C','A','D','F','H') 
BEGIN

	SET @CoefMes00 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,-1,@pFDesde)) ) 
	SET @CoefMes01 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(@pFDesde) ) 
	SET @CoefMes02 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,1,@pFDesde)) ) 
	SET @CoefMes03 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,2,@pFDesde)) ) 
	SET @CoefMes04 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,3,@pFDesde)) ) 
	SET @CoefMes05 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,4,@pFDesde)) ) 
	SET @CoefMes06 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,5,@pFDesde)) ) 
	SET @CoefMes07 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,6,@pFDesde)) ) 
	SET @CoefMes08 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,7,@pFDesde)) ) 
	SET @CoefMes09 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,8,@pFDesde)) ) 
	SET @CoefMes10 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,9,@pFDesde)) ) 
	SET @CoefMes11 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,10,@pFDesde)) ) 
	SET @CoefMes12 = (SELECT TOP 1 Coeficiente FROM Valores_Monedas_Inflacion  WHERE Id_Moneda = @pId_Moneda AND DBO.CF_EOMonth(Fecha) = DBO.CF_EOMonth(DATEADD(MM,11,@pFDesde)) ) 


	SET @CoefActual = @CoefMes01
	IF dbo.CF_FOMONTH(DATEADD(MM, 1,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes02
	IF dbo.CF_FOMONTH(DATEADD(MM, 2,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes03
	IF dbo.CF_FOMONTH(DATEADD(MM, 3,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes04
	IF dbo.CF_FOMONTH(DATEADD(MM, 4,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes05
	IF dbo.CF_FOMONTH(DATEADD(MM, 5,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes06
	IF dbo.CF_FOMONTH(DATEADD(MM, 6,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes07
	IF dbo.CF_FOMONTH(DATEADD(MM, 7,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes08
	IF dbo.CF_FOMONTH(DATEADD(MM, 8,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes09
	IF dbo.CF_FOMONTH(DATEADD(MM, 9,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes10
	IF dbo.CF_FOMONTH(DATEADD(MM, 10,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes11
	IF dbo.CF_FOMONTH(DATEADD(MM, 11,@pFDesde)) <= @pFHasta 
	   SET @CoefActual = @CoefMes12


	SET @CoefAplicMes00 = @CoefActual / @CoefMes00
	SET @CoefAplicMes01 = @CoefActual / @CoefMes01
	SET @CoefAplicMes02 = @CoefActual / @CoefMes02
	SET @CoefAplicMes03 = @CoefActual / @CoefMes03
	SET @CoefAplicMes04 = @CoefActual / @CoefMes04
	SET @CoefAplicMes05 = @CoefActual / @CoefMes05
	SET @CoefAplicMes06 = @CoefActual / @CoefMes06
	SET @CoefAplicMes07 = @CoefActual / @CoefMes07
	SET @CoefAplicMes08 = @CoefActual / @CoefMes08
	SET @CoefAplicMes09 = @CoefActual / @CoefMes09
	SET @CoefAplicMes10 = @CoefActual / @CoefMes10
	SET @CoefAplicMes11 = @CoefActual / @CoefMes11
	SET @CoefAplicMes12 = @CoefActual / @CoefMes12

	IF dbo.CF_FOMONTH(DATEADD(MM, 0,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes01 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 1,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes02 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 2,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes03 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 3,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes04 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 4,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes05 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 5,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes06 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 6,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes07 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 7,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes08 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 8,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes09 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 9,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes10 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 10,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes11 = 0

	IF dbo.CF_FOMONTH(DATEADD(MM, 11,@pFDesde)) > @pFHasta 
		SET @CoefAplicMes12 = 0



	INSERT INTO #Resultado 
			SELECT 'C',0,'','Coeficientes',0,'',''
					,@CoefMes00 AS Mes00
					,@CoefMes01 AS Mes01
					,@CoefMes02 AS Mes02
					,@CoefMes03 AS Mes03
					,@CoefMes04 AS Mes04
					,@CoefMes05 AS Mes05
					,@CoefMes06 AS Mes06
					,@CoefMes07 AS Mes07
					,@CoefMes08 AS Mes08
					,@CoefMes09 AS Mes09
					,@CoefMes10 AS Mes10
					,@CoefMes11 AS Mes11
					,@CoefMes12 AS Mes12
					,0
					,0
	IF UPPER(@pTipo) <> 'C'
		INSERT INTO #Resultado 
			SELECT 'A',0,'','Coeficientes a Aplicar',0,'',''
				,@CoefAplicMes00 AS Mes00 
				,@CoefAplicMes01 AS Mes01
				,@CoefAplicMes02 AS Mes02
				,@CoefAplicMes03 AS Mes03
				,@CoefAplicMes04 AS Mes04
				,@CoefAplicMes05 AS Mes05
				,@CoefAplicMes06 AS Mes06
				,@CoefAplicMes07 AS Mes07
				,@CoefAplicMes08 AS Mes08
				,@CoefAplicMes09 AS Mes09
				,@CoefAplicMes10 AS Mes10
				,@CoefAplicMes11 AS Mes11
				,@CoefAplicMes12 AS Mes12
				,0
				,0
END

IF UPPER(@pTipo) IN ('H','A','D')
BEGIN
	DECLARE AjusInfla CURSOR FOR 
		SELECT P.Id_Cta_Contable,P.Nro_Cta_Contable,P.Descripcion,P.Alias,ISNULL(I.Id_Centro_Costo,''),ISNULL(CC.Descripcion,''),Isnull(P.Id_Alias_Cta_Ajust_Inf,0)
			FROM Pla_Ctas_Cuentas_Contables P, Asientos A, Asientos_Items I, Centro_Costos CC
			WHERE P.Id_Plan_Cta = @pId_Plan_Cta 
			AND ISNULL(P.Monetaria,0) = 0 
			AND P.Imputable = 1
			AND I.Id_Cta_Contable = P.Id_Cta_Contable
			AND A.Id_Asiento = I.Id_Asiento
			AND CC.Id_Centro_Costo = I.Id_Centro_Costo
			AND A.Fecha >= @pFDesde
			AND A.Fecha <  @pFHasta
			GROUP BY P.Id_Cta_Contable,P.Nro_Cta_Contable,P.Descripcion,P.Alias,I.Id_Centro_Costo,CC.Descripcion,P.Id_Alias_Cta_Ajust_Inf
			ORDER BY P.Nro_Cta_Contable,I.Id_Centro_Costo

    OPEN AjusInfla
	FETCH NEXT FROM AjusInfla INTO @Id_Cta_Contable, @Nro_Cta_Contable, @Descripcion ,@Alias, @Id_Centro_Costo, @Descripcion_CC,@AliasCtaAju
					

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @Mes00 = dbo.CF_Cta_Cont_Saldo_Asi	(@pFDesde  ,              DBO.CF_EOMonth(@pFDesde) , @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',dbo.CF_xParam('Asi_Tipo_Apertura') )
   	    SET @Mes01 = dbo.CF_Cta_Cont_Saldo		(@pFDesde  ,              DBO.CF_EOMonth(@pFDesde) , @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1) 

		SET @Mes01 = @Mes01 - @Mes00

		IF dbo.CF_FOMONTH(DATEADD(MM, 1,@pFDesde)) <= @pFHasta 
			SET @Mes02 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 1,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 1,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 2,@pFDesde)) <= @pFHasta 
			SET @Mes03 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 2,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 2,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 3,@pFDesde)) <= @pFHasta 
			SET @Mes04 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 3,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 3,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 4,@pFDesde)) <= @pFHasta 
			SET @Mes05 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 4,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 4,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 5,@pFDesde)) <= @pFHasta 
			SET @Mes06 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 5,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 5,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 6,@pFDesde)) <= @pFHasta 
			SET @Mes07 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 6,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 6,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 7,@pFDesde)) <= @pFHasta 
			SET @Mes08 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 7,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 7,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 8,@pFDesde)) <= @pFHasta 
			SET @Mes09 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 8,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 8,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 9,@pFDesde)) <= @pFHasta 
			SET @Mes10 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM, 9,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM, 9,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 10,@pFDesde)) <= @pFHasta 
			SET @Mes11 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM,10,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM,10,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)
		IF dbo.CF_FOMONTH(DATEADD(MM, 11,@pFDesde)) <= @pFHasta 
			SET @Mes12 = dbo.CF_Cta_Cont_Saldo( dbo.CF_FOMONTH(DATEADD(MM,11,@pFDesde)),DBO.CF_EOMonth(DATEADD(MM,11,@pFDesde)), @Alias, @pId_Plan_Cta,0,1,@Id_Centro_Costo,0,0,'',1)	

	    INSERT INTO #ValorH (Id_Cta_Contable,Nro_Cta_Contable,Descripcion,Alias,Id_Centro_Costo, Descripcion_CC,
						Mes00,Mes01,Mes02,Mes03,Mes04,Mes05,Mes06,Mes07,Mes08,Mes09,Mes10,Mes11,Mes12,AliasCtaAju)
				VALUES (@Id_Cta_Contable, @Nro_Cta_Contable, @Descripcion ,@Alias, @Id_Centro_Costo, @Descripcion_CC
					,@Mes00,@Mes01,@Mes02,@Mes03,@Mes04,@Mes05,@Mes06,@Mes07,@Mes08,@Mes09,@Mes10,@Mes11,@Mes12,@AliasCtaAju)

		FETCH NEXT FROM AjusInfla INTO @Id_Cta_Contable, @Nro_Cta_Contable, @Descripcion ,@Alias, @Id_Centro_Costo, @Descripcion_CC,@AliasCtaAju
	END
	CLOSE AjusInfla
	DEALLOCATE AjusInfla

	INSERT INTO #Resultado 
		SELECT 'H',Id_Cta_Contable,Nro_Cta_Contable,Descripcion,Alias, Id_Centro_Costo, Descripcion_CC,
					Mes00,
					Mes01,
					Mes02,
					Mes03,
					Mes04,
					Mes05,
					Mes06,
					Mes07,
					Mes08,
					Mes09,
					Mes10,
					Mes11,
					Mes12,
					Mes00 + Mes01 + Mes02 + Mes03 + Mes04 + Mes05 + Mes06 + Mes07 + Mes08 + Mes09 + Mes10 + Mes11 + Mes12,
					AliasCtaAju
				FROM #ValorH
END


IF UPPER(@pTipo) IN ('A','D')
BEGIN
	INSERT INTO #Resultado 
	SELECT 	'A',Id_Cta_Contable 
			,Nro_Cta_Contable
			,Descripcion
			,Alias
			,Id_Centro_Costo
			,Descripcion_CC 
			,Mes00 * @CoefAplicMes00 AS Mes00
			,Mes01 * @CoefAplicMes01 AS Mes01
			,Mes02 * @CoefAplicMes02 AS Mes02
			,Mes03 * @CoefAplicMes03 AS Mes03
			,Mes04 * @CoefAplicMes04 AS Mes04
			,Mes05 * @CoefAplicMes05 AS Mes05
			,Mes06 * @CoefAplicMes06 AS Mes06
			,Mes07 * @CoefAplicMes07 AS Mes07
			,Mes08 * @CoefAplicMes08 AS Mes08
			,Mes09 * @CoefAplicMes09 AS Mes09
			,Mes10 * @CoefAplicMes10 AS Mes10
			,Mes11 * @CoefAplicMes11 AS Mes11
			,Mes12 * @CoefAplicMes12 AS Mes12
			,(Mes00 * @CoefAplicMes00) + (Mes01 * @CoefAplicMes01) + (Mes02 * @CoefAplicMes02) + (Mes03 * @CoefAplicMes03) + (Mes04 * @CoefAplicMes04) + (Mes05 * @CoefAplicMes05) + (Mes06 * @CoefAplicMes06) +
			 (Mes07 * @CoefAplicMes07) + (Mes08 * @CoefAplicMes08) + (Mes09 * @CoefAplicMes09) + (Mes10 * @CoefAplicMes10) + (Mes11 * @CoefAplicMes11) + (Mes12 * @CoefAplicMes12)
			,AliasCtaAju
			FROM  #ValorH
END

IF UPPER(@pTipo) IN ('D') 
	INSERT INTO #Resultado 
		SELECT 	'D',Id_Cta_Contable 
			,Nro_Cta_Contable
			,Descripcion
			,Alias 
			,Id_Centro_Costo
			,Descripcion_CC 
			,(Mes00 * @CoefAplicMes00) - Mes00  AS Mes00
			,(Mes01 * @CoefAplicMes01) - Mes01  AS Mes01
			,(Mes02 * @CoefAplicMes02) - Mes02  AS Mes02
			,(Mes03 * @CoefAplicMes03) - Mes03  AS Mes03
			,(Mes04 * @CoefAplicMes04) - Mes04  AS Mes04
			,(Mes05 * @CoefAplicMes05) - Mes05  AS Mes05
			,(Mes06 * @CoefAplicMes06) - Mes06  AS Mes06
			,(Mes07 * @CoefAplicMes07) - Mes07  AS Mes07
			,(Mes08 * @CoefAplicMes08) - Mes08  AS Mes08
			,(Mes09 * @CoefAplicMes09) - Mes09  AS Mes09
			,(Mes10 * @CoefAplicMes10) - Mes10  AS Mes10
			,(Mes11 * @CoefAplicMes11) - Mes11  AS Mes11
			,(Mes12 * @CoefAplicMes12) - Mes12  AS Mes12
			,((Mes00 * @CoefAplicMes00) - Mes00) + ((Mes01 * @CoefAplicMes01) - Mes01) + ((Mes02 * @CoefAplicMes02) - Mes02) + ((Mes03 * @CoefAplicMes03) - Mes03) + ((Mes04 * @CoefAplicMes04) - Mes04) +
			 ((Mes05 * @CoefAplicMes05) - Mes05) + ((Mes06 * @CoefAplicMes06) - Mes06) + ((Mes07 * @CoefAplicMes07) - Mes07) + ((Mes08 * @CoefAplicMes08) - Mes08) +
			 ((Mes09 * @CoefAplicMes09) - Mes09) + ((Mes10 * @CoefAplicMes10) - Mes10) + ((Mes11 * @CoefAplicMes11) - Mes11) + ((Mes12 * @CoefAplicMes12) - Mes12)
			,AliasCtaAju
			FROM  #ValorH


DROP TABLE #ValorH

SELECT * FROM #Resultado

DROP TABLE #Resultado

RETURN


-- EXEC CP_Asientos_AjuInflacion 'C','PES','20170101','20171231',5, 5   --coefcientes
-- EXEC CP_Asientos_AjuInflacion 'F','PES','20170101','20171231',5, 5  --coefcientes a aplicar
-- EXEC CP_Asientos_AjuInflacion 'H','PES','20170101','20171231',5, 5   --Valores Historicos
-- EXEC CP_Asientos_AjuInflacion 'A','PES','20170101','20171231',5, 5   --Valores Actuales
-- EXEC CP_Asientos_AjuInflacion 'D','PES','20170101','20171231',5, 5  --difenrencia
