USE FastMoneyPro_Remit

GO

ALTER PROC [dbo].[proc_cancelTran] (	 
	 @flag				VARCHAR(50)
	,@controlNo			VARCHAR(20)		= NULL
	,@user				VARCHAR(30)		= NULL
	,@agentRefId		VARCHAR(50)		= NULL
	,@tranId			INT				= NULL	
	,@sCountry			INT				= NULL
	,@sFirstName		VARCHAR(30)		= NULL
	,@sMiddleName		VARCHAR(30)		= NULL
	,@sLastName1		VARCHAR(30)		= NULL
	,@sLastName2		VARCHAR(30)		= NULL
	,@sMemId			VARCHAR(30)		= NULL
	,@sId				BIGINT			= NULL	
	,@sTranId			VARCHAR(50)		= NULL	
	,@rCountry			INT				= NULL
	,@rFirstName		VARCHAR(30)		= NULL
	,@rMiddleName		VARCHAR(30)		= NULL
	,@rLastName1		VARCHAR(30)		= NULL
	,@rLastName2		VARCHAR(30)		= NULL
	,@rMemId			VARCHAR(30)		= NULL
	,@rId				BIGINT			= NULL
	,@pCountry			INT				= NULL

	,@customerId		INT				= NULL
	,@agentId			INT				= NULL
	,@senderId			INT				= NULL
	,@benId				INT				= NULL
	,@cancelReason		VARCHAR(200)	= NULL
	,@refund			CHAR(1)			= NULL
	,@sortBy            VARCHAR(50)		= NULL
	,@sortOrder         VARCHAR(5)		= NULL
	,@pageSize          INT				= NULL
	,@pageNumber        INT				= NULL
) 
AS

DECLARE 
	@tranStatus VARCHAR(20) = NULL,
	@payStatus	VARCHAR(50)  = NULL,
	@tranType	CHAR(1)		= NULL


DECLARE 
	 @select_field_list VARCHAR(MAX)
	,@extra_field_list  VARCHAR(MAX)
	,@table             VARCHAR(MAX)
	,@sql_filter        VARCHAR(MAX)
	,@sAgent			INT
	,@tAmt				BIGINT
	,@pAmt				MONEY
	,@message			VARCHAR(200)

SET NOCOUNT ON
SET XACT_ABORT ON
SELECT @pageSize = 1000, @pageNumber = 1

DECLARE
	 @code						VARCHAR(50)
	,@userName					VARCHAR(50)
	,@password					VARCHAR(50)
	,@lockStatus				VARCHAR(20)

DECLARE @controlNoEncrypted VARCHAR(20)
SELECT @controlNoEncrypted = dbo.FNAEncryptString(UPPER(LTRIM(RTRIM(@controlNo))))

IF @flag = 'cancelRequest'
BEGIN
	IF @user IS NULL
	BEGIN
		EXEC proc_errorHandler 1, 'Your session has expired. Cannot send cancel request', NULL
		RETURN
	END
	
	SELECT 
		@tranStatus = tranStatus,
		@payStatus	= payStatus,
		@tranId		= id,
		@lockStatus = lockStatus
	FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted

	IF (@tranStatus IS NULL)
	BEGIN
		EXEC proc_errorHandler 1, 'Transaction not found', @controlNoEncrypted
		RETURN
	END
	
	SELECT @agentId = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user
	IF @agentId <> (SELECT dbo.FNAGetHOAgentId()) 
	BEGIN
		IF EXISTS(SELECT 'X' FROM remitTran WITH(NOLOCK) 
		  WHERE controlNo = @controlNoEncrypted AND sBranch <> @agentId)
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is not in authorized mode', @controlNoEncrypted
			RETURN
		END
	END
	IF (@tranStatus = 'Block')
	BEGIN
		EXEC proc_errorHandler 1, 'Transaction is blocked. Please Contact HO', @controlNoEncrypted
		RETURN
	END
	IF (@tranStatus = 'Paid')
	BEGIN
		EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
		RETURN
	END
	IF (@payStatus = 'Paid')
	BEGIN
		EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
		RETURN
	END
	IF (@tranStatus = 'Cancel')
	BEGIN
		EXEC proc_errorHandler 1, 'Transaction already been cancelled', @controlNoEncrypted
		RETURN
	END

	IF (@tranStatus = 'CancelRequest')
	BEGIN
		EXEC proc_errorHandler 1, 'Cancel Request has already been sent', @controlNoEncrypted
		RETURN
	END
	BEGIN TRANSACTION
		UPDATE remitTran SET
			 tranStatus				= 'CancelRequest'					--Transaction  Hold
			,cancelRequestBy		= @user
			,cancelRequestDate		= dbo.FNAGetDateInNepalTZ()
			,cancelRequestDateLocal	= dbo.FNAGetDateInNepalTZ()
			,cancelReason			= @cancelReason
		WHERE controlNo = @controlNoEncrypted
	
	SELECT @message = 'Transaction requested for Cancel. Reason : ''' + @cancelReason + ''''
	
	INSERT INTO tranCancelrequest(tranId,controlNo,cancelReason,cancelStatus,createdBy,createdDate,tranStatus)
	SELECT @tranId,@controlNoEncrypted,@cancelReason,'CancelRequest',@user,GETDATE(),@tranStatus
	
	EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Cancel Request'
	IF @@TRANCOUNT > 0
		COMMIT TRANSACTION
		
	EXEC proc_errorHandler 0, 'Request for cancel done successfully', @controlNoEncrypted
	
	EXEC proc_addCommentAPI @flag = 'i', @controlNo = @controlNo, @user = @user, @message = @message, @agentRefId = NULL
END

ELSE IF @flag = 'cancel'	
	BEGIN
		DECLARE @TEMPTBL TABLE (ERRORCODE VARCHAR(5), MSG VARCHAR(MAX), ID VARCHAR(50))
		-- @refund ='N' TREATED AS NORMAL / FULL RETUND  --WHEN RT.Pagent IN( 221226) then (RT.cAmt-rt.serviceCharge) 
		DECLARE @sBranch INT, @pLocation INT, @pAgentComm MONEY, @cancelCharge MONEY, @returnAmt MONEY
			, @idNumber VARCHAR(25),@accountType VARCHAR(20),@txnDate DATETIME, @schemeId VARCHAR(10), @holdTranId BIGINT, @schemePremium FLOAT, @email VARCHAR(100)
			, @pAgent BIGINT
		SELECT 
			 @tranStatus		= RT.tranStatus
			,@sBranch			= RT.sBranch
			,@sAgent			= RT.sAgent
			,@pLocation			= RT.pLocation
			,@tAmt				= RT.tAmt 
			,@returnAmt			= (CASE WHEN ISNULL(@refund,'')='D' OR RT.Pagent IN( 221226,404522) then rt.tAmt  else RT.cAmt end) - ISNULL(rt.schemePremium,0)
			,@pAmt				= RT.pAmt
			,@tranId			= RT.id
			,@customerId		= S.customerId
			,@idNumber			= S.idNumber
			,@refund			= case when RT.Pagent IN( 221226,404522) then 'D' else @refund end
			,@accountType		= rt.SrouteId
			,@txnDate			= rt.ApprovedDate
			,@schemeId			= RT.schemeId
			,@holdTranId        = RT.holdTranId
			,@schemePremium		= RT.schemePremium
			,@email				= S.email
			,@pAgent			= RT.pAgent
		FROM remitTran RT WITH(NOLOCK) 
		INNER JOIN tranSenders S WITH(NOLOCK) ON S.tranId = RT.id
		WHERE controlNo = @controlNoEncrypted
		IF @user IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Your session has expired. Cannot cancel transaction', NULL
			RETURN
		END
		IF (@tranStatus IS NULL)
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction not found', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Cancel')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction already been cancelled', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Hold' AND @pAgent NOT IN(590815,590852,393228,601361,601392)) ----EXCLUDE NI BANK
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is hold. Transaction must be approved for cancellation.', NULL
			RETURN
		END
				
		SET @cancelCharge = 0

		BEGIN TRANSACTION
			UPDATE remitTran SET
				  tranStatus				= 'Cancel'
				 ,cancelApprovedBy			= @user
				 ,cancelApprovedDate		= GETDATE()
				 ,cancelApprovedDateLocal	= GETDATE()
				 ,cancelReason				= @cancelReason
				 ,refund					= @refund
			WHERE controlNo = @controlNoEncrypted
		
		IF @@TRANCOUNT > 0
		COMMIT TRANSACTION

			--SELECT @message = 'Cancel Request Approved'
			SELECT @message = @cancelReason
			EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Cancel Approved'
			
			DECLARE @SMSBody VARCHAR(90) = 'Your GME transaction '+@controlNo+' was cancelled. Balance returned to your GME wallet.'
			--IF @accountType = 'A' ----##AUTO DEBIT TRANSACTION
			--	SET @SMSBody = 'Your GME transaction '+@controlNo+' was cancelled.Balance refunded to your account.'
			DECLARE @Mobile VARCHAR(20)

			----## SMS TO IDENTIFY EVERY CUSTOMRE
			SELECT @Mobile = mobile
			FROM FastMoneyPro_Remit.dbo.customerMaster (nolock)
			WHERE customerId = @customerId
			EXEC FastMoneyPro_Remit.dbo.proc_CallToSendSMS @FLAG = 'I',@SMSBody = @SMSBody,@MobileNo = @Mobile

		DECLARE @ref_num VARCHAR(20),@tran_type CHAR(1) = 'T'
		SET @tran_type = CASE WHEN YEAR(@txnDate) = 2018 THEN 'J' ELSE @tran_type END

		SELECT TOP 1  @ref_num = t.ref_num FROM FastMoneyPro_Account.dbo.VW_PostedAccountDetail t(nolock)
		WHERE field1 = @controlNo AND t.tran_type = @tran_type AND field2='remittance voucher' AND acct_type_code IS NULL
		
		IF @ref_num IS NULL
		BEGIN
			--## generate voucher entry 
			INSERT INTO @tempTbl(errorcode, msg, id)
			EXEC FastMoneyPro_Account.dbo.proc_transactionVoucherEntry @controlNo= @controlNo

			SELECT TOP 1  @ref_num = t.ref_num FROM FastMoneyPro_Account.dbo.VW_PostedAccountDetail t(nolock)
			WHERE field1 = @controlNo AND t.tran_type = @tran_type AND field2='remittance voucher' AND acct_type_code IS NULL
		END

		IF @ref_num is NOT null
		BEGIN
			IF @accountType = 'A' AND @txnDate < '2019-11-06'
				SET @cancelReason =' old Cancellation and refund of '+@controlNo
			ELSE
				SET @cancelReason =' Cancellation and refund of '+@controlNo
			
			IF @refund = 'N'
			BEGIN
				INSERT INTO @tempTbl(errorcode, msg, id)
				EXEC FastMoneyPro_Account.dbo.proc_CancelTranVoucher @flag = 'TxnWithoutCharge',@refNum = @ref_num,@vType = @tran_type,@user = @user,@remarks = @cancelReason
			END
			ELSE
			BEGIN
				----EXEC FastMoneyPro_Account.dbo.proc_EditVoucher @flag='REVERSE',@refNum =@ref_num,@vType = @tran_type,@User = @user,@remarks = @cancelReason
				INSERT INTO @tempTbl(errorcode, msg, id)
				EXEC FastMoneyPro_Account.dbo.proc_CancelTranVoucher @flag = 'TxnWithCharge',@refNum = @ref_num,@vType = @tran_type,@user = @user,@remarks = @cancelReason
			END

			SET @message =  ' And voucher reversed successfully'

			IF @accountType = 'W' ----##AUTOWALLET TRANSACTION
			BEGIN
				IF EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @customerId AND approvedDate IS NOT NULL)
				BEGIN
					UPDATE customermaster SET availableBalance = availableBalance + isnull(@returnAmt,0) 
					WHERE customerId = @customerId
				END
			END
			ELSE IF @accountType = 'A' AND @txnDate>'2019-11-06'
			BEGIN
				IF EXISTS(SELECT 'A' FROM customerMaster(NOLOCK) WHERE customerId = @customerId AND approvedDate IS NOT NULL)
				BEGIN
					UPDATE customermaster SET availableBalance = availableBalance + isnull(@returnAmt,0) 
					WHERE customerId = @customerId
				END
			END
		END
		
		SET @message = 'Transaction Cancelled '+isnull(@message,'')
		/*Reward entry for send txn*/

		EXEC PROC_PointMasterDetails @flag='cancel',@customerId=@customerId,@tranId=@tranId


		-------START cancel coupon
				IF EXISTS(SELECT '1' FROM CouponIssue(NOLOCK) WHERE rowId = @schemeId)
				BEGIN
					DECLARE @LimitCount int, @CurrentCount int
					
					SELECT @LimitCount = CS.usageLimit,
					@CurrentCount = CI.usedCount 
					FROM CouponIssue(NOLOCK) AS CI
					INNER JOIN CouponSetup(NOLOCK) AS CS
					ON CI.couponId = CS.rowId 
					AND CI.rowId = @schemeId

					UPDATE CouponIssue SET
					usedCount = @CurrentCount - 1,					
					isActive = 'Y'					
					WHERE rowId = @schemeId

					-------Start insert Coupon History
					INSERT INTO CouponHistory (couponIssueId,customerId,actualDiscountValue,usedCount,holdTranId,couponStatus,createdBy,createdDate)
					VALUES (@schemeId,@customerId,@schemePremium,@CurrentCount - 1,@holdTranId,'9',@email, GETDATE())
					-------End   insert Coupon History
				END			
		-------END cancel coupon
		EXEC [proc_errorHandler] 0, @message, @tranId

	END
ELSE IF @flag = 'cancelReject'
BEGIN
	BEGIN TRANSACTION
		UPDATE remitTran SET
			 tranStatus				= 'Payment'					
		WHERE controlNo = @controlNoEncrypted
	
	SELECT @tranId = id	FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
	SELECT @message = 'Cancel Request for this transaction rejected'
	EXEC proc_transactionLogs 'i', @user, @tranId, @message, 'Cancel Reject'
	IF @@TRANCOUNT > 0
		COMMIT TRANSACTION
		
	EXEC proc_errorHandler 0, 'Cancel Request rejected successfully', @controlNoEncrypted
	
	EXEC proc_addCommentAPI @flag = 'i', @controlNo = @controlNo, @user = @user, @message = @message, @agentRefId = NULL	
END
ELSE IF @flag = 'detailsAgent'
BEGIN
		--SELECT @agentId = agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user

		SELECT 
			@tranStatus = tranStatus,
			@payStatus = payStatus			
		FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted

		IF (@tranStatus IS NOT NULL)
		BEGIN
			INSERT INTO tranViewHistory(
				 controlNumber
				,tranViewType
				,createdBy
				,createdDate
			)
			SELECT
				 @controlNoEncrypted
				,'C'
				,@user
				,GETDATE()
		END
		ELSE
		BEGIN
			EXEC proc_errorHandler 1000, 'No Transaction Found', @controlNoEncrypted
			RETURN
		END
		
		--IF NOT EXISTS(SELECT 'X' FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted AND pBranch = @agentId)
		--BEGIN
		--	EXEC proc_errorHandler 1, 'Transaction is not in authorized mode', @controlNoEncrypted
		--	RETURN
		--END
		IF (@tranStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@payStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'CancelRequest')
		BEGIN
			EXEC proc_errorHandler 1, 'Cancel Request has already been sent', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Cancel')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction already been cancelled', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Lock')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is locked. Please contact HO', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Block')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is blocked. Please contact HO', @controlNoEncrypted
			RETURN
		END
		--IF (@payStatus = 'Post')
		--BEGIN
		--	EXEC proc_errorHandler 1, 'Transaction is Post. Please contact Head Office.', @controlNoEncrypted
		--	RETURN
		--END
		EXEC proc_errorHandler 0, 'Transaction Found', @controlNoEncrypted
		SELECT 
			 trn.id
			,controlNo = dbo.FNADecryptString(trn.controlNo)
			,sMemId = sen.membershipId
			,sCustomerId = sen.customerId
			,senderName = sen.firstName + ISNULL( ' ' + sen.middleName, '') + ISNULL( ' ' + sen.lastName1, '') + ISNULL( ' ' + sen.lastName2, '')
			,sCountryName = sen.country
			,sStateName = sen.state
			,sDistrict = sen.district
			,sCity = sen.city
			,sAddress = sen.address
			,sContactNo = COALESCE(sen.mobile, sen.homephone, sen.workphone)
			
			,sIdType = sen.idType
			,sIdNo = sen.idNumber
			,sValidDate = sen.validDate
			,sEmail = sen.email
			
			,rMemId = rec.membershipId
			,rCustomerId = rec.customerId
			,receiverName = rec.firstName + ISNULL( ' ' + rec.middleName, '') + ISNULL( ' ' + rec.lastName1, '') + ISNULL( ' ' + rec.lastName2, '')
			,rCountryName = rec.country
			,rStateName = rec.state
			,rDistrict = rec.district
			,rCity = rec.city
			,rAddress = rec.address
			,rContactNo = COALESCE(rec.mobile, rec.homephone, rec.workphone)
			,rIdType = rec.idType
			,rIdNo = rec.idNumber
			
			,sBranchName = trn.sBranchName
			,sAgentName = CASE WHEN trn.sAgent = trn.sBranch THEN trn.sSuperAgentName ELSE trn.sAgentName END
			,sAgentLocation = sLoc.districtName
			,sAgentDistrict = sa.agentDistrict
			,sAgentCity = sa.agentCity
			,sAgentCountry = sa.agentCountry
			
			,pAgentName = CASE WHEN trn.pAgentName = trn.pBranchName THEN trn.pSuperAgentName ELSE trn.pAgentName END
			,pBranchName = trn.pBranchName
			,pAgentCountry = trn.pCountry
			,pAgentState = trn.pState
			,pAgentDistrict = trn.pDistrict
			,pAgentLocation = pLoc.districtName
			,pAgentCity = pa.agentCity
			,pAgentAddress = pa.agentAddress
			
			,trn.tAmt
			,trn.serviceCharge
			,handlingFee = ISNULL(trn.handlingFee, 0)
			,trn.cAmt
			,trn.pAmt
			
			,relationship = ISNULL(trn.relWithSender, '-')
			,purpose = ISNULL(trn.purposeOfRemit, '-')
			,sourceOfFund = ISNULL(trn.sourceOfFund, '-')
			,collMode = trn.collMode
			,trn.collCurr
			,paymentMethod = trn.paymentMethod
			,trn.payoutCurr
			,trn.tranStatus
			,trn.payStatus
			,payoutMsg = ISNULL(trn.pMessage, '-')
			,trn.createdBy
			,trn.createdDate
			,trn.approvedBy
			,trn.approvedDate
		FROM remitTran trn WITH(NOLOCK)
		LEFT JOIN tranSenders sen WITH(NOLOCK) ON trn.id = sen.tranId
		LEFT JOIN tranReceivers rec WITH(NOLOCK) ON trn.id = rec.tranId
		LEFT JOIN agentMaster sa WITH(NOLOCK) ON trn.sBranch = sa.agentId
		LEFT JOIN agentMaster pa WITH(NOLOCK) ON trn.pBranch = pa.agentId
		LEFT JOIN api_districtList sLoc WITH(NOLOCK) ON sa.agentLocation = sLoc.districtCode
		LEFT JOIN api_districtList pLoc WITH(NOLOCK) ON trn.pLocation = pLoc.districtCode
		WHERE trn.controlNo = @controlNoEncrypted
		
	END

ELSE IF @flag = 'cancelReceipt'
BEGIN
	DECLARE @AccName NVARCHAR(100),@AccNo VARCHAR(30),@BankName NVARCHAR(100),@bankCode VARCHAR(5),@srouteId CHAR(1)

	SELECT @controlNoEncrypted = DBO.fnaDecryptstring(Controlno),@tranStatus= tranStatus,@srouteId = sRouteId FROM remitTran(NOLOCK) WHERE ID = @tranId

	IF @tranStatus <>'CANCEL'
	BEGIN
		SELECT '1' AS ERRORCODE,'Transaction status changed to '+@tranStatus MSG,@controlNoEncrypted  as controlNo
		RETURN
	END

	SELECT
		 controlNo = dbo.FNADecryptString(controlNo)
		,'0' AS ERRORCODE
		,postedBy = trn.sBranchName
		,createdDate
		,cancelDate = cancelApprovedDate
		,sender = sen.firstName + ISNULL(' ' + sen.middleName, '') + ISNULL(' ' + sen.lastName1, '') + ISNULL(' ' + sen.lastName2, '')
		,receiver = rec.firstName + ISNULL(' ' + rec.middleName, '') + ISNULL(' ' + rec.lastName1, '') + ISNULL(' ' + rec.lastName2, '')
		,rContactNo = rec.mobile
		,trn.collCurr
		,trn.cAmt
		,trn.serviceCharge
		,trn.pAmt
		,trn.cancelCharge
		,returnAmt = CASE WHEN refund = 'D' THEN tAmt ELSE cAmt END
	FROM remitTran trn WITH(NOLOCK)
	INNER JOIN tranSenders sen WITH(NOLOCK) ON trn.id = sen.tranId
	INNER JOIN tranReceivers rec WITH(NOLOCK) ON trn.id = rec.tranId
	WHERE trn.id = @tranId
	
END
ELSE IF @flag = 's'
BEGIN
	
	SET @table = '(
				SELECT 
					 trn.id
					,controlNo = dbo.FNADecryptString(trn.controlNo)
					,sCustomerId = sen.customerId
					,senderName = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')
					,sCountryName = sen.country
					,sStateName = sen.state
					,sCity = sen.city
					,sAddress = sen.address
					,rCustomerId = rec.customerId
					,receiverName = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
					,rCountryName = rec.country
					,rStateName = rec.state
					,rCity = rec.city
					,rAddress = rec.address
				FROM remitTran trn WITH(NOLOCK)
				LEFT JOIN tranSenders sen WITH(NOLOCK) ON trn.id = sen.tranId
				LEFT JOIN tranReceivers rec WITH(NOLOCK) ON trn.id = rec.tranId
				WHERE trn.tranStatus = ''CancelRequest'' and TranType =''D''
	'
	SET @sql_filter = ''
	
	IF @controlNo IS NOT NULL
		SET @table = @table + ' AND trn.controlNo = ''' + @controlNoEncrypted + '''' 
		
	IF @sFirstName IS NOT NULL
		SET @table = @table + ' AND sen.firstName LIKE ''' + @sFirstName + '%'''
		
	IF @sMiddleName IS NOT NULL
		SET @table = @table + ' AND sen.middleName LIKE ''' + @sMiddleName + '%'''
		
	IF @sLastName1 IS NOT NULL
		SET @table = @table + ' AND sen.lastName1 LIKE ''' + @sLastName1 + '%'''
		
	IF @sLastName2 IS NOT NULL
		SET @table = @table + ' AND sen.lastName2 LIKE ''' + @sLastName2 + '%'''
		
	IF @sMemId IS NOT NULL
		SET @table = @table + ' AND sen.membershipId = ' + CAST(@sMemId AS VARCHAR)
			
	IF @rFirstName IS NOT NULL
		SET @table = @table + ' AND rec.firstName LIKE ''' + @rFirstName + '%'''
		
	IF @rMiddleName IS NOT NULL
		SET @table = @table + ' AND rec.middleName LIKE ''' + @rMiddleName + '%'''
		
	IF @rLastName1 IS NOT NULL
		SET @table = @table + ' AND rec.lastName1 LIKE ''' + @rLastName1 + '%'''
		
	IF @rLastName2 IS NOT NULL
		SET @table = @table + ' AND rec.lastName2 LIKE ''' + @rLastName2 + '%'''		
		
	IF @rMemId IS NOT NULL
		SET @table = @table + ' AND c.membershipId = ' + CAST(@rMemId AS VARCHAR)
		
	SET @select_field_list ='
				 id
				,controlNo
				,sCustomerId
				,senderName
				,sCountryName
				,sStateName
				,sCity
				,sAddress
				,rCustomerId
				,receiverName
				,rCountryName
				,rStateName
				,rCity
				,rAddress				
			   '
	SET @table = @table + ') x'
			
	EXEC dbo.proc_paging
            @table
           ,@sql_filter
           ,@select_field_list
           ,@extra_field_list
           ,@sortBy
           ,@sortOrder
           ,@pageSize
           ,@pageNumber
END

ELSE IF @flag = 'checkCancleTxn'
BEGIN
	SELECT 
		@tranStatus = tranStatus,
		@tranType	= tranType,
		@tranId		= id
	FROM remitTran WITH(NOLOCK) WHERE controlNo = @controlNoEncrypted
	
	IF (@tranStatus IS NULL)
	BEGIN
		EXEC proc_errorHandler 1, 'Invalid transaction', @controlNo
		RETURN
	END
	IF (@tranStatus = 'Cancel')
	BEGIN
		  SELECT errorCode = 0, msg = 'Success', id = @controlNo, extra = @tranType, extra2 = @tranId
		  RETURN
	END
	ELSE 
	BEGIN
		EXEC proc_errorHandler 1, 'Invalid transaction', @controlNo
	END
END