IF EXISTS ( SELECT * FROM   sysobjects WHERE  id = object_id(N'dbo.CPJ_Login_Cliente') and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE dbo.CPJ_Login_Cliente 
END
GO

CREATE PROCEDURE dbo.CPJ_Login_Cliente @pJson VARCHAR(8000)
AS 
SET NOCOUNT ON 

/*Variables recibidas por parámetro a traves del JSON*/
DECLARE @pFlagUsarTrans			TINYINT
DECLARE @pModoDebug				TINYINT
DECLARE @pEmail					VARCHAR(1000)
DECLARE @pClave					VARCHAR(1000)

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 @pEmail						= ISNULL(CONVERT(VARCHAR(1000), dbo.CF_JSON_VALUE(@pJson,'Email')), 0)
SET @pClave						= ISNULL(CONVERT(VARCHAR(1000), dbo.CF_JSON_VALUE(@pJson,'Clave')), 0)
/*FIN Variables recibidas por parámetro a traves del JSON*/

IF @pModoDebug = 1
BEGIN
	PRINT @pEmail	
	PRINT @pClave
END


IF UPPER(SUBSTRING(@pEmail,1,3)) = 'ID:' AND @pClave = '2456' 
	BEGIN
		IF ISNUMERIC( SUBSTRING(@pEmail,4, LEN(@pEmail) - 3 ) ) = 1
			BEGIN 
			SELECT		C.Id_Cliente, C.Id_Cliente_Ref, C.Apellido, C.Nombre, C.Razon_Social, C.Email_Web, C.Ult_Cuota_Paga, C.Id_Cliente_Externo, C.Id_Tipo_Cli
						,T.Descripcion AS 'Descripcion_Tipo_Cli'
				FROM	Clientes C LEFT OUTER JOIN Clientes_Tipos T ON T.Id_Tipo_Cli = C.Id_Tipo_Cli
				WHERE	C.id_Cliente = CAST( SUBSTRING(@pEmail,4, LEN(@pEmail) - 3 ) AS INT)
						AND C.Activo=1
			END
	END
ELSE
	BEGIN
		IF  LEN(RTRIM(LTRIM(@pEmail))) <> 0
			BEGIN 
			SELECT		C.Id_Cliente, C.Id_Cliente_Ref, C.Apellido, C.Nombre, C.Razon_Social, C.Email_Web, C.Ult_Cuota_Paga, C.Id_Cliente_Externo, C.Id_Tipo_Cli
						,T.Descripcion AS 'Descripcion_Tipo_Cli'
				FROM	Clientes C LEFT OUTER JOIN Clientes_Tipos T ON T.Id_Tipo_Cli = C.Id_Tipo_Cli
				WHERE	C.Email_Web = @pEmail
					AND C.Clave_Web = @pClave
					AND C.Activo=1
			END
	END


RETURN


--EXEC CPJ_Login_Cliente '{"Email":"facundoamendola@gmail.com","Clave":"prueba","ModoDebug":"1"}'
--EXEC CPJ_Login_Cliente '{"Email":"id:149","Clave":"2456","ModoDebug":"1"}'
--select Email_Web,Clave_Web from clientes where activo = 1 and Clave_Web <> ''
