

ALTER PROC PROC_DROPDOWN_LIST
(
	@Flag VARCHAR(20)
	,@UserEmail VARCHAR(150) = NULL
	,@User VARCHAR(150) = NULL
	,@ShowAll VARCHAR(5) = NULL
	,@selectedValue1 VARCHAR(20) = NULL
	,@CountryId INT = NULL
	,@PaymentMethod INT = NULL
	,@country VARCHAR(50)	= NULL
)
AS;
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRY
	DECLARE @ErrorMsg VARCHAR(MAX)
	DECLARE @agentid INT = 394395
	IF @Flag = 'PayoutMethod'
	BEGIN
		DECLARE @PayoutCurrency VARCHAR(5)

		SELECT @PayoutCurrency = CM.currencyCode
		FROM countryCurrency CC(NOLOCK)
		INNER JOIN currencyMaster CM(NOLOCK) ON CM.currencyId = CC.currencyId
		WHERE CC.countryId = @CountryId
		AND ISNULL(CC.isActive, 'Y') = 'Y'
		AND CC.isDefault = 'Y'

		IF @PaymentMethod IS NULL
		BEGIN
			SELECT agentId
				,isRealTime
				,exRateCalByPartner
				,CM.COUNTRYCODE
				,CM.CountryName
				,AgentId
				,PayoutCurrency = @PayoutCurrency
			FROM TblPartnerwiseCountry P(NOLOCK)
			INNER JOIN COUNTRYMASTER CM(NOLOCK) ON CM.COUNTRYID = P.COUNTRYID
			WHERE P.countryId = @CountryId
				AND (
					PaymentMethod IS NULL
					OR PaymentMethod IS NOT NULL
					)
				AND P.IsActive = 1
		END
		ELSE
		BEGIN
			SELECT agentId
				,isRealTime
				,exRateCalByPartner
				,CM.COUNTRYCODE
				,CM.CountryName
				,AgentId
				,PayoutCurrency = @PayoutCurrency
			FROM TblPartnerwiseCountry P(NOLOCK)
			INNER JOIN COUNTRYMASTER CM(NOLOCK) ON CM.COUNTRYID = P.COUNTRYID
			WHERE P.countryId = @CountryId
				AND ISNULL(PaymentMethod, @PaymentMethod) = @PaymentMethod
				AND P.IsActive = 1
		END
	END
	ELSE IF @Flag = 'CustomerReceiver'
	BEGIN
		SELECT TOP 3 Id = receiverId
				, Name = FULLNAME
				, Country = country
				, Mobile
				, TransactionType = SM.typeTitle
				, errorCode = 0
				, msg = 'Success'
		FROM receiverInformation RI(NOLOCK)
		INNER JOIN serviceTypeMaster SM(NOLOCK) ON SM.serviceTypeId = RI.paymentMode
	END
	ELSE IF @Flag = 'receiverList'
	BEGIN
		SELECT Id = receiverId
				, Text = FULLNAME
				, errorCode = 0
				, msg = 'Success'
		FROM receiverInformation RI(NOLOCK)
		INNER JOIN countryMaster CM(NOLOCK) ON CM.countryName = RI.country
		WHERE CM.countryId = @selectedValue1
	END
	ELSE IF @Flag = 'purpose'
	BEGIN
		SELECT Id = valueId
				, Text = detailTitle
				, errorCode = 0
				, msg = 'Success'
		FROM staticdatavalue (NOLOCK) 
		WHERE typeid=3800  
		AND ISNULL(ISActive, 'Y') = 'Y' 
		AND ISNULL(IS_DELETE, 'N') = 'N'
		ORDER BY detailTitle ASC
	END
	ELSE IF @Flag = 'howToPay'
	BEGIN
		SELECT Id = detailTitle 
				, Text = CASE detailTitle WHEN 'ONLINE' THEN 'Online Banking(Best Rate)'   WHEN 'DEBIT_CARD' THEN  'DEBIT CARD' ELSE 'E-Banking (Good rate)' END
		FROM staticDataValue (NOLOCK)
		WHERE typeId = '8109'
		AND ISNULL(isactive, 'Y') = 'Y'
	END
	ELSE IF @Flag = 'pCountry'
	BEGIN
		SET @countryId = 233
		SELECT Id = countryId
				, Text = UPPER(countryName)
				, errorCode = 0
				, msg = 'Success'
		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
	END
	ELSE IF @Flag = 'pMode'
	BEGIN
		SET @countryId = 233
		SELECT Id = serviceTypeId
				, Text = UPPER(typeTitle)
				, errorCode = 0
				, msg = 'Success'
		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 = @selectedValue1
				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 = @selectedValue1
				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 = @selectedValue1
				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 = @selectedValue1
				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 ='allCountrylist'  
BEGIN
	--select Id = @CountryId,text = UPPER(countryName)
	--FROM dbo.countryMaster (nolock) 
	--order by ISNULL(isOperativeCountry,'N') DESC,countryName
	SELECT countryId AS [id] ,
	countryName AS [text]
	FROM countryMaster with (NOLOCK)
	
	ORDER BY ISNULL(isOperativeCountry, 'N') Desc, countryName
END  

	IF @flag = 'getSourceOfFound'
	BEGIN
		--SOURCE OF FUND
		SELECT   
		    valueId AS [id]
			,detailTitle AS [text]
			FROM staticDataValue(NOLOCK)
			WHERE TYPEID = 3900
				--AND ISNULL(ISACTIVE, 'Y') = 'Y'
				--AND ISNULL(IS_DELETE, 'N') = 'N'
			
		
		END


	ELSE IF @Flag ='getGender'  
BEGIN
	SELECT valueId AS [id]
			,detailTitle AS [text]
		FROM staticdatavalue WITH (NOLOCK)
		WHERE typeid = 4
			--AND ISNULL(ISActive, 'N') = 'Y'
			AND ISNULL(IS_DELETE, 'N') = 'N'
		ORDER BY [text] --Gender

end
	ELSE IF @Flag ='getOccuptttion'  
BEGIN
		SELECT valueId AS [id]
			,detailTitle AS [text]
		FROM staticdatavalue WITH (NOLOCK)
		WHERE typeid = 2000
			AND ISNULL(ISActive, 'Y') = 'Y'
			AND ISNULL(IS_DELETE, 'N') = 'N'
	   --occuptttion

		END

			ELSE IF @Flag ='getIdType'  
 BEGIN
			SELECT detailTitle AS id,detailTitle AS text  
		FROM dbo.staticDataValue (NOLOCK) 
		WHERE typeID=1300 
		AND ISNULL(ISACTIVE, 'Y') = 'Y'
		AND ISNULL(IS_DELETE, 'N') = 'N'

		END

ELSE IF @flag = 'payoutMethods'
BEGIN
	DECLARE @payoutMethods TABLE ([Key] INT,[Value] VARCHAR(50),DISORDER INT)
	INSERT INTO @payoutMethods([Key],[Value])
	SELECT 
			Id = serviceTypeId
		,Text = UPPER(typetitle)
	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
				INNER JOIN COUNTRYMASTER CM (NOLOCK) ON CM.COUNTRYID = SL.COUNTRYID 
				WHERE CM.COUNTRYNAME = @country
				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 
				INNER JOIN COUNTRYMASTER CM (NOLOCK) ON CM.COUNTRYID = SL.receivingCountry 
				--WHERE CM.COUNTRYNAME = @country
				WHERE SL.tranType IS  NULL
				AND CM.COUNTRYNAME = @country 
				AND receivingAgent IS NULL
				AND ISNULL(SL.isActive,'N')='Y'
				AND ISNULL(SL.isDeleted,'N')='N'
					  
				UNION ALL
					  
				SELECT tranType, MAX(maxLimitAmt) maxLimitAmt
				FROM sendTranLimit SL WITH (NOLOCK) 
				INNER JOIN COUNTRYMASTER CM (NOLOCK) ON CM.COUNTRYID = SL.receivingCountry 
				WHERE CM.COUNTRYNAME = @country 
				AND ISNULL(SL.isActive,'N')='Y'
				AND ISNULL(SL.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)
				INNER JOIN COUNTRYMASTER CM (NOLOCK) ON CM.COUNTRYID = SL.receivingCountry 
				WHERE CM.COUNTRYNAME = @country 
				AND ISNULL(SL.isActive,'N')='Y'
				AND ISNULL(SL.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
		UPDATE @payoutMethods SET DISORDER = CASE WHEN @country in('Bangladesh','MONGOLIA','THAILAND','INDIA','PAKISTAN') AND [Key]=2 THEN 0 ELSE [Key] END
		
		DELETE FROM @payoutMethods WHERE @country IN ('CAMBODIA') AND [Key] = '2'
		--DELETE FROM @payoutMethods WHERE @country='thailand' AND [Key] = '1'

		SELECT [Key],[Value] FROM @payoutMethods ORDER BY DISORDER
END


END TRY
BEGIN CATCH
	SET @ErrorMsg = 'Internal Server Error: ' + ERROR_MESSAGE()
	SELECT errorCode = 1, msg = @ErrorMsg, id = NULL
END CATCH


