

ALTER PROC mobile_proc_customerMaster_V2
(
	@flag VARCHAR(20)
	, @username VARCHAR(100) = NULL
	, @type VARCHAR(100) = NULL
	, @sourceOfFund VARCHAR(100) = NULL
	, @monthlyIncome VARCHAR(100) = NULL
	, @businessType VARCHAR(100) = NULL
	, @mobile VARCHAR(100) = NULL
	, @email VARCHAR(100) = NULL
	, @additionalAddress NVARCHAR(100) = NULL
	, @idType VARCHAR(100) = NULL
	, @additionalIdType VARCHAR(100) = NULL
	, @idFront VARCHAR(100) = NULL
	, @idBack VARCHAR(100) = NULL
	, @occupation VARCHAR(100) = NULL
	, @idSide VARCHAR(100) = NULL
	, @additionalId VARCHAR(100) = NULL
	, @additionalIdBack VARCHAR(100) = NULL
	, @facePicture VARCHAR(100) = NULL
	, @currentPin VARCHAR(20) = NULL
	, @NewPassword VARCHAR(50) = NULL
	, @newPin VARCHAR(20) = NULL
	, @currentPassword VARCHAR(50) = NULL
	, @confirmNewPin VARCHAR(20) = NULL
	, @ConfirmNewPassword VARCHAR(50) = NULL
	, @employeerName VARCHAR(100) = NULL
	, @visaStatus VARCHAR(50) = NULL
	, @occupationOther VARCHAR(150) = NULL
	, @otherIdType VARCHAR(150) = NULL
	, @otherOccupation VARCHAR(150) = NULL
	, @otherSourceOfFund VARCHAR(150) = NULL
	, @purposeOfRegistration	INT = NULL
	, @SettingType VARCHAR(30) = NULL
	, @customerPassword NVARCHAR (20) = NULL
	, @token VARCHAR(MAX) = NULL
)
AS;
------------------------------------------------------------------------------------------------------------
	-- Task #225 - Create Token For Bioemtric - flag = 'update-settings' , insert for mobiletoken
	--#101 Mobile changes for multi lingual
------------------------------------------------------------------------------------------------------------
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
	DECLARE @customerId BIGINT, @cust BIGINT	  ,@errorMsg   VARCHAR(MAX)  
	SELECT @customerId = customerId 
				FROM customerMaster WITH(nolock) 
				WHERE username = @userName 
	
	IF @flag = 'id-update'
	BEGIN
		SELECT @customerId = customerId
		FROM CUSTOMERMASTER (NOLOCK)
		WHERE USERNAME = @username		

		IF ISNULL(@idFront, '') <> ''
			INSERT INTO customerDocumentTmp (customerId, fileName, createdBy, createdDate, documentType)
			SELECT @customerId, @idFront, @username, GETDATE(), 11394
		
		IF ISNULL(@idBack, '') <> ''
			INSERT INTO customerDocumentTmp (customerId, fileName, createdBy, createdDate, documentType)
			SELECT @customerId, @idBack, @username, GETDATE(), 11395

		EXEC proc_errorHandler 0, 'Document uploaded successfully!', @userName  
		RETURN  
	END
	ELSE IF @flag = 'update-settings'
	BEGIN
		IF NOT EXISTS(SELECT * FROM customerMaster (NOLOCK) WHERE USERNAME = @username) OR 
			NOT EXISTS(SELECT * FROM mobile_userRegistration (NOLOCK) WHERE USERNAME = @username)
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid customer data!', @userName  
			RETURN  
		END
		IF @SettingType NOT IN('FINGER_PRINT_LOGIN')
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid data!', @userName  
			RETURN  
		END
		IF @type NOT IN ('True', 'False')
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid data!', @userName  
			RETURN  
		END
		IF NOT EXISTS (SELECT * FROM customerMaster WHERE customerPassword = [REDACTED] AND username = @username)
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid Username/Password.', @userName  
			RETURN
		END

		
		--IF @type = 'True'
		--BEGIN
		--	IF  EXISTS(SELECT TOP 1 * FROM mobile_userRegistration (NOLOCK) WHERE USERNAME = @username AND isbiometricLogin = 1) 
		--	BEGIN
		--		SELECT @errorMsg = @SettingType + ' is already enabled'
		--		EXEC proc_errorHandler 1, @errorMsg ,''  
		--		RETURN  
		--	END
		--END

		--IF @type = 'False'
		--BEGIN
		--	IF  EXISTS(SELECT TOP 1  * FROM mobile_userRegistration (NOLOCK) WHERE USERNAME = @username AND isbiometricLogin = 0) 
		--	BEGIN
		--		SELECT @errorMsg = @SettingType + ' is already disabled'
		--		EXEC proc_errorHandler 1, @errorMsg ,'' 
		--		RETURN  
		--	END
		--END

		UPDATE  mr 
		SET BiometricLoginType = @SettingType, IsBiometricLogin = CASE WHEN @type = 'True' THEN 1 ELSE 0 END
		FROM mobile_userRegistration mr
		INNER JOIN customerMaster cm WITH (NOLOCK) ON mr.username = cm.email
		WHERE mr.username = @username 

		UPDATE MobileToken SET activeStatus = 'N' WHERE userName = @username
		
		IF @type = 'True'
		BEGIN
			INSERT INTO MobileToken (username, activeStatus, token , loginType,createdOn)    
			SELECT @userName,  'Y'  , @token, @SettingType ,GETDATE()
			EXEC proc_errorHandler 0, 'Settings updated successfully!', @token
			RETURN
		END
		
		EXEC proc_errorHandler 0, 'Settings updated successfully!', ''  
		RETURN  
	END
	ELSE IF @flag = 'c-pin-pwd'
	BEGIN
		IF ISNULL(@newPin, 'A') <> ISNULL(@confirmNewPin, 'B')
		BEGIN
			EXEC proc_errorHandler 1, 'New pin and Confirm pin are not same!', @userName  
			RETURN  
		END

		IF ISNULL(@NewPassword, 'A') <> ISNULL(@ConfirmNewPassword, 'B')
		BEGIN
			EXEC proc_errorHandler 2, 'New password and Confirm password are not same!', @userName  
			RETURN  
		END
		IF DBO.FNAencryptstring(@currentPassword) = DBO.FNAencryptstring(@NewPassword)
		BEGIN
			EXEC proc_errorHandler 1, 'New Password can not be same as your old password, Please try again!', @userName
			RETURN;
		END

		IF DBO.FNAencryptstring(@currentPin) = DBO.FNAencryptstring(@newPin)
		BEGIN
			EXEC proc_errorHandler 1, 'New pin can not be same as your old pin, Please try again!', @userName
			RETURN;
		END

		IF NOT EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username)
		BEGIN
			EXEC proc_errorHandler 3, 'Invalid customer found!', @userName  
			RETURN  
		END
		IF NOT EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username AND txnPin = @currentPin)
		BEGIN
			EXEC proc_errorHandler 4, 'Invalid pin number!', @userName  
			RETURN  
		END
		IF NOT EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username AND customerPassword = [REDACTED]
		BEGIN
			EXEC proc_errorHandler 5, 'Invalid password!', @userName  
			RETURN  
		END

		UPDATE customerMaster SET txnPin = @newPin, customerPassword = [REDACTED] isForcedPwdChange = 0 
		WHERE username = @username

		UPDATE mobile_userRegistration SET isForcePassChange = 0 where username = @username
		EXEC proc_errorHandler 0, 'You have successfully changed your pin and password' , @username  
		RETURN
	END
	ELSE IF @flag = 'c-pin'
	BEGIN
		IF ISNULL(@newPin, 'A') <> ISNULL(@confirmNewPin, 'B')
		BEGIN
			EXEC proc_errorHandler 1, 'New pin and Confirm pin are not same!', @userName  
			RETURN  
		END
		IF EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username AND @currentPin = @newPin)
		BEGIN
			EXEC proc_errorHandler 2, 'Old pin and new pin cannot be same!', @userName  
			RETURN  
		END

		IF NOT EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username)
		BEGIN
			EXEC proc_errorHandler 3, 'Invalid customer found!', @userName  
			RETURN  
		END
		IF NOT EXISTS(SELECT 'A' FROM customerMaster (NOLOCK) WHERE username = @username AND txnPin = @currentPin)
		BEGIN
			EXEC proc_errorHandler 4, 'Invalid pin number!', @userName  
			RETURN  
		END

		UPDATE customerMaster SET txnPin = @newPin, isForcedPwdChange = 0 WHERE username = @username AND txnPin = @currentPin

		EXEC proc_errorHandler 0, 'You have successfully changed your pin', @userName  
		RETURN
	END
	ELSE IF @flag = 'i'
	BEGIN
		
		IF NOT EXISTS(SELECT * FROM StaticDataValue(nolock) WHERE typeID = 2000 AND isActive='Y' AND CAST(valueid AS VARCHAR) = @occupation)
		BEGIN
			SET @occupationOther = @occupation

			SELECT @occupation = valueId
			FROM StaticDataValue(nolock) WHERE typeID = 2000 AND isActive='Y' AND detailTitle = 'Other (Please specify)'
		END

		IF EXISTS(SELECT TOP 1 'A' FROM customerMaster(NOLOCK) WHERE username = @userName)  AND EXISTS(SELECT TOP 1 'A' FROM customerMasterTemp(NOLOCK) WHERE username = @userName)
		BEGIN  
			EXEC proc_errorHandler 1, 'Username already taken, please select new username!', @userName  
			RETURN  
		END  

		IF EXISTS(SELECT 'X' FROM customerMaster with (nolock)  WHERE mobile = @mobile and ISNULL(onlineUser, 'N')='Y' and isnull(isDeleted,'N')='N' and customerId <> @customerId)  
		BEGIN  
			SELECT @errorMsg = 'Customer with mobile number ' + @mobile + ' already exist.'  
			EXEC proc_errorHandler 2, @errorMsg, @customerId  
			RETURN  
		END 

		IF @idType IS NULL AND @otherIdType IS NOT NULL
			SET @idType = 11402
		IF @occupation IS NULL AND @otherOccupation IS NOT NULL
			SET @occupation = 11383

		--alter table customerMasterTemp add documentType varchar(20)
		BEGIN TRANSACTION  
			IF @type IN ('0', '1')
			BEGIN
				IF EXISTS (SELECT 'X' FROM dbo.customerMasterTemp (NOLOCK) WHERE username = @userName)
				BEGIN
					UPDATE dbo.customerMasterTemp SET 
						occupation	=	ISNULL(@occupation, occupation),  
						monthlyIncome=	ISNULL(@monthlyIncome, monthlyIncome),
						employeeBusinessType=ISNULL(@businessType, employeeBusinessType),
						mobile = ISNULL(@mobile, mobile),
						ADDITIONALADDRESS	=	ISNULL(@additionalAddress, ADDITIONALADDRESS),
						idType		=	ISNULL(@idType, idType),
						documentType		=	ISNULL(@additionalIdType, documentType),
						nameOfEmployeer = ISNULL(@employeerName, nameOfEmployeer),
						otherIdNumber = @otherIdType,
						occupationOther = @otherOccupation,
						purposeOfRegistration = ISNULL(@purposeOfRegistration, purposeOfRegistration)
					WHERE username = @userName

					INSERT INTO dbo.customerMaster  
					(  
						fullName,firstName,mobile,gender,dob,occupation,nativeCountry,country,bankName,bankAccountNo,idType,idNumber,homePhone,idIssueDate  
						,idExpiryDate,verifyDoc1,verifyDoc2,verifyDoc3,SelfieDoc,referelCode,createdBy,createdDate,isActive,onlineUser,customerPassword,[address]  
						,city,state2,customerType,ADDITIONALADDRESS,monthlyIncome,organizationType,username,anotherIdType,employeeBusinessType,documentType,remittanceallowed
						,txnPin, nameOfEmployeer,isExistingCustomer, HasDeclare, createdFrom, occupationOther, purposeOfRegistration, email, customerEmail, otherIdNumber, isEmailVerified
					)  
					SELECT fullName,CMT.firstName,mobile,gender,dob,CMT.occupation,nativeCountry,113,bankName,bankAccountNo,idType,idNumber,CMT.homePhone,CMT.idIssueDate
						,idExpiryDate,verifyDoc1,verifyDoc2,verifyDoc3,CMT.selfie,CMT.referelCode,CMT.createdBy,createdDate,'Y','Y',customerPassword,[address]  
						,city,state2,4700, ADDITIONALADDRESS,monthlyIncome,organizationType,username,anotherIdType,employeeBusinessType,documentType,1
						,null, nameOfEmployeer,0, 0, 'M', @occupationOther, @purposeOfRegistration,email, customerEmail, otherIdNumber, isEmailVerified
					FROM dbo.CustomerMasterTemp AS CMT(NOLOCK)  
					WHERE CMT.username = @userName 

					
					SET @cust = @@IDENTITY      
					UPDATE dbo.mobile_userRegistration SET customerId = @cust WHERE username = @userName  

					DELETE FROM customerMasterTemp WHERE username = @userName
				END
				ELSE
				BEGIN
					IF EXISTS(SELECT 'x' FROM dbo.customerMaster (NOLOCK) WHERE username = @userName)
					BEGIN
						UPDATE dbo.customerMaster SET 
							occupation	=	ISNULL(@occupation, occupation),  
							monthlyIncome=	ISNULL(@monthlyIncome, monthlyIncome),
							employeeBusinessType=ISNULL(@businessType, employeeBusinessType),
							mobile = ISNULL(@mobile, mobile),
							ADDITIONALADDRESS	=	ISNULL(@additionalAddress, ADDITIONALADDRESS),
							idType		=	ISNULL(@idType, idType),
							documentType		=	ISNULL(@additionalIdType, documentType),
							nameOfEmployeer = ISNULL(@employeerName, nameOfEmployeer),
							otherIdNumber = @otherIdType,
							occupationOther = @otherOccupation,
							purposeOfRegistration = ISNULL(@purposeOfRegistration, purposeOfRegistration)
						WHERE username = @userName 
					END
				END
			END

			IF @type = '1'
			BEGIN
				IF ISNULL(@idFront, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idFront, 'idFrontCustUpload', @userName, GETDATE(), 11394, 'Y'
					
				IF ISNULL(@idBack, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idBack, 'idBackCustUpload', @userName, GETDATE(), 11395, 'Y'
					
				IF ISNULL(@idSide, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idSide, 'idSideCustUpload', @userName, GETDATE(), 11388, 'Y'
					
				IF ISNULL(@additionalId, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @additionalId, 'additionalIdCustUpload', @userName, GETDATE(), 11396, 'Y'
					
				IF ISNULL(@additionalIdBack, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @additionalIdBack, 'additionalIdBackCustUpload', @userName, GETDATE(), 11397, 'Y'	
					
				IF ISNULL(@facePicture, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @facePicture, 'facePictureCustUpload', @userName, GETDATE(), 11385, 'Y'			

				UPDATE dbo.customerMaster SET 
					HasDeclare = 1
				WHERE customerId = @customerId 
			END

		COMMIT TRANSACTION   
		IF @@TRANCOUNT=0  
		BEGIN  
			SELECT  0 as ERRORCODE, 'KYC Submitted successfully' AS MSG, @userName AS ID,@cust AS EXTRA  
			RETURN  
		END 
		EXEC proc_errorHandler 1, 'Failed to Submit KYC', @userName  
		RETURN  
	END
	ELSE IF @flag = 'i-existing'
	BEGIN
		IF NOT EXISTS(SELECT TOP 1 'A' FROM customerMaster(NOLOCK) WHERE username = @userName)
		BEGIN  
			EXEC proc_errorHandler 1, 'Invalid user supplied!', @userName  
			RETURN  
		END  
			
		--IF EXISTS(SELECT 'X' FROM customerMaster with (nolock)  
		--		WHERE mobile = @mobile 
		--		and ISNULL(onlineUser, 'N')='Y' 
		--		AND isnull(isDeleted,'N')='N' 
		--		AND customerId <> @customerId)  
		--BEGIN  
		--	SELECT @errorMsg = 'Customer with mobile number ' + @mobile + ' already exist.'  
		--	EXEC proc_errorHandler 1, @errorMsg, @customerId  
		--	RETURN  
		--END 
		IF @idType IS NULL AND @otherIdType IS NOT NULL
			SET @idType = 11402
		IF @occupation IS NULL AND @otherOccupation IS NOT NULL
			SET @occupation = 11383

		--alter table customerMasterTemp add documentType varchar(20)
		BEGIN TRANSACTION  
			IF @type IN ('0', '1')
			BEGIN
				IF EXISTS (SELECT 'X' FROM dbo.customerMasterTemp (NOLOCK) WHERE username = @userName)
				BEGIN
					UPDATE dbo.customerMasterTemp SET 
						occupation	=	ISNULL(@occupation, occupation), 
						monthlyIncome=	ISNULL(@monthlyIncome, monthlyIncome),
						employeeBusinessType=ISNULL(@businessType, employeeBusinessType),
						mobile = ISNULL(@mobile, mobile),
						ADDITIONALADDRESS	=	ISNULL(@additionalAddress, ADDITIONALADDRESS),
						idType		=	ISNULL(@idType, idType),
						documentType		=	ISNULL(@additionalIdType, documentType),
						nameOfEmployeer = ISNULL(@employeerName, nameOfEmployeer),
						otherIdNumber = @otherIdType,
						occupationOther = @otherOccupation,
						purposeOfRegistration = ISNULL(@purposeOfRegistration, purposeOfRegistration)
					WHERE username = @userName

					INSERT INTO dbo.customerMaster  
					(  
						fullName,firstName,mobile,gender,dob,occupation,nativeCountry,country,bankName,bankAccountNo,idType,idNumber,homePhone,idIssueDate  
						,idExpiryDate,verifyDoc1,verifyDoc2,verifyDoc3,SelfieDoc,referelCode,createdBy,createdDate,isActive,onlineUser,customerPassword,[address]  
						,city,state2,customerType,ADDITIONALADDRESS,monthlyIncome,organizationType,username,anotherIdType,employeeBusinessType,documentType,remittanceallowed
						,txnPin, nameOfEmployeer, visaStatus, occupationOther, purposeOfRegistration
					)  
					SELECT fullName,CMT.firstName,mobile,gender,dob,CMT.occupation,nativeCountry,113,bankName,bankAccountNo,idType,idNumber,CMT.homePhone,  
						CMT.idIssueDate,idExpiryDate,verifyDoc1,verifyDoc2,verifyDoc3,CMT.selfie,CMT.referelCode,CMT.createdBy,createdDate,'Y','Y',customerPassword,[address]  
						,city,state2,4700, ADDITIONALADDRESS,monthlyIncome,organizationType,username,anotherIdType,employeeBusinessType,documentType,1
						,null, nameOfEmployeer, visaStatus, @occupationOther, @purposeOfRegistration
					FROM dbo.CustomerMasterTemp AS CMT(NOLOCK)  
					WHERE CMT.username = @userName 

					SET @cust = @@IDENTITY      
					UPDATE dbo.mobile_userRegistration SET customerId = @cust WHERE username = @userName  
					
					DELETE FROM customerMasterTemp WHERE username = @userName
				END
				ELSE
				BEGIN
					SELECT @customerId = customerId 
					FROM customerMaster WITH(nolock) 
					WHERE username = @userName 

					IF EXISTS(SELECT 'x' FROM dbo.customerMaster (NOLOCK) WHERE username = @userName)
					BEGIN
						UPDATE dbo.customerMaster SET 
							occupation	=	ISNULL(@occupation, occupation),  
							monthlyIncome=	ISNULL(@monthlyIncome, monthlyIncome),
							employeeBusinessType=ISNULL(@businessType, employeeBusinessType),
							mobile = ISNULL(@mobile, mobile),
							ADDITIONALADDRESS	=	ISNULL(@additionalAddress, ADDITIONALADDRESS),
							idType		=	ISNULL(@idType, idType),
							documentType		=	ISNULL(@additionalIdType, documentType),
							nameOfEmployeer = ISNULL(@employeerName, nameOfEmployeer),
							visaStatus = ISNULL(@visaStatus, visaStatus),
							otherIdNumber = @otherIdType,
							occupationOther = @otherOccupation,
							purposeOfRegistration = ISNULL(@purposeOfRegistration, purposeOfRegistration)
						WHERE username = @userName
					END
				END
			END

			IF @type = '1'
			BEGIN
				IF ISNULL(@idFront, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idFront, 'idFrontCustUpload', @userName, GETDATE(), 11394, 'Y'
					
				IF ISNULL(@idBack, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idBack, 'idBackCustUpload', @userName, GETDATE(), 11395, 'Y'
					
				IF ISNULL(@idSide, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @idSide, 'idSideCustUpload', @userName, GETDATE(), 11388, 'Y'
					
				IF ISNULL(@additionalId, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @additionalId, 'additionalIdCustUpload', @userName, GETDATE(), 11396, 'Y'
					
				IF ISNULL(@additionalIdBack, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @additionalIdBack, 'additionalIdBackCustUpload', @userName, GETDATE(), 11397, 'Y'
					
				IF ISNULL(@facePicture, '') <> ''
					INSERT INTO customerDocument (customerId, fileName, fileDescription, createdBy, createdDate, documentType, isOnlineDoc)
					SELECT ISNULL(@customerId, @cust), @facePicture, 'facePictureCustUpload', @userName, GETDATE(), 11385, 'Y'		

				UPDATE dbo.customerMaster SET 
					HasDeclare = 1
				WHERE customerId = @customerId 
			END

		COMMIT TRANSACTION   
		IF @@TRANCOUNT=0  
		BEGIN  
			SELECT  0 as ERRORCODE, 'KYC Submitted successfully' AS MSG, @userName AS ID,@cust AS EXTRA  
			RETURN  
		END 
		EXEC proc_errorHandler 2, 'Failed to Submit KYC', @userName  
		RETURN  
	END
END

