      
ALTER PROCEDURE [dbo].[JsonRx_Proc_UserRegistration_V2](      
	 @language  VARCHAR(100) = 'en'      
	,@customerId VARCHAR(100) = NULL      
	,@username  VARCHAR(100) = NULL      
	,@flag   VARCHAR(100) = NULL      
	,@password  VARCHAR(100) = NULL      
	,@txnPin  VARCHAR(100) = NULL      
	,@idNumber  VARCHAR(100) = NULL      
	,@clientId  VARCHAR(100) = NULL      
	,@IMEI   VARCHAR(100) = NULL      
	,@appVersion VARCHAR(100) = NULL      
	,@phoneBrand VARCHAR(100) = NULL      
	,@phoneOs  VARCHAR(100) = NULL      
	,@osVersion  VARCHAR(100) = NULL      
	,@deviceId  VARCHAR(100) = NULL      
	,@dob   VARCHAR(100) = NULL      
	,@uuid   VARCHAR(100) = NULL      
	,@fcmId   VARCHAR(250) = NULL      
	,@mobile  VARCHAR(100) = NULL        
	,@nativeCountry VARCHAR(5)  = NULL  
	,@referralCode VARCHAR(100)  = NULL  
	,@referenceId BIGINT = NULL  
)AS      
BEGIN TRY      
	DECLARE @dobDB VARCHAR(200),@code VARCHAR(100),@_errorMsg VARCHAR(300),@verifiedDate DATETIME, @customerIdNo VARCHAR(50), @customerIdOld BIGINT = NULL,
				@firstName VARCHAR(100), @userNameRandom VARCHAR(100), @mobileNumberCompare VARCHAR(30)
    
	IF @flag = 'sign-up-pre-old-cust'
	BEGIN
		SET @mobileNumberCompare = substring(@mobile, 4, len(@mobile)-3)

		IF (SELECT COUNT(0) 
				FROM CUSTOMERMASTER (NOLOCK) 
				WHERE IDNUMBER = @idNumber 
				AND mobile LIKE '%' + @mobileNumberCompare 
				AND CAST(dob AS DATE) = @dob 
				AND ISNULL(isDeleted, 'N') = 'N'
				AND ISNULL(ISACTIVE, 'Y') = 'Y') > 1
		BEGIN
			SELECT 1 ErrorCode,'Duplicate data found, please contact JME Support' Msg, NULL Id  
			RETURN
		END

		SELECT @customerIdOld = CUSTOMERID, @firstName = ISNULL(firstName, '')
		FROM CUSTOMERMASTER (NOLOCK)
		WHERE IDNUMBER = @idNumber
		AND mobile LIKE '%' + @mobileNumberCompare
		AND CAST(dob AS DATE) = @dob
		AND ISNULL(isDeleted, 'N') = 'N'
		AND ISNULL(ISACTIVE, 'Y') = 'Y'
		AND isExistingCustomer = 1
		AND USERNAME IS NULL

		IF @customerIdOld IS NULL 
		BEGIN
			SELECT 2 ErrorCode,'No records match with provided details, please contact JME Support' Msg, NULL Id  
			RETURN
		END
		
		--already registered      
		IF EXISTS(SELECT 'x' FROM dbo.mobile_userRegistration(NOLOCK) AS MUR WHERE customerId = @customerIdOld)      
		BEGIN      
			SELECT 3 ErrorCode,'You are already registered with JME!' Msg, NULL Id      
			RETURN      
		END

		IF EXISTS (SELECT * FROM CustomerMasterTemp (NOLOCK) WHERE customerId = @customerIdOld AND createdBy IS NOT NULL)
		BEGIN
			SELECT 3 ErrorCode,'You are already registered with JME!' Msg, NULL Id  
			RETURN
		END
		IF EXISTS (SELECT * FROM CustomerMaster (NOLOCK) WHERE customerId = @customerIdOld AND username IS NOT NULL)
		BEGIN
			SELECT 3 ErrorCode,'You are already registered with JME!' Msg, NULL Id  
			RETURN
		END
		
		DELETE FROM dbo.CustomerMasterTemp WHERE OldCustomerId = @customerIdOld

		SELECT @userNameRandom = DBO.GetRandomUsername(@firstName)

		IF EXISTS(SELECT TOP 1 1 FROM CustomerMasterTemp (NOLOCK) WHERE USERNAME = @userNameRandom)
		BEGIN
			SET @userNameRandom = DBO.GetRandomUsername(@firstName)
		END 

		INSERT INTO dbo.CustomerMasterTemp(      
						OldCustomerId,dob,mobile,isActive, idNumber    
					)      
		SELECT      
						@customerIdOld,@dob,@mobile,'N', @idNumber

		SET @customerId=SCOPE_IDENTITY()      

		SELECT 0 ErrorCode, 'Success' Msg, @customerId Id, '' userName
	END
	ELSE IF @flag = 'sign-up-v2-old-cust'
	BEGIN
		IF ISNULL(@username, '') = ''
		BEGIN
			SELECT 1 ErrorCode,'Username can not be blank!' Msg, NULL Id      
			RETURN  
		END
		--user already registered      
		IF EXISTS(SELECT 'x' FROM dbo.customerMaster AS CM(NOLOCK) WHERE CM.username = @username)      
		BEGIN      
			SELECT 2 ErrorCode,'Customer already registered, with this username!' Msg, NULL Id      
			RETURN      
		END 

		--invalid referenceid
		IF NOT EXISTS(SELECT 'x' FROM dbo.CustomerMasterTemp AS CM(NOLOCK) WHERE customerId = @referenceId)      
		BEGIN      
			SELECT 3 ErrorCode,'You can not modify email once created, please contact JME!' Msg, NULL Id      
			RETURN      
		END  

		--invalid referenceid
		IF NOT EXISTS(SELECT 'x' FROM dbo.CustomerMasterTemp AS CM(NOLOCK) WHERE customerId = @referenceId AND createdBy IS NULL)      
		BEGIN      
			SELECT 4 ErrorCode,'Invalid data!' Msg, NULL Id      
			RETURN      
		END    
		
		--user already registered      
		IF EXISTS(SELECT 'x' FROM dbo.customerMasterTemp AS CM(NOLOCK) WHERE username = @username)      
		BEGIN      
			SELECT 5 ErrorCode,'User already registered!' Msg, NULL Id      
			RETURN      
		END      
		
		--Username already taken      
		IF EXISTS(SELECT 'x' FROM dbo.mobile_userRegistration(NOLOCK) AS MUR WHERE username = @username)      
		BEGIN      
			SELECT 6 ErrorCode,'Username already taken!' Msg, NULL Id      
			RETURN      
		END

		BEGIN TRAN
			SELECT @customerIdOld = OldCustomerId
			FROM CustomerMasterTemp (NOLOCK)
			WHERE customerId = @referenceId

			UPDATE CustomerMaster SET username = @username, customerPassword = dbo.FNAEncryptString(@password)
											,MODIFIEDBY = @username, MODIFIEDDate = GETDATE(), isActive = 'Y', txnPin = @txnPin
											,email = @username, customerEmail = @username, createdFrom = 'C'
			WHERE customerId = @customerIdOld

			INSERT INTO dbo.mobile_userRegistration(      
								clientId,username,createdDate,IMEI,appVersion,phoneBrand,phoneOs,osVersion,deviceId,customerId,cmRegistrationId      
						)      
			SELECT      
								@clientId,@username,GETDATE(),@IMEI,@appVersion,@phoneBrand,@phoneOs,@osVersion,@fcmId,@customerIdOld,@uuid   

			DELETE FROM CustomerMasterTemp WHERE customerId = @referenceId
			COMMIT TRAN

		IF @@TRANCOUNT=0      
		BEGIN   
			--successful registered      
			SELECT 0 ErrorCode,'Your login with JME is activated, you are now able to send money using JME Mobile Application. Thank You.' Msg, NULL Id, rowId, PdfName, AgreePdfPath, *       
			FROM customerAgreeDocumentTbl        
			WHERE targetObj = 'STAGING'      
			RETURN      
		END      
			  
	END
	IF @flag='sign-up-v2-new-cust'      
	BEGIN    
		DECLARE @COUNTRYID INT  
		--user already registered      
		IF EXISTS(SELECT 'x' FROM dbo.customerMaster AS CM(NOLOCK) WHERE CM.userName = @username )      
		BEGIN      
			SELECT 1 ErrorCode,'Customer already registered, with this username!' Msg, NULL Id      
			RETURN         
		END      
      
		--user already registered      
		IF EXISTS(SELECT 'x' FROM dbo.customerMasterTemp AS CM(NOLOCK) WHERE username=@username)      
		BEGIN      
			SELECT 2 ErrorCode,'User already registered!' Msg, NULL Id      
			RETURN   
		END      
      
		--Username already taken      
		IF EXISTS(SELECT 'x' FROM dbo.mobile_userRegistration(NOLOCK) AS MUR WHERE username=@username)      
		BEGIN      
			SELECT 3 ErrorCode,'Username already taken!' Msg, NULL Id      
			RETURN       
		END      

		SELECT @COUNTRYID = countryId
		FROM countryMaster (NOLOCK) 
		WHERE COUNTRYCODE = @nativeCountry
      
		BEGIN TRAN         
			INSERT INTO dbo.CustomerMasterTemp(      
							username,customerPassword,createdBy,createdDate,isActive,nativeCountry, referelCode, idNumber, email, customerEmail
						)      
			SELECT      
							@username,dbo.FNAEncryptString(@password),@username,GETDATE(),'Y', @COUNTRYID, @referralCode, @idNumber, @username, @username
			
			SET @customerId=SCOPE_IDENTITY()      
		
			INSERT INTO dbo.mobile_userRegistration(      
							clientId,username,createdDate,appVersion,phoneBrand,phoneOs,osVersion,deviceId,customerId, cmRegistrationId   
						)      
			SELECT      
							@clientId,@username,GETDATE(),@appVersion,@phoneBrand,@phoneOs,@osVersion,@fcmId,@customerId, @uuid

		COMMIT TRAN      
		IF @@TRANCOUNT=0      
		BEGIN      
			--successful registered      
			SELECT 0 ErrorCode,'Your login with JME is activated, you are now able to send money using JME Mobile Application. Thank You.' Msg, NULL Id, rowId, PdfName, AgreePdfPath       
			FROM customerAgreeDocumentTbl        
			WHERE targetObj = 'STAGING'      
		RETURN      
		END      
	END  
END TRY      
BEGIN CATCH      
	IF @@TRANCOUNT<>0      
		ROLLBACK TRAN   
		   
	--Execption      
	SELECT 1 ErrorCode,dbo.GetMessage(@language,'9999')+CONVERT(VARCHAR,ERROR_LINE())+ERROR_MESSAGE() Msg, NULL Id      
	RETURN      
END CATCH      

