ALTER PROCEDURE [dbo].[sp_cotContratoFacturar]
(
@coddoc int,
@numdoc int,
@ctacte int,
@mes int,
@dia int,
@grupo int=0,
@periodo int=2017,
@uninegocio int = 0,
@deuda INT = 0
)
AS
DECLARE @saldos AS TABLE
(
	contrato INT,
	saldo DECIMAL(18, 4)
)
DECLARE @ultimodia as int
DECLARE @dthoy as date
DECLARE @dtultimo as date
DECLARE @dtfiltro as date

-----NUEVO----
DECLARE @fecinimes AS DATE	
DECLARE @fecfinmes AS DATE
-------------

Declare @DayOfMonth TinyInt Set @DayOfMonth = 1
Declare @Month TinyInt Set @Month = @mes
Declare @Year Integer Set @Year = @periodo
-- ------------------------------------
SET @dthoy = DateAdd(day, @DayOfMonth - 1,DateAdd(month, @Month - 1, DateAdd(Year, @Year-1900, 0)))

SET @dtultimo = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dthoy)+1,0))

SET @ultimodia = DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dthoy)+1,0)))

SET @dtfiltro = DateAdd(day, @dia - 1  ,DateAdd(month, @mes - 1 , DateAdd(Year, @periodo-1900, 0))) -- ?

 ----NUEVO----
SET @fecinimes = DateAdd(day, @DayOfMonth - 1,DateAdd(month, @Month - 1, DateAdd(Year, @Year-1900, 0)))
SET @fecfinmes = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dthoy)+1,0))
-------------
INSERT @saldos SELECT C.numdoc, SUM(ISNULL(S.saldo, 0))
FROM cotContrato C
LEFT OUTER JOIN cotContratoReferencias R ON C.numdoc = R.Contrato
LEFT OUTER JOIN venSaldoDocumentos S ON R.numinv = S.numinv AND R.codsii = S.codsii
WHERE C.inhabilitado = 0
GROUP BY C.numdoc
ORDER BY numdoc

IF @deuda = 0
	BEGIN
		SELECT  cotContratoDetalle.coddoc, 
			cotContratoDetalle.numdoc,
			cotContratoDetalle.numlin,
			invArticulos.codbus,
			cotContratoDetalle.nomart, 
			cotContratoDetalle.cantidad,
			cotContratoDetalle.precio,
			cotContratoDetalle.porcdescuento,
			cotContratoDetalle.descuento,
			cotContratoDetalle.total, 
			cotContratoDetalle.prelis, 
			cotContratoDetalle.glosa,
			' ' AS checkglolin, 
			'0' AS preunipar, 
			'0' AS pretotpar,
			invArticulos.coduni, 
			--invUnidadMedida.abruni,
			cotContratoDetalle.canalt,
			cotContratoDetalle.porcadicional,
			cotContratoDetalle.adicional,
			cotContratoDetalle.codcen,
			cotContratoDetalle.codana,
			cotContratoDetalle.tip_impadi,
			(cotContratoDetalle.cantidad - cotContratoDetalle.cant_des) as Saldo,
			cotContratoDetalle.dirdes,
			cotContratoDetalle.comdes,
			cotContratoDetalle.ciudes,
			cotContratoDetalle.regdes,
			invArticulos.nomart as nomartori,
			invArticulos.medalt,
			--invUnidadMedidaAlt.abruni AS abrunialt,
			--invUnidadMedidaAlt.nomuni, 
			--invUnidadMedida.nomuni AS nomuniAlt,
			invArticulos.faccon,idlinea, imp_base,imp_variable,
			grupo,tipodocumento,
			cotContrato.dia,
			cotcontrato.cliente,
			cotcontrato.fecha_ingreso,
			cotcontrato.fecha_termino,
			cotcontrato.moneda,
			cotcontrato.tasa,
			convert(decimal(18,2),0) as tasa2,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=1) OR vENDocumentos.afecto <> 'T' THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoexento,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=0) THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoafecto,
			0 as montoiva,
			0 as montoneto,
			[listaprecio]
			,0.000 as [descuentoneto]
			,0.000 as [descuentoglobal]
			,0.000 as 'ColumnIvaLinealTotal'
			,0.000 as 'ColumnDescuentoLineal'
			,0.000 as 'ColumnDescuentoNetoExento'
			,0.000 as 'MontoDescuentoGlobalNetoExento'
			,0.000 as 'ColumnPrecioNetoLinealTotal'
			,0.000 as 'ColumnUnitarioNetoExento' ,'' as pos_item , cotcontrato.costo ,
			--@dthoy AS fechaemision 
			 ----NUEVO----
			@fecinimes as fechaemision
			-------------
			,tipoemision
			,cotContrato.codbod
		INTO #contratos
		FROM cotContratoDetalle INNER JOIN
			invArticulos ON cotContratoDetalle.cod_articulo = invArticulos.codbus INNER JOIN
			cotContrato ON cotContratoDetalle.coddoc = cotContrato.coddoc AND cotContratoDetalle.numdoc = cotContrato.numdoc INNER JOIN
			invFamilia ON invArticulos.codfam = invFamilia.codfam INNER JOIN
			venDocumentos ON cotContratoDetalle.tipodocumento = venDocumentos.codsii
		WHERE    venDocumentos.grudoc=10 AND (cotContratoDetalle.coddoc = @coddoc) 
			AND (@ctacte=0 OR cotContrato.cliente=@ctacte)
			AND
				(
				(mes01=1 AND @MES=1) OR (mes02=1 AND @MES=2) or (mes03=1 AND @MES=3) or 
				(mes04=1 AND @MES=4) or (mes05=1 AND @MES=5) OR (mes06=1 AND @MES=6) or
				(mes07=1 AND @MES=7) or (mes08=1 AND @MES=8) or (mes09=1 AND @MES=9) OR 
				(mes10=1 AND @MES=10) or (mes11=1 AND @MES=11) or (mes12=1 AND @MES=12)
				)
			AND (dia = @dia or (dia= 0 and @ultimodia=@dia) OR @dia=-2)
			AND NOT cotcontrato.numdoc IN 
				( 
					 SELECT CONTRATO
					 FROM cotcontratoreferencias REF
					 where REF.contrato=cotContratoDetalle.numdoc 
					 AND REF.grupo=cotContratoDetalle.grupo 
					 AND REF.MES=@MES
					 AND REF.periodo=@periodo
					 AND NOT EXISTS 
					 (SELECT E.numinv FROM venEncabezado E 
					 WHERE E.tipref1=REF.codsii
					 AND E.docref1=CONVERT(VARCHAR,REF.numinv)
					 AND E.grudoc=10 AND E.estado <> 9 AND E.motivo = 1)
				)
			----NUEVO----
			AND ((cotcontrato.fecha_termino > = @fecinimes OR cotContrato.fecha_termino = '1900-01-01') AND (fecha_validez < = @fecfinmes  OR cotContrato.fecha_validez = '1900-01-01'))
			AND (cotContrato.inhabilitado <> 1 or cotContrato.inhabilitado IS NULL) 
			-------------
			--AND (cotContrato.fecha_termino > @dtfiltro OR NOT cotContrato.fecha_termino <> '1900-01-01')
			AND (cotcontrato.costo = @uninegocio OR @uninegocio = 0)

			ORDER BY cotContratoDetalle.numlin

		IF @GRUPO=0
			BEGIN
				SELECT numdoc,
					cliente,
					convert(varchar,rutcte) + '-' + digcte as rut,
					nomcte,
					fecha_ingreso,
					min(fecha_termino) as fechatermino,
					count(numlin) as lineas,
					grupo,
					moneda,
					nommon,
					tipodocumento,
					case dia WHEN 0 THEN @ultimodia else dia END as dia,tasa2,
					sum(montoexento) as montoexento,
					sum(montoafecto) as montoafecto,
					sum( montoiva) as montoiva,
					sum(montoneto) as montoneto,
					tasa,sum(total) as montofacturar, 
					fechaemision,
					tipoemision,costo, 
					conUnidadNegocio.descripcion as descUnineg
				FROM #contratos 
					INNER JOIN conCtacte ON #contratos .cliente = conCtacte.rutcte
					INNER JOIN conMonedas ON #contratos.moneda = conMonedas.codmon
					INNER JOIN conUnidadNegocio ON #contratos.costo = conUnidadNegocio.idUniNeg
				GROUP BY numdoc,cliente,fecha_ingreso,grupo,moneda,tasa,rutcte,digcte,nomcte,nommon,tipodocumento,dia
				,tasa2, fechaemision ,tipoemision,costo,conUnidadNegocio.descripcion
			END

		IF @GRUPO=0
			BEGIN
				SELECT  *, 
					0 as 'np',
					idlinea as idlinearef,
					1 as nueva,precio as preuni,
					0 as pretot,glosa as glolin,
					0 as saldoGRAL,0 as 'StockBodega', 
					0 as costo,
					0 as costobodega,
					0 as 'stockGral',
					0 as 'stockBodega', 
					0 as 'saldoGral', 
					'01-01-1900' as 'fechacosto'
				FROM #contratos
		--WHERE grupo = @grupo
			END

		SELECT 
			CASE @mes WHEN 1 THEN mes01
				WHEN 1 THEN mes01
				WHEN 2 THEN mes02
				WHEN 3 THEN mes03
				WHEN 4 THEN mes04
				WHEN 5 THEN mes05
				WHEN 6 THEN mes06
				WHEN 7 THEN mes07
				WHEN 8 THEN mes08
				WHEN 9 THEN mes09
				WHEN 10 THEN mes10
				WHEN 11 THEN mes11
				WHEN 12 THEN mes12
			END valor,
			dia,
			codmon,
			año
		 FROM venParidades 
		 WHERE año=@periodo OR año=@periodo-1 OR año=@periodo +1
	END
--CON DEUDA
IF @deuda = 1
	BEGIN
		SELECT  cotContratoDetalle.coddoc, 
			cotContratoDetalle.numdoc,
			cotContratoDetalle.numlin,
			invArticulos.codbus,
			cotContratoDetalle.nomart, 
			cotContratoDetalle.cantidad,
			cotContratoDetalle.precio,
			cotContratoDetalle.porcdescuento,
			cotContratoDetalle.descuento,
			cotContratoDetalle.total, 
			cotContratoDetalle.prelis, 
			cotContratoDetalle.glosa,
			' ' AS checkglolin, 
			'0' AS preunipar, 
			'0' AS pretotpar,
			invArticulos.coduni, 
			--invUnidadMedida.abruni,
			cotContratoDetalle.canalt,
			cotContratoDetalle.porcadicional,
			cotContratoDetalle.adicional,
			cotContratoDetalle.codcen,
			cotContratoDetalle.codana,
			cotContratoDetalle.tip_impadi,
			(cotContratoDetalle.cantidad - cotContratoDetalle.cant_des) as Saldo,
			cotContratoDetalle.dirdes,
			cotContratoDetalle.comdes,
			cotContratoDetalle.ciudes,
			cotContratoDetalle.regdes,
			invArticulos.nomart as nomartori,
			invArticulos.medalt,
			--invUnidadMedidaAlt.abruni AS abrunialt,
			--invUnidadMedidaAlt.nomuni, 
			--invUnidadMedida.nomuni AS nomuniAlt,
			invArticulos.faccon,idlinea, imp_base,imp_variable,
			grupo,tipodocumento,
			cotContrato.dia,
			cotcontrato.cliente,
			cotcontrato.fecha_ingreso,
			cotcontrato.fecha_termino,
			cotcontrato.moneda,
			cotcontrato.tasa,
			convert(decimal(18,2),0) as tasa2,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=1) OR vENDocumentos.afecto <> 'T' THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoexento,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=0) THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoafecto,
			0 as montoiva,
			0 as montoneto,
			[listaprecio]
			,0.000 as [descuentoneto]
			,0.000 as [descuentoglobal]
			,0.000 as 'ColumnIvaLinealTotal'
			,0.000 as 'ColumnDescuentoLineal'
			,0.000 as 'ColumnDescuentoNetoExento'
			,0.000 as 'MontoDescuentoGlobalNetoExento'
			,0.000 as 'ColumnPrecioNetoLinealTotal'
			,0.000 as 'ColumnUnitarioNetoExento' ,'' as pos_item , cotcontrato.costo ,
			--@dthoy AS fechaemision 
			 ----NUEVO----
			@fecinimes as fechaemision
			-------------
			,tipoemision
			,cotContrato.codbod
		INTO #contratos1
		FROM cotContratoDetalle INNER JOIN
			invArticulos ON cotContratoDetalle.cod_articulo = invArticulos.codbus INNER JOIN
			cotContrato ON cotContratoDetalle.coddoc = cotContrato.coddoc AND cotContratoDetalle.numdoc = cotContrato.numdoc INNER JOIN
			invFamilia ON invArticulos.codfam = invFamilia.codfam INNER JOIN
			venDocumentos ON cotContratoDetalle.tipodocumento = venDocumentos.codsii
		WHERE    venDocumentos.grudoc=10 AND (cotContratoDetalle.coddoc = @coddoc) 
			AND (@ctacte=0 OR cotContrato.cliente=@ctacte)
			AND
				(
				(mes01=1 AND @MES=1) OR (mes02=1 AND @MES=2) or (mes03=1 AND @MES=3) or 
				(mes04=1 AND @MES=4) or (mes05=1 AND @MES=5) OR (mes06=1 AND @MES=6) or
				(mes07=1 AND @MES=7) or (mes08=1 AND @MES=8) or (mes09=1 AND @MES=9) OR 
				(mes10=1 AND @MES=10) or (mes11=1 AND @MES=11) or (mes12=1 AND @MES=12)
				)
			AND (dia = @dia or (dia= 0 and @ultimodia=@dia) OR @dia=-2)
			AND NOT cotcontrato.numdoc IN 
				( 
					 SELECT CONTRATO
					 FROM cotcontratoreferencias REF
					 where REF.contrato=cotContratoDetalle.numdoc 
					 AND REF.grupo=cotContratoDetalle.grupo 
					 AND REF.MES=@MES
					 AND REF.periodo=@periodo
					 AND NOT EXISTS 
					 (SELECT E.numinv FROM venEncabezado E 
					 WHERE E.tipref1=REF.codsii
					 AND E.docref1=CONVERT(VARCHAR,REF.numinv)
					 AND grudoc=10 AND estado <> 9 AND motivo = 1)
				)
			AND cotcontrato.numdoc IN
				( 
					 SELECT CONTRATO
					 FROM @saldos
					 WHERE saldo > 0
				)
			----NUEVO----
			AND ((cotcontrato.fecha_termino > = @fecinimes OR cotContrato.fecha_termino = '1900-01-01') AND (fecha_validez < = @fecfinmes  OR cotContrato.fecha_validez = '1900-01-01'))
			AND (cotContrato.inhabilitado <> 1 or cotContrato.inhabilitado IS NULL) 
			-------------
			--AND (cotContrato.fecha_termino > @dtfiltro OR NOT cotContrato.fecha_termino <> '1900-01-01')
			AND (cotcontrato.costo = @uninegocio OR @uninegocio = 0)

			ORDER BY cotContratoDetalle.numlin

		--select * FROM @saldos
		IF @GRUPO=0
			BEGIN
				SELECT numdoc,
					cliente,
					convert(varchar,rutcte) + '-' + digcte as rut,
					nomcte,
					fecha_ingreso,
					min(fecha_termino) as fechatermino,
					count(numlin) as lineas,
					grupo,
					moneda,
					nommon,
					tipodocumento,
					case dia WHEN 0 THEN @ultimodia else dia END as dia,tasa2,
					sum(montoexento) as montoexento,
					sum(montoafecto) as montoafecto,
					sum( montoiva) as montoiva,
					sum(montoneto) as montoneto,
					tasa,sum(total) as montofacturar, 
					fechaemision,
					tipoemision,costo, 
					conUnidadNegocio.descripcion as descUnineg
				FROM #contratos1 
					INNER JOIN conCtacte ON #contratos1 .cliente = conCtacte.rutcte
					INNER JOIN conMonedas ON #contratos1.moneda = conMonedas.codmon
					INNER JOIN conUnidadNegocio ON #contratos1.costo = conUnidadNegocio.idUniNeg
				GROUP BY numdoc,cliente,fecha_ingreso,grupo,moneda,tasa,rutcte,digcte,nomcte,nommon,tipodocumento,dia
				,tasa2, fechaemision ,tipoemision,costo,conUnidadNegocio.descripcion
			END

		IF @GRUPO=0
			BEGIN
				SELECT  *, 
					0 as 'np',
					idlinea as idlinearef,
					1 as nueva,precio as preuni,
					0 as pretot,glosa as glolin,
					0 as saldoGRAL,0 as 'StockBodega', 
					0 as costo,
					0 as costobodega,
					0 as 'stockGral',
					0 as 'stockBodega', 
					0 as 'saldoGral', 
					'01-01-1900' as 'fechacosto'
				FROM #contratos1
		--WHERE grupo = @grupo
			END

		SELECT 
			CASE @mes WHEN 1 THEN mes01
				WHEN 1 THEN mes01
				WHEN 2 THEN mes02
				WHEN 3 THEN mes03
				WHEN 4 THEN mes04
				WHEN 5 THEN mes05
				WHEN 6 THEN mes06
				WHEN 7 THEN mes07
				WHEN 8 THEN mes08
				WHEN 9 THEN mes09
				WHEN 10 THEN mes10
				WHEN 11 THEN mes11
				WHEN 12 THEN mes12
			END valor,
			dia,
			codmon,
			año
		 FROM venParidades 
		 WHERE año=@periodo OR año=@periodo-1 OR año=@periodo +1
	END

--SIN DEUDA
IF @deuda = 2
	BEGIN
		SELECT  cotContratoDetalle.coddoc, 
			cotContratoDetalle.numdoc,
			cotContratoDetalle.numlin,
			invArticulos.codbus,
			cotContratoDetalle.nomart, 
			cotContratoDetalle.cantidad,
			cotContratoDetalle.precio,
			cotContratoDetalle.porcdescuento,
			cotContratoDetalle.descuento,
			cotContratoDetalle.total, 
			cotContratoDetalle.prelis, 
			cotContratoDetalle.glosa,
			' ' AS checkglolin, 
			'0' AS preunipar, 
			'0' AS pretotpar,
			invArticulos.coduni, 
			--invUnidadMedida.abruni,
			cotContratoDetalle.canalt,
			cotContratoDetalle.porcadicional,
			cotContratoDetalle.adicional,
			cotContratoDetalle.codcen,
			cotContratoDetalle.codana,
			cotContratoDetalle.tip_impadi,
			(cotContratoDetalle.cantidad - cotContratoDetalle.cant_des) as Saldo,
			cotContratoDetalle.dirdes,
			cotContratoDetalle.comdes,
			cotContratoDetalle.ciudes,
			cotContratoDetalle.regdes,
			invArticulos.nomart as nomartori,
			invArticulos.medalt,
			--invUnidadMedidaAlt.abruni AS abrunialt,
			--invUnidadMedidaAlt.nomuni, 
			--invUnidadMedida.nomuni AS nomuniAlt,
			invArticulos.faccon,idlinea, imp_base,imp_variable,
			grupo,tipodocumento,
			cotContrato.dia,
			cotcontrato.cliente,
			cotcontrato.fecha_ingreso,
			cotcontrato.fecha_termino,
			cotcontrato.moneda,
			cotcontrato.tasa,
			convert(decimal(18,2),0) as tasa2,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=1) OR vENDocumentos.afecto <> 'T' THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoexento,
			CASE WHEN (vENDocumentos.afecto='T' AND invFamilia.exento=0) THEN cotContratoDetalle.precio * cotContratoDetalle.cantidad ELSE 0 END as montoafecto,
			0 as montoiva,
			0 as montoneto,
			[listaprecio]
			,0.000 as [descuentoneto]
			,0.000 as [descuentoglobal]
			,0.000 as 'ColumnIvaLinealTotal'
			,0.000 as 'ColumnDescuentoLineal'
			,0.000 as 'ColumnDescuentoNetoExento'
			,0.000 as 'MontoDescuentoGlobalNetoExento'
			,0.000 as 'ColumnPrecioNetoLinealTotal'
			,0.000 as 'ColumnUnitarioNetoExento' ,'' as pos_item , cotcontrato.costo ,
			--@dthoy AS fechaemision 
			 ----NUEVO----
			@fecinimes as fechaemision
			-------------
			,tipoemision
			,cotContrato.codbod
		INTO #contratos2
		FROM cotContratoDetalle INNER JOIN
			invArticulos ON cotContratoDetalle.cod_articulo = invArticulos.codbus INNER JOIN
			cotContrato ON cotContratoDetalle.coddoc = cotContrato.coddoc AND cotContratoDetalle.numdoc = cotContrato.numdoc INNER JOIN
			invFamilia ON invArticulos.codfam = invFamilia.codfam INNER JOIN
			venDocumentos ON cotContratoDetalle.tipodocumento = venDocumentos.codsii
		WHERE    venDocumentos.grudoc=10 AND (cotContratoDetalle.coddoc = @coddoc) 
			AND (@ctacte=0 OR cotContrato.cliente=@ctacte)
			AND
				(
				(mes01=1 AND @MES=1) OR (mes02=1 AND @MES=2) or (mes03=1 AND @MES=3) or 
				(mes04=1 AND @MES=4) or (mes05=1 AND @MES=5) OR (mes06=1 AND @MES=6) or
				(mes07=1 AND @MES=7) or (mes08=1 AND @MES=8) or (mes09=1 AND @MES=9) OR 
				(mes10=1 AND @MES=10) or (mes11=1 AND @MES=11) or (mes12=1 AND @MES=12)
				)
			AND (dia = @dia or (dia= 0 and @ultimodia=@dia) OR @dia=-2)
			AND NOT cotcontrato.numdoc IN 
				( 
					 SELECT CONTRATO
					 FROM cotcontratoreferencias REF
					 WHERE REF.contrato=cotContratoDetalle.numdoc 
					 AND REF.grupo=cotContratoDetalle.grupo 
					 AND REF.MES=@MES
					 AND REF.periodo=@periodo
					 AND NOT EXISTS 
					 (SELECT E.numinv FROM venEncabezado E 
					 WHERE E.tipref1=REF.codsii
					 AND E.docref1=CONVERT(VARCHAR,REF.numinv)
					 AND grudoc=10 AND estado <> 9 AND motivo = 1)
				)
			AND cotcontrato.numdoc IN
				( 
					 SELECT CONTRATO
					 FROM @saldos
					 WHERE saldo <= 0					 
				)
			----NUEVO----
			AND ((cotcontrato.fecha_termino > = @fecinimes OR cotContrato.fecha_termino = '1900-01-01') AND (fecha_validez < = @fecfinmes  OR cotContrato.fecha_validez = '1900-01-01'))
			AND (cotContrato.inhabilitado <> 1 or cotContrato.inhabilitado IS NULL) 
			-------------
			--AND (cotContrato.fecha_termino > @dtfiltro OR NOT cotContrato.fecha_termino <> '1900-01-01')
			AND (cotcontrato.costo = @uninegocio OR @uninegocio = 0)

			ORDER BY cotContratoDetalle.numlin
		--select * FROM @saldos
		IF @GRUPO=0
			BEGIN
				SELECT numdoc,
					cliente,
					convert(varchar,rutcte) + '-' + digcte as rut,
					nomcte,
					fecha_ingreso,
					min(fecha_termino) as fechatermino,
					count(numlin) as lineas,
					grupo,
					moneda,
					nommon,
					tipodocumento,
					case dia WHEN 0 THEN @ultimodia else dia END as dia,tasa2,
					sum(montoexento) as montoexento,
					sum(montoafecto) as montoafecto,
					sum( montoiva) as montoiva,
					sum(montoneto) as montoneto,
					tasa,sum(total) as montofacturar, 
					fechaemision,
					tipoemision,costo, 
					conUnidadNegocio.descripcion as descUnineg
				FROM #contratos2 
					INNER JOIN conCtacte ON #contratos2.cliente = conCtacte.rutcte
					INNER JOIN conMonedas ON #contratos2.moneda = conMonedas.codmon
					INNER JOIN conUnidadNegocio ON #contratos2.costo = conUnidadNegocio.idUniNeg
				GROUP BY numdoc,cliente,fecha_ingreso,grupo,moneda,tasa,rutcte,digcte,nomcte,nommon,tipodocumento,dia
				,tasa2, fechaemision ,tipoemision,costo,conUnidadNegocio.descripcion
			END

		IF @GRUPO=0
			BEGIN
				SELECT  *, 
					0 as 'np',
					idlinea as idlinearef,
					1 as nueva,precio as preuni,
					0 as pretot,glosa as glolin,
					0 as saldoGRAL,0 as 'StockBodega', 
					0 as costo,
					0 as costobodega,
					0 as 'stockGral',
					0 as 'stockBodega', 
					0 as 'saldoGral', 
					'01-01-1900' as 'fechacosto'
				FROM #contratos2
		--WHERE grupo = @grupo
			END

		SELECT 
			CASE @mes WHEN 1 THEN mes01
				WHEN 1 THEN mes01
				WHEN 2 THEN mes02
				WHEN 3 THEN mes03
				WHEN 4 THEN mes04
				WHEN 5 THEN mes05
				WHEN 6 THEN mes06
				WHEN 7 THEN mes07
				WHEN 8 THEN mes08
				WHEN 9 THEN mes09
				WHEN 10 THEN mes10
				WHEN 11 THEN mes11
				WHEN 12 THEN mes12
			END valor,
			dia,
			codmon,
			año
		 FROM venParidades 
		 WHERE año=@periodo OR año=@periodo-1 OR año=@periodo +1
	END
 
  --and (dia=@dia OR @DIA=0)


--SELECT @mes