
ALTER PROC [dbo].[proc_online_approve_Customer]  
 @flag						VARCHAR(50)		= NULL   
,@user						VARCHAR(30)		= NULL  
,@customerId				VARCHAR(30)		= NULL 
,@searchCriteria			VARCHAR(30)		= NULL
,@searchValue				VARCHAR(50)		= NULL
,@fromDate					DATETIME		= NULL
,@toDate					DATETIME		= NULL
,@cusType					VARCHAR(50)		= NULL
,@accountNumber				VARCHAR(100)	= NULL
,@CustomerBankName			NVARCHAR(100)	= NULL
,@obpId						VARCHAR(50)     =NULL
--grid parameters
,@pageSize					VARCHAR(50)		= NULL
,@pageNumber				VARCHAR(50)		= NULL
,@sortBy					VARCHAR(50)		= NULL
,@sortOrder					VARCHAR(50)		= NULL
,@virtualAccountNo			VARCHAR(50)		= NULL
,@primaryAccountNo			VARCHAR(50)		= NULL
,@isKftcOnly				VARChar(2)		= NULL    
AS  
SET NOCOUNT ON;  
SET XACT_ABORT ON;  
BEGIN TRY
	DECLARE  @table				VARCHAR(MAX)
			,@select_field_list	VARCHAR(MAX)
			,@extra_field_list	VARCHAR(MAX)
			,@sql_filter		VARCHAR(MAX)
	DECLARE @email VARCHAR(200)
			,@username VARCHAR(50)
			,@pwd VARCHAR(50)
			,@channel VARCHAR(20)=NULL

	IF @flag='vl' --verified list/approve pending list
	BEGIN
	SET @sortBy = 'createdDate'
	SET @sortOrder = 'desc'

	SET @table ='(
		SELECT SN=ROW_NUMBER() over(ORDER BY cm.customerId asc)
				,customerId=cm.customerId
				,email=cm.email
				,fullName= REPLACE(ISNULL(cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName1, ''''), ''  '', '' '')
				,dob=CONVERT(VARCHAR,cm.dob,101)
				,address=cm.[address]
				,nativeCountry=com.countryName
				,idtype=sdv.detailTitle
				,idNumber=cm.idNumber
				,createdDate=CAST(cm.createdDate AS DATE)
				,verifiedBy=cm.verifiedBy
				,branchName=''''
				,verifiedDate=CAST(cm.verifiedDate AS DATE)
				,ipAddress=cm.ipAddress
				,mobile=cm.mobile
				,bankAccountNo
				,bankName=bl.bankName
		FROM customerMaster cm(NOLOCK)
		LEFT JOIN countryMaster com(NOLOCK) ON cm.nativeCountry=com.countryId
		INNER JOIN staticDataValue sdv (NOLOCK) ON sdv.valueId=cm.idType AND sdv.typeID=1300
		LEFT JOIN vwBankLists bl (NOLOCK) ON cm.bankName = bl.rowId
		WHERE cm.verifiedDate IS NOT NULL AND cm.approvedDate IS NULL'
		
	IF ISNULL(@fromDate,'')<>'' AND ISNULL(@toDate,'')<>''
	SET @table=@table + ' AND cm.createdDate BETWEEN ''' +CAST(@fromDate AS VARCHAR)+''' AND ''' +CAST(@toDate AS VARCHAR)+''''

	SET @table = @table + ')x'
	SET @sql_filter = ''

	IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
	BEGIN 
		IF @searchCriteria='idNumber'
		BEGIN
			--IF ISNUMERIC(@searchValue)<>1
			--	SET @searchValue='-1'	--to ignore string value for datatype integer/customerID
			--SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
			SET @sql_Filter=@sql_Filter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
		END
		ELSE IF @searchCriteria='emailId'
			SET @sql_Filter=@sql_Filter + ' AND email like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='customerName'
			SET @sql_Filter=@sql_Filter + ' AND fullName like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='mobile'
			SET @sql_Filter=@sql_Filter + ' AND mobile = ''' +@searchValue+''''
		ELSE IF @searchCriteria='walletAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND walletAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='nativeCountry'
			SET @sql_Filter=@sql_Filter + ' AND nativeCountry = ''' +@searchValue+''''
		ELSE IF @searchCriteria='uniqueid'
			SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
		
	END

	
	SET @select_field_list ='
				 SN,customerId,email,fullName,dob,address,nativeCountry,idtype,idNumber
				,createdDate,verifiedBy,branchName,verifiedDate,bankAccountNo,bankName
				'	
	EXEC dbo.proc_paging
			@table,@sql_filter,@select_field_list,@extra_field_list
			,@sortBy,@sortOrder,@pageSize,@pageNumber

		RETURN
	END

	ELSE IF @flag='al' --approved list
	BEGIN
	IF @sortBy IS NULL
			SET @sortBy = 'customerId'
	IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
	SET @table ='(
		SELECT SN=ROW_NUMBER() over(ORDER BY cm.customerId asc)
				,customerId=cm.customerId
				,cm.membershipId
				,email=cm.email
				,fullName= cm.firstName
				,idtype=sdv.detailTitle
				,idNumber=cm.idNumber
				,mobile=cm.mobile
				,bankName=bl.bankName
				,cm.bankAccountNo
				,CM.walletAccountNo
				,cm.availableBalance
				,cm.dob,cm.address
				,country = ''South Korea''
				,nativeCountry = com.CountryName
				,cm.createdDate
		FROM customerMaster cm(NOLOCK)
		INNER JOIN staticDataValue sdv (NOLOCK) ON sdv.valueId=cm.idType 
		LEFT JOIN countryMaster com(NOLOCK) ON cm.nativeCountry=com.countryId
		LEFT JOIN KoreanBankList bl (NOLOCK) ON cm.bankName = bl.rowId
		WHERE cm.approvedDate is not null '
		
	IF (SELECT DATEDIFF(DAY,@fromDate,@toDate)) >10
		SET @table=@table + ' AND 1=2 '
	IF ISNULL(@fromDate,'')<>'' AND ISNULL(@toDate,'')<>''
	SET @table=@table + ' AND cm.approvedDate BETWEEN ''' +CAST(CAST(@fromDate AS DATE) AS VARCHAR)+''' AND ''' +CAST(CAST(@toDate AS DATE) AS VARCHAR)+' 23:59:59'+''''

	SET @table = @table + ')x'
	SET @sql_filter = ''

	IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
	BEGIN 
		IF @searchCriteria='idNumber'
		BEGIN
			--IF ISNUMERIC(@searchValue)<>1
			--	SET @searchValue='-1'	--to ignore string value for datatype integer/customerID
			--SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
			SET @sql_Filter=@sql_Filter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
		END
		ELSE IF @searchCriteria='emailId'
			SET @sql_Filter=@sql_Filter + ' AND email = ''' +@searchValue+''''
		ELSE IF @searchCriteria='customerName'
			SET @sql_Filter=@sql_Filter + ' AND fullName like ''%' +@searchValue+'%'''
		ELSE IF @searchCriteria='mobile'
			SET @sql_Filter=@sql_Filter + ' AND mobile = ''' +@searchValue+''''
		ELSE IF @searchCriteria='walletAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND walletAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='goldCardNumber'
			SET @sql_Filter=@sql_Filter + ' AND membershipId = ''' +@searchValue+''''
		ELSE IF @searchCriteria='nativeCountry'
			SET @sql_Filter=@sql_Filter + ' AND nativeCountry = ''' +@searchValue+''''
		ELSE IF @searchCriteria='uniqueid'
			SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
		
	END

	SET @select_field_list ='
				 SN,customerId,email,fullName,idtype,idNumber,mobile,bankName,bankAccountNo,walletAccountNo,availableBalance
				,dob,address,country,nativeCountry,createdDate'	
	EXEC dbo.proc_paging
			@table,@sql_filter,@select_field_list,@extra_field_list,@sortBy,@sortOrder,@pageSize,@pageNumber

		RETURN
	END
	ELSE IF @flag ='IL'   -- inactive customer list
	BEGIN
	IF @sortBy IS NULL
			SET @sortBy = 'customerId'
	IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
	SET @table ='(
		SELECT SN=ROW_NUMBER() over(ORDER BY cm.customerId asc)
				,customerId=cm.customerId
				,email=cm.email
				,fullName= cm.firstName
				,idtype=sdv.detailTitle
				,idNumber=cm.idNumber
				,mobile=cm.mobile
				,bankName=bl.bankName
				,cm.bankAccountNo
				,CM.walletAccountNo
				,cm.availableBalance
				,cm.dob,cm.address
				,country = ''South Korea''
				,nativeCountry = com.CountryName
				,cm.createdDate
				,cm.modifiedDate
				,cm.modifiedBy
		FROM customerMaster cm(NOLOCK)
		INNER JOIN staticDataValue sdv (NOLOCK) ON sdv.valueId=cm.idType 
		LEFT JOIN countryMaster com(NOLOCK) ON cm.nativeCountry=com.countryId
		LEFT JOIN KoreanBankList bl (NOLOCK) ON cm.bankName = bl.rowId
		WHERE  ISNULL(cm.isActive,''N'') =''N'' '
		
	
	IF ISNULL(@fromDate,'')<>'' AND ISNULL(@toDate,'')<>''
	SET @table=@table + ' AND cm.createdDate BETWEEN ''' +CAST(CAST(@fromDate AS DATE) AS VARCHAR)+''' AND ''' +CAST(CAST(@toDate AS DATE) AS VARCHAR)+' 23:59:59'+''''

	SET @table = @table + ')x'
	SET @sql_filter = ''

	IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
	BEGIN 
		IF @searchCriteria='idNumber'
		BEGIN
			--IF ISNUMERIC(@searchValue)<>1
			--	SET @searchValue='-1'	--to ignore string value for datatype integer/customerID
			--SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
			SET @sql_Filter=@sql_Filter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
		END
		ELSE IF @searchCriteria='emailId'
			SET @sql_Filter=@sql_Filter + ' AND email like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='customerName'
			SET @sql_Filter=@sql_Filter + ' AND fullName like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='mobile'
			SET @sql_Filter=@sql_Filter + ' AND mobile = ''' +@searchValue+''''
		ELSE IF @searchCriteria='walletAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND walletAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='nativeCountry'
			SET @sql_Filter=@sql_Filter + ' AND nativeCountry = ''' +@searchValue+''''
		ELSE IF @searchCriteria='uniqueid'
			SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
	END

	SET @select_field_list ='
				 SN,customerId,email,fullName,idtype,idNumber,mobile,bankName,bankAccountNo,walletAccountNo,availableBalance
				,dob,address,country,nativeCountry,createdDate,modifiedDate,modifiedBy'	
	EXEC dbo.proc_paging
			@table,@sql_filter,@select_field_list,@extra_field_list,@sortBy,@sortOrder,@pageSize,@pageNumber

		RETURN
	END
	ELSE IF @flag='p' --pending list
	BEGIN
	IF @sortBy IS NULL
			SET @sortBy = 'customerId'
	IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
	SET @table ='(
		SELECT SN=ROW_NUMBER() over(ORDER BY cm.customerId asc)
				,customerId=cm.customerId
				,email=cm.email
				,fullName= REPLACE(ISNULL(cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName1, ''''), ''  '', '' '')
				,dob=CONVERT(VARCHAR,cm.dob,101)
				,address=cm.[address]
				,country=c.countryName
				,ipAddress=isnull(cm.ipAddress,'''')
				,nativeCountry=com.countryName
				,idtype=sdv.detailTitle
				,idNumber=cm.idNumber
				,telNo=isnull(cm.telNo,'''')
				,mobile=cm.mobile
				,createdDate=CAST(cm.createdDate AS DATE)
				,bankName=bl.bankName
				,cm.bankAccountNo
		FROM customerMaster cm(NOLOCK)
		LEFT JOIN countryMaster com(NOLOCK) ON cm.nativeCountry=com.countryId
		LEFT JOIN countryMaster c(NOLOCK) ON cm.country=c.countryId
		INNER JOIN staticDataValue sdv (NOLOCK) ON sdv.valueId=cm.idType AND sdv.typeID=1300
		LEFT JOIN vwBankLists bl (NOLOCK) ON cm.bankName = bl.rowId
		WHERE cm.verifiedDate IS NULL'

	
	IF ISNULL(@fromDate,'')<>'' AND ISNULL(@toDate,'')<>''
	SET @table=@table + ' AND cm.createdDate BETWEEN ''' +CAST(@fromDate AS VARCHAR)+''' AND ''' +CAST(@toDate AS VARCHAR)+''''

	SET @table = @table + ')x'
	SET @sql_filter = ''

	IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
	BEGIN 
		IF @searchCriteria='idNumber'
		BEGIN
			SET @sql_Filter=@sql_Filter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
		END
		ELSE IF @searchCriteria='emailId'
			SET @sql_Filter=@sql_Filter + ' AND email like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='customerName'
			SET @sql_Filter=@sql_Filter + ' AND fullName like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='mobile'
			SET @sql_Filter=@sql_Filter + ' AND mobile = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='nativeCountry'
			SET @sql_Filter=@sql_Filter + ' AND nativeCountry = ''' +@searchValue+''''
		ELSE IF @searchCriteria='uniqueid'
			SET @sql_Filter=@sql_Filter + ' AND customerid = ''' +@searchValue+''''
	END

	SET @select_field_list ='
				 SN
				,customerId
				,email
				,fullName
				,dob
				,address
				,country
				--,ipAddress
				,nativeCountry
				,idtype
				,idNumber
				--,telNo
				,mobile
				,createdDate
				,bankName
				,bankAccountNo
				'	
	EXEC dbo.proc_paging
			@table
			,@sql_filter
			,@select_field_list
			,@extra_field_list
			,@sortBy
			,@sortOrder
			,@pageSize
			,@pageNumber

		RETURN
	END

	ELSE IF @flag='searchCriteria'
	BEGIN
		SELECT  ''	value,			'Select' [text]		UNION ALL
		SELECT  'emailId',			'User ID'			UNION ALL
		SELECT  'IdNumber',			'ID - Number' UNION ALL
		--SELECT  'nativeCountry',	'Native Country' UNION ALL
		SELECT  'customerName',		'Customer Name'		UNION ALL
		SELECT  'walletAccountNo',	'Virtual Account No' UNION ALL
		SELECT  'bankAccountNo',	'Registered Bank Account No' UNION ALL
		SELECT  'mobile',			'Mobile No'		UNION ALL 
		SELECT  'goldCardNumber',	'Gold Card Number'	UNION all	
		SELECT  'uniqueid',	'Unique Id'		
		
		RETURN
	END
	ELSE IF @flag='UnApprovesearch'
	BEGIN
		SELECT  ''	value,			'Select' [text]		UNION ALL
		SELECT  'emailId',			'User ID'			UNION ALL
		SELECT  'IdNumber',			'ID - Number' UNION ALL
		SELECT  'nativeCountry',	'Native Country' UNION ALL
		SELECT  'customerName',		'Customer Name'		UNION ALL
		SELECT  'walletAccountNo',	'Virtual Account No' UNION ALL
		SELECT  'bankAccountNo',	'Registered Bank Account No' UNION ALL
		SELECT  'mobile',			'Mobile No'		UNION ALL 
		SELECT  'goldCardNumber',	'Gold Card Number'	UNION ALl
		SELECT  'uniqueid',	'Unique Id'		
		
		RETURN
	END
	ELSE IF @flag='ddlCustomerType'
	BEGIN
		SELECT  '' value,  'Select'	[text]	UNION ALL
		SELECT  'n',  'Newly Registered'	UNION ALL
		SELECT  'y',  'Existing'
		RETURN
	END

	ELSE IF @flag='verify-pending'
	BEGIN
		UPDATE dbo.customerMaster SET verifiedDate=GETDATE(),verifiedBy=@user  WHERE customerId=@customerId
		SELECT '0' ErrorCode , 'Customer verified successfully.' Msg , @customerId id
        
		RETURN
	END

	ELSE IF @flag='approve-pending'
	BEGIN

	DECLARE @custIdNumber VARCHAR(50),@AlreadyExistCustomer  VARCHAR(100),@bankName VARCHAR(100)
	 
	 SELECT @custIdNumber = idNumber , @bankName=bankName , @accountNumber=bankAccountNo
	 FROM dbo.customerMaster (NOLOCK) 
	 WHERE customerId = @customerId
	 
	 select @cusType = userType from applicationUsers(nolock) where userName = @user

	 SELECT TOP 1 @AlreadyExistCustomer=email FROM dbo.customerMaster (NOLOCK) WHERE  bankName=@bankName AND bankAccountNo=@accountNumber AND approvedBy IS NOT NULL

	 IF EXISTS (SELECT 'X' FROM dbo.customerMaster (NOLOCK) WHERE replace(idNumber,'-','') = replace(@custIdNumber, '-', '')
	   GROUP BY replace(idNumber,'-','') having count(1)>1)
	 BEGIN
		SELECT '1' ErrorCode , 'Duplicate id number found for customer' Msg ,null
		RETURN
	 END
	 
	 IF EXISTS (SELECT 'X' FROM dbo.customerMaster (NOLOCK) WHERE replace(idNumber,'-','') = replace(@custIdNumber, '-', '') AND approvedBy IS NOT NULL)
	 BEGIN
		SELECT '1' ErrorCode , 'Customer with same id number already approved.' Msg ,null
		RETURN
	 END
	 IF EXISTS (SELECT 'X' FROM dbo.customerMaster (NOLOCK) WHERE  bankName=@bankName AND bankAccountNo=@accountNumber AND approvedBy IS NOT NULL)
	 BEGIN
		SELECT '1' ErrorCode , 'Customer with same bank details already exists. Already Exists Customer : '+ISNULL(@AlreadyExistCustomer,'') Msg ,null
		RETURN
	 END

	 IF NOT EXISTS ( SELECT TOP 1 virtualAccNumber from VirtualAccountMapping  WITH (NOLOCK) )
	 BEGIN
		SELECT '1' ErrorCode , 'No stock Virtual account found, please upload  ! Warning ' Msg ,null
		RETURN
	 END

	 IF @cusType <> 'HO'
	 BEGIN
			--UPDATE  customerMaster
			--SET dob = dbo.FNAGETDOB_FROM_ALIENCARD(LEFT(idNumber,6),RIGHT(LEFT(idNumber,8),1))
			--WHERE DOB IS NULL AND idNumber LIKE '%-%'

			UPDATE customerMaster SET verifiedBy = @user,verifiedDate = GETDATE(),customerStatus ='S' where customerId = @customerId
			
			INSERT INTO SuspiciousRegisteredCustomer(OrgCustomerId,MatchedCustomerId)
			EXEC [Proc_ValidateDuplicateCustomer] @customerId = @customerId

			if exists(select 'a' from SuspiciousRegisteredCustomer(nolock) where OrgCustomerId = @customerId)
			begin
				SELECT '1' ErrorCode , 'Suspicious customer verified but can not approve,please contact HO ' Msg ,null
				return
			end
	 END

	 IF EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @customerId AND walletAccountNo IS NULL)
	 BEGIN
		IF ISNULL(@isKftcOnly,'N') = 'Y'
		BEGIN
			DECLARE @dummyWallet BIGINT
			SELECT TOP 1 @dummyWallet = VirtualWalletNo from FastMoneyPro_Account.dbo.billSetting WITH (NOLOCK)

			SET @accountNumber  = CONVERT(VARCHAR,@dummyWallet + 1)
			UPDATE 	FastMoneyPro_Account.dbo.billSetting SET  VirtualWalletNo = @dummyWallet + 1 WHERE rowid = 1
		END
		ELSE
		BEGIN
			SELECT TOP 1 @accountNumber = virtualAccNumber from VirtualAccountMapping  WITH (NOLOCK) where customerId is null

			UPDATE VirtualAccountMapping SET customerId = @customerId WHERE virtualAccNumber = @accountNumber 
		END

		UPDATE dbo.customerMaster SET  walletAccountNo=@accountNumber , CustomerBankName = @CustomerBankName
				,DOB = CASE WHEN DOB IS NULL AND idType IN(1302,8008) THEN dbo.FNAGETDOB_FROM_ALIENCARD(LEFT(@custIdNumber,6),RIGHT(LEFT(@custIdNumber,8),1)) 
					   ELSE DOB END,IsKftcOnly =  @isKftcOnly
		WHERE customerId = @customerId
	END

	DECLARE @newPassword varchar(20)  =  RIGHT('0000000' + CAST(CHECKSUM(NEWID()) AS VARCHAR), 7)

	IF EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @customerId and customerPassword is not null)
		SELECT @newPassword = [REDACTED] FROM customerMaster(NOLOCK) WHERE customerId = @customerId and customerPassword is not null
	ELSE
	BEGIN
		UPDATE dbo.customerMaster SET  customerPassword = [REDACTED] WHERE customerId = @customerId
	END
	-- @Max
	-- 2018.09
	SELECT 		username = ISNULL(cm.email,''),
				[password] = dbo.FNADecryptString(cm.customerPassword),
				[channel]  = 'registration',
				[account]  = ISNULL(cm.membershipId,''),
				walletAccountNo,
				bankAccountNo,
				fullName,
				CustomerBankName,
				k.bankCode,
				cm.idType,				
				REPLACE(cm.idNumber, ' ', '') AS [idNumber],
				CONVERT(VARCHAR(6), cm.dob, 12) AS [dobYMD],
				CASE WHEN cm.gender='97' THEN '7'
					 WHEN cm.gender='98' THEN '8' END AS [genderCode],
				CASE WHEN cm.nativeCountry = '238' THEN '1'
					 WHEN cm.nativeCountry = '113' THEN '2'
					 WHEN cm.nativeCountry = '45'  THEN '3'
					 ELSE '4' END AS [nativeCountryCode]				
	FROM 		dbo.customerMaster cm(NOLOCK) 
	INNER JOIN 	KoreanBankList k (nolock) on k.rowId = cm.bankName
	WHERE 		cm.customerId = @customerId
		
	SELECT '0' ErrorCode , 'Customer verified successfully.' Msg ,@customerId id
		RETURN
	END

	ELSE IF @flag='update-obpId'
	BEGIN
		BEGIN TRAN
			UPDATE dbo.customerMaster SET 
				obpId			=	@obpId 
				,approvedDate	=	GETDATE(),approvedBy=@user ,customerStatus= 'V'
				,verifiedBy		=	case when verifiedBy is null then @user else verifiedBy end
				,verifiedDate	=	case when verifiedDate is null then GETDATE() else verifiedDate end
				,isActive		=	'Y'
			WHERE customerId = @customerId

			DECLARE @Mobile VARCHAR(20)
			select @virtualAccountNo =  walletAccountNo
					,@CustomerBankName = firstName+'- Principle'
					,@Mobile	 = mobile
			from customerMaster(nolock)
			WHERE customerId = @customerId

			----#### SEND NOTIFICATION TO CUSTOMER
			DECLARE @SMSBody VARCHAR(90) = 'Dear '+LEFT(@CustomerBankName,14)+' You are successfully registered with GME.Thank you for choosing GME.'
			
			exec FastMoneyPro_Remit.dbo.proc_CallToSendSMS @FLAG = 'I',@SMSBody = @SMSBody,@MobileNo = @Mobile

			IF not EXISTS(SELECT 'A' FROM FastMoneyPro_Account.dbo.ac_master (NOLOCK) WHERE acct_num = @virtualAccountNo)
			begin
				DECLARE @GL INT = 79

				insert into FastMoneyPro_Account.dbo.ac_master 
					(acct_num, acct_name,gl_code, branch_id,acct_ownership,acct_rpt_code
					, acct_opn_date,clr_bal_amt, system_reserved_amt,lien_amt, utilised_amt, available_amt,created_date,created_by,company_id)

				select @virtualAccountNo,@CustomerBankName,@GL,@customerId,'c' ,'CP'
					,getdate(),0,0,0,0,0,getdate(),@user,1
			end

			----coupon id '100% coupon-1 time'= 100% service free
			IF NOT EXISTS(SELECT 'A' FROM CouponIssue(NOLOCK) WHERE customerId = @customerId)
			BEGIN
				insert into CouponIssue(customerId,couponId,startDate,endDate,createdDate,createdBy,isActive,usedCount)
				select @customerId,'21',GETDATE(),'2021-01-01 00:00:00.000',GETDATE(),'system','Y',0
			END
			
		commit tran
		SELECT '0' ErrorCode , 'Customer registered successfully.' Msg , @customerId id
		--EXEC PROC_GOLDCARD_ACCOUNT_ENTRY @customerId
	END

	ELSE IF @flag='checkVirtualNo'
	BEGIN
		IF NOT EXISTS (SELECT 'X' FROM dbo.customerMaster WHERE walletAccountNo=@virtualAccountNo)
		SELECT '1' ErrorCode , 'Invalid Virtual AccountNo' Msg , NULL id
		RETURN;
	END

	ELSE IF @flag='checkPrimaryAccountNo'
	BEGIN
		IF NOT EXISTS (SELECT 'X' FROM dbo.customerMaster WHERE bankAccountNo=@primaryAccountNo)
		SELECT '1' ErrorCode , 'Invalid Primary AccountNo' Msg , NULL id
	END
	ELSE IF @flag='AuditList' --AUDITED DOC LIST
	BEGIN
	SET @sortBy = 'createdDate'
	SET @sortOrder = 'desc'

	SET @table ='(
		SELECT SN=ROW_NUMBER() over(ORDER BY cm.customerId asc)
				,customerId=cm.customerId
				,email=cm.email
				,fullName= REPLACE(ISNULL(cm.firstName, '''') + ISNULL('' '' + cm.middleName, '''') + ISNULL('' '' + cm.lastName1, ''''), ''  '', '' '')
				,dob=CONVERT(VARCHAR,cm.dob,101)
				,address=cm.[address]
				,nativeCountry=com.countryName
				,idtype=sdv.detailTitle
				,idNumber=cm.idNumber
				,createdDate=CAST(cm.createdDate AS DATE)
				,verifiedBy=cm.verifiedBy
				,branchName=''''
				,verifiedDate=CAST(cm.verifiedDate AS DATE)
				,ipAddress=cm.ipAddress
				,mobile=cm.mobile
				,bankAccountNo
				,bankName=bl.bankName
				,cm.AuditBy,cm.AuditDate
		FROM customerMaster cm(NOLOCK)
		LEFT JOIN countryMaster com(NOLOCK) ON cm.nativeCountry=com.countryId
		INNER JOIN staticDataValue sdv (NOLOCK) ON sdv.valueId=cm.idType AND sdv.typeID=1300
		LEFT JOIN vwBankLists bl (NOLOCK) ON cm.bankName = bl.rowId
		WHERE cm.verifiedDate IS NOT NULL AND cm.AuditDate IS NOT NULL'
		
	IF ISNULL(@fromDate,'')<>'' AND ISNULL(@toDate,'')<>''
	SET @table=@table + ' AND cm.createdDate BETWEEN ''' +CAST(@fromDate AS VARCHAR)+''' AND ''' +CAST(@toDate AS VARCHAR)+''''

	SET @table = @table + ')x'
	SET @sql_filter = ''

	IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
	BEGIN 
		IF @searchCriteria='idNumber'
		BEGIN
			--IF ISNUMERIC(@searchValue)<>1
			--	SET @searchValue='-1'	--to ignore string value for datatype integer/customerID
			--SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
			SET @sql_Filter=@sql_Filter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
		END
		ELSE IF @searchCriteria='emailId'
			SET @sql_Filter=@sql_Filter + ' AND email like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='customerName'
			SET @sql_Filter=@sql_Filter + ' AND fullName like ''' +@searchValue+'%'''
		ELSE IF @searchCriteria='mobile'
			SET @sql_Filter=@sql_Filter + ' AND mobile = ''' +@searchValue+''''
		ELSE IF @searchCriteria='walletAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND walletAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='bankAccountNo'
			SET @sql_Filter=@sql_Filter + ' AND bankAccountNo = ''' +@searchValue+''''
		ELSE IF @searchCriteria='nativeCountry'
			SET @sql_Filter=@sql_Filter + ' AND nativeCountry = ''' +@searchValue+''''
		ELSE IF @searchCriteria='uniqueid'
			SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
	END

	
	SET @select_field_list ='
				 SN,customerId,email,fullName,dob,address,nativeCountry,idtype,idNumber
				,createdDate,verifiedBy,branchName,verifiedDate,bankAccountNo,bankName,AuditBy,AuditDate
				'	
	EXEC dbo.proc_paging
			@table,@sql_filter,@select_field_list,@extra_field_list
			,@sortBy,@sortOrder,@pageSize,@pageNumber

		RETURN
	END

END TRY
BEGIN CATCH
	 IF @@TRANCOUNT > 0  
     ROLLBACK TRANSACTION  
     DECLARE @errorMessage VARCHAR(MAX)  
     SET @errorMessage = ERROR_MESSAGE()  
     EXEC proc_errorHandler 1, @errorMessage, NULL  
END CATCH


