USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_applicationIntlLogin]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_applicationIntlLogin]
GO
/****** Object:  StoredProcedure [dbo].[proc_applicationIntlLogin]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[proc_applicationIntlLogin]
	  @flag							VARCHAR(50)		= NULL
     ,@userId						VARCHAR(10)		= NULL
     ,@user                         VARCHAR(30)		= NULL
     ,@userName                     VARCHAR(30)		= NULL
     ,@agentCode					VARCHAR(20)		= NULL
     ,@pwd                          VARCHAR(255)	= NULL
     ,@UserInfoDetail				VARCHAR(MAX)	= NULL
     ,@employeeId					VARCHAR(10)		= NULL
     ,@ipAddress					VARCHAR(100)	= NULL
	 ,@dcLogindcUserName			VARCHAR(MAX)	= NULL
	 ,@dcSerialNumber				VARCHAR(100)	= NULL
	 ,@dcUserName					VARCHAR(100)	= NULL
AS

SET NOCOUNT ON;
SET XACT_ABORT ON;

	DECLARE
		 @UserData				VARCHAR(200)
		,@parentAgentId			VARCHAR(200)
		,@agentType				VARCHAR(20)
		,@actAsBranch			VARCHAR(20)
		,@lastPwdChangedOn		DATETIME
		,@forceChangePwd		CHAR(1)
		,@pwdChangeDays			VARCHAR(20)
		,@msg					VARCHAR(2000) 
		,@pwdChangeWarningDays	VARCHAR(5)
		,@agentId				INT
		 
	DECLARE 
		 @countryId				INT 
		,@country				VARCHAR(50)
		,@branch				INT
		,@branchName			VARCHAR(100)
		,@agent					INT
		,@superAgent			INT
		,@superAgentName		VARCHAR(100)
		,@mapCodeInt			VARCHAR(8)
		,@parentMapCodeInt		VARCHAR(8)
		,@agentName				VARCHAR(250)
		,@settlingAgent			INT
		,@parentId				INT
		,@mapCodeDom			VARCHAR(8)
		,@isHeadOffice			CHAR(1)
		,@superAgentId			VARCHAR(10)
		
	DECLARE 
	     @agentActiveStatus		CHAR(1)
	    ,@branchActiveStatus	CHAR(1)
		,@loginUserName			VARCHAR(100)
		,@loginPwd				VARCHAR(100)
		,@userActive			CHAR(1)
		,@loginUserAgtCode		VARCHAR(100)
		,@loginUserAgtId		VARCHAR(100)
		,@empId					VARCHAR(100)
		,@isLocked				CHAR(1)
		,@isDeleted				CHAR(1)
		,@loginTime				TIME
		,@logoutTime			TIME
		,@startDate				DATETIME
		,@endDate				DATETIME
		,@lastLoginTs			DATETIME
		,@lockUserDays			INT
		,@userDcSerialNumber	VARCHAR(100)
		,@agentLocation			INT
		,@agentGrp				INT
		,@agentEmail			VARCHAR(100)
		,@agentPhone			VARCHAR(100)

--## Intl Agent Login
IF @flag = 'lfai' 
BEGIN 		
	SET @UserData ='User:' + @userName + ', UserCode:' + CAST(@employeeId AS VARCHAR(20)) + ', AgentCode:' + CAST(@agentCode AS VARCHAR(20))
	
	SELECT 
		 @userId			= userId
		,@loginUserName		= userName
		,@userActive		= ISNULL(au.isActive, 'N')
		,@loginPwd			= pwd
		,@loginUserAgtCode	= au.agentCode
		,@loginUserAgtId	= au.agentId
		,@empId				= employeeId
		,@isLocked			= ISNULL(isLocked, 'N')
		,@loginTime			= loginTime
		,@logoutTime		= logoutTime	
		,@lastLoginTs		= lastLoginTs
		,@agentType			= am.agentType
		,@actAsBranch		= am.actAsBranch
		,@agentId			= am.agentId
		,@agentActiveStatus	= ISNULL(am.isActive, 'N')	
		,@userDcSerialNumber= dcSerialNumber
		,@agentLocation		= am.agentLocation
		,@agentGrp			 = am.agentGrp
		,@forceChangePwd		= au.forceChangePwd
	FROM applicationUsers au WITH(NOLOCK)
	INNER JOIN agentMaster am WITH(NOLOCK) ON au.agentId = am.agentId
	WHERE userName = @userName and ISNULL(au.isDeleted, 'N') = 'N'
	AND AM.AGENTCOUNTRY <> 'NEPAL'
		
	SELECT
		 @startDate			= startDate
		,@endDate			= endDate
	FROM userLockDetail WITH(NOLOCK)
	WHERE userId = @userId 
	AND ISNULL(isDeleted,'N') = 'N'
	
	IF @userId IS NULL
	BEGIN
		SELECT 1 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id
		SET @UserInfoDetail = 'Reason = Login fails, Incorrect user name.-:::-' + @UserInfoDetail
		
		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Invalid Username',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		RETURN		
	END
	-- ## Except IME EXCHANGE COUNTER- TIA
	--IF @userDcSerialNumber IS NOT NULL AND @userDcSerialNumber <> @dcSerialNumber AND @agentId <> '9943' 
	--BEGIN
	--	SELECT 1 errorCode, 'Login fails, Not a valid Digital Certificate for this user.' mes, @userName id
	--	SET @UserInfoDetail = 'Reason = Login fails, Not a valid Digital Certificate for this user.-:::-'+@UserInfoDetail

	--	EXEC proc_applicationLogs 
	--		@flag			= 'login',
	--		@logType		= 'Login fails', 
	--		@createdBy		= @userName, 
	--		@Reason			= 'Not a valid Digital Certificate for this user',
	--		@UserData		= @UserData,
	--		@fieldValue		= @UserInfoDetail,
	--		@agentId		= @agentId,
	--		@IP				= @ipAddress,
	--		@dcSerialNumber	= @dcSerialNumber,
	--		@dcUserName		= @dcUserName
	--	RETURN
	--END
	
	--IF @dcSerialNumber IS NOT NULL AND @dcSerialNumber not like '%00-05-00-00%' 
	--BEGIN
		
	--	SELECT 1 errorCode, 'Login fails, Not a valid IME CERTIFICATE, Please Use Valid IME Certificate.' mes, @userName id
	--	SET @UserInfoDetail = 'Reason = Login fails, Not a valid IME CERTIFICATE-:::-' + @UserInfoDetail
		
	--	EXEC proc_applicationLogs 
	--		@flag			= 'login',
	--		@logType		= 'Login fails', 
	--		@createdBy		= @userName, 
	--		@Reason			= 'Not a valid IME CERTIFICATE',
	--		@UserData		= @UserData,
	--		@fieldValue		= @UserInfoDetail,
	--		@agentId		= @agentId,
	--		@IP				= @ipAddress,
	--		@dcSerialNumber	= @dcSerialNumber,
	--		@dcUserName		= @dcUserName		

	--	RETURN
	--END
	
	IF @userActive = 'N' 
	BEGIN
		SELECT 1 errorCode, 'User is not active.' mes, @userName id
		SET @UserInfoDetail = 'Reason = User has not been approved.-:::-' + @UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'User is not active',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		RETURN
	END
	
	IF @agentActiveStatus = 'N'
	BEGIN
		SELECT 1 errorCode, 'Your account is blocked.' mes, @userName id
		SET @UserInfoDetail = 'Reason = Branch is not active.-:::-' + @UserInfoDetail
		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Branch is not active',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		RETURN
	END

	DECLARE @attemptsCount INT
	SELECT TOP 1 @attemptsCount = loginAttemptCount, @lockUserDays = ISNULL(lockUserDays,30) FROM passwordFormat WITH(NOLOCK)
    
	IF (@loginPwd <> ISNULL(dbo.FNAEncryptString(@pwd), ''))
	BEGIN
		
		SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
		SET @UserInfoDetail = 'Reason = Login fails, Incorrect password.-:::-'+@UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Incorrect password',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		RETURN		
	END

	IF (@loginUserAgtCode <> ISNULL(@agentCode, ''))
	BEGIN
		SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
		SET @UserInfoDetail = 'Reason = Login fails, Incorrect AgentCode.-:::-' + @UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Incorrect AgentCode',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		RETURN		
	END
	
	IF (@empId <> ISNULL(@employeeId, ''))
	BEGIN
		SELECT 2 errorCode, 'Login fails, Incorrect user name or password.' mes, @userName id, @attemptsCount ac
		SET @UserInfoDetail = 'Reason = Login fails, Incorrect userId.-:::-'+@UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Incorrect userId',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName

		RETURN		
	END

	IF(@isLocked = 'Y')
	BEGIN
		SELECT 1 errorCode, 'Your account has been locked. Please, contact your administrator.' mes, @userName id
		SET @UserInfoDetail = 'Reason = Your account has been locked. Please, contact your administrator.-:::-'+@UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Your account has been locked',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName

		RETURN;		
	END	
	
	IF CAST(GETDATE() AS TIME) < @loginTime AND CAST(GETDATE() AS TIME) > @logoutTime
	BEGIN
		SELECT 1 errorCode, 'You are not permitted to login at this time. Please, contact your administrator.' mes, @userName id
		SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-' + @UserInfoDetail
		
		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Not permitted to login at this time',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName

		RETURN
	END

	IF (GETDATE() BETWEEN @startDate AND CONVERT(VARCHAR(20), @endDate,101) + ' 23:59:59')
	BEGIN
		SELECT 1 errorCode, 'You are not permitted to login for this date. Please, contact your administrator' mes, @userName id
		SET @UserInfoDetail = 'Reason = You are not permitted to login at this time. Please, contact your administrator-:::-'+@UserInfoDetail
		
		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Not permitted to login for this date',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName

		RETURN
	END
		
	-- Last Login date check for Locking
    IF(DATEDIFF(DAY, @lastLoginTs, GETDATE()) >= @lockUserDays)
	BEGIN
		UPDATE applicationUsers SET 
			 isLocked		= 'Y'
			,lastLoginTs	= dbo.FNAGetDateInNepalTZ()
		WHERE userId = @userId

		SELECT 1 errorCode, 'You are locked this time. Please, contact your administrator' mes, @userName id
		SET @UserInfoDetail = 'Reason = You are locked this time. Please, contact your administrator.-:::-' + @UserInfoDetail

		EXEC proc_applicationLogs 
			@flag			= 'login',
			@logType		= 'Login fails', 
			@createdBy		= @userName, 
			@Reason			= 'Not Login for fix period, now user is locked',
			@UserData		= @UserData,
			@fieldValue		= @UserInfoDetail,
			@agentId		= @agentId,
			@IP				= @ipAddress,
			@dcSerialNumber = @dcSerialNumber,
			@dcUserName		= @dcUserName
		
		INSERT INTO userLockHistory(username, lockReason, createdBy, createdDate)
		SELECT @userName, 'Your account has been locked due to not login for fix period', 'system', GETDATE()		
		RETURN;

	END
		
	UPDATE applicationUsers SET	
		 lastLoginTs		= dbo.FNAGetDateInNepalTZ()
		,dcSerialNumber		= ISNULL(dcSerialNumber, @dcSerialNumber)
		,dcUserName			= ISNULL(dcUserName, @dcUserName)
	WHERE  [userName] = @userName
			
	SELECT
		 @country			= am.agentCountry
		,@countryId			= am.agentCountryId
		,@branch			= au.agentId 
		,@branchName		= am.agentName
		,@agentName			= am.agentName
		,@mapCodeInt		= am.mapCodeInt
		,@parentmapCodeInt	= am.mapCodeInt
		,@mapCodeDom		= am.mapCodeDom
		,@agentType			= am.agentType
		,@actAsBranch		= actAsBranch
		,@agent				= CASE WHEN am.agentType = 2903 THEN au.agentId ELSE am.parentId END
		,@superAgent		= am.parentId
		,@parentId			= am.parentId
		,@settlingAgent		= CASE WHEN ISNULL(am.isSettlingAgent, 'N') = 'Y' THEN au.agentId ELSE NULL END
		,@agentEmail		= am.agentEmail1
		,@agentPhone		= am.agentPhone1
	FROM agentMaster am WITH(NOLOCK) 
	INNER JOIN applicationUsers au WITH(NOLOCK) ON am.agentId = au.agentId 		
	WHERE au.userName = @userName
	
	IF @country = 'Nepal'
	BEGIN
	    SELECT 1 errorCode, 'You are not permitted to login from International agent Login panel. Please, contact your administrator.' mes, @userName id
		SET @UserInfoDetail = 'Reason = You are not permitted from International agent Login panel. Please, contact your administrator-:::-' + @UserInfoDetail
		RETURN
	END		

	IF(@agentType <> 2903) 
	BEGIN
		SELECT 
			 TOP 1
			 @agentName			= agentName
			,@parentMapCodeInt	= mapCodeInt
			,@superAgent		= parentId 
		FROM agentMaster WITH(NOLOCK) WHERE agentId = @agent
	END
		
	SELECT TOP 1 @superAgentName = agentName FROM agentMaster WITH(NOLOCK) WHERE agentId = @superAgent
	
	IF @settlingAgent IS NULL
		SELECT @settlingAgent = agentId FROM agentMaster WITH(NOLOCK) WHERE agentId = @agent AND isSettlingAgent = 'Y'
		
	SELECT 
		TOP 1
		 0 errorCode
		,'Login success.' mes
		,@userName Id
		,au.userAccessLevel
		,au.sessionTimeOutPeriod
		,au.UserID
		,fullName = au.firstName + ISNULL(' ' + au.middleName, '') + ISNULL(' ' + au.lastName, '') 
		,parentId = ISNULL(@parentId, 0)
		,agentType = ISNULL(@agentType, 2901)
		,settlingAgent = ISNULL(@settlingAgent, 0)
		,isActAsBranch = ISNULL(@actAsBranch, 'N')
		,mapCodeInt = ISNULL(@mapCodeInt, '0000')
		,parentMapCodeInt = ISNULL(@parentMapCodeInt, '0000')
		,mapCodeDom = ISNULL(@mapCodeDom, '0000')
		,country = @country
		,countryId = @countryId
		,branch = @branch
		,branchName = @branchName
		,agent = @agent
		,agentName = @agentName
		,superAgent = ISNULL(@superAgent, 0)
		,superAgentName = ISNULL(@superAgentName, 0)
		,au.lastLoginTs 
		,fromSendTrnTime	= CASE WHEN x.globalOperationTimeEnable = 'Y' AND @settlingAgent <> 1249 THEN operationTimeFrom ELSE au.fromSendTrnTime END
		,toSendTrnTime		= CASE WHEN x.globalOperationTimeEnable = 'Y' AND @settlingAgent <> 1249 THEN operationTimeTo ELSE au.toSendTrnTime END
		,fromPayTrnTime		= CASE WHEN x.globalOperationTimeEnable = 'Y' AND @settlingAgent <> 1249 THEN operationTimeFrom ELSE au.fromPayTrnTime END
		,toPayTrnTime		= CASE WHEN x.globalOperationTimeEnable = 'Y' AND @settlingAgent <> 1249 THEN operationTimeTo ELSE au.toPayTrnTime END
		,au.userType
		,isHeadOffice = @isHeadOffice
		,newBranchId = newBranchId
		,agentLocation = @agentLocation
		,agentGrp	= @agentGrp
		,agentEmail = @agentEmail
		,agentPhone	= @agentPhone
		,isForcePwdChanged = au.forceChangePwd
	FROM applicationUsers au WITH(NOLOCK), passwordFormat x WITH(NOLOCK)
    WHERE au.userName = @userName
				
	EXEC proc_applicationLogs 
		@flag			= 'login',
		@logType		='Login', 
		@createdBy		= @userName, 
		@Reason			= 'Agent Login',
		@UserData		= @UserData,
		@fieldValue		= @UserInfoDetail,
		@agentId		= @agentId,
		@IP				= @ipAddress,
		@dcSerialNumber = @dcSerialNumber,
		@dcUserName		= @dcUserName	
	
	RETURN	
END
GO
