USE [IHSecurity]
GO
/****** Object:  StoredProcedure [dbo].[DSS_MAN_GetPermisos]    Script Date: 12/19/2012 10:51:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

DROP PROCEDURE [dbo].[DSS_MAN_GetPermisos]
GO

CREATE PROCEDURE [dbo].[DSS_MAN_GetPermisos]
(
    @vIdModulo     INT,
    @vIdAplicacion INT,
    @vIdUsuario    INT
)
AS
BEGIN 

	SELECT *
	INTO   #AutUsuario
	FROM   Acceso_Opc
	WHERE  id_oper = @vIdUsuario

	SELECT mod.id_modulo,
	       mod.desc_modulo,
	       app.id_app,
	       app.desc_app,
	       opc.id_opc,
	       opc.desc_opc,
	       CASE ISNULL(aut.id_opc, 0)
	            WHEN 0 THEN 0
	            ELSE 1
	       END AS Concedido,
	       (SELECT nombre FROM Operador WHERE id_oper = @vIdUsuario) AS Usuario
	INTO            #Permisos
	FROM            Modulo       mod 
	INNER JOIN      Aplicaciones app ON mod.id_modulo = app.id_modulo
	INNER JOIN      Opciones     opc ON app.id_app    = opc.id_app
	LEFT OUTER JOIN #AutUsuario  aut ON opc.id_opc    = aut.id_opc
	
	
	IF @vIdModulo > 0
	BEGIN
		DELETE FROM #Permisos WHERE id_modulo <> @vIdModulo
	END
	
	IF @vIdAplicacion > 0
	BEGIN
		DELETE FROM #Permisos WHERE id_app <> @vIdAplicacion
	END
	
	SELECT   *
	FROM     #Permisos
    ORDER BY id_modulo, id_app, id_opc
    
	RETURN

END
