USE [FastMoneyPro_Remit]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC PROC_API_ROUTE_PARTNERS
(
	@FLAG				VARCHAR(20)
	--grid parameters
	,@user				VARCHAR(50)		= NULL
	,@pageSize			VARCHAR(50)		= NULL
	,@pageNumber		VARCHAR(50)		= NULL
	,@sortBy			VARCHAR(50)		= NULL
	,@sortOrder			VARCHAR(50)		= NULL
	,@agentID			BIGINT			= NULL
	,@PaymentMethod		INT				= NULL	
	,@CountryId			INT				= NULL	
	,@minAmount		    Money			= NULL	
	,@maxAmount			Money			= NULL	
	,@isActive			VARCHAR(10)		= NULL
	,@rowId				INT				= NULL
)
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
	DECLARE  @table				VARCHAR(MAX)
			,@select_field_list	VARCHAR(MAX)
			,@extra_field_list	VARCHAR(MAX)
			,@sql_filter		VARCHAR(MAX)

	IF @FLAG = 'I'
	BEGIN
		IF EXISTS (SELECT 1 FROM TblPartnerwiseCountry (NOLOCK) WHERE CountryId = @CountryId AND PaymentMethod = @PaymentMethod AND AgentId = @agentID)
		BEGIN
			SELECT '0' ErrorCode , 'Record already exists, please use existing record.' Msg , NULL id	 
			RETURN
		END

		INSERT INTO TblPartnerwiseCountry (CountryId, AgentId, IsActive, CreatedBy, CreatedDate, PaymentMethod,MinTxnAmount,MaxTxnAmount)
		SELECT @CountryId, @agentID, @isActive, @user, GETDATE(), @PaymentMethod,@minAmount,@maxAmount

		SELECT '0' ErrorCode , 'Record has been added successfully.' Msg , NULL id
	END
	IF @FLAG = 'u'
	BEGIN
		INSERT INTO TblPartnerwiseCountryHistory (partnerWiseCountryRowId, CountryId, AgentId, IsActive, PaymentMethod,MinTxnAmount,MaxTxnAmount, ModifiedBy, ModifiedDate)
		SELECT @rowId, CountryId, AgentId, IsActive, PaymentMethod, MinTxnAmount, MaxTxnAmount, @user, GETDATE() FROM TblPartnerwiseCountry (NOLOCK) WHERE id = @rowId

		UPDATE TblPartnerwiseCountry SET 
					CountryId = @CountryId, 
					IsActive = @isActive, 
					ModifiedBy = @user, 
					ModifiedDate = GETDATE(), 
					PaymentMethod = @PaymentMethod,
					AgentId = @agentID,
					MinTxnAmount = @minAmount,
					MaxTxnAmount = @maxAmount
		WHERE id = @rowId

		SELECT '0' ErrorCode , 'Record has been modified successfully.' Msg , NULL id
	END
	ELSE IF @FLAG = 'select'
	BEGIN
		SELECT id,CountryId,P.AgentId,PaymentMethod,A.agentName as providerName,GetRateFrom,p.CreatedBy,p.CreatedDate,p.IsActive,ROUND(CAST(MinTxnAmount AS DECIMAL),0) MinTxnAmount 
			,ROUND(CAST(MaxTxnAmount AS DECIMAL),0) MaxTxnAmount ,p.ModifiedBy,p.ModifiedDate 
		FROM TblPartnerwiseCountry p(NOLOCK) 
		INNER JOIN agentMaster A(NOLOCK) ON A.agentId  =P.AgentId
		WHERE id = @rowId
	END
	ELSE IF @FLAG = 'S'
	BEGIN
		SET @sortBy = 'id'
		SET @sortOrder = 'desc'

		SET @table = '( SELECT  TP.id
								,AM.agentName
								,CM.countryName
								,[PAYOUT_METHOD] = ISNULL(SM.typeTitle, ''ALL'')
								,[IS_ACTIVE] = CASE WHEN TP.isActive = 1 THEN ''YES'' ELSE ''NO'' END
								,[ScheduleEnabled] = CASE WHEN TP.ScheduleEnabled = 1 THEN ''YES'' ELSE ''NO'' END
								,TP.countryId
								,TP.agentId
								,TP.PaymentMethod
								,TP.MinTxnAmount
								,TP.MaxTxnAmount
						FROM TblPartnerwiseCountry TP(NOLOCK)
						INNER JOIN countryMaster CM(NOLOCK) ON CM.countryId = TP.CountryId
						INNER JOIN agentMaster AM(NOLOCK) ON AM.agentId = TP.AgentId
						LEFT JOIN serviceTypeMaster SM(NOLOCK) ON SM.serviceTypeId = TP.PaymentMethod '

		SET @sql_filter = ''
		SET @table = @table + ')x'
			
		IF @agentID <> 0
			SET @sql_filter = @sql_filter+' And agentId =  '''+CAST(@agentID AS VARCHAR)+''''

		IF @PaymentMethod <> 0
			SET @sql_filter = @sql_filter+' And PaymentMethod =  '''+CAST(@PaymentMethod AS VARCHAR)+''''

		IF @CountryId <> 0
			SET @sql_filter = @sql_filter+' And CountryId =  '''+CAST(@CountryId AS VARCHAR)+''''

		SET @select_field_list  = '
				 id,agentName,countryName,PAYOUT_METHOD,IS_ACTIVE,MinTxnAmount,MaxTxnAmount,ScheduleEnabled'
				 	
		EXEC dbo.proc_paging @table, @sql_filter, @select_field_list, @extra_field_list
				, @sortBy, @sortOrder, @pageSize, @pageNumber
	END
	ELSE IF @FLAG = 'country-list'
	BEGIN
		SELECT * FROM (
		SELECT '' [value], 'Select Country' [text]	UNION ALL

		SELECT CAST(CM.COUNTRYID AS VARCHAR) [value], CM.COUNTRYNAME [text] FROM COUNTRYMASTER CM(NOLOCK)
		INNER JOIN (
			SELECT DISTINCT COUNTRYID
			FROM TblPartnerwiseCountry (NOLOCK)
		)X ON X.COUNTRYID = CM.COUNTRYID
		) X 
		ORDER BY X.[value] ASC
	END
	ELSE IF @FLAG = 'payout-list'
	BEGIN
		SELECT * FROM (
		SELECT '' [value], 'Select Payout Method' [text]	UNION ALL

		SELECT CAST(serviceTypeId AS VARCHAR) [value], typeTitle [text]
		FROM serviceTypeMaster (NOLOCK) 
		WHERE ISNULL(isActive, 'Y') = 'Y'
		)X
		ORDER BY X.[value] ASC
	END
	ELSE IF @FLAG = 'agent-list'
	BEGIN
		SELECT * FROM (
		SELECT '' [value], 'Select Partner' [text]	UNION ALL

		SELECT CAST(AM.AGENTID AS VARCHAR) [value], AM.AGENTNAME [text] FROM AGENTMASTER AM(NOLOCK)
		INNER JOIN (
			SELECT DISTINCT AGENTID
			FROM TblPartnerwiseCountry (NOLOCK)
		)X ON X.AGENTID = AM.AGENTID
		) X 
		ORDER BY X.[value] ASC
	END
	ELSE IF @FLAG = 'enable-disable'
	BEGIN

		SELECT  @CountryId=CountryId,@PaymentMethod = PaymentMethod FROM TblPartnerwiseCountry (NOLOCK)WHERE id = @rowId

		IF @isActive <> 'YES' AND EXISTS(SELECT TOP 1 'A' FROM TblPartnerwiseCountry (NOLOCK) WHERE CountryId=@CountryId AND PaymentMethod = @PaymentMethod AND IsActive=1)
		BEGIN
			SELECT '1' ErrorCode , 'Service is already activated for this country,Please disable same service first and enable again.' Msg , @rowId id	 
			RETURN
		END
		IF @isActive = 'YES' 
		BEGIN
			update TblPartnerwiseCountry set  IsActive = 0 where id = @rowId

			SELECT '0' ErrorCode , 'Record has been disabled successfully.' Msg , @rowId id	 
		END
		ELSE
		BEGIN
			update TblPartnerwiseCountry set  IsActive = 1 where id = @rowId

			SELECT '0' ErrorCode , 'Record has been enabled successfully.' Msg , @rowId id	 
		END
	END
	ELSE IF @FLAG = 'SchedulerSetup'
	BEGIN

		IF EXISTS(SELECT TOP 1 'A' FROM TblPartnerwiseCountry (NOLOCK) WHERE id = @rowId AND ScheduleEnabled = 1)
		BEGIN
			UPDATE TblPartnerwiseCountry SET ScheduleEnabled = 0 WHERE id = @rowId
			SELECT '0' ErrorCode , 'Scheduler is set off to push transaction.' Msg , @rowId id	 
			RETURN
		END
		ELSE
		BEGIN
			UPDATE TblPartnerwiseCountry SET ScheduleEnabled = 1 WHERE id = @rowId
			SELECT '0' ErrorCode , 'Scheduler started to send transaction.' Msg , @rowId id	 
			RETURN
		END
	END
	ELSE IF @FLAG = 'payout-method'
	BEGIN
		SELECT SM.serviceTypeId, SM.typeTitle FROM countryReceivingMode CR(NOLOCK)
		INNER JOIN serviceTypeMaster SM(NOLOCK) ON SM.serviceTypeId = CR.receivingMode
		WHERE CR.countryId = @CountryId
	END
	ELSE IF @FLAG = 'partner'
	BEGIN
		SELECT agentId, agentName FROM agentMaster (NOLOCK) WHERE parentId = 0 AND agentId NOT IN (1001, 1008, 1009) 
		UNION ALL
		SELECT agentId, agentName FROM agentMaster (NOLOCK) WHERE agentId IN (1036,1056)
	END
END	

