USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_sendPageLoadData]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_sendPageLoadData]
GO
/****** Object:  StoredProcedure [dbo].[proc_sendPageLoadData]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[proc_sendPageLoadData]
	 @flag			VARCHAR(200)
    ,@param			VARCHAR(200)		= NULL
    ,@param1		VARCHAR(200)		= NULL
    ,@user			VARCHAR(30)			= NULL
    ,@country		VARCHAR(50)			= NULL
    ,@countryId		VARCHAR(10)			= NULL
    ,@countryName   VARCHAR(100)		= NULL
    ,@agentId		VARCHAR(50)			= NULL
    ,@pCountryId	VARCHAR(10)			= NULL
    ,@pCountryName  VARCHAR(100)		= NULL
    ,@sAgent		VARCHAR(100)		= NULL
    ,@sBranch		VARCHAR(100)		= NULL
    ,@rAgent		VARCHAR(100)		= NULL
    ,@sCustomerId	VARCHAR(10)			= NULL
    ,@blackListIds	VARCHAR(MAX)		= NULL
    ,@agentRefId	VARCHAR(20)			= NULL
    ,@deliveryMethodId INT				= NULL
    ,@pBankType			CHAR(1)			= NULL
	,@complianceTempId	INT				= NULL
	,@csDetailRecId		INT				= NULL
	,@pMode			VARCHAR(5)			= NULL
	,@pLocation		BIGINT				= NULL
	,@pAgent		INT					= NULL
AS

	SET NOCOUNT ON;
	IF @flag = 'recModeByCountry'--@author:bijay; Receiving Mode By CountryId
	BEGIN    
		SELECT 
				 serviceTypeId
				,UPPER(typetitle) typeTitle
				,MIN(maxLimitAmt) maxLimitAmt
		   FROM serviceTypeMaster stm WITH (NOLOCK)
		   INNER JOIN (
						SELECT 
							receivingMode, maxLimitAmt 
						FROM countryReceivingMode crm WITH(NOLOCK) 
						INNER JOIN  sendTranLimit SL WITH (NOLOCK) ON crm.countryId = SL.receivingCountry 
						WHERE SL.countryId = @countryId AND SL.receivingCountry = @pcountryId
						AND SL.agentId IS NULL AND SL.tranType IS NULL AND receivingAgent IS NULL

						UNION ALL
					   
						SELECT 
							receivingMode, maxLimitAmt 
						FROM countryReceivingMode crm WITH(NOLOCK) 
						INNER JOIN  sendTranLimit SL WITH (NOLOCK) ON crm.countryId = SL.receivingCountry 
						AND SL.receivingCountry = @pcountryId AND SL.countryId = @countryId 
						WHERE agentId = @agentId
						AND SL.tranType IS  NULL
						AND receivingAgent IS NULL
						AND ISNULL(isActive,'N')='Y'
						AND ISNULL(isDeleted,'N')='N'
					  
						UNION ALL
					  
						SELECT tranType, MAX(maxLimitAmt) maxLimitAmt
						FROM sendTranLimit SL WITH (NOLOCK) 
						WHERE countryId = @countryId 
						AND SL.receivingCountry=@pcountryId
						AND ISNULL(isActive,'N')='Y'
						AND ISNULL(isDeleted,'N')='N'
						AND SL.agentId IS NULL
						AND SL.tranType IS NOT NULL
						AND SL.receivingAgent IS NULL
						GROUP BY tranType
					  
						UNION ALL
					  
						SELECT tranType, MAX(maxLimitAmt) maxLimitAmt
						FROM sendTranLimit SL WITH (NOLOCK)
						WHERE countryId = @countryId 
						AND SL.receivingCountry=@pcountryId
						AND SL.agentId=@agentid
						AND ISNULL(isActive,'N')='Y'
						AND ISNULL(isDeleted,'N')='N'
						AND receivingAgent IS NULL
						AND SL.tranType IS NOT NULL
						AND SL.receivingAgent IS NULL
						GROUP BY tranType 
				   ) X ON  X.receivingMode = stm.serviceTypeId
		   WHERE ISNULL(STM.isActive,'N') = 'Y' AND ISNULL(STM.isDeleted,'N') = 'N'
		   AND (STM.serviceTypeId NOT IN (5))
		   --AND (STM.serviceTypeId NOT IN (3,5))
		   GROUP BY serviceTypeId,typetitle
		   HAVING MIN(X.maxLimitAmt)>0
		   ORDER BY serviceTypeId ASC
	END

	ELSE IF @flag = 'recModeByCountry-txnReport'
	BEGIN
		SELECT 
			 serviceTypeId
			,UPPER(typetitle) typeTitle
			,MIN(maxLimitAmt) maxLimitAmt
	   FROM serviceTypeMaster stm WITH (NOLOCK)
	   INNER JOIN (
				SELECT 
					receivingMode, 
					maxLimitAmt 
				FROM countryReceivingMode crm WITH(NOLOCK) 
				INNER JOIN  sendTranLimit SL WITH (NOLOCK) ON crm.countryId = SL.receivingCountry 
				WHERE SL.countryId = @countryId AND SL.receivingCountry = ISNULL(@pcountryId, SL.receivingCountry)
				AND SL.agentId IS NULL AND SL.tranType IS NULL AND receivingAgent IS NULL
			
				UNION ALL
					   
				SELECT 
					receivingMode, 
					maxLimitAmt 
				FROM countryReceivingMode crm WITH(NOLOCK) 
				INNER JOIN  sendTranLimit SL WITH (NOLOCK) ON crm.countryId = SL.receivingCountry 
				AND SL.receivingCountry = ISNULL(@pcountryId, SL.receivingCountry) AND SL.countryId = @countryId 
				WHERE agentId = @agentId
				AND SL.tranType IS  NULL
				AND receivingAgent IS NULL
				AND ISNULL(isActive,'N')='Y'
				AND ISNULL(isDeleted,'N')='N'			 
					  
				UNION ALL
			  
				SELECT 
					tranType, 
					MAX(maxLimitAmt) maxLimitAmt
				FROM sendTranLimit SL WITH (NOLOCK) 
				WHERE countryId = @countryId 
					AND SL.receivingCountry=ISNULL(@pcountryId, SL.receivingCountry)
					AND ISNULL(isActive,'N')='Y'
					AND ISNULL(isDeleted,'N')='N'
					AND SL.agentId IS NULL					
					AND SL.tranType IS NOT NULL
					AND SL.receivingAgent IS NULL			
				GROUP BY tranType
					  
				UNION ALL
			  
				SELECT 
					tranType, 
					MAX(maxLimitAmt) maxLimitAmt
				FROM sendTranLimit SL WITH (NOLOCK)
				WHERE countryId = @countryId 
				AND SL.receivingCountry=ISNULL(@pcountryId, SL.receivingCountry)
				AND SL.agentId=@agentid
				AND ISNULL(isActive,'N')='Y'
				AND ISNULL(isDeleted,'N')='N'
				AND receivingAgent IS NULL					
				AND SL.tranType IS NOT NULL
				AND SL.receivingAgent IS NULL			
				GROUP BY tranType 
			) pt ON  pt.receivingMode = stm.serviceTypeId
		   WHERE ISNULL(STM.isActive,'N')='Y' AND ISNULL(STM.isDeleted,'N')='N'
		   GROUP BY serviceTypeId,typetitle
		   HAVING MIN(pt.maxLimitAmt)>0
		   ORDER BY typeTitle ASC

	END

	ELSE IF @flag = 'sCountry'--CountryName List
	BEGIN
		SELECT 
			countryId,
			countryName
		FROM countryMaster Where isnull(isOperativeCountry,'') ='Y'
		AND countryName <>'Worldwide Others'
		ORDER BY countryName ASC
		RETURN
	END

	ELSE IF @flag = 'pLocation'
	BEGIN
		IF NOT EXISTS(SELECT 'A' FROM tblServicewiseLocation (NOLOCK) WHERE countryId = @pCountryId)
		BEGIN
			SELECT 'Any State' LOCATIONNAME,'0' LOCATIONID 
			RETURN
		END
		SELECT location LOCATIONNAME
				,rowId LOCATIONID 
		FROM tblServicewiseLocation (NOLOCK) 
		WHERE countryId = @pCountryId
		AND ISNULL(serviceTypeId, @pMode) = @pMode 
		AND isActive = 1
		RETURN
	END

	ELSE IF @flag = 'subPLocation'
	BEGIN
		IF NOT EXISTS(SELECT 'A' FROM tblSubLocation (NOLOCK) WHERE locationId = @pLocation)
		BEGIN
			SELECT 'Any location' LOCATIONNAME,'0' LOCATIONID 
			RETURN
		END
		SELECT subLocation LOCATIONNAME
				,rowId LOCATIONID 
		FROM tblSubLocation (NOLOCK) 
		WHERE locationId = @pLocation
		AND isActive = 1
		RETURN
	END

	ELSE IF @flag = 'pCountry'-- CountryName List
	BEGIN
		 SELECT 
			countryId,
			UPPER(countryName) countryName
		FROM countryMaster CM WITH (NOLOCK)
		INNER JOIN 
		(
			SELECT  receivingCountry,min(maxLimitAmt) maxLimitAmt
			FROM(
					SELECT   receivingCountry,max (maxLimitAmt) maxLimitAmt
					FROM sendTranLimit SL WITH (NOLOCK) 
					WHERE --countryId = @countryId
					--AND
					 ISNULL(isActive,'N')='Y'
					AND ISNULL(isDeleted,'N')='N'
					AND ISNULL(agentId,ISNULL(@agentid,0))=ISNULL(@agentid,0)
					GROUP BY receivingCountry
              
					UNION ALL
               
					SELECT    receivingCountry,max (maxLimitAmt)maxLimitAmt
					FROM sendTranLimit SL WITH (NOLOCK) 
					WHERE agentId=@agentid
					AND ISNULL(isActive,'N')='Y'
					AND ISNULL(isDeleted,'N')='N'
					GROUP BY receivingCountry  
                 
			) x GROUP  BY receivingCountry
		) Y ON  Y.receivingCountry=CM.countryId
		WHERE ISNULL(isOperativeCountry,'') ='Y'
		AND Y.maxLimitAmt>0
		ORDER BY countryName ASC
		RETURN
	END

	ELSE If @flag ='pcurr'--load currency by pcountry
	begin
		SELECT distinct CM.currencyCode,cc.isDefault from currencyMaster CM WITH (NOLOCK)
		INNER JOIN countryCurrency CC WITH (NOLOCK) ON CM.currencyId=CC.currencyId
		WHERE CC.countryId=@countryId
	end

	ELSE IF @flag = 'recAgentByRecModeAjaxagent'
	BEGIN	
		DECLARE @maxPayoutLimit DECIMAL(10,2)

		SELECT @deliveryMethodId = serviceTypeId FROM serviceTypeMaster(nolock) where typeTitle = @param
		
		select @maxPayoutLimit = maxLimitAmt from receiveTranLimit(NOLOCK) 
		WHERE countryId = @pCountryId AND tranType = @deliveryMethodId
		and sendingCountry = @countryId

		IF @param = 'CASH PAYMENT' AND @pCountryId IN ('151', '36')
		BEGIN
			SELECT AGENTID = '', 0 NS,FLAG = 'E',AGENTNAME = '[ANY WHERE]',maxPayoutLimit = @maxPayoutLimit
			
			RETURN
		END	
		ELSE IF @param = 'DOOR TO DOOR' AND @pCountryId IN ('203')
		BEGIN
			SELECT AGENTID = '', 0 NS,FLAG = 'E',AGENTNAME = '[ANY WHERE]',maxPayoutLimit = @maxPayoutLimit
			
			RETURN
		END	
		ELSE IF @param = 'BANK DEPOSIT' OR (@pCountryId = '203' AND @param = 'CASH PAYMENT')
		BEGIN
			SELECT AGENTID = '', 0 NS,FLAG = 'E',AGENTNAME = '[SELECT BANK]' ,maxPayoutLimit = @maxPayoutLimit
			UNION ALL
			SELECT distinct bankId = agentId, 0 NS,FLAG = 'E',AGENTNAME,maxPayoutLimit = @maxPayoutLimit
			FROM  AgentMaster(NOLOCK) 
			WHERE AGENTTYPE = '2903' and IsIntl = 1
			and agentCountryId = @pCountryId
			ORDER BY AGENTNAME
			
			RETURN
		END	
		ELSE
		BEGIN
			SELECT AGENTID = agentId, 
					0 NS,
					FLAG = 'E',
					AGENTNAME = agentName,
					maxPayoutLimit = @maxPayoutLimit 
			FROM agentMaster (NOLOCK)
			WHERE agentCountryId = @pCountryId 
			AND agentRole = 1
			
			RETURN
		END
	END

	ELSE IF @flag = 'agentsetting'
	BEGIN
		IF @pBankType = 'I'
		BEGIN
			DECLARE @rtlId INT
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId = @agentId AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId = @agentId AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId IS NULL AND countryId = @countryId AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId IS NULL AND countryId = @countryId AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			
			SELECT
				 maxLimitAmt
				,agMaxLimitAmt
				,branchSelection
				,benificiaryIdReq
				,relationshipReq		= ''
				,benificiaryContactReq
				,acLengthFrom
				,acLengthTo
				,acNumberType
			FROM receiveTranLimit WITH(NOLOCK)
			WHERE rtlId = @rtlId
		END
		ELSE IF @pBankType = 'E'
		BEGIN
			SELECT
				 maxLimitAmt			= ''
				,agMaxLimitAmt			= ''
				,branchSelection		= IsBranchSelectionRequired
				,benificiaryIdReq		= ''
				,relationshipReq		= ''
				,benificiaryContactReq	= ''
				,acLengthFrom			= ''
				,acLengthTo				= ''
				,acNumberType			= ''
			FROM externalBank WITH(NOLOCK)
			WHERE extBankId = @agentId
		END
		ELSE
		BEGIN
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId IS NULL AND countryId = @countryId AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK) WHERE agentId IS NULL AND countryId = @countryId AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			
			SELECT
				 maxLimitAmt
				,agMaxLimitAmt
				,branchSelection
				,benificiaryIdReq
				,relationshipReq		= ''
				,benificiaryContactReq
				,acLengthFrom
				,acLengthTo
				,acNumberType
			FROM receiveTranLimit WITH(NOLOCK)
			WHERE rtlId = @rtlId
		END
	END

ELSE IF @flag = 'branchAjax'-- Select branchName List According to AgentName By pralhad
BEGIN
	--SELECT * FROM receiveTranLimit WITH(NOLOCK) WHERE agentId IS NOT NULL
	DECLARE @branchSelection VARCHAR(50)
	SELECT @branchSelection = ISNULL(branchSelection,'A')  FROM receiveTranLimit WITH (NOLOCK) WHERE agentId = @agentId
	
	SELECT @branchSelection [branchSelection]
	RETURN
END

ELSE IF @flag = 'schemeBySCountry'
BEGIN

	SELECT rowId as schemeCode ,schemeName 
	FROM schemeSetup WITH (NOLOCK) 
END

ELSE IF @flag = 'schemeBySCountryRCountry'
BEGIN
	DECLARE @customerTypeId INT
	SET @customerTypeId = 4700
	
	DECLARE @schemeTable TABLE(schemeCode VARCHAR(50), schemeName VARCHAR(100), sCountry VARCHAR(10), sAgent VARCHAR(10), sBranch VARCHAR(10), rCountry VARCHAR(10), rAgent VARCHAR(10), customerType VARCHAR(10))
	INSERT INTO @schemeTable
	SELECT
		 schemeCode = rowId
		,schemeName
		,'118' AS sCountry,'' sAgent,'' sBranch
		,rCountry,rAgent
		,'' customerType
	FROM schemeSetup WITH (NOLOCK) 
	WHERE rCountry = @pCountryId
	AND GETDATE() BETWEEN ISNULL(schemeStartDate, '1900-01-01') AND ISNULL(schemeEndDate, '2100-01-01')
	
	IF EXISTS(SELECT 'X' FROM @schemeTable)
	BEGIN
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sBranch = @sBranch)
		BEGIN
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent = @rAgent AND customerType = @customerTypeId)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent = @rAgent AND customerType = @customerTypeId 
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent = @rAgent AND customerType IS NULL)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent = @rAgent AND customerType IS NULL
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent IS NULL AND customerType = @customerTypeId)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch = @sBranch AND rAgent IS NULL AND customerType IS NULL)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL
				RETURN
			END
		END
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL)
		BEGIN
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent = @rAgent AND customerType = @customerTypeId)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent = @rAgent AND customerType = @customerTypeId
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent = @rAgent AND customerType IS NULL)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent = @rAgent AND customerType IS NULL
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId
				RETURN
			END
			IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL)
			BEGIN
				SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL
				RETURN
			END
		END
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId)
		BEGIN
			SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId
			RETURN
		END
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL)
		BEGIN
			SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent = @sAgent AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL
			RETURN
		END 
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent IS NULL AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId)
		BEGIN
			SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent IS NULL AND sBranch IS NULL AND rAgent IS NULL AND customerType = @customerTypeId
			RETURN
		END
		IF EXISTS(SELECT 'X' FROM @schemeTable WHERE sAgent IS NULL AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL)
		BEGIN
			SELECT schemeCode, schemeName FROM @schemeTable WHERE sAgent IS NULL AND sBranch IS NULL AND rAgent IS NULL AND customerType IS NULL
			RETURN
		END 
	END
END

ELSE IF @flag = 'loadRulesCountry'
BEGIN
	
	SELECT D.paymentMode,D.tranCount,D.amount FROM csMaster M WITH (NOLOCK)
	INNER JOIN csDetail D WITH(NOLOCK) ON M.csMasterId=D.csDetailId
	WHERE sCountry=@countryId
	AND ISNULL(M.isActive,'Y')='Y' AND ISNULL(D.isActive,'Y')='Y' 

END

ELSE IF @flag = 'loadOccupation'
BEGIN
	SELECT occupationId,detailTitle
	FROM occupationMaster WITH (NOLOCK) 
	WHERE ISNULL(isActive,'Y')='Y' AND ISNULL(isDeleted,'N')<>'Y'
END

ELSE IF @flag = 'idTypeBySCountry' -- 
BEGIN
	SELECT 
		 valueId		= CAST(SV.valueId AS VARCHAR) + '|' + ISNULL(CID.expiryType, 'E')
		,detailTitle	= SV.detailTitle
		,expiryType	= CID.expiryType
	FROM countryIdType CID WITH(NOLOCK)
	INNER JOIN staticDataValue SV WITH(NOLOCK) ON CID.IdTypeId = SV.valueId
	WHERE countryId = @countryId AND ISNULL(isDeleted,'N') <> 'Y'
    AND (spFlag IS NULL OR ISNULL(spFlag, 0) = 5200)
END

ELSE IF @flag = 'idTypeByPCountry'
BEGIN
	SELECT
		 valueId
		,detailTitle
	FROM staticDataValue sdv WITH(NOLOCK)
	WHERE typeID = 1300
	AND ISNULL(IS_DELETE, 'N') = 'N'
END

ELSE IF @FLAG = 'pageField' ---FIELD SELECTED FOR SEND TXN
BEGIN
	DECLARE @rowId INT = NULL
	SELECT @rowId = rowId FROM sendPayTable WITH(NOLOCK) WHERE agent = @agentId AND ISNULL(isDeleted, 'N') = 'N'
	IF @rowId IS NULL
		SELECT @rowId = rowId FROM sendPayTable WITH(NOLOCK) WHERE country = @countryId AND agent IS NULL AND ISNULL(isDeleted, 'N') = 'N'
		
	 SELECT 
		 customerRegistration 
		,newCustomer
		,collection
		,id
		,idIssueDate
		,iDValidDate
		,dob
		,address
		,city
		,nativeCountry
		,contact
		,occupation
		,company
		,salaryRange
		,purposeofRemittance
		,sourceofFund
		,rId
		,rPlaceOfIssue
		,raddress
		,rcity
		,rContact
		,rRelationShip
		,rIdValidDate
		,rDOB
	 FROM sendPayTable WITH (NOLOCK)
	 WHERE rowId = @rowId

END

ELSE IF @flag = 'agentByExtAgent' --Get Principle Agent By External Agent
BEGIN
	SELECT DISTINCT
		 am.agentId
		,am.agentName
	FROM agentMaster am WITH(NOLOCK)
	INNER JOIN ExternalBankCode ebc WITH(NOLOCK) ON am.agentId = ebc.agentId
	WHERE bankId = @param
	AND ISNULL(am.isActive, 'N') = 'Y' 
	AND ISNULL(ebc.isDeleted, 'N') = 'N'
END

ELSE IF @flag = 'agentByExtBranch'
BEGIN
	--SELECT * FROM externalBankCode ORDER BY bankId
	SELECT @param = extBankId FROM externalBankBranch WITH(NOLOCK) WHERE extBranchId = @param
	SELECT DISTINCT
		 am.agentId
		,am.agentName
	FROM agentMaster am WITH(NOLOCK)
	INNER JOIN ExternalBankCode ebc WITH(NOLOCK) ON am.agentId = ebc.agentId
	WHERE bankId = @param
	AND ISNULL(am.isActive, 'N') = 'Y' 
	AND ISNULL(ebc.isDeleted, 'N') = 'N'
END

ELSE IF @flag = 'payoutLimitInfo'
BEGIN
	--6. Payout Per Txn Limit
	DECLARE @pCurr VARCHAR(3)
	SELECT @pCurr = cm.currencyCode 
	FROM countryCurrency cc INNER JOIN currencyMaster cm ON cc.currencyId = cm.currencyId
	WHERE cc.countryId = @pCountryId
	
	IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK) WHERE sendingCountry = @countryId
				AND countryId = @pCountryId AND (agentId = @rAgent OR agentId IS NULL) AND currency = @pCurr
				AND ISNULL(tranType, ISNULL(@deliveryMethodId, 0)) = ISNULL(@deliveryMethodId, 0)
				AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
				)
	BEGIN
		SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
		WHERE sendingCountry = @countryId AND countryId = @pCountryId AND agentId = @rAgent AND currency = @pCurr 
		AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
		
		IF @rtlId IS NULL			
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
			WHERE sendingCountry = @countryId AND countryId = @pCountryId AND agentId = @rAgent AND currency = @pCurr 
			AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
		
		IF @rtlId IS NULL
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
			WHERE sendingCountry = @countryId AND countryId = @pCountryId AND agentId IS NULL AND currency = @pCurr 
			AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
		
		IF @rtlId IS NULL
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
			WHERE sendingCountry = @countryId AND countryId = @pCountryId AND agentId IS NULL AND currency = @pCurr 
			AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'	
	END
	IF @rtlId IS NULL
	BEGIN
		IF EXISTS(SELECT 'X' FROM receiveTranLimit WITH(NOLOCK) WHERE sendingCountry IS NULL
					AND countryId = @pCountryId AND (agentId = @rAgent OR agentId IS NULL) AND currency = @pCurr
					AND ISNULL(tranType, ISNULL(@deliveryMethodId, 0)) = ISNULL(@deliveryMethodId, 0)
					AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
					)
		BEGIN
			SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
			WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId = @rAgent AND currency = @pCurr 
			AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			
			IF @rtlId IS NULL			
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
				WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId = @rAgent AND currency = @pCurr 
				AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
				WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId IS NULL AND currency = @pCurr 
				AND tranType = @deliveryMethodId AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'
			
			IF @rtlId IS NULL
				SELECT @rtlId = rtlId FROM receiveTranLimit WITH(NOLOCK)
				WHERE sendingCountry IS NULL AND countryId = @pCountryId AND agentId IS NULL AND currency = @pCurr 
				AND tranType IS NULL AND ISNULL(isActive, 'N') = 'Y' AND ISNULL(isDeleted, 'N') = 'N'	
		END
	END
	SELECT maxLimitAmt FROM receiveTranLimit 
	WITH(NOLOCK) WHERE rtlId = @rtlId
END

ELSE IF @flag = 'ofac'
BEGIN

	--EXEC proc_sendPageLoadData @flag = 'ofac', @user = 'admin', @blackListIds = 'OFAC10,UNSCR111952'
	--EXEC proc_sendPageLoadData @flag = 'ofac', @user = 'admin', @blackListIds = 'OFAC10,UNSCR111952'
	IF OBJECT_ID('tempdb..#tempMaster') IS NOT NULL 
	DROP TABLE #tempMaster
	
	IF OBJECT_ID('tempdb..#tempDataTable') IS NOT NULL 
	DROP TABLE #tempDataTable
		

	CREATE TABLE #tempDataTable(DATA VARCHAR(MAX) NULL)
	
	SELECT A.val ofacKeyId
	INTO #tempMaster
	FROM
	(
		SELECT * FROM dbo.SplitXML(',', @blackListIds)
	)A
	INNER JOIN
	(
		SELECT distinct ofacKey FROM blacklist with(nolock)
	)B ON A.val = B.ofacKey
	
	ALTER TABLE #tempMaster ADD ROWID INT IDENTITY(1,1)

	DECLARE @TNA_ID AS INT
			,@MAX_ROW_ID AS INT
			,@ROW_ID AS INT=1
			,@ofacKeyId VARCHAR(100)
			,@SDN VARCHAR(MAX)=''
			,@ADDRESS VARCHAR(MAX)=''
			,@REMARKS AS VARCHAR(MAX)=''
			,@ALT AS VARCHAR(MAX)=''
			,@DATA AS VARCHAR(MAX)=''
			,@DATA_SOURCE AS VARCHAR(200)=''
	
	SELECT @MAX_ROW_ID=MAX(ROWID) FROM #tempMaster	
	WHILE @MAX_ROW_ID >=  @ROW_ID
	BEGIN	
		
		SELECT @ofacKeyId=ofacKeyId FROM #tempMaster WHERE ROWID=@ROW_ID		

		SELECT @SDN='<b>'+ISNULL(entNum,'')+'</b>,  <b>Name:</b> '+ ISNULL(name,''),@DATA_SOURCE='<b>Data Source:</b> '+ISNULL(dataSource,'')
		FROM blacklist with(nolock) WHERE ofacKey = @ofacKeyId AND vesselType='sdn'		
		
		SELECT @ADDRESS=ISNULL(name,'')+', '+ISNULL(address,'')+', '+ISNULL(city,'')+', '+ISNULL(STATE,'')+', '+ISNULL(zip,'')+', '+ISNULL(country,'')
		FROM blacklist with(nolock) WHERE ofacKey = @ofacKeyId AND vesselType='add'
		
		SELECT @ALT = COALESCE(@ALT + ', ', '') +CAST(ISNULL(NAME,'') AS VARCHAR(MAX))
		FROM blacklist with(nolock) WHERE ofacKey = @ofacKeyId AND vesselType IN ('alt','aka')			
				
		SELECT @REMARKS=ISNULL(remarks,'')
		FROM blacklist with(nolock) WHERE ofacKey = @ofacKeyId AND vesselType='sdn'

		SET @SDN=RTRIM(LTRIM(@SDN))
		SET @ADDRESS=RTRIM(LTRIM(@ADDRESS))
		SET @ALT=RTRIM(LTRIM(@ALT))
		SET @REMARKS=RTRIM(LTRIM(@REMARKS))	
		
		SET @SDN=REPLACE(@SDN,', ,','')
		SET @ADDRESS=REPLACE(@ADDRESS,', ,','')
		SET @ALT=REPLACE(@ALT,', ,','')
		SET @REMARKS=REPLACE(@REMARKS,', ,','')
		
		SET @SDN=REPLACE(@SDN,'-0-','')
		SET @ADDRESS=REPLACE(@ADDRESS,'-0-','')
		SET @ALT=REPLACE(@ALT,'-0-','')
		SET @REMARKS=REPLACE(@REMARKS,'-0-','')
		
		SET @SDN=REPLACE(@SDN,',,','')
		SET @ADDRESS=REPLACE(@ADDRESS,',,','')
		SET @ALT=REPLACE(@ALT,',,','')
		SET @REMARKS=REPLACE(@REMARKS,',,','')
		
		IF @DATA_SOURCE IS NOT NULL AND @DATA_SOURCE<>'' 
			SET @DATA=@DATA_SOURCE
			
		IF @SDN IS NOT NULL AND @SDN<>'' 
			SET @DATA=@DATA+'<BR>'+@SDN
			
		IF @ADDRESS IS NOT NULL AND @ADDRESS<>'' 
			SET @DATA=@DATA+'<BR><b>Address: </b>'+@ADDRESS
			
		IF @ALT IS NOT NULL AND @ALT<>'' AND @ALT<>' '
			SET @DATA=@DATA+'<BR>'+'<b>a.k.a :</b>'+@ALT+''

		IF @REMARKS IS NOT NULL AND @REMARKS<>'' 
			SET @DATA=@DATA+'<BR><b>Other Info :</b>'+@REMARKS

		IF @DATA IS NOT NULL OR @DATA <>''
		BEGIN
			INSERT INTO #tempDataTable		
			SELECT REPLACE(@DATA,'<BR><BR>','')
		END
		
		SET @ROW_ID=@ROW_ID+1
	END
		
	ALTER TABLE #tempDataTable ADD ROWID INT IDENTITY(1,1)
	SELECT ROWID [S.N.],DATA [Remarks] FROM #tempDataTable
END

ELSE IF @flag = 'Compliance'
BEGIN
	SELECT
		 id
		,csDetailRecId 
		,[S.N.]		= ROW_NUMBER()OVER(ORDER BY id)	
		,[Remarks]	= RTRIM(LTRIM(dbo.FNAGetDataValue(condition))) + ' ' + 
						CASE WHEN checkType = 'Sum' THEN 'Transaction Amount' 
							 WHEN checkType = 'Count' THEN 'Transaction Count' END
						+ ' exceeds ' + CAST(parameter AS VARCHAR) + ' limit within ' + CAST(period AS VARCHAR)+ ' day(s) ' + dbo.FNAGetDataValue(criteria)   
		,[Matched Tran ID] = rtc.matchTranId
	FROM remitTranComplianceTemp rtc 
	INNER JOIN csDetailRec cdr ON rtc.csDetailTranId = cdr.csDetailRecId 
	WHERE rtc.agentRefId = @agentRefId
END

ELSE IF @flag='COMPL_DETAIL'
BEGIN
/*
5000	By Sender ID
5001	By Sender Name
5002	By Sender Mobile
5003	By Beneficiary ID
5004	By Beneficiary ID(System)
5005	By Beneficiary Name
5006	By Beneficiary Mobile
5007	By Beneficiary A/C Number
*/
	DECLARE @tranIds AS VARCHAR(MAX), @criteria AS INT, @totalTran AS INT, @criteriaValue AS VARCHAR(500), @id AS INT,@reason VARCHAR(500)
	SELECT 
		@tranIds = matchTranId
	FROM remitTranComplianceTemp with(nolock) 
	WHERE id = @complianceTempId --(ROWID) --id of remitTranCompliance

	SELECT @criteria = criteria FROM csDetailRec with(nolock) WHERE csDetailRecId = @csDetailRecId--id of csDetailRec
	
	DECLARE @tranIdTemp TABLE(tranId BIGINT)
	INSERT INTO @tranIdTemp
	SELECT value FROM dbo.Split(',', @tranIds)
	
	SELECT @totalTran = COUNT(*) FROM @tranIdTemp
			 
	SELECT
		 REMARKS	= CASE WHEN @csDetailRecId = 0 THEN @reason ELSE
						RTRIM(LTRIM(dbo.FNAGetDataValue(condition))) + ' ' + 
						CASE WHEN checkType = 'Sum' THEN 'Transaction Amount' 
							 WHEN checkType = 'Count' THEN 'Transaction Count' END
						+ ' exceeds ' + CAST(parameter AS VARCHAR) + ' limit within ' + CAST(period AS VARCHAR)+ ' day(s) ' + dbo.FNAGetDataValue(criteria)+': <font size=''2px''>'+ISNULL(@criteriaValue,'')+'</font>'
						END
		,totTran	= 'Total Count: <b>'+ CASE WHEN @csDetailRecId = 0 THEN '1' ELSE  CAST(@totalTran AS VARCHAR) END +'</b>'
	FROM csDetailRec with(nolock)
	WHERE csDetailRecId= CASE WHEN @csDetailRecId = 0 THEN 1 ELSE @csDetailRecId END

	SELECT 
		 [S.N.]			= ROW_NUMBER() OVER(ORDER BY @complianceTempId)
		,[CONTROL NO.]  = dbo.FNADecryptString(trn.controlNo)
		,[TRAN AMOUNT]	= dbo.ShowDecimal(trn.cAmt) 
		,[CURRENCY]		= trn.collCurr 
		,[TRAN DATE]	= CONVERT(VARCHAR,trn.createdDate,101)  		
	FROM VWremitTran trn with(nolock) 
	INNER JOIN @tranIdTemp t ON trn.id = t.tranId
	
	UNION ALL
	---- RECORD DISPLAY FROM CANCEL TRANSACTION TABLE
	SELECT 
		 [S.N.]			= ROW_NUMBER() OVER(ORDER BY @complianceTempId)
		,[CONTROL NO.]  = dbo.FNADecryptString(trn.controlNo)
		,[TRAN AMOUNT]	= dbo.ShowDecimal(trn.cAmt) 
		,[CURRENCY]		= trn.collCurr 
		,[TRAN DATE]	= CONVERT(VARCHAR,trn.createdDate,101)  		
	FROM cancelTranHistory trn with(nolock)
	INNER JOIN @tranIdTemp t ON trn.id = t.tranId
END