USE FastMoneyPro_Remit
GO

ALTER  PROC [dbo].[proc_ApproveHoldedTXN] (	 
	 @flag				VARCHAR(50)	
	,@user				VARCHAR(130)
	,@userType			VARCHAR(10)		= NULL
	,@branch			VARCHAR(50)		= NULL	
	,@id				VARCHAR(40)		= NULL
	,@country			VARCHAR(50)		= NULL
	,@sender			VARCHAR(50)		= NULL
	,@receiver			VARCHAR(50)		= NULL
	,@amt				MONEY			= NULL
	,@bank				VARCHAR(50)		= NULL
	,@voucherNo			VARCHAR(50)		= NULL	
	,@branchId			INT				= NULL
	,@pin				VARCHAR(50)		= NULL
	,@errorCode			VARCHAR(10)		= NULL	
	,@msg				VARCHAR(500)	= NULL
	,@idList			XML				= NULL
	,@txnDate			VARCHAR(20)		= NULL
	,@txncreatedBy		VARCHAR(50)		= NULL
	,@xml				VARCHAR(MAX)	= NULL
	,@remarks			VARCHAR(MAX)	= NULL
	,@settlingAgentId	INT				= NULL
    ,@ControlNo			VARCHAR(50)		= NULL
	,@txnType			VARCHAR(1)		= NULL
	,@sendCountry		VARCHAR(50)		= NULL
	,@sendAgent			VARCHAR(50)		= NULL
	,@sendBranch		VARCHAR(50)		= NULL

) 

AS
BEGIN TRY

	DECLARE 
		 @table             VARCHAR(MAX)
		,@sql		        VARCHAR(MAX)
		,@sqlSelfTxn		VARCHAR(MAX)
		,@sRouteId VARCHAR(5)

	SET NOCOUNT ON;
	SET XACT_ABORT ON;

	DECLARE 
		 @pinEncrypted VARCHAR(50) = dbo.FNAEncryptString(@pin)
		,@cAmt MONEY
		,@userId INT		
		,@createdBy	VARCHAR(50)
		,@tranStatus VARCHAR(50)
		,@message	VARCHAR(200)
		,@sBranch	BIGINT			
		,@invicePrintMethod VARCHAR(50)
		,@parentId	BIGINT
		,@tablesql AS VARCHAR(MAX)
		,@branchList VARCHAR(MAX) 
		,@denyAmt	MONEY

	IF @pin IS NULL
	BEGIN
		SELECT @pin = dbo.FNADecryptString(controlNo), @pinEncrypted = controlNo FROM remitTranTemp WITH(NOLOCK) WHERE id = @id		
	END
	ELSE
	BEGIN
		SET @pinEncrypted  = dbo.FNAEncryptString(@pin)
	END
	
	DECLARE @PinList TABLE(id VARCHAR(50), pin VARCHAR(50),hasProcess CHAR(1),isOFAC CHAR(1),errorMsg VARCHAR(MAX),tranId INT,createdBy	VARCHAR(50))
	DECLARE @TempcompTable TABLE(errorCode INT,msg VARCHAR(MAX),id VARCHAR(50))
	DECLARE @isSelfApprove VARCHAR(1)
	
	IF @flag = 'provider'
	BEGIN
		DECLARE @pAgent INT
		SELECT
			@pAgent = pAgent
		FROM remitTranTemp WITH(NOLOCK)
		WHERE id = @id
		
		IF @pAgent IS NULL
		BEGIN
			SELECT
				@pAgent = pAgent
			FROM vwremitTran WITH(NOLOCK)
			WHERE id = @id
		END

		SELECT @pAgent pAgent
		RETURN
	END

	IF @flag = 'reject'
	BEGIN
		
		SELECT @tranStatus = transtatus,
			   @denyAmt = ISNULL(cAmt,0) - ISNULL(sAgentComm,0)	- ISNULL(agentFxGain,0),
			   @sRouteId = sRouteId,
			   @pinEncrypted = controlNo,
			   @ControlNo = dbo.fnaDecryptstring(controlNo),
			   @txncreatedBy = createdBy
		FROM remitTranTemp WITH(NOLOCK) WHERE id=@id
		
		IF LEN(@pinEncrypted)<7
		BEGIN
			select TOP 1 @ControlNo = controlNo from Application_Log.dbo.vwtpapilogs(nolock) where requestedby=@txncreatedBy AND ERRORCODE='999' order by rowid desc
		END
		IF LEN(@ControlNo) < 7
		BEGIN
			EXEC proc_errorHandler 1, 'Sorry,Cannot Reject Transaction', @id
			RETURN
		END

		BEGIN TRANSACTION
		
		INSERT INTO tranCancelrequest(tranId,controlNo,cancelReason,cancelStatus,
				createdBy,createdDate,tranStatus,approvedBy,approvedDate,scRefund,isScRefund)
		SELECT id,controlNo,@remarks,'Approved',@user,GETDATE(),tranStatus,@user,GETDATE(),
		cAmt,'Y' FROM remitTranTemp WITH(NOLOCK) WHERE id=@id
		
		UPDATE remitTranTemp SET
				  tranStatus				= 'Cancel'
				 ,cancelApprovedBy			= @user
				 ,cancelApprovedDate		= GETDATE()
				 ,cancelApprovedDateLocal	= dbo.FNAGetDateInNepalTZ()
				 ,cancelRequestBy			= @user
				 ,cancelRequestDate			= GETDATE()
				 ,cancelRequestDateLocal	= dbo.FNADateFormatTZ(GETDATE(), @user)
				 ,trnStatusBeforeCnlReq		= @tranStatus
				 ,controlNo					= dbo.fnaEncryptstring(@ControlNo)
		WHERE id = @id

		--IF @sRouteId = 'w'
		--BEGIN
			EXEC proc_UpdateCustomerBalance @controlNo=@pinEncrypted
		--END
		
		INSERT INTO cancelTranHistory(
				tranId,controlNo,sCurrCostRate,sCurrHoMargin,sCurrSuperAgentMargin,sCurrAgentMargin,pCurrCostRate
				,pCurrHoMargin,pCurrSuperAgentMargin,pCurrAgentMargin,agentCrossSettRate,customerRate,sAgentSettRate,pDateCostRate,agentFxGain
				,treasuryTolerance,customerPremium,schemePremium,sharingValue,sharingType,serviceCharge,handlingFee,sAgentComm,sAgentCommCurrency
				,sSuperAgentComm,sSuperAgentCommCurrency,pAgentComm,pAgentCommCurrency,pSuperAgentComm,pSuperAgentCommCurrency,promotionCode
				,promotionType,pMessage,sCountry,sSuperAgent,sSuperAgentName,sAgent,sAgentName,sBranch,sBranchName,pCountry,pSuperAgent,pSuperAgentName
				,pAgent,pAgentName,pBranch,pBranchName,paymentMethod,pBank,pBankName,pBankBranch,pBankBranchName,accountNo,externalBankCode,collMode
				,collCurr,tAmt,cAmt,pAmt,payoutCurr,relWithSender,purposeOfRemit,sourceOfFund,tranStatus,payStatus,createdDate,createdDateLocal
				,createdBy,modifiedDate,modifiedDateLocal,modifiedBy,approvedDate,approvedDateLocal,approvedBy,paidDate,paidDateLocal,paidBy
				,cancelRequestDate,cancelRequestDateLocal,cancelRequestBy,cancelReason,refund,cancelCharge,cancelApprovedDate,cancelApprovedDateLocal
				,cancelApprovedBy,blockedDate,blockedBy,lockedDate,lockedDateLocal,lockedBy,payTokenId,tranType,ContNo
				,uploadLogId,voucherNo,controlNo2,pBankType,trnStatusBeforeCnlReq,sRouteId
			)
			SELECT 
				id,controlNo,sCurrCostRate,sCurrHoMargin,sCurrSuperAgentMargin,sCurrAgentMargin,pCurrCostRate
				,pCurrHoMargin,pCurrSuperAgentMargin,pCurrAgentMargin,agentCrossSettRate,customerRate,sAgentSettRate,pDateCostRate,agentFxGain
				,treasuryTolerance,customerPremium,schemePremium,sharingValue,sharingType,serviceCharge,handlingFee,sAgentComm,sAgentCommCurrency
				,sSuperAgentComm,sSuperAgentCommCurrency,pAgentComm,pAgentCommCurrency,pSuperAgentComm,pSuperAgentCommCurrency,promotionCode
				,promotionType,pMessage,sCountry,sSuperAgent,sSuperAgentName,sAgent,sAgentName,sBranch,sBranchName,pCountry,pSuperAgent,pSuperAgentName
				,pAgent,pAgentName,pBranch,pBranchName,paymentMethod,pBank,pBankName,pBankBranch,pBankBranchName,accountNo,externalBankCode,collMode
				,collCurr,tAmt,cAmt,pAmt,payoutCurr,relWithSender,purposeOfRemit,sourceOfFund,tranStatus,payStatus,createdDate,createdDateLocal
				,createdBy,modifiedDate,modifiedDateLocal,modifiedBy,approvedDate,approvedDateLocal,approvedBy,paidDate,paidDateLocal,paidBy
				,cancelRequestDate,cancelRequestDateLocal,cancelRequestBy,@remarks,refund,cancelCharge,GETDATE(),dbo.FNADateFormatTZ(GETDATE(), @user)
				,@user,blockedDate,blockedBy,lockedDate,lockedDateLocal,lockedBy,payTokenId,tranType,ContNo
				,uploadLogId,voucherNo,controlNo2,pBankType,trnStatusBeforeCnlReq,sRouteId
			 FROM remitTranTemp WHERE id =  @id
			
			INSERT INTO cancelTranSendersHistory 
			(tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,STATE,district,
			zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,occupation,idType,
			idNumber,idPlaceOfIssue,issuedDate,validDate,extCustomerId,cwPwd,ttName,isFirstTran,customerRiskPoint,countryRiskPoint,
			gender,salary,companyName,address2,dcInfo,ipAddress,notifySms,txnTestQuestion,txnTestAnswer)
			SELECT tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,STATE,district,
			zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,occupation,idType,
			idNumber,idPlaceOfIssue,issuedDate,validDate,extCustomerId,cwPwd,ttName,isFirstTran,customerRiskPoint,countryRiskPoint,
			gender,salary,companyName,address2,dcInfo,ipAddress,notifySms,txnTestQuestion,txnTestAnswer 
			FROM transenderstemp WITH(NOLOCK) WHERE tranId = @id
			
			INSERT INTO cancelTranReceiversHistory 
			(tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,
			state,district,zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,
			occupation,idType,idNumber,idPlaceOfIssue,issuedDate,validDate,idType2,idNumber2,idPlaceOfIssue2,issuedDate2,
			validDate2,relationType,relativeName,gender,address2,dcInfo,ipAddress)
			SELECT tranId,customerId,membershipId,firstName,middleName,lastName1,lastName2,fullName,country,address,
			state,district,zipCode,city,email,homePhone,workPhone,mobile,nativeCountry,dob,placeOfIssue,customerType,
			occupation,idType,idNumber,idPlaceOfIssue,issuedDate,validDate,idType2,idNumber2,idPlaceOfIssue2,issuedDate2,
			validDate2,relationType,relativeName,gender,address2,dcInfo,ipAddress
			FROM tranReceiversTemp WITH(NOLOCK)	WHERE tranId = @id

			DELETE FROM remitTranTemp WHERE id = @id
			DELETE FROM tranSendersTemp WHERE tranId = @id
			DELETE FROM tranReceiversTemp WHERE tranId = @id
			SELECT @message = 'Transaction cancel has been done successfully.'
			
			EXEC proc_transactionLogs @flag='i', @user=@user, @tranId=@id, @message=@message,@msgType='Cancel'	
			
		IF @@TRANCOUNT > 0
		COMMIT TRANSACTION
		EXEC proc_errorHandler 0, 'Transaction Rejected Successfully', @id
		RETURN
	END
	
	IF @flag = 'approve'
	BEGIN

		DECLARE @pBank INT,@paymentMethod VARCHAR(50),@tranType CHAR(1),@Mobile VARCHAR(15),@controlNo2 VARCHAR(20)

		IF @createdBy = @user
		BEGIN
			EXEC proc_errorHandler 1, 'Same user cannot approve the Transaction', @id
			RETURN
		END		
		IF EXISTS (select 'X' FROM remitTranTemp(nolock) where id = @id AND isOnlineTxn = 'Y')
		BEGIN
			SELECT
				 @cAmt		= cAmt
				,@userId	= cm.customerId 
				,@createdBy = r.createdBy
				,@controlNo = dbo.FNADecryptString(controlNo)
				,@controlNo2= controlNo2
				,@sBranch	= sBranch
				,@pBank		= pBank
				,@paymentMethod = paymentMethod
				,@tranType	= tranType
				,@sRouteId	= sRouteId
				,@Mobile	= CM.mobile
			FROM remitTranTemp r WITH(NOLOCK)
			INNER JOIN customerMaster cm WITH(NOLOCK) ON r.createdBy = cm.email
			WHERE r.id = @id
		END 
		ELSE
		BEGIN
			SELECT
				 @cAmt = cAmt
				,@userId = au.userId 
				,@createdBy = r.createdBy
				,@controlNo = dbo.FNADecryptString(controlNo)
				,@controlNo2= controlNo2
				,@sBranch	= sBranch
			FROM remitTranTemp r WITH(NOLOCK)
			INNER JOIN applicationUsers au WITH(NOLOCK) ON r.createdBy = au.userName 
			WHERE r.id = @id
		END	

		IF @sRouteId = 'w'
		BEGIN
			SELECT @denyAmt = availableBalance FROM customerMaster(NOLOCK) WHERE email = @createdBy
			IF ISNULL(@denyAmt,0) < 0
			BEGIN
				INSERT INTO tranCancelrequest(tranId,controlNo,cancelReason,cancelStatus,
					createdBy,createdDate,tranStatus,approvedBy,approvedDate,scRefund,isScRefund)
				SELECT id,controlNo,'Auto rejected due to invalid amount','Approved',@user,GETDATE(),tranStatus,@user,GETDATE(),cAmt,'Y' 
				FROM remitTranTemp WITH(NOLOCK) WHERE id=@id

				UPDATE remitTranTemp SET tranStatus='Hold' WHERE id = @id

				EXEC proc_errorHandler 1, 'Auto rejected due to invalid amount', @id
				return
			END
		END

		--SELECT @parentId = parentId FROM agentMaster WITH(NOLOCK) WHERE agentId=@sBranch
		--SELECT @invicePrintMethod = invoicePrintMethod FROM agentMaster A WITH(NOLOCK) 
		--INNER JOIN agentBusinessFunction B WITH(NOLOCK) ON A.agentId=B.agentId
		--WHERE A.agentId=@parentId
		
		BEGIN TRANSACTION

		UPDATE remitTranTemp SET
				tranStatus				=  CASE tranStatus
												WHEN 'Hold' THEN 'Payment'
												WHEN 'Compliance Hold' THEN 'Compliance'
												WHEN 'OFAC Hold' THEN 'OFAC'
												WHEN 'OFAC/Compliance Hold' THEN 'OFAC/Compliance'
												ELSE 'Payment'
											END
				,approvedBy				= @user
				,approvedDate			= GETDATE()	
				,approvedDateLocal		= GETUTCDATE()		     
		WHERE id = @id
		
		
		IF @sRouteId = 'w'
		BEGIN
			SELECT @denyAmt = availableBalance FROM customerMaster(NOLOCK) WHERE email = @createdBy
			IF ISNULL(@denyAmt,0)<0
				UPDATE remitTranTemp SET tranStatus = 'HOLD' WHERE id = @id
		END

		--send email to customers
		INSERT INTO tempTransactionMailQueue(controlNo, createdDate, [status])--tranType is used to know either mail is sent or not, we put 'N' for not sent
		SELECT rt.controlNo, GETDATE(), 'N' FROM remitTranTemp rt (NOLOCK) where rt.id = @id
		----## pish temp into main table
		EXEC proc_remitTranTempToMain @id

		INSERT INTO PinQueueList(ICN)
		SELECT @pinEncrypted
		
		IF ISNULL(@controlNo2,'a') <> 'a' 
		BEGIN
			IF NOT EXISTS(SELECT 'A' FROM PinQueueList(NOLOCK) WHERE icn = @controlNo2)
			BEGIN
				INSERT INTO PinQueueList(ICN)
				SELECT @controlNo2
			END
		END
		
		--UPDATE userWiseTxnLimit SET
		--		sendTodays = ISNULL(sendTodays, 0) + @cAmt
		--WHERE userId = @userId AND ISNULL(isActive, 'N') = 'Y'				
		
		IF @@TRANCOUNT > 0
		COMMIT TRANSACTION

		EXEC proc_errorHandler 0, 'Transaction Approved Successfully', @controlNo	

		DECLARE @SMSBody VARCHAR(90) = 'Your transaction GME No: '+@controlNo+' is successfully sent. Thank you for using GME.'
		----## SMS TO IDENTIFY EVERY CUSTOMRE
		----IF NOT EXISTS(SELECT TOP 1 'A' FROM KT_SMS.DBO.SDK_SMS_REPORT(nolock) WHERE SMS_MSG LIKE '%'+@controlNo+'%')
		IF @Mobile IS NOT NULL
			EXEC FastMoneyPro_Remit.dbo.proc_CallToSendSMS @FLAG = 'I',@SMSBody = @SMSBody,@MobileNo = @Mobile

		--IF @invicePrintMethod = 'aa'
		--	EXEC proc_errorHandler 11, 'Transaction Approved Successfully', @controlNo
		--ELSE IF (@pBank = 1056 AND @paymentMethod = 'BANK DEPOSIT' OR @paymentMethod ='CASH PAYMENT') 
		--	EXEC proc_errorHandler 5, 'Transaction Approved Successfully', @controlNo		
		--ELSE
		--	EXEC proc_errorHandler 0, 'Transaction Approved Successfully', @controlNo	
			
		--## generate voucher entry 
		--EXEC FastMoneyPro_Account.dbo.proc_transactionVoucherEntry @controlNo= @controlNo	
		RETURN
	END

	IF @flag = 'approve-all'
	BEGIN
		EXEC proc_ApproveHoldedTXN_Sub @user = @user, @idList = @idList
		RETURN
	END

	DECLARE @cdTable VARCHAR(MAX) = ''
	
	SET @cdTable = '
	LEFT JOIN (
					SELECT
						DISTINCT
						 tranId
						,cb.bankName
						,cd.countryBankId
					FROM collectionDetails cd WITH(NOLOCK)
					LEFT JOIN countryBanks cb WITH(NOLOCK) ON cd.countryBankId = cb.countryBankId
					INNER JOIN remitTranTemp trn WITH(NOLOCK) ON cd.tranId = trn.id
						AND (trn.tranStatus = ''Hold'' OR trn.tranStatus = ''Compliance Hold'' OR trn.tranStatus = ''OFAC Hold'' ) 
						AND trn.payStatus = ''Unpaid'' 
						AND trn.approvedBy IS NULL							
				) cd ON cd.tranId = trn.id '
	
	IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[#collModeList]') AND type = 'D')
	BEGIN
		DROP TABLE #collModeList
	END

	CREATE TABLE #collModeList(tranId INT, hasProcess CHAR(1), proMode varchar(50))
	DECLARE @tranId1 INT,@collMode VARCHAR(100)
		
IF @flag = 's-admin'
	BEGIN
		SET @table = '
						SELECT DISTINCT
							 trn.id
							 ,controlNo = dbo.fnadecryptstring(controlNo)
							,branch = am.agentName							
							,country = trn.pCountry
							,senderId = sen.customerId
							,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')						
							,receiverId = rec.customerId
							,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
							,amt = CAST(trn.cAmt AS DECIMAL(18, 2))
							,trn.paymentMethod 
							,voucherNo = trn.voucherNo
							,txnDate = CAST(trn.createdDate AS DATE)
							,txncreatedBy = trn.createdBy
						FROM remitTranTemp trn WITH(NOLOCK) ' + @cdTable + '
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId														
						WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL  AND 
							  ISNULL(trn.isOnlineTxn,''N'') <>''Y'' AND trn.tranType<>''O''
					'
			
			IF @id IS NOT NULL			
				SET @table = @table + ' AND trn.id = ''' + @id + '''' 
				
			IF @branch IS NOT NULL
				SET @table = @table + ' AND am.agentId = ''' + @branch + '''' 
			
			IF @country IS NOT NULL
				SET @table = @table + ' AND trn.pCountry LIKE ''' + @country + '%''' 
			
			IF @sendCountry IS NOT NULL
				SET @table = @table + ' AND trn.sCountry LIKE ''' + @sendCountry + '%''' 

			IF @amt IS NOT NULL
				SET @table = @table + ' AND trn.pAmt = ' + CAST(@amt AS VARCHAR(20))+ '' 	
			
			IF @voucherNo IS NOT NULL
				SET @table = @table + ' AND trn.voucherNo = ''' + @voucherNo + ''''
				
			IF @txncreatedBy IS NOT NULL
				SET @table = @table + '  AND trn.createdBy = '''+@txncreatedBy+''''
				
			IF @txnDate IS NOT NULL
				SET @table = @table + ' AND CAST(trn.createdDate AS DATE)='''+@txnDate +''''
			
			IF @ControlNo IS NOT NULL
				SET @table = @table + ' AND trn.controlNo = dbo.fnaEncryptString('''+@ControlNo+''')'
						
			IF @sendAgent IS NOT NULL
				SET @table = @table + ' AND trn.sAgent  = '''+@sendAgent+''''
				
			IF @sendBranch IS NOT NULL
				SET @table = @table + ' AND trn.sBranch = '''+@sendBranch+''''
			
			
			SET @sql = '
				SELECT 
					* 
				FROM (
					' + @table + '
				) x
				WHERE 1 = 1 '
				
			IF @sender IS NOT NULL
				SET @sql = @sql + ' AND sender LIKE ''' + @sender + '%'''
			IF @receiver IS NOT NULL
				SET @sql = @sql + ' AND receiver LIKE ''' + @receiver + '%'''					

			PRINT @sql
			EXEC (@sql)		
		RETURN	
	END

	IF @flag = 's-admin-online'
	BEGIN
		SET @table = '
						SELECT DISTINCT
							 trn.id
							,branch = am.agentName							
							,country = trn.pCountry
							,senderId = sen.customerId
							,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')						
							,receiverId = rec.customerId
							,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
							,amt = CAST(trn.cAmt AS DECIMAL(18, 2))
							
							,txnDate = CAST(trn.createdDate AS DATE)
							,txncreatedBy = trn.createdBy
							,CASE WHEN trn.pAgent=1100 OR trn.pAgent = 1043 THEN 1 ELSE 0 END isThirdPartyTran
						FROM remitTranTemp trn WITH(NOLOCK) ' + @cdTable + '
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId														
						WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL  AND ISNULL(trn.isOnlineTxn,''N'') =''Y'' 
					'
			
			IF @id IS NOT NULL			
				SET @table = @table + ' AND trn.id = ''' + @id + '''' 
				
			IF @branch IS NOT NULL
				SET @table = @table + ' AND am.agentId = ''' + @branch + '''' 
			
			IF @country IS NOT NULL
				SET @table = @table + ' AND trn.pCountry LIKE ''' + @country + '%''' 
			
			IF @sendCountry IS NOT NULL
				SET @table = @table + ' AND trn.sCountry LIKE ''' + @sendCountry + '%''' 

			IF @amt IS NOT NULL
				SET @table = @table + ' AND trn.pAmt = ' + CAST(@amt AS VARCHAR(20))+ '' 	
			
			IF @voucherNo IS NOT NULL
				SET @table = @table + ' AND trn.voucherNo = ''' + @voucherNo + ''''
				
			IF @txncreatedBy IS NOT NULL
				SET @table = @table + '  AND trn.createdBy = '''+@txncreatedBy+''''
				
			IF @txnDate IS NOT NULL
				SET @table = @table + ' AND CAST(trn.createdDate AS DATE)='''+@txnDate +''''
			
			IF @ControlNo IS NOT NULL
				SET @table = @table + ' AND trn.controlNo = dbo.fnaEncryptString('''+@ControlNo+''')'
						
			IF @sendAgent IS NOT NULL
				SET @table = @table + ' AND trn.sAgent  = '''+@sendAgent+''''
				
			IF @sendBranch IS NOT NULL
				SET @table = @table + ' AND trn.sBranch = '''+@sendBranch+''''
			
			SET @sql = '
				SELECT 
					* , STUFF((SELECT '''' + US.voucherNo +'' - ''+ CONVERT(VARCHAR(11),US.voucherDate,6) +'' - ''+ CAST(US.voucherAmt AS VARCHAR)+'' || ''
          FROM bankCollectionVoucherDetail US
          WHERE US.tempTranId = x.id
          FOR XML PATH('''')), 1, 1, '''') [voucherDetail]
				FROM (
					' + @table + '
				) x
				WHERE 1 = 1 '
				
			IF @sender IS NOT NULL
				SET @sql = @sql + ' AND sender LIKE ''' + @sender + '%'''
			IF @receiver IS NOT NULL
				SET @sql = @sql + ' AND receiver LIKE ''' + @receiver + '%'''					

			PRINT @sql
			EXEC (@sql)		
		RETURN	
	END
	
	IF @flag = 's-agent'
	BEGIN		
		SELECT 
			@isSelfApprove = ISNULL(b.isSelfTxnApprove,'N') 
		FROM agentmaster a WITH(NOLOCK)
		LEFT JOIN agentBusinessFunction b WITH(NOLOCK) ON a.parentId=b.agentId
		WHERE a.agentId= @branchId

		SET @branchList = '
			INNER JOIN ( 
				SELECT ' + CAST(@branchId AS VARCHAR) + ' agentId '
				
		IF @userType = 'RH'
		BEGIN
			SET @branchList = @branchList + '
							UNION ALL 	
							SELECT
								 am.agentId 			
							FROM agentMaster am WITH(NOLOCK)
							INNER JOIN regionalBranchAccessSetup rba ON am.agentId = rba.memberAgentId
							WHERE rba.agentId = ' + CAST(@branchId AS VARCHAR) + '
							AND ISNULL(rba.isDeleted, ''N'') = ''N''
							AND ISNULL(rba.isActive, ''N'') = ''Y''
							AND memberAgentId <> ' + CAST(@branchId AS VARCHAR) + ''
		END
		IF @userType = 'AH'
		BEGIN
			SET @branchList = @branchList + '
							UNION ALL 	
							select agentId from agentMaster with(nolock) where parentId = 
							(select parentId from agentmaster with(nolock) where agentId=' + CAST(@branchId AS VARCHAR) + ')
							and agentId <> ' + CAST(@branchId AS VARCHAR) + ''
		END

		SET @branchList = @branchList + '			
			) bl ON trn.sBranch = bl.agentId			
			'

		SET @table = '
						SELECT DISTINCT
							 trn.id
							,branch = am.agentName							
							,country = trn.pCountry
							,senderId = sen.customerId
							,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')						
							,receiverId = rec.customerId
							,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
							,amt = CAST(trn.cAmt AS DECIMAL(18, 3))
							,voucherNo = trn.voucherNo
							,trn.createdBy
							,collMode = ''Cash''
							,txnDate = CAST(trn.createdDate AS DATE)
							,txncreatedBy = trn.createdBy		
							,CASE WHEN trn.pAgent = 1100 OR trn.pAgent = 1043 THEN 1 ELSE 0 END isThirdPartyTran					
						FROM remitTranTemp trn WITH(NOLOCK) 
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						' + @branchList + '	
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
						WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL 
					'
			
			IF @txnType IS NOT NULL			
				SET @table = @table + ' AND trn.tranType = ''' + @txnType + '''' 
			else
				SET @table = @table + ' AND trn.tranType=''I''' 

			IF @id IS NOT NULL			
				SET @table = @table + ' AND trn.id = ''' + @id + '''' 
				
			IF @branch IS NOT NULL
				SET @table = @table + ' AND am.agentId = ''' + @branch + '''' 
			
			IF @country IS NOT NULL
				SET @table = @table + ' AND trn.pCountry LIKE ''' + @country + '%''' 
			
			IF @amt IS NOT NULL
				SET @table = @table + ' AND trn.cAmt = ' + CAST(@amt AS VARCHAR(50))
			
			IF @voucherNo IS NOT NULL
				SET @table = @table + ' AND trn.voucherNo = ''' + @voucherNo + ''''				
			
			IF @txnDate IS NOT NULL
				SET @table = @table + ' AND CAST(trn.createdDate AS DATE)='''+@txnDate +''''	
						
			if @isSelfApprove = 'N'			
				SET @table = @table + '  AND trn.createdBy <> '''+ @user +''''
			
			SET @sql = '
				SELECT 
					* 
				FROM (
					' + @table + ' 
				) x
				WHERE 1 = 1 '

			IF @txncreatedBy IS NOT NULL
				SET @sql = @sql + '  AND txncreatedBy = '''+@txncreatedBy+''''
				
			
			IF @sender IS NOT NULL
				SET @sql = @sql + ' AND sender LIKE ''' + @sender + '%'''

			IF @receiver IS NOT NULL
				SET @sql = @sql + ' AND receiver LIKE ''' + @receiver + '%'''			
					
			--PRINT @sql
			EXEC (@sql)
		RETURN	
	END
	
	IF @flag = 's-agent-self-txn'
	BEGIN		
		SET @branchList = '
			INNER JOIN ( 
				SELECT ' + CAST(@branchId AS VARCHAR) + ' agentId '
				
		IF @userType = 'RH'
		BEGIN
			SET @branchList = @branchList + '
							UNION ALL 	
							SELECT
								 am.agentId 			
							FROM agentMaster am WITH(NOLOCK)
							INNER JOIN regionalBranchAccessSetup rba ON am.agentId = rba.memberAgentId
							WHERE rba.agentId = ' + CAST(@branchId AS VARCHAR) + '
							AND ISNULL(rba.isDeleted, ''N'') = ''N''
							AND ISNULL(rba.isActive, ''N'') = ''Y'''
		END
		SET @branchList = @branchList + '			
			) bl ON trn.sBranch = bl.agentId			
			'

		SET @table = '
						SELECT DISTINCT
							 trn.id
							,branch = am.agentName							
							,country = trn.pCountry
							,senderId = sen.customerId
							,sender = sen.firstName + ISNULL( '' '' + sen.middleName, '''') + ISNULL( '' '' + sen.lastName1, '''') + ISNULL( '' '' + sen.lastName2, '''')						
							,receiverId = rec.customerId
							,receiver = rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
							,amt = CAST(trn.cAmt AS DECIMAL(18, 3))
							,voucherNo = trn.voucherNo
							,trn.createdBy
							,collMode = ''''
							,txnDate = CAST(trn.createdDate AS DATE)
							,txncreatedBy = trn.createdBy
						FROM remitTranTemp trn WITH(NOLOCK) 
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						' + @branchList + '	
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
						--INNER JOIN #collModeList T ON T.tranId = trn.id
						WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL 
					'
			IF @txnType IS NOT NULL			
				SET @table = @table + ' AND trn.tranType = ''' + @txnType + '''' 
			else
				SET @table = @table + ' AND trn.tranType=''I''' 

			IF @id IS NOT NULL			
				SET @table = @table + ' AND trn.id = ''' + @id + '''' 
				
			IF @branch IS NOT NULL
				SET @table = @table + ' AND am.agentId = ''' + @branch + '''' 
			
			IF @country IS NOT NULL
				SET @table = @table + ' AND trn.pCountry LIKE ''' + @country + '%''' 
			
			IF @amt IS NOT NULL
				SET @table = @table + ' AND trn.cAmt = ' + CAST(@amt AS VARCHAR(50))
			
			IF @voucherNo IS NOT NULL
				SET @table = @table + ' AND trn.voucherNo = ''' + @voucherNo + ''''
				
			
			IF @txnDate IS NOT NULL
				SET @table = @table + ' AND CAST(trn.createdDate AS DATE)='''+@txnDate +''''
				
			SET @sqlSelfTxn = '
				SELECT 
					* 
				FROM (
					' + @table + ' AND trn.createdBy = '''+@user+'''
				) x
				WHERE 1 = 1 '
				
			IF @sender IS NOT NULL
				SET @sqlSelfTxn = @sqlSelfTxn + ' AND sender LIKE ''' + @sender + '%'''
			IF @receiver IS NOT NULL
				SET @sqlSelfTxn = @sqlSelfTxn + ' AND receiver LIKE ''' + @receiver + '%'''
	
			EXEC (@sqlSelfTxn)
		RETURN	
	END
	
	IF @flag = 's_txn_summary'
	BEGIN
			
			SET @branchList = '
			INNER JOIN ( 
				SELECT ' + CAST(@branchId AS VARCHAR) + ' agentId '
				
			IF @userType = 'RH'
			BEGIN
				SET @branchList = @branchList + '
								UNION ALL 	
								SELECT
									 am.agentId 			
								FROM agentMaster am WITH(NOLOCK)
								INNER JOIN regionalBranchAccessSetup rba ON am.agentId = rba.memberAgentId
								WHERE rba.agentId = ' + CAST(@branchId AS VARCHAR) + '
								AND ISNULL(rba.isDeleted, ''N'') = ''N''
								AND ISNULL(rba.isActive, ''N'') = ''Y'''
			END
			IF @userType = 'AH'
			BEGIN
				SET @branchList = @branchList + '
								UNION ALL 	
								select agentId from agentMaster with(nolock) where parentId = 
								(select parentId from agentmaster with(nolock) where agentId=' + CAST(@branchId AS VARCHAR) + ')
								and agentId <> ' + CAST(@branchId AS VARCHAR) + ''
			END

			SET @branchList = @branchList + '			
				) bl ON trn.sBranch = bl.agentId			
				'
			
			SET @tablesql = '
				select country,sum(txnCount) txnCount,sum(txnHoldCount) txnHoldCount from 
				(
					select country,txnCount,txnHoldCount from 
					(
						SELECT 					
							 country = CASE WHEN trn.tranType = ''B'' then pCountry+''(B2B)'' else  pCountry end
							,txnCount = case when trn.createdBy <> '''+@user+''' then 1 else 0 end
							,txnHoldCount = case when trn.createdBy = '''+@user+''' then 1 else 0 end
						FROM remitTranTemp trn WITH(NOLOCK) 						
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						' + @branchList + '	
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
						WHERE  trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL 
					)z
				)x group by country'
			
			--print(@tablesql)
			EXEC(@tablesql)
	END
	
	IF @flag = 's_admin_txn_summary'
	BEGIN
			
			SET @tablesql = '
						SELECT 
							 sn = row_number() over(order by trn.sCountry)						
							,country = upper(trn.sCountry)
							,txnCount = count(''x'')
						FROM remitTranTemp trn WITH(NOLOCK) 						
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
						WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
							  trn.payStatus = ''Unpaid'' AND
							  trn.approvedBy IS NULL AND trn.tranType=''I''  AND 
							  trn.tranType <> ''O'' AND isOnlineTxn <> ''Y''
						group by trn.sCountry
					'
			EXEC(@tablesql)
			--print @tablesql
	END

	IF @flag = 'OnlineTxn-waitingList'
	BEGIN
			
			SET @tablesql = '
						SELECT 
							 sn = row_number() over(order by trn.sCountry)						
							,country = upper(trn.sCountry)
							,txnCount = count(''x'')
						FROM remitTranTemp trn WITH(NOLOCK) 						
						LEFT JOIN apiRoutingTable art WITH(NOLOCK) ON trn.pAgent = art.agentId
						INNER JOIN agentMaster am WITH(NOLOCK) ON trn.sBranch = am.agentId	
						INNER JOIN tranSendersTemp sen WITH(NOLOCK) ON trn.id = sen.tranId
						INNER JOIN tranReceiversTemp rec WITH(NOLOCK) ON trn.id = rec.tranId
						--WHERE trn.tranStatus IN (''Hold'',''Compliance Hold'',''OFAC Hold'',''OFAC/Compliance Hold'') AND 
						--	  trn.payStatus = ''Unpaid'' AND
						--	  trn.approvedBy IS NULL AND trn.tranType=''O'' AND isOnlineTxn =''Y''
						group by trn.sCountry
					'
			EXEC(@tablesql)
			--print @tablesql
	END
	
END TRY
BEGIN CATCH
     IF @@TRANCOUNT > 0
     ROLLBACK TRANSACTION
   SELECT 1 error_code, ERROR_MESSAGE() mes, NULL id
END CATCH
