SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIfIER OFF
GO

IF EXISTS (Select * From sys.objects 
   Where object_id = OBJECT_ID(N'WINV_ResuxConsecutivo') )
DROP Procedure WINV_ResuxConsecutivo
GO

CREATE PROCEDURE WINV_ResuxConsecutivo
					@list_alma varchar(8000), 
					@list_secc varchar(8000), 
					@fdesde datetime,
					@fhasta datetime,
					@tipo_oper int,
					@pEndientes bit,
					@operador int
AS

Begin
/*
   Date: Apr 20,2016

   Resumido por Consecutivo

*/

   Declare @Cad varchar(8000), 
			@Filtro_fecha varchar(200), 
			@Filtro_destino varchar(8000), 
			@Filtro_opers varchar(200),
			@cmov int,
			@id_datos as integer,
			@base_datos as varchar(30),
			@desc_datos as varchar(50)

/* =========================================
   Region 01
   -- Preparo los parámetros
   =========================================
*/

	Set @Filtro_fecha=''

	If @fdesde is not null
		Select @Filtro_fecha = ' and DatedIff(dd, convert(datetime,"'
							 + convert(varchar(20),@fdesde,101)
							 + '",101), oper.fec_oper) >= 0'
	If @fhasta is not null
		Select @Filtro_fecha = @Filtro_fecha 
							 + ' and DatedIff(dd, oper.fec_oper, convert(datetime,"'
							 + convert(varchar(20),@fhasta,101)
							 + '",101)) >= 0'

	Select @cmov = null

	If @tipo_oper is not null
		Select @cmov=id_cmov 
		   From stoktmov 
		   Where id_tmov=@tipo_oper

	Select @Filtro_opers=''

	If @tipo_oper is not null
		Select @Filtro_opers = ' and oper.id_tmov = '
							 + convert(varchar(14),@tipo_oper)

	If @pEndientes = 0
		Select @Filtro_opers = @Filtro_opers
							 + ' and oper.estado != 1'
	If @operador is not null
		Select @Filtro_opers = @Filtro_opers
							 +' and oper.id_oper = '
							 + convert(varchar(14),@operador)

/* =========================================
   Region 02
   -- Crear tablas temporales de empresas destinos
   =========================================
*/

	CREATE TABLE #Almas (
			id_datos int,
			desc_datos varchar(50),
			id_alma smallint,
			codigo varchar(5),
			descripcion varchar(50) )

	CREATE TABLE #Seccs (
			id_datos int,
			desc_datos varchar(50),
			id_secc smallint,
			codigo varchar(5),
			descripcion varchar(50) )

	Declare curdatos cursor 
	    For Select CAST(d.id_datos as varchar(14)), 
		           d.base_datos, d.descripcion  
			   From STOKVMAL v 
			        inner join Datos d on v.id_datos = d.id_datos
	Open curdatos
	  Fetch Next From curdatos 
	        into @id_datos, @base_datos, @desc_datos

	While @@fetch_status = 0
		Begin
			execute('
					Insert #Almas Select '
							+ @id_datos
							+ ', "'
							+ @desc_datos
							+ '", id_alma, codigo, descripcion  From '
							+ @base_datos+'.dbo.stokalma

					Insert #Seccs Select '
							+ @id_datos	+ ', "'
							+ @desc_datos +'",id_secc, codigo, descripcion  From '
							+ @base_datos+'.dbo.stoksecc
				')
		
			Fetch Next From curdatos into @id_datos, @base_datos, @desc_datos
		End
	Close curdatos
	Deallocate curdatos


/* =========================================
   Region 03
   -- Crear tabla temporal
   =========================================
*/
CREATE TABLE #opers (
		id_operacion int,
		id_tmov smallint,
		consecutivo int,
		referencia char(10),
		desc_tmov char(50),
		estado tinyint,
		fec_ent datetime,
		fec_oper datetime,
		id_oper int,
		descripcion varchar(200) )

/* =========================================
   Region 04
     -- Comienzo a seleccionar las operaciones
   =========================================
*/


/* =========================================
   Region 05
     -- Compras
   =========================================
*/ 

If     @cmov is null 
   or (@cmov is not null and @cmov=1)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
			                       + @list_alma + 
								   ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
				                       + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in ('
					                       + @list_secc + ') )'
			End

		Select @Cad = 
		  'Insert #opers
		      Select oper.id_operacion, oper.id_tmov, oper.consecutivo, 
			         oper.referencia, desc_tmov=tmov.descripcion, oper.estado,
			         oper.fec_ent, oper.fec_oper, oper.id_oper, 
					 Case When i.id_alma is not null 
					      Then "Alm. "+dbo.FullDescripcionAlmacen(i.id_alma) 
						  Else "Secc. "+dbo.FullDescripcionSeccion(i.id_secc) 
						  End
			              +" Prov. "+RTRIM(LTRIM(p.siglas)) 
						  +" Fact. "+LTRIM(c.factura)
		From stokoper oper
			INNER JOIN stokcmpc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stokcmpi i ON c.id_operacion=i.id_operacion
			INNER JOIN stokprov p ON c.id_prov=p.id_prov
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 1 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 06
   -- Reversiones de Compras
   =========================================
*/ 
If @cmov is null 
   or (@cmov is not null and @cmov=2)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
								   + @list_alma + 
								   ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
			          oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
				      oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Rev. Nro. "+RTRIM(convert(varchar(14),r.consecutivo))
			          +"   "+ Case When i.id_alma is not null 
					               Then "Alm. "+dbo.FullDescripcionAlmacen(i.id_alma) 
								   Else "Secc. "+dbo.FullDescripcionSeccion(i.id_secc) 
								   End
			          +" Prov. "+RTRIM(LTRIM(p.siglas))+" Fact. "+LTRIM(c.factura)
		From stokoper oper
			INNER JOIN stokoper r ON oper.id_revertido=r.id_operacion
			INNER JOIN stokcmpc c ON r.id_operacion=c.id_operacion
			INNER JOIN stokcmpi i ON c.id_operacion=i.id_operacion
			INNER JOIN stokprov p ON c.id_prov=p.id_prov
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 2 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End


/* =========================================
   Region 07
   -- Devoluciones
   =========================================
*/ 

If @cmov is null 
   or (@cmov is not null and @cmov = 32)
	Begin
		Select @Filtro_destino = ''
		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in ('
			                       + @list_alma + 
								   ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
									       + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, oper.consecutivo, 
		              oper.referencia, desc_tmov=tmov.descripcion, oper.estado,
			          oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Devol. Nro. "+RTRIM(convert(varchar(14),oper.consecutivo))
			+"   "+Case When i.id_alma is not null 
					    Then "Alm. "+dbo.FullDescripcionAlmacen(i.id_alma) 
						Else "Secc. "+dbo.FullDescripcionSeccion(i.id_secc) 
						End
			+" Prov. "+RTRIM(LTRIM(p.siglas))
		From stokoper oper
			INNER JOIN stokdevc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stokdevi i ON c.id_operacion=i.id_operacion
			INNER JOIN stokprov p ON c.id_prov=p.id_prov
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 32 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino

		EXECUTE (@CAD)
	End

/* =========================================
   Region 08
   --Traslados
   =========================================
*/ 

If @cmov is null 
	or (@cmov is not null and @cmov=3)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
			and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_almaO is not null and id_almaO in (' 
								   + @list_alma + 
								   ')) or (id_seccO is not null and id_seccO in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_almaO is not null and id_almaO in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_seccO is not null and id_seccO in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Orig. "+Case When c.id_almaO is not null 
							        Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_almaO) 
									Else "Secc. "+dbo.FullDescripcionSeccion(c.id_seccO) 
									End
			+"   Dest. "+Case When c.id_almaD is not null 
							  Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_almaD) 
							  Else "Secc. "+dbo.FullDescripcionSeccion(c.id_seccD) 
							  End
		From stokoper oper
			INNER JOIN stoktrac c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 3 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 09
   -- Reversiones de Traslados
   =========================================
*/ 
If @cmov is null 
   or (@cmov is not null and @cmov=4)
	Begin
		Select @Filtro_destino = ''
		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_almaO is not null and id_almaO in ('
								   + @list_alma + ')) or (id_seccO is not null and id_seccO in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_almaO is not null and id_almaO in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_seccO is not null and id_seccO in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		  'Insert #opers
		      Select oper.id_operacion, oper.id_tmov, 
			         oper.consecutivo, oper.referencia,
					 desc_tmov=tmov.descripcion, oper.estado,
					 oper.fec_ent, oper.fec_oper, oper.id_oper, 
					 "Rev. Nro. "+RTRIM(convert(varchar(14),r.consecutivo))
			+"   Orig. "+ Case When c.id_almaO is not null 
			              Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_almaO) 
						  Else "Secc. "+dbo.FullDescripcionSeccion(c.id_seccO) 
						  End
			+"   Dest. "+ Case When c.id_almaD is not null 
			                   Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_almaD) 
							   Else "Secc. "+dbo.FullDescripcionSeccion(c.id_seccD) 
							   End
		From stokoper oper
			INNER JOIN stokoper r ON oper.id_revertido=r.id_operacion
			INNER JOIN stoktrac c ON r.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 4 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 10
   --MultiTraslados
   =========================================
*/ 

If @cmov is null 
   or (@cmov is not null and @cmov=7)
	Begin
		Select @Filtro_destino = ''
		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_almaO is not null and id_almaO in (' 
			                       + @list_alma + ')) or (id_seccO is not null and id_seccO in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_almaO is not null and id_almaO in (' 
				                       + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_seccO is not null and id_seccO in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  Case When c.id_dato_or = dbo.GetCurrentDato() 
						   Then "Saliente " 
						   Else "Entrante " 
						   End +
			"Orig. Empr. "+ao.desc_datos+ 
			   Case When c.id_almaO is not null
			        Then " Alm. "+ao.codigo+" - "+ao.descripcion 
					Else " Secc. "+so.codigo+" - "+so.descripcion 
					End +
			"   Dest. Empr. "+ ad.desc_datos
			            + Case When c.id_almaD is not null 
						       Then " Alm. "+ad.codigo+" - "+ad.descripcion 
							   Else " Secc. "+sd.codigo+" - "+sd.descripcion 
							   End
		From stokoper oper
			INNER JOIN stokmalc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
			left join #Almas as ao on c.id_almaO = ao.id_alma 
			     and c.id_dato_or =  ao.id_datos 
				 and c.id_almaO is not null
			left join #Seccs as so on c.id_seccO =  so.id_secc 
			     and c.id_dato_or =  so.id_datos  
				 and c.id_seccO is not null
			left join #Almas as ad on c.id_almaD = ad.id_alma 
				 and c.id_dato_de =  ad.id_datos 
				 and c.id_almaD is not null
			left join #Seccs as sd on c.id_seccD =  sd.id_secc 
				 and c.id_dato_de =  sd.id_datos  
				 and c.id_seccD is not null
		Where tmov.id_cmov = 7 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 11
   -- Ajustes
   =========================================
*/ 

If @cmov is null 
   or (@cmov is not null and @cmov=5)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
								   + @list_alma + ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
										   + @list_secc + ') )'
			End

		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  Case When c.id_alma is not null 
						   Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_alma) 
						   Else "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
						   End
		From stokoper oper
			INNER JOIN stokajuc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 5 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 12
   v
   =========================================
*/ 
If @cmov is null
   or (@cmov is not null and @cmov=6)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
								   + @list_alma + ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
										   + @list_secc + ') )'
			End

		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Rev. Nro. "+RTRIM(convert(varchar(14),r.consecutivo))
			+"   "+ Case When c.id_alma is not null 
				    Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_alma) 
					Else "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
					End
		From stokoper oper
			INNER JOIN stokoper r ON oper.id_revertido=r.id_operacion
			INNER JOIN stokajuc c ON r.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 6 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 13
   -- Fijación de inventarios
   =========================================
*/ 

If @cmov is null 
   or (@cmov is not null and @cmov=9)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
								   + @list_alma + ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  Case When c.id_alma is not null 
						   Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_alma) 
						   Else "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
						   End
		From stokoper oper
			INNER JOIN stokinvc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 9 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 14
   -- Despieces
   =========================================
*/ 
If @cmov is null 
   or (@cmov is not null and @cmov=10)
	Begin
		Select @Filtro_destino = ''
		If @list_alma is not NULL
		   and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
			                       + @list_alma + ')) or (id_secc is not null and id_secc in (' 
								   + @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
									   + @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
										   + @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  Case When c.id_alma is not null 
					       Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_alma) 
						   Else "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
						   End
		From stokoper oper
			INNER JOIN stokdesc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 10 '  
		+ @Filtro_fecha 
		+ @Filtro_opers
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 15
   -- Reversiones de Despieces
   =========================================
*/ 

If @cmov is null 
	or (@cmov is not null and @cmov=11)
	Begin
		Select @Filtro_destino = ''

		If @list_alma is not NULL 
			and @list_secc is not NULL
			Select @Filtro_destino = ' AND ( (id_alma is not null and id_alma in (' 
									+ @list_alma + ')) or (id_secc is not null and id_secc in (' 
									+ @list_secc + ')) ) '
		Else
			Begin 
			 If @list_alma is not NULL
			 	Select @Filtro_destino = ' AND (id_alma is not null and id_alma in (' 
										+ @list_alma + ') )'
			 Else
				If @list_secc is not NULL
					Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
											+ @list_secc + ') )'
			End
		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Rev. Nro. "+RTRIM(convert(varchar(14),r.consecutivo))
				+"   "+ Case When c.id_alma is not null 
							 Then "Alm. "+dbo.FullDescripcionAlmacen(c.id_alma) 
							 Else "Secc. "+dbo.FullDescripcionSeccion(c.id_secc)
							 End
		From stokoper oper
			INNER JOIN stokoper r ON oper.id_revertido=r.id_operacion
			INNER JOIN stokdesc c ON r.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 11 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 16
   -- Rebaja Automatica
   =========================================
*/ 
If @cmov is null 
	or (@cmov is not null and @cmov=12)
	Begin
		Select @Filtro_destino = ''

		If @list_secc is not NULL
			Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
									+ @list_secc + ') )'

		Select @Cad = 
		   'Insert #opers
		       Select oper.id_operacion, oper.id_tmov, 
					  oper.consecutivo, oper.referencia, 
					  desc_tmov=tmov.descripcion, oper.estado,
					  oper.fec_ent, oper.fec_oper, oper.id_oper, 
					  "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
					  +"   Pto. Vta. "+ v.cod_pvta+" "
					  +RTRIM(v.desc_datos)
					  +"   Cierre Nro. "+convert(varchar(14),c.nro_cierre)
		From stokoper oper
			INNER JOIN stokrebc c ON oper.id_operacion=c.id_operacion
			INNER JOIN datopvta v ON c.id_pvta=v.id_pvta 
			  and c.id_dato=v.id_datos
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 12 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 17
   -- Cambio de código
   =========================================
*/ 
If @cmov is null 
	or (@cmov is not null and @cmov=13)
	Begin
		Select @Filtro_destino = ''

		Select @Cad = 
			'Insert #opers
				Select oper.id_operacion, oper.id_tmov, 
					   oper.consecutivo, oper.referencia, 
					   desc_tmov=tmov.descripcion, oper.estado,
					   oper.fec_ent, oper.fec_oper, oper.id_oper, 
					   "Articulo "+dbo.FullArtiCode(c.id_arti) 
		From stokoper oper
			INNER JOIN stokcamc c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 13 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
	
		EXECUTE (@CAD)
	End

/* =========================================
   Region 18
   -- Cambios de UM, orígenes y destinos
   =========================================
*/ 
If @cmov is null 
	or (@cmov is not null and @cmov=14)
	Begin
		Select @Filtro_destino = ''

		Select @Cad = 
			'Insert #opers
				Select oper.id_operacion, oper.id_tmov, 
					   oper.consecutivo, oper.referencia, 
					   desc_tmov=tmov.descripcion, oper.estado,
					   oper.fec_ent, oper.fec_oper, oper.id_oper, 
					   "Articulo "+dbo.FullArtiCode(c.id_arti) 
		From stokoper oper
			INNER JOIN stokcamu c ON oper.id_operacion=c.id_operacion
			INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
		Where tmov.id_cmov = 14 ' 
		+ @Filtro_fecha 
		+ @Filtro_opers 
		+ @Filtro_destino
		EXECUTE (@CAD)
	End

/* =========================================
   Region 19
   -- Elaboraciones
   =========================================
*/ 
If @cmov is null 
	or (@cmov is not null and @cmov=15)
	Begin
		Select @Filtro_destino = ''

		If @list_secc is not NULL
			Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
									+ @list_secc + ') )'

		Select @Cad = 
			'Insert #opers
			    Select oper.id_operacion, oper.id_tmov, 
					   oper.consecutivo, oper.referencia, 
					   desc_tmov=tmov.descripcion, oper.estado,
					   oper.fec_ent, oper.fec_oper, oper.id_oper, 
					   "Secc. "+dbo.FullDescripcionSeccion(c.id_secc) 
			From stokoper oper
				INNER JOIN stokeelo c ON oper.id_operacion=c.id_operacion
				INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
			Where tmov.id_cmov = 15 ' 
			+ @Filtro_fecha 
			+ @Filtro_opers
			+ @Filtro_destino

		EXECUTE (@CAD)
	End

/* =========================================
   Region 20
   -- Reversion de Elaboraciones
   =========================================
*/ 
If @cmov is null 
	or (@cmov is not null and @cmov=16)
	Begin
		Select @Filtro_destino = ''

		If @list_secc is not NULL
			Select @Filtro_destino = ' AND (id_secc is not null and id_secc in (' 
									+ @list_secc + ') )'

		Select @Cad = 
			'Insert #opers
				Select oper.id_operacion, oper.id_tmov, 
					   oper.consecutivo, oper.referencia, 
					   desc_tmov=tmov.descripcion, oper.estado,
					   oper.fec_ent, oper.fec_oper, oper.id_oper, 
					   "Rev. Nro. "+RTRIM(convert(varchar(14),r.consecutivo))
				+"   "+"Secc. "+dbo.FullDescripcionSeccion(c.id_secc)
			From stokoper oper
				INNER JOIN stokoper r ON oper.id_revertido=r.id_operacion
				INNER JOIN stokeelo c ON r.id_operacion=c.id_operacion
				INNER JOIN stoktmov tmov ON oper.id_tmov=tmov.id_tmov
			Where tmov.id_cmov = 16 ' 
			+ @Filtro_fecha 
			+ @Filtro_opers 
			+ @Filtro_destino

		EXECUTE (@CAD)
	End

   Select @Cad = 'Select * 
                     From #opers 
					 order by id_tmov,consecutivo'

   Execute(@Cad)

   drop table #opers
   drop table #Almas
   drop table #Seccs


End





