

ALTER PROC PROC_DYNAMIC_TABLE
(
	@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
	,@Password VARCHAR(40) = NULL
	,@ConfirmPassword VARCHAR(40) = NULL
	,@ipAddress VARCHAR(20) = NULL
	,@id BIGINT = NULL
	,@SchemeId VARCHAR(30) = NULL
)
AS;
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRY
	DECLARE @ErrorMsg VARCHAR(MAX)
	DECLARE @agentid INT = 394395
	IF @Flag = 'CustomerTxn'
	BEGIN
		SELECT TOP 3 Id
				, Amount = pAmt
				, Status = CASE WHEN payStatus = 'Paid' THEN 'Paid' WHEN tranStatus = 'Cancel' OR payStatus = 'Cancel' THEN 'Cancel' ELSE 'Processing' END
				, TransactionDay = DATEPART(DAY, createdDate)
				, TransactionMonth = DATENAME(MONTH, createdDate)
				, PaymentMethod
				, PBankName
				, PCurrency = payoutCurr
				, errorCode = 0
				, msg = 'Success'
		FROM remitTranTemp (NOLOCK)
	END
	ELSE IF @Flag = 'ForceChange'
	BEGIN
		DECLARE @OldPassword VARCHAR(40) = NULL

		SELECT @OldPassword = DBO.DECRYPTDB(customerPassword)
		FROM CUSTOMERMASTER (NOLOCK) 
		WHERE EMAIL = @UserEmail 
		AND isForcedPwdChange = 1

		IF ISNULL(@OldPassword, '') = ''
		BEGIN
			SELECT errorCode = 1, msg = 'Invalid customer!', id = NULL
			RETURN;
		END

		IF @OldPassword = @Password
		BEGIN
			SELECT errorCode = 1, msg = 'Old password and new password can not be same!', id = NULL
			RETURN;
		END

		UPDATE CUSTOMERMASTER 
			SET isForcedPwdChange = 0
				, customerPassword = dbo.fnaencryptstring(@Password)
		WHERE EMAIL = @UserEmail

		SELECT errorCode = 0, msg = 'Password changed successfully!', id = NULL
		RETURN;
	END
	ELSE IF @Flag = 'PayoutMethod'
	BEGIN
		DECLARE @PayoutCurrency VARCHAR(5), @tpExRate MONEY
		
		SELECT @tpExRate = detailDesc
		FROM staticDataValue (NOLOCK)
		WHERE typeId = '8109'
		AND detailTitle = @SchemeId

		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
				,tpExRate = @tpExRate
			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
				,tpExRate = @tpExRate
			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 = 'ONLINE', Text = 'Online Banking(Best Rate)', extra = 0, errorCode = 0, msg = 'Success' UNION ALL
		SELECT Id = 'EBANKING', Text = 'E-Banking/ (Good rate)', extra = 0, errorCode = 0, msg = 'Success' UNION ALL
		SELECT Id = 'DEBIT_CARD', Text = 'DEBIT CARD', extra = 0.15, errorCode = 0, msg = 'Success'
	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 = 'Txn-Detail'
	BEGIN
		SELECT ControlNo = dbo.decryptdb(rt.controlNo), rt.receivername, rt.camt, rt.id, rt.collCurr, tr.address, rt.createdDate, rt.paymentMethod
					, collMode = rt.depositType, rt.pbankname, rt.pbankBranchName, rt.accountNo, rt.purposeofremit
					, status = CASE WHEN payStatus = 'Paid' THEN 'Paid' WHEN tranStatus = 'Cancel' OR payStatus = 'Cancel' THEN 'Cancel' ELSE 'Processing' END
					, rt.pamt, rt.payoutCurr, rt.customerRate
					, PromotionPremiumRate = 0
					, rt.tamt, rt.collCurr, rt.servicecharge
					, PromotionDiscount = 0
					, rt.camt, rt.pCountry
		FROM remitTranTemp rt(NOLOCK)
		INNER JOIN tranReceiversTemp tr(NOLOCK) on tr.tranid = rt.id
		WHERE rt.id = @id
	END
END TRY
BEGIN CATCH
	SET @ErrorMsg = 'Internal Server Error: ' + ERROR_MESSAGE()
	SELECT errorCode = 1, msg = @ErrorMsg, id = NULL
END CATCH


