ALTER PROC [dbo].[INBOUND_PROC_CUSTOMERBANKVERIFICATION]
(
	@flag					VARCHAR(20)
	,@pCustomerId			BIGINT			= NULL	
	,@pCertNumber  			CHAR(4)		 	= NULL
	,@pCertLimitCount 		INT 			= NULL
	,@pBankCode				VARCHAR(10)     = NULL
	,@pBankAccountNo 		VARCHAR(20) 	= NULL
	,@pIdType 				VARCHAR(10) 	= NULL
	,@pIdNumber 			VARCHAR(50) 	= NULL
	,@pDob 					VARCHAR(50)		= NULL
	,@pGender 				VARCHAR(10) 	= NULL
	,@pNativeCountry 		INT 			= NULL
	,@pAction 				VARCHAR(10) 	= NULL	
	,@pEmail 				VARCHAR(100) 	= NULL
	,@autodebitId 			VARCHAR(100) 	= NULL
)
AS

SET NOCOUNT ON;

DECLARE @vCustomerId 	BIGINT = 0
DECLARE @vTemp 			VARCHAR(10)
DECLARE	@vAction 		VARCHAR(10)
DECLARE	@vCertLimitCount INT
DECLARE @errorMessage 	VARCHAR(MAX)
DECLARE @vErrorCode		INT
DECLARE @tempTbl TABLE (errorcode VARCHAR(5), msg VARCHAR(MAX), id VARCHAR(50))
BEGIN TRY

	IF @flag = 'REQ'
	BEGIN
		SET 		@vTemp = 'N'
		SELECT @vTemp = 'Y',
			@vAction = action,
			@vCertLimitCount = certLimitCount
		FROM INBOUND_CUSTOMERBANKLIST(nolock)
		WHERE customerId = @pCustomerId AND bankCode = @pBankCode AND accountNo = @pBankAccountNo
		
		Declare @customerName nvarchar(100);
		select @pEmail = email
			,@customerName = firstName
		from customerMaster(nolock) cm
		where cm.customerId = @pCustomerId
		------------------------------------------------
		BEGIN TRANSACTION
		------------------------------------------------
	
		IF ISNULL(@vTemp,'') <> 'Y'
		BEGIN
			INSERT INTO INBOUND_CUSTOMERBANKLIST
			(
				customerId, customerName , certNumber, certLimitCount,bankCode,
				accountNo, action, reqDate, createdDate, createdBy
			)
			VALUES
			(
				@pCustomerId, @customerName, @pCertNumber, '2', @pBankCode,
				@pBankAccountNo, @pAction, GETDATE(), GETDATE(), @pEmail
			)
			
			SET @errorMessage = 'Data saved successfully.'	
			EXEC proc_errorHandler 0, @errorMessage, @pBankAccountNo

			INSERT INTO FastMoneyPro_Account.dbo.temp_tran(entry_user_id,acct_num,part_tran_type,tran_amt,field1,field2,sessionID,refrence)
			SELECT 'system','100241011536','cr',1,@pIdNumber,'Penny Test',@pIdNumber,@pCustomerId 
			union all
			SELECT 'system','910141047584','dr',1,@pIdNumber,'Penny Test',@pIdNumber,@pCustomerId  

		END	
		ELSE	
		BEGIN
			IF (@vAction = 'SUCCESS')
			BEGIN
				SET @errorMessage = 'This account is already registered.'	
				EXEC proc_errorHandler 1, @errorMessage, @pBankAccountNo
			END	
			ELSE IF (@vCertLimitCount <= '0')
			BEGIN
				SET @errorMessage = 'You have exceeded the number of requests.'	
				EXEC proc_errorHandler 0, @errorMessage, @pBankAccountNo
			END	
			ELSE
			BEGIN
				SET @vCertLimitCount = @vCertLimitCount - 1
				
				UPDATE 		INBOUND_CUSTOMERBANKLIST
				SET			--customerId 	= @pCustomerId,
							certNumber 	= @pCertNumber,
							certLimitCount = @vCertLimitCount,
							--idType 		= @pIdType,
							--idNumber 	= @pIdNumber,
							--dob 		= @pDob,
							--gender 		= @pGender,
							--nativeCountry = @pNativeCountry,
							action 		= @pAction,
							reqDate  	= GETDATE()
				WHERE		customerId = @pCustomerId AND  bankCode = @pBankCode AND accountNo = @pBankAccountNo
				
				SET @errorMessage = 'Data saved successfully.'	
				EXEC proc_errorHandler 0, @errorMessage, @pBankAccountNo

				INSERT INTO FastMoneyPro_Account.dbo.temp_tran(entry_user_id,acct_num,part_tran_type,tran_amt,field1,field2,sessionID,refrence)
				SELECT 'system','100241011536','cr',1,@pIdNumber,'Penny Test',@pIdNumber,@pCustomerId 
				union all
				SELECT 'system','910141047584','dr',1,@pIdNumber,'Penny Test',@pIdNumber,@pCustomerId  
			END	
		END
		
		------------------------------------------------
		COMMIT TRANSACTION
		DECLARE @vDate date = GETDATE(),@vRemarks varchar(200) 
		SELECT @vRemarks = 'Penny Test : '+ @pBankAccountNo+'('+BankName+') ID:' + @pIdNumber FROM KoreanBankList(NOLOCK) WHERE bankCode = @pBankCode

		INSERT INTO @tempTbl(errorcode, msg, id)
		exec FastMoneyPro_Account.dbo.[spa_saveTempTrn] @flag='i',@sessionID= @pIdNumber,@date=@vDate,@narration = @vRemarks,@company_id=1,@v_type='j',@user='system'
		------------------------------------------------
	END
	ELSE IF @flag = 'TRY'
	BEGIN

		if exists(select 1 from INBOUND_CUSTOMERBANKLIST(nolock) where customerId = @pCustomerId and certNumber = @pCertNumber AND bankCode = @pBankCode AND accountNo = @pBankAccountNo)
		begin
			if exists(select 1 from inbound_customerbanklist(nolock) where action = 'REQ' and customerId = @pCustomerId and certNumber = @pCertNumber AND bankCode = @pBankCode AND accountNo = @pBankAccountNo)
			begin
				UPDATE 	INBOUND_CUSTOMERBANKLIST
					SET action 		= 'SUCCESS',
						certDate  	= GETDATE(),
						isPennyTest = 1
					WHERE customerId = @pCustomerId AND action = 'REQ' AND bankCode = @pBankCode AND accountNo = @pBankAccountNo

				SET @errorMessage = 'Bank verified successfully.'	
				EXEC proc_errorHandler 0, @errorMessage, @pBankAccountNo	
				return;
			end

			if exists(select 1 from INBOUND_CUSTOMERBANKLIST(nolock) where action = 'SUCCESS' and customerId = @pCustomerId and certNumber = @pCertNumber AND bankCode = @pBankCode AND accountNo = @pBankAccountNo)
			begin
				SET @errorMessage = 'This account is already registered.'	
				EXEC proc_errorHandler 0, @errorMessage, @pBankAccountNo	
				return;
			end
		end
		else
		BEGIN
			SET @errorMessage = 'Bank verification failed,Please check the statement and enter the 4 digit number display after GME.'	
			EXEC proc_errorHandler 1, @errorMessage, @pBankAccountNo
		END	
		
	END
	ELSE IF @flag = 'CUSTOMER-INFO'
	BEGIN
		IF @pEmail IS NOT NULL
			SELECT @pCustomerId= customerId FROM customerMaster(NOLOCK) WHERE EMAIL = @pEmail
		SET @vErrorCode = 0
		
		
		SELECT 		@vAction = action,
					@vCertLimitCount = certLimitCount
		FROM 		INBOUND_CUSTOMERBANKLIST(nolock)
		WHERE		customerId = @pCustomerId AND bankCode = @pBankCode AND accountNo = @pBankAccountNo

		SELECT @vErrorCode = CASE WHEN @vAction = 'SUCCESS' OR @vCertLimitCount = 0 THEN 1 WHEN @vAction = 'REQ' AND @vCertLimitCount > 0 THEN 10 ELSE 0 END

		SELECT	@vErrorCode AS ErrorCode, @errorMessage as Msg,
				CM.IdType, CM.IdNumber,Dob = FORMAT(CM.dob,'MM/dd/yyyy'), CM.Gender, CM.NativeCountry,
				KBL.BankCode, KBL.BankName, CM.BankAccountNo, CM.CustomerId	 
		FROM 	customerMaster CM (nolock)
		INNER JOIN  KoreanBankList KBL (nolock) ON CM.bankName = KBL.rowId
		where 	customerId	= @pCustomerId
	END
	ELSE IF @flag = 'CUSTOMER-BANK-INFO'
	BEGIN
		IF EXISTS (	SELECT 		1
					FROM 		INBOUND_CUSTOMERBANKLIST(nolock)
					WHERE		customerId 		= @pCustomerId
					AND 		action 			= 'REQ' AND bankCode=@pBankCode AND accountNo=@pBankAccountNo  )
		BEGIN			
			SELECT 		KBL.BankName, CC.accountNo, CC.certLimitCount  
			FROM  		customerMaster CM (nolock)
			INNER JOIN  KoreanBankList KBL (nolock)
			ON 			CM.bankName = KBL.rowId
			LEFT OUTER JOIN INBOUND_CUSTOMERBANKLIST CC (nolock)
			ON 			CM.customerId = CC.customerId
			WHERE 		CM.customerId  = @pCustomerId and CC.action = 'REQ' 
			AND cc.bankCode=@pBankCode AND cc.accountNo=@pBankAccountNo

		END	
	END
	ELSE IF @flag = 'AutodebitById'
	BEGIN
		SELECT TOP 1 bankCodeStd AS BankCode, accountNum AS AccountNo FROM dbo.KFTC_CUSTOMER_SUB(NOLOCK) WHERE RowId= @autodebitId
		RETURN
	END
END TRY
BEGIN CATCH
    IF @@TRANCOUNT <> 0
        ROLLBACK TRANSACTION;
    SET @errorMessage = 'Error Occur while requesting.'
    EXEC proc_errorHandler 1, @errorMessage, @pCustomerId	
END CATCH

