SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WINV_Seccion_GetCombo') )
DROP PROCEDURE WINV_Seccion_GetCombo
GO

CREATE PROCEDURE WINV_Seccion_GetCombo --Carga la informacion a el combobox
AS
BEGIN

	SELECT	LTRIM(RTRIM(codigo))codigo, LTRIM(RTRIM(descripcion))descripcion, id_secc
	INTO	#TEM_Secc
	FROM	STOKSECC
	ORDER BY codigo
	
	SELECT	*
	FROM	#TEM_Secc
	
END
GO

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WINV_Seccion_Edicion_GetCombo') )
DROP PROCEDURE WINV_Seccion_Edicion_GetCombo
GO

CREATE PROCEDURE WINV_Seccion_Edicion_GetCombo
AS
BEGIN

	SELECT	DISTINCT LTRIM(RTRIM(s.codigo))codigo, LTRIM(RTRIM(s.descripcion))descripcion, s.id_secc
	INTO	#TEM_Secc
	FROM	stoksecc s left join stokalse a on s.id_secc=a.id_secc 
	where	a.inv_cerrado=1 
	order by 1
	
	SELECT	*
	FROM	#TEM_Secc

END
GO

-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WINV_Seccion_TrasladoED_Filtro_GetCombo') )
DROP PROCEDURE WINV_Seccion_TrasladoED_Filtro_GetCombo
GO

CREATE PROCEDURE WINV_Seccion_TrasladoED_Filtro_GetCombo
		@origen INT,
		@id_origen INT
		
AS
BEGIN

	DECLARE @Sql nVARCHAR(MAX)
	SET @Sql = ''
	
	SET @Sql = 'SELECT DISTINCT LTRIM(RTRIM(s.codigo))codigo, LTRIM(RTRIM(s.descripcion))descripcion'

	SET @Sql = @Sql + ', s.id_secc FROM stoksecc s'
	SET @Sql = @Sql + ' left join stokalse a on s.id_secc = a.id_secc'
	
	IF(@id_origen > 0)
	BEGIN
		IF(@origen = 0)
		BEGIN
			SET @Sql = @Sql + ' where a.id_alma = '+STR(@id_origen, 14, 0)
		END
		ELSE
		BEGIN
			SET @Sql = @Sql +	' where a.id_alma in (select al.id_alma from stokalse al where al.id_secc = '+STR(@id_origen, 14, 0)+') '+	
								' and s.id_secc != '+STR(@id_origen, 14, 0)
		END
		
		SET @Sql = @Sql + ' and a.inv_cerrado = 1'
	END
	ELSE
	BEGIN
		SET @Sql = @Sql + ' where a.inv_cerrado = 1'
	END

	IF(@id_origen > 0)
	BEGIN
		IF(@origen = 0)
		BEGIN
			SET @Sql = @Sql + 'and dbo.IsSeccionAlmaGasto('+STR(@id_origen, 14 ,0)+',s.id_secc) = 0'
		END
		ELSE
		BEGIN
			SET @Sql = @Sql + 'and dbo.IsSeccionSeccGasto('+STR(@id_origen, 14, 0)+',s.id_secc) = 0'
		END 
	END
	
	SET @Sql = @Sql + ' order by 1'

	EXEC sp_executesql @Sql
	
END
GO


