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

/*
declare @complienceMessage varchar(1000)
 EXEC [proc_complianceRuleDetail]
@flag= 'sender-limit',@user= 'orji@gme.com',@sIdType= 'Passport',@sIdNo= 'A09506060'
,@cAmt= '900000.00',@cAmtUSD= '800',@customerId= '136509',@pCountryId= 151,@deliveryMethod= 1,@message= @complienceMessage OUTPUT
select @complienceMessage

*/
CREATE PROC [dbo].[proc_complianceRuleDetail]

	@flag				VARCHAR(30)		= 'core'
	,@user				VARCHAR(50)		
	,@pCountryId		INT
	,@deliveryMethod	INT	
	,@cAmt				MONEY			= NULL
	,@cAmtUSD			INT	
	,@customerId		VARCHAR(20)			
	,@receiverName		VARCHAR(150)	= NULL
	,@sIdNo				VARCHAR(50)     = NULL	
	,@sIdType			VARCHAR(50)		= NULL
	,@receiverMobile	VARCHAR(25)		= NULL
	,@message			VARCHAR(1000)	= NULL	OUTPUT
	,@shortMessage		VARCHAR(100)	= NULL	OUTPUT
	,@errCode			TINYINT			= NULL	OUTPUT
	,@ruleId			INT				= NULL	OUTPUT
AS
SET NOCOUNT ON
/*
	1> Get the data for per txn, monthly txn and yearly txn limit amount
	2> Check for per txn limit, and return with proper error message if limit amount exceeded
    3> Check for monthly txn limit, and return with proper error message if limit amount exceeded
    4> Check for yearly txn limit, and return with proper error message if limit amount exceeded
    5> Return success message and code if no issue with compliance rule
*/			
BEGIN TRY

    DECLARE @perTxnLimitAmt		MONEY			,@limitAmt			DECIMAL(10,2)
			,@comRuleId			INT				,@csMasterId		INT
			,@YearStart			DATE			,@YearEnd			DATE
			,@MonthStart		DATE			,@MonthEnd			DATE
			,@ruleType			CHAR(1)			,@customerType		VARCHAR(20)
			,@txnLimitCount		INT				,@txnCount			INT
			,@fdate				DATE 			,@tDate				DATE = GETDATE()+1
			,@period			INT

			,@comRuleId_ws		BIGINT			,@limitAmt_ws MONEY
			,@ruleType_ws CHAR(1)

	SELECT @YearStart	= DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
		   ,@YearEnd	= DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)
		   ,@MonthStart = DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
		   ,@MonthEnd	= DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
	
	SELECT @customerType = customerType 
		 FROM customerMaster(NOLOCK) 
	WHERE customerId = @customerId

	

	IF @flag = 'core'
	BEGIN
		IF OBJECT_ID('tempdb..#tempC') IS NOT NULL
		 DROP TABLE #tempC
		---- aggregate rule
		SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
					,condition		,trancount			,amount
					,period			,CD.nextAction AS nextAction	,csDetailId 
		INTO #tempC
		FROM dbo.csDetail CD(NOLOCK)
		INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId 
		WHERE CD.condition = 4600
		--AND ISNULL(CM.rCountry,'') =ISNULL(@pCountryId,ISNULL (CM.rCountry,''))		
		--AND ISNULL(CD.paymentMode,'') = ISNULL(@deliveryMethod,ISNULL(CD.paymentMode,''))		 
		AND ISNULL(CD.isActive, 'Y') = 'Y' 
		AND ISNULL(CD.isDeleted, 'N') = 'N'
		AND ISNULL(CD.isEnable, 'Y') = 'Y'
		AND ISNULL(CM.isActive, 'Y') = 'Y'
		AND ISNULL(CM.isDeleted, 'N') = 'N'
		order by period 
		 

		DECLARE	 @totalRowC		INT
		DECLARE  @counterC		INT = 1
		SELECT   @totalRowC		= COUNT('X') FROM #tempC
			
	    
		
		CREATE TABLE #tempTran(id BIGINT, tAmt MONEY, tAmtUSD MONEY, sIdType VARCHAR(50),sIdNo VARCHAR(50),approvedDate DATETIME,createdDate DATETIME,tranStatus VARCHAR(20))

		CREATE TABLE #tempTranR(id BIGINT, tAmt MONEY, tAmtUSD MONEY, approvedDate DATETIME, createdDate DATETIME, tranStatus VARCHAR(20),receiverName VARCHAR(15))
	
		DECLARE 
			  @sql		    VARCHAR(MAX)
			 ,@sumTxnAmt	MONEY
			 ,@sumTxnAmtUSD MONEY

		-- Get the record of 365 days into temp table
		INSERT INTO #tempTran(id,tAmt,tAmtUSD,sIdType,sIdNo,approvedDate,createdDate,tranStatus)
		SELECT r.id,r.tAmt, r.tAmt/(r.sCurrCostRate + ISNULL(r.sCurrHoMargin, 0)) ,s.idType,s.idNumber,r.approvedDate,r.createdDate,r.tranStatus 
		FROM vwRemitTran R(nolock)
		INNER JOIN vwtranSenders S(nolock) ON R.ID = S.tranId
		WHERE r.tranStatus <> 'Cancel'
		AND S.customerId = @customerId
		AND r.approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)
		
		
	  WHILE(@counterC <= @totalRowC)
	  BEGIN			
			SELECT  @limitAmt		=	amount
					,@period		=	ISNULL(PERIOD,0)
					,@ruleType		=	nextAction
					,@comRuleId_ws  =   csDetailId 
					,@comRuleId     =   csDetailId 
			FROM #tempC WHERE sno=@counterC
	

			SET @fdate = GETDATE()+1 - @period		
			SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END

			IF @customerType = '11048' ----EASY REMIT CUSTOMER 
			BEGIN			
				IF(SELECT dbo.CheckCustomerPerDayAmt(@customerId,@cAmt)) = 1
				BEGIN
				 SET @ruleType_ws = 'B'
				 SET @message = 'The transaction is 
								<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType_ws = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
								(<b>'+CAST(@sumTxnAmt AS VARCHAR)+' KRW</b>) and by doing this transaction (<b>'+CAST(@cAmt AS VARCHAR)+' KRW</b>)
								<b>per day transaction</b> Limit (<b>'+CAST(@limitAmt_ws AS VARCHAR)+' KRW</b>) is exceeded.'

						--SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
						SELECT @errCode = CASE WHEN @ruleType_ws = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId_ws, @shortMessage = 'Easy remit Per day limit exceeded(c).'
						RETURN
				END
			END

			
			/*per txn limit*/
			IF @cAmtUSD > @limitAmt AND @period = 0
			BEGIN
					SET @message = 'The transaction is 
							<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because the transaction 
							amount (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>), is exceeded as <b>per transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>).'

					SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = 'Per txn limit exceeded(c).'
					RETURN
			END	
	

			/*Per day txn limit */
			SELECT @sumTxnAmt = SUM(ISNULL(tAmt,0)),
					@sumTxnAmtUSD = SUM(ISNULL(tAmtUSD, 0))
			FROM #tempTran 
			WHERE approvedDate BETWEEN @fdate AND @tDate
	

			IF (ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt
			BEGIN
				SET @message = 'The transaction is 
							<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
							(<b>'+CAST(@sumTxnAmtUSD AS VARCHAR)+' USD</b>) and by doing this transaction (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>)
							<b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.'
			
				SET @shortMessage = 'Customer ' + CASE WHEN @period = 1 THEN 'Per day ' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' limit exceeded(c).'

					--SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
				SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
				RETURN
			 END

			SET @counterC = @counterC + 1 
		END	

	 IF ISNULL(@receiverName, '') = ''
	 BEGIN
		SELECT @errCode = 0, @message = 'Success', @ruleId = 0
		RETURN
	 END

		
		-- ## Start of multiple sender same Reciever txn limit
		INSERT INTO  #tempTranR(id,tAmt, tAmtUSD,receiverName,approvedDate,createdDate,tranStatus)
		SELECT rt.id,tAmt, tAmt/(sCurrCostRate + ISNULL(sCurrHoMargin, 0)),tr.firstName,approvedDate,createdDate,tranStatus 
		FROM vwRemitTran rt WITH(NOLOCK) 
		INNER JOIN dbo.vwTranReceivers tr WITH(NOLOCK) ON tr.tranId = rt.id 
		WHERE tr.fullName = @receiverName AND TR.mobile = @receiverMobile AND tranStatus <> 'CANCEL'		
		AND approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)
		
		IF OBJECT_ID('tempdb..#tempCR') IS NOT NULL
		 DROP TABLE #tempCR

		 SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
				,condition	,trancount		,amount
				,period		,nextAction		,csDetailId 
		INTO #tempCR	FROM dbo.csDetail CD(NOLOCK)
		INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId 
		WHERE CD.condition = 4603 
		AND ISNULL(CD.isActive, 'Y') = 'Y' 
		AND ISNULL(CD.isDeleted, 'N') = 'N'
		AND ISNULL(CD.isEnable, 'Y') = 'Y'
		AND ISNULL(CM.isActive, 'Y') = 'Y'
		AND ISNULL(CM.isDeleted, 'N') = 'N' 
		ORDER BY period 

		DECLARE	 @totalRowCR		INT
		DECLARE  @counterCR			INT = 1
		SELECT   @totalRowCR		= COUNT('X') FROM #tempCR
		
		WHILE(@counterCR<= @totalRowCR)
		BEGIN	
		 SELECT  @limitAmt		=	amount
				,@period		=	ISNULL(PERIOD,0)
				,@ruleType		=	nextAction
				,@txnLimitCount =	ISNULL(trancount,0) 
		FROM #tempCR WHERE sno=@counterCR
	
		SET @fdate = GETDATE()+1 - @period
		SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END

		/*per day limit*/
		SELECT @sumTxnAmt		= SUM(ISNULL(tAmt,0)),
			   @sumTxnAmtUSD	= SUM(ISNULL(tAmtUSD, 0)),
			   @txnCount		= COUNT('x')   
		FROM #tempTranR  
		WHERE approvedDate BETWEEN CAST(@fdate AS DATE) AND @tDate
		
		/*BASED ON TRANSACTION LIMIT*/
		IF ISNULL(@txnLimitCount,0) = 0 AND ((ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt)
		BEGIN
			SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
						<b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.(' + CAST((@sumTxnAmtUSD + @cAmtUSD) AS VARCHAR) + ' USD)'

			SET @shortMessage = 'Customer ' + CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' limit exceeded for same receiver(c).'

			SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
			RETURN
		END	

		/*BASED ON TRANSACTION COUNT*/

		IF @txnLimitCount >0 AND (ISNULL(@txnCount,0)) >= @txnLimitCount
		BEGIN
			SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
						<b>'''+CAST(@period AS VARCHAR)+''' day no of '''+CAST(@txnLimitCount AS VARCHAR)+''' transactions</b> Limit (<b>'+CAST(@txnCount AS VARCHAR)+' </b>) is exceeded.(' + CAST(@txnLimitCount as VARCHAr) + ' )'

			SET @shortMessage = CASE WHEN @period = 1 THEN 'Per ' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+'transactions limit exceeded for same receiver(c).'

			SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
			RETURN
		END
		
		SET @counterCR = @counterCR + 1

		END

		--Return success message if there is no complaince matched txn
		SELECT @errCode = 0, @message = 'Success', @ruleId = 0
		RETURN
	END
	ELSE IF @flag = 'sender-limit'
	BEGIN
		DECLARE @returnStatus BIT = 0
	     IF OBJECT_ID('tempdb..#temp') IS NOT NULL
		 DROP TABLE #temp
		SELECT *
		INTO #temp
		FROM(

			SELECT 	condition	,trancount	,amount
							,CD.period		,CD.nextAction AS nextAction	,csDetailId 
							,CASE WHEN DATEADD(DAY,- CD.period,@tDate) < @YearStart THEN @YearStart ELSE DATEADD(DAY,- CD.period,@tDate) END fdate
				FROM dbo.csDetail CD(NOLOCK)
				INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId 
				AND CD.condition = 4600
				AND ISNULL(CD.isActive, 'Y') = 'Y' 
				AND ISNULL(CD.isDeleted, 'N') = 'N'
				AND ISNULL(CD.isEnable, 'Y') = 'Y'
				AND ISNULL(CM.isActive, 'Y') = 'Y'
				AND ISNULL(CM.isDeleted, 'N') = 'N'
		)tb
		OUTER APPLY
		(SELECT SUM(R.tAmt) sumTxnAmt ,SUM(r.tAmt/(r.sCurrCostRate + ISNULL(r.sCurrHoMargin, 0)))sumTxnAmtUSD
			FROM vwRemitTran R(nolock)
			INNER JOIN vwtranSenders S(nolock) ON R.ID = S.tranId
			AND r.tranStatus <> 'Cancel'
			AND S.customerId = @customerId
			AND r.approvedDate BETWEEN tb.fdate AND @tDate)s

			IF @customerType = '11048' AND dbo.CheckCustomerPerDayAmt(@customerId,@cAmt) = 1
			BEGIN
				SELECT TOP 1 @limitAmt		=	amount
					,@period		=	ISNULL(PERIOD,0)
					,@ruleType		=	nextAction
					,@comRuleId_ws  =   csDetailId 
					,@comRuleId     =   csDetailId 
				FROM #temp t
				ORDER BY period

				 SET @ruleType_ws = 'B'
				 SET @message = 'The transaction is 
								<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType_ws = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
								(<b>'+CAST(@sumTxnAmt AS VARCHAR)+' KRW</b>) and by doing this transaction (<b>'+CAST(@cAmt AS VARCHAR)+' KRW</b>)
								<b>per day transaction</b> Limit (<b>'+CAST(@limitAmt_ws AS VARCHAR)+' KRW</b>) is exceeded.'
					
						--SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
						SELECT @errCode =  1 , @message = @message, @ruleId = @comRuleId_ws, @shortMessage = 'Easy remit Per day limit exceeded(c).'
						RETURN
			END
			--select * from #temp
			--IF EXISTS(SELECT TOP 1 1 FROM #temp T WHERE @cAmtUSD > T.amount AND T.period = 0)
			--BEGIN
					SELECT TOP 1 @limitAmt		=	amount
					,@period		=	ISNULL(PERIOD,0)
					,@ruleType		=	nextAction
					,@comRuleId_ws  =   csDetailId 
					,@comRuleId     =   csDetailId 
					,@returnStatus	=	1
					FROM #temp t
					WHERE @cAmtUSD > T.amount AND T.period = 0

					SET @message = 'The transaction is 
							<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because the transaction 
							amount (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>), is exceeded as <b>per transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>).'
					IF @returnStatus = 1
					BEGIN
							SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = 'Per txn limit exceeded(c).'
							RETURN
					END	
					SET @returnStatus = 0

			--IF EXISTS(SELECT TOP 1 1 FROM #temp T WHERE ISNULL(T.sumTxnAmt,0) + @cAmtUSD > T.amount AND T.period <> 0)
			--BEGIN
				SELECT TOP 1 @limitAmt		=	amount
					,@period		=	ISNULL(PERIOD,0)
					,@ruleType		=	nextAction
					,@comRuleId_ws  =   csDetailId 
					,@comRuleId     =   csDetailId
					,@sumTxnAmtUSD	=	ISNULL(T.sumTxnAmtUSD,0)
					,@returnStatus	=	1
				FROM #temp t
				WHERE ISNULL(T.sumTxnAmtUSD,0) + @cAmtUSD > T.amount AND t.period <> 0
				ORDER BY t.period

				SET @message = 'The transaction is 
							<b style=''background-color:red; color:white;''>'+CASE WHEN @ruleType = 'B' THEN 'blocked' ELSE 'hold' END+'</b> because previous transaction sum is
							(<b>'+CAST(@sumTxnAmtUSD AS VARCHAR)+' USD</b>) and by doing this transaction (<b>'+CAST(@cAmtUSD AS VARCHAR)+' USD</b>)
							<b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.'
				IF @returnStatus = 1
				BEGIN
						--SELECT @errCode = 1, @message = 'Daily txn limit exceeded.', @ruleId = @comRuleId
					SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = ''''+CAST(@period AS VARCHAR)+''' day limit exceeded(c).'
					RETURN
				 END	
		
		SELECT @errCode = 0, @message = 'Success', @ruleId = 0
		RETURN

	END
	ELSE IF @flag = 'receiver-limit'
	BEGIN
		CREATE TABLE #tempTransactionR(id BIGINT, tAmt MONEY, tAmtUSD MONEY, approvedDate DATETIME, createdDate DATETIME, tranStatus VARCHAR(20),receiverName VARCHAR(50))
			
		 IF OBJECT_ID('tempdb..#tempR') IS NOT NULL
		 DROP TABLE #tempR
		
		-- ## Start of multiple sender same Reciever txn limit
		INSERT INTO  #tempTransactionR(id,tAmt, tAmtUSD,receiverName,approvedDate,createdDate,tranStatus)
		SELECT rt.id,tAmt, tAmt/(sCurrCostRate + ISNULL(sCurrHoMargin, 0)),tr.firstName,approvedDate,createdDate,tranStatus 
		FROM vwRemitTran rt WITH(NOLOCK) 
		INNER JOIN dbo.vwTranReceivers tr WITH(NOLOCK) ON tr.tranId = rt.id 
		WHERE tr.fullName = @receiverName AND TR.mobile = @receiverMobile AND rt.tranStatus <> 'CANCEL' 
		AND approvedDate BETWEEN @YearStart AND CONVERT(VARCHAR,@YearEnd,101)


		SELECT ROW_NUMBER() OVER(ORDER BY period ASC) AS sno
				,condition	,trancount		,amount
				,period		,nextAction		,csDetailId 
		INTO #tempR	FROM dbo.csDetail CD(NOLOCK)
		INNER JOIN csMaster CM(NOLOCK) ON CM.csMasterId = CD.csMasterId 
		WHERE condition = 4603 
		--AND ISNULL(CM.rCountry,'') =ISNULL(@pCountryId,ISNULL (CM.rCountry,''))		
		--AND ISNULL(CD.paymentMode,'') = ISNULL(@deliveryMethod,ISNULL(CD.paymentMode,''))
		AND ISNULL(CD.isActive,'Y')='Y'
		AND ISNULL(CD.isDeleted, 'N') = 'N'
		AND ISNULL(CD.isEnable, 'Y') = 'Y'
		AND ISNULL(CM.isActive, 'Y') = 'Y'
		AND ISNULL(CM.isDeleted, 'N') = 'N' 
		ORDER BY period 

				
		DECLARE	 @totalRowR		INT
		DECLARE  @counterR		INT = 1
		SELECT   @totalRowR		= COUNT('X') FROM #tempR


	WHILE(@counterR<=@totalRowR)
	BEGIN	
		SELECT  @limitAmt	    =			amount
				,@period	    =			ISNULL(PERIOD,0)
				,@ruleType	    =			nextAction
				,@txnLimitCount =			ISNULL(trancount,0) 
				,@comRuleId     =			csDetailId
		FROM #tempR WHERE sno = @counterR 
	
		SET @fdate = GETDATE() + 1 - @period
		SET @fdate = CASE WHEN @fdate < @YearStart THEN @YearStart ELSE @fdate END
		/*per day limit*/
		SELECT @sumTxnAmt		= SUM(ISNULL(tAmt,0)),
				@sumTxnAmtUSD	= SUM(ISNULL(tAmtUSD, 0)),
				@txnCount		= COUNT('x') 
		FROM #tempTransactionR  
		WHERE approvedDate BETWEEN @fdate AND @tDate
		

		IF ISNULL(@txnLimitCount,0) = 0 AND ((ISNULL(@sumTxnAmtUSD,0) + @cAmtUSD) > @limitAmt)
		BEGIN
			SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
						<b>'''+CAST(@period AS VARCHAR)+''' day transaction</b> Limit (<b>'+CAST(@limitAmt AS VARCHAR)+' USD</b>) is exceeded.(' + CAST((@sumTxnAmtUSD + @cAmtUSD) AS VARCHAR) + ' USD)'

			SET @shortMessage = CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+'limit exceeded for same receiver(r).'

			SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
			RETURN
		END	

		IF @txnLimitCount >0 AND (ISNULL(@txnCount,0)) >= @txnLimitCount
		BEGIN
			SET @message = 'The transaction is in <b style=''background-color:red; color:white;''>hold</b> because same reciever
						<b>'''+CAST(@period AS VARCHAR)+''' day no of transactions</b> Limit (<b>'+CAST(@txnCount AS VARCHAR)+' </b>) is exceeded.(' + CAST(@txnLimitCount as VARCHAr) + ' )'

			SET @shortMessage = CASE WHEN @period = 1 THEN 'Per day' WHEN @period = 30 THEN 'Molthly ' WHEN @period = 365 THEN 'Yearly ' end+' no of transactions limit exceeded for same receiver(r).'

			SELECT @errCode = CASE WHEN @ruleType = 'B' THEN 1 ELSE 2 END, @message = @message, @ruleId = @comRuleId, @shortMessage = @shortMessage
			
			RETURN
		END

		SET @counterR = @counterR + 1
		END
		--Return success message if there is no complaince matched txn
		SELECT @errCode = 0, @message = 'Success', @ruleId = 0
		RETURN
	END
END TRY
BEGIN CATCH
	SET @message = ERROR_MESSAGE()
	SELECT @errCode = 1, @message = @message, @ruleId = ERROR_LINE()
END CATCH
