USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_payTxnValidation]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_payTxnValidation]
GO
/****** Object:  StoredProcedure [dbo].[proc_payTxnValidation]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_payTxnValidation](
		 @flag				VARCHAR(1)			
		,@user			    VARCHAR(50) 			
	    ,@rowId				BIGINT			    = NULL
		,@controlNo			VARCHAR(50)		    = NULL
		,@partnerId         VARCHAR(30)			= NULL
		,@pBranchId			VARCHAR(50)			= NULL
)
AS

DECLARE 
	@mapCodeDom VARCHAR(50)
	,@tranStatus VARCHAR(50)
	,@tranId INT
	,@payStatus VARCHAR(50)
	,@controlNoEncrypted VARCHAR(50)
	,@agentType VARCHAR(50)
	,@pTxnLocation VARCHAR(50)
	,@pAgentLocation VARCHAR(50)
	,@pAgent VARCHAR(50)
	,@paymentMethod VARCHAR(50)
	,@sBranchId VARCHAR(50)
	,@branchName VARCHAR(200)
	,@mapCodeInt VARCHAR(50)
	,@lockStatus VARCHAR(50)
	,@userAgentId INT
	,@complianceHoldPay CHAR(1)
	   
	SET @controlNo = UPPER(@controlNo)
	SET @controlNoEncrypted = DBO.FNAEncryptString(LTRIM(RTRIM(@controlNo))
IF @flag = 'S'
BEGIN
	IF (@partnerId='IME-D')    
	BEGIN 		
	
		IF @user IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Your session has expired. Please relogin to the system.', @controlNo
			RETURN
		END
		SELECT @userAgentId = agentId 
			from applicationUsers with(nolock) where userName = @user

		IF @userAgentId <> 1001
		BEGIN
			DECLARE @hasRight CHAR(1), @payDomesticFunctionId VARCHAR(50)
			SET @payDomesticFunctionId = '40101311,40101520'
			SELECT @hasRight = dbo.FNAHasRight(@user, @payDomesticFunctionId)
			
			IF(@hasRight = 'N')
			BEGIN
				EXEC proc_errorHandler 1, 'You are not authorized to pay domestic transaction.', NULL
				RETURN
			END
		END
		
		DECLARE @settlingAgent INT
		
		IF EXISTS(SELECT 'A' FROM dbo.agentMaster WHERE agentId = @userAgentId AND agentType = 2903)
		BEGIN
		    SET @settlingAgent = @userAgentId
		END

		ELSE
        BEGIN
            SELECT @settlingAgent = parentId FROM dbo.agentMaster WHERE agentId = @userAgentId
        END

		DECLARE @limitBal MONEY
		
		SET @limitBal = [dbo].FNAGetLimitBal(@settlingAgent)

		IF NOT EXISTS(SELECT 'A' FROM dbo.creditlimit (NOLOCK) WHERE agentId = @settlingAgent) AND ISNULL(@limitBal, 0) = 0
		BEGIN
		    SELECT 1 errorCode,'Credit limit not set for receiving agent, please contact HO.' msg,NULL id,'creditLimit' vtype
			RETURN
		END

		IF @pBranchId IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Please Choose Paying Agent', NULL
			RETURN
		END
		SELECT 
			@mapCodeDom = mapCodeDom,
			@agentType = agentType, 
			@pAgentLocation = agentLocation,
			@branchName = agentName			 
		FROM agentMaster WITH(NOLOCK) WHERE agentId = @pBranchId

		IF (@mapCodeDom IS NULL OR @mapCodeDom = '' OR @mapCodeDom = 0)
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid Map Code', NULL
			RETURN
		END
	
		SELECT 
			@tranStatus = tranStatus, 
			@tranId = id,
			@payStatus = payStatus ,
			@controlNoEncrypted = rt.controlNo,
			@paymentMethod = paymentMethod,
			@sBranchId = sBranch,
			@pTxnLocation = pLocation,
			@complianceHoldPay =  CASE WHEN ISNULL(tc.controlNo ,'')='' THEN 'N' 
										ELSE 'Y' 
									END
		FROM remitTran rt WITH(NOLOCK) 
		LEFT JOIN tranPayCompliance tc WITH(NOLOCK)
		ON rt.controlNo = tc.controlNo
		WHERE rt.controlNo  = @controlNoEncrypted
		
		IF @tranStatus IS NULL
		BEGIN
			EXEC proc_errorHandler 1000, 'Transaction not found', NULL
			RETURN
		END

		IF @agentType = 2903		
		BEGIN
			SET @pAgent = @pBranchId
		END
		
		INSERT INTO tranViewHistory(
			 controlNumber
			,tranViewType
			,agentId
			,createdBy
			,createdDate
			,tranId
		)
		SELECT
			 @controlNoEncrypted
			,'PAY'
			,@pBranchId
			,@user
			,GETDATE()
			,@tranId
				
		IF @paymentMethod = 'Bank Deposit'
		BEGIN
			EXEC proc_errorHandler 1, 'Cannot process payment for Payment Type Bank Deposit', NULL
			RETURN	
		END
		IF @sBranchId = @pBranchId
		BEGIN
			EXEC proc_errorHandler 1, 'Cannot process payment for same POS', @tranId
			RETURN
		END
		
		IF (@tranStatus = 'CancelRequest')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has been requested for cancel', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Lock' )
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is locked', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Block')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is blocked. Please Contact HO', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@payStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Hold')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is hold', @controlNoEncrypted
			RETURN
		END
		IF @tranStatus IN ('Hold','OFAC Hold','Compliance Hold','OFAC/Compliance Hold','Compliance Hold Pay')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is hold', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Cancel')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is cancelled', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus <> 'Payment')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is not in authorized mode', @controlNoEncrypted
			RETURN
		END


		DECLARE @tranDistrictId INT, @payAgentDistrictId INT			
		SELECT @payAgentDistrictId = districtId FROM apiLocationMapping WHERE apiDistrictCode = @pAgentLocation
		SELECT @tranDistrictId = districtId FROM apiLocationMapping WHERE apiDistrictCode = @pTxnLocation
		IF @payAgentDistrictId IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Location not found. Please Contact HO', @controlNo
			RETURN
		END
		IF @tranDistrictId IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Location not found. Please Contact HO', @controlNo
			RETURN
		END
		IF(@tranDistrictId <> @payAgentDistrictId)
		BEGIN
			EXEC proc_errorHandler 1, 'You are not allowed to pay this TXN. It is not within your district.', @controlNoEncrypted
			RETURN
		END
		IF @complianceHoldPay = 'Y'
		BEGIN
			EXEC proc_errorHandler 101, 'Transaction Verification Successful', @tranId
		END

		EXEC proc_errorHandler 0, 'Transaction Verification Successful', @tranId

	END
	IF (@partnerId='IME-I')  
	BEGIN 
		IF @pBranchId IS NULL
		BEGIN
			EXEC proc_errorHandler 1, 'Please Choose Agent', NULL
			RETURN
		END

		SELECT 
		     @mapCodeInt = mapCodeInt
			,@agentType = agentType
			,@pAgentLocation = agentLocation 
		FROM agentMaster WITH(NOLOCK) WHERE agentId = @pBranchId

		IF (@mapCodeInt IS NULL OR @mapCodeInt = '' OR @mapCodeInt = 0)
		BEGIN
			EXEC proc_errorHandler 1, 'Invalid Map Code', NULL
			RETURN
		END
	
		SELECT 
			  @tranStatus = tranStatus
			, @tranId = id 
			, @lockStatus = lockStatus
			, @payStatus = payStatus
			, @sBranchId = sBranch
			, @paymentMethod = paymentMethod
			, @complianceHoldPay =  CASE WHEN ISNULL(tc.controlNo ,'')='' THEN 'N' 
										ELSE 'Y' 
									END
		FROM remitTran rt WITH(NOLOCK) 
		LEFT JOIN tranPayCompliance tc WITH(NOLOCK)
		ON rt.controlNo = tc.controlNo
		WHERE rt.controlNo = @controlNoEncrypted

		IF @tranStatus IS NULL
		BEGIN
			EXEC proc_errorHandler 1000, 'Transaction not found', NULL
			RETURN
		END

		IF @agentType = 2903	
		BEGIN
			SET @pAgent = @pBranchId
		END

		INSERT INTO tranViewHistory(
			 controlNumber
			,tranViewType
			,agentId
			,createdBy
			,createdDate
			,tranId
		)
		SELECT
			 @controlNoEncrypted
			,'PAY'
			,@pBranchId
			,@user
			,GETDATE()
			,@tranId
		
		IF @paymentMethod = 'Bank Deposit'
		BEGIN
			EXEC proc_errorHandler 1, 'Cannot process payment for Payment Type Bank Deposit', NULL
			RETURN	
		END
		IF @sBranchId = @pBranchId
		BEGIN
			EXEC proc_errorHandler 1, 'Cannot process payment for same POS', @tranId
			RETURN
		END
		
		IF (@tranStatus = 'CancelRequest')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has been requested for cancel', @controlNoEncrypted
			RETURN
		END
		IF (@lockStatus = 'Lock' )
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is locked', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Lock' )
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is locked', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Block')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is blocked. Please Contact HO', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END
		IF (@payStatus = 'Paid')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction has already been paid', @controlNoEncrypted
			RETURN
		END

		IF (@tranStatus = 'Compliance')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is in Compliance !!!', @controlNoEncrypted
			RETURN
		END	

		IF (@tranStatus = 'Hold')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is hold', @controlNoEncrypted
			RETURN
		END
		IF @tranStatus IN ('Hold','OFAC Hold','Compliance Hold','OFAC/Compliance Hold','Compliance Hold Pay')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is hold', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus = 'Cancel')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is cancelled', @controlNoEncrypted
			RETURN
		END
		IF (@tranStatus <> 'Payment')
		BEGIN
			EXEC proc_errorHandler 1, 'Transaction is not in authorized mode', @controlNoEncrypted
			RETURN
		END
		IF @complianceHoldPay = 'Y'
		BEGIN
			EXEC proc_errorHandler 101, 'Transaction Verification Successful', @tranId
		END
		EXEC proc_errorHandler 0, 'Transaction Verification Successful', @tranId
	END
END



GO
