USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_PayAcDepositV3]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_PayAcDepositV3]
GO
/****** Object:  StoredProcedure [dbo].[proc_PayAcDepositV3]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[proc_PayAcDepositV3]
	 @flag			VARCHAR(50)
	,@pAgent		INT				= NULL
	,@mapCodeInt	VARCHAR(100)	= NULL
	,@tranIds		VARCHAR(MAX)	= NULL
	,@fromDate		VARCHAR(20)		= NULL
	,@toDate		VARCHAR(20)		= NULL
	,@user			VARCHAR(50)		= NULL
	,@fromTime		VARCHAR(20)		= NULL
	,@toTime		VARCHAR(20)		= NULL
	,@isHOPaid		CHAR(1)			= NULL
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE  @pAgentName varchar(200)
		,@pBranch int
		,@pBranchName varchar(200)
		,@pState varchar(200)
		,@pDistrict	varchar(200)
		,@pLocation	varchar(50)
		,@tranNos VARCHAR(MAX)
		,@sql VARCHAR(MAX)

	/*
		Exec proc_PayAcDepositV3 @flag='pendingList',@user='admin'
		Exec proc_PayAcDepositV3 @flag='pendingListDom',@mapCodeInt = '11300000',@user='dipesh'
		EXEC proc_PayAcDepositV3 @flag = 'domList', @user = 'dipesh', @bankId = '11300000'
	*/
	IF @fromTime IS NOT NULL
		SET @fromDate = @fromDate+' '+@fromTime
	ELSE 
		SET @fromDate = @fromDate+' 00:00:00'
	IF @toTime IS NOT NULL
		SET @toDate = @toDate+' '+@toTime 
	ELSE
		SET @toDate = @toDate+' 23:59:59'
		
	IF @flag = 'pending'
	BEGIN
		SET @sql =
		'SELECT
			 pAgent			= pAgent
			,pAgentName		= pAgentName
			,txnCount		= COUNT(*)
			,amt			= SUM(pAmt)
		FROM remitTran rt WITH(NOLOCK)
		WHERE 1=1'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
			AND paymentMethod in (''Bank Deposit'' ,''Relief Fund'')
			AND tranStatus = ''Payment'' 
			AND payStatus = ''Post'' 
			AND tranType = ''I''
			--AND ISNULL(expectedPayoutAgent,'''') <> ''iso''
			GROUP BY pAgent, pAgentName'

		EXEC(@sql)
		
		SET @sql= 'SELECT
			 pAgent			= rt.pBank
			,pAgentName		= rt.pBankName
			,txnCount		= COUNT(*)
			,amt			= SUM(rt.pAmt)
		FROM remitTran rt WITH(NOLOCK)
		INNER JOIN agentMaster am on rt.pBank=am.agentId
		WHERE 1=1  
		AND (am.agentGrp =''4301'' OR am.agentGrp IS NULL) 
		AND (am.agentType=''2903'' OR am.agentType=''2905'')
		'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
			AND paymentMethod = ''Bank Deposit'' 
			AND tranStatus = ''Payment'' 
			AND payStatus = ''Post'' 
			AND tranType = ''D''
			AND ISNULL(expectedPayoutAgent,'''') <> ''iso''
			GROUP BY pBank, pBankName'
		EXEC(@sql)


		SET @sql= 'SELECT
			 pAgent			= rt.pBank
			,pAgentName		= rt.pBankName
			,txnCount		= COUNT(*)
			,amt			= SUM(rt.pAmt)
		FROM remitTran rt WITH(NOLOCK)
		INNER JOIN agentMaster am on rt.pBank=am.agentId
		WHERE  am.agentGrp=''8026'' AND am.agentType IN(''2903'',''2904'')
		'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
			AND paymentMethod = ''Bank Deposit'' 
			AND tranStatus = ''Payment'' 
			AND payStatus = ''Post'' 
			AND tranType = ''D''
			AND ISNULL(expectedPayoutAgent,'''') <> ''iso''
			GROUP BY pBank, pBankName' 

		EXEC(@sql)
		RETURN
	END

	IF @flag = 'pendingIntl'				
	BEGIN
		SET @sql =
		'SELECT
			 [isApi]			= ''N''
			,rowId = ''''
			,[Control No]		= dbo.FNADecryptString(rt.controlNo)
			,[Tran No]			= rt.id
			,[Sending Country]	= rt.sCountry
			,[Sending Agent]	= rt.sAgentName
			,[Bank Name]		= rt.pBankName
			,[Branch Name]		= rt.pBankBranchName								
			,[Receiver Name]	= rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
			,[Bank A/C No]		= rt.accountNo
			,[Confirm Date]		= rt.approvedDate
			,[Payout Amount]	= rt.pAmt
			,[Unpaid Days]		= DATEDIFF(D,rt.approvedDate,GETDATE())
			
		FROM [dbo].remitTran rt WITH(NOLOCK)
		inner join tranReceivers rec with(nolock) on rt.id = rec.tranId
		WHERE 1=1'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
		AND pAgent = '''+CAST(@pAgent AS VARCHAR)+'''
		AND tranStatus = ''Payment''
		AND paymentMethod IN (''Bank Deposit'' ,''Relief Fund'')
		AND payStatus = ''Post'' 
		AND rt.sCountry <> ''Nepal''
		AND rt.tranType = ''I''
		--AND ISNULL(expectedPayoutAgent,'''') <> ''iso''
		ORDER BY [Unpaid Days] DESC'

		EXEC(@sql)
		RETURN
		
	END

	IF @flag = 'pendingDom'				
	BEGIN
		
		SET @sql =
		'SELECT
			 [Control No]		= dbo.FNADecryptString(rt.controlNo)
			,[Tran No]			= rt.id
			,[Sending Agent]	= rt.sAgentName
			,[Bank Name]		= rt.pBankName
			,[Branch Name]		= rt.pBankBranchName								
			,[Receiver Name]	= rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
			,[Bank A/C No]		= rt.accountNo
			,[Confirm Date]		= rt.approvedDate
			,[Payout Amount]	= rt.pAmt
			,[Unpaid Days]		= DATEDIFF(D,rt.approvedDate,GETDATE())
		FROM [dbo].remitTran rt WITH(NOLOCK)
		inner join tranReceivers rec with(nolock) on rt.id = rec.tranId
		WHERE 1=1'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
		AND rt.pBank = '''+CAST(@pAgent AS VARCHAR)+'''
		AND tranStatus = ''Payment''
		AND paymentMethod = ''Bank Deposit''
		AND payStatus = ''Post'' 
		AND rt.sCountry = ''Nepal''
		AND tranType = ''D''
		AND ISNULL(expectedPayoutAgent,'''') <> ''iso''
		ORDER BY [Unpaid Days] DESC'


	
		EXEC(@sql)
		RETURN
		
	END
	IF @flag = 'pendingCooperative'				
		BEGIN
		
			SET @sql =
			'SELECT
				 [Control No]		= dbo.FNADecryptString(rt.controlNo)
				,[Tran No]			= rt.id
				,[Sending Agent]	= rt.sAgentName
				,[Bank Name]		= rt.pBankName
				,[Branch Name]		= rt.pBankBranchName								
				,[Receiver Name]	= rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
				,[Bank A/C No]		= rt.accountNo
				,[Confirm Date]		= rt.approvedDate
				,[Payout Amount]	= rt.pAmt
				,[Unpaid Days]		= DATEDIFF(D,rt.approvedDate,GETDATE())
			FROM [dbo].remitTran rt WITH(NOLOCK)
			inner join tranReceivers rec with(nolock) on rt.id = rec.tranId
			INNER JOIN agentMaster am on rt.pBank=am.agentId
	       WHERE am.agentGrp=''8026'' AND am.agentType IN(''2903'',''2904'')					
			'
			IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
			SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
			SET @sql = @sql +'
			AND rt.pBank = '''+CAST(@pAgent AS VARCHAR)+'''
			AND tranStatus = ''Payment''
			AND paymentMethod = ''Bank Deposit''
			AND payStatus = ''Post'' 
			AND rt.sCountry = ''Nepal''
			AND tranType = ''D''
			ORDER BY [Unpaid Days] DESC'

			print @sql
			EXEC(@sql)
			RETURN
		
		END
	IF @flag = 'pendingDom'				
	BEGIN
		
		SET @sql =
		'SELECT
			 [Control No]		= dbo.FNADecryptString(rt.controlNo)
			,[Tran No]			= rt.id
			,[Sending Agent]	= rt.sAgentName
			,[Bank Name]		= rt.pBankName
			,[Branch Name]		= rt.pBankBranchName								
			,[Receiver Name]	= rec.firstName + ISNULL( '' '' + rec.middleName, '''') + ISNULL( '' '' + rec.lastName1, '''') + ISNULL( '' '' + rec.lastName2, '''')
			,[Bank A/C No]		= rt.accountNo
			,[Confirm Date]		= rt.approvedDate
			,[Payout Amount]	= rt.pAmt
			,[Unpaid Days]		= DATEDIFF(D,rt.approvedDate,GETDATE())
		FROM [dbo].remitTran rt WITH(NOLOCK)
		inner join tranReceivers rec with(nolock) on rt.id = rec.tranId
		WHERE 1=1'
		IF @fromDate IS NOT NULL AND @toDate IS NOT NULL
		SET @sql = @sql +' AND rt.postedDate between '''+@fromDate+''' and '''+@toDate+''''
		SET @sql = @sql +'
		AND pBank = '''+CAST(@pAgent AS VARCHAR)+'''
		AND tranStatus = ''Payment''
		AND paymentMethod = ''Bank Deposit''
		AND payStatus = ''Post'' 
		AND rt.sCountry = ''Nepal''
		AND tranType = ''D''
		ORDER BY [Unpaid Days] DESC'

		EXEC(@sql)
		RETURN
		
	END

	IF @flag = 'payIntl'
	BEGIN
		DECLARE @tranDetail TABLE(id INT IDENTITY(1,1), tranId VARCHAR(50), controlNo VARCHAR(50), sRouteId VARCHAR(5))
		SET @sql = 'SELECT id, controlNo, sRouteId FROM dbo.remitTran WITH(NOLOCK) WHERE id IN (' + @tranIds + ') 
		AND tranStatus = ''Payment'' 
		AND payStatus = ''Post''
		--AND ISNULL(expectedPayoutAgent,'''') <> ''iso'''
		
		INSERT INTO @tranDetail
		EXEC (@sql)

		IF NOT EXISTS(SELECT 'X' FROM @tranDetail)
		BEGIN
			EXEC proc_errorHandler 1, 'No Transaction Found.', NULL
			RETURN
		END
		
		SELECT  
		     @pAgent			= am.agentId
			,@pAgentName		= am.agentName
			,@pBranch			= bm.agentId
			,@pBranchName		= bm.agentName 
			,@pState			= bm.agentState
			,@pDistrict			= bm.agentDistrict
			,@pLocation			= bm.agentLocation
		FROM agentMaster am WITH(NOLOCK)
		LEFT JOIN agentMaster bm WITH(NOLOCK) ON am.agentId = bm.parentId AND bm.isHeadOffice = 'Y'
		WHERE am.agentId = @pAgent and isnull(bm.isDeleted,'N') = 'N' and bm.isActive = 'Y'
		
		IF @pBranch IS NULL
		BEGIN
			SELECT TOP 1
				 @pAgent			= am.agentId
				,@pAgentName		= am.agentName
				,@pBranch			= bm.agentId
				,@pBranchName		= bm.agentName 
				,@pState			= bm.agentState
				,@pDistrict			= bm.agentDistrict
				,@pLocation			= bm.agentLocation
			FROM agentMaster am WITH(NOLOCK)
			LEFT JOIN agentMaster bm WITH(NOLOCK) ON am.agentId = bm.parentId
			WHERE am.agentId = @pAgent and isnull(bm.isDeleted,'N') = 'N' and bm.isActive = 'Y'
		END

		--IF EXISTS(SELECT TOP 1 'A' FROM APPLICATIONUSERS(NOLOCK) WHERE UserName = @user and userType ='HO')
		IF @isHOPaid = 'Y'
		BEGIN
			SELECT @pAgentName = agentName,@pAgent = agentId,@pBranch = agentId,@pBranchName = agentName,@isHOPaid = 'Y'
			FROM AGENTMASTER(NOLOCK) WHERE agentId = 4680
		END	
		BEGIN TRAN
			UPDATE remitTran SET
				 pBranch					= @pBranch
				,pBranchName				= @pBranchName
				,pState						= @pState
				,pDistrict					= @pDistrict
				,pAgentComm					= case when rt.paymentMethod='Relief Fund' then 0 else (
												 SELECT amount FROM dbo.FNAGetPayComm(
														NULL
														,(SELECT countryId FROM countryMaster WITH(NOLOCK) WHERE countryName = rt.sCountry), NULL, 1002, 151, @pLocation, @pBranch, 'NPR'
														,2, rt.cAmt, rt.pAmt, rt.serviceCharge, NULL, NULL
													)
												)
												end
				,pAgentCommCurrency			= 'NPR'
				,pSuperAgentComm			= 0
				,pSuperAgentCommCurrency	= 'NPR'
				,tranStatus					= 'Paid'
				,payStatus					= 'Paid'
				,paidBy						= @user
				,paidDate					= dbo.FNAGetDateInNepalTZ()
				,paidDateLocal				= GETDATE()	
			FROM remitTran rt WITH(NOLOCK)
			INNER JOIN @tranDetail td on rt.id = td.tranId

			-- ## Update Accounting
			EXEC dbo.proc_payAcDepositAC
				 @flag				= 'payIntl'
				,@user				= @user
				,@tranIds			= @tranIds

			---- ## sending sms
			--insert into smsQueueAcDepositTxn(tranId)
			--select tranId from @tranDetail
				
			-- ## Queue Table for Data Integration
			INSERT INTO payQueue2(controlNo, pAgent, pAgentName, pBranch, pBranchName, paidBy, paidDate, paidBenIdType, paidBenIdNumber, routeId)
			SELECT controlNo, @pAgent, @pAgentName, @pBranch, @pBranchName, @user, dbo.FNAGetDateInNepalTZ(), NULL, NULL, sRouteId
			FROM @tranDetail WHERE sRouteId IS NOT NULL
						
		COMMIT TRAN
		
		EXEC proc_errorHandler 0, 'Transaction(s) paid successfully', NULL
		RETURN
	END
	IF @flag = 'payDom'
	BEGIN
		DECLARE @tranDomDetail TABLE(id INT IDENTITY(1,1),tranId VARCHAR(50),controlNo VARCHAR(50), controlNoEncInficare VARCHAR(20))
		DECLARE @sqlDom VARCHAR(MAX)
		SET @sqlDom = 'SELECT id, controlNo, controlNoEncInficare = dbo.encryptDbLocal(dbo.FNADecryptString(controlNo)) FROM dbo.remitTran WITH(NOLOCK) 
		
		WHERE id IN (' + @tranIds + ') 
		AND tranStatus = ''Payment'' 
		AND payStatus = ''Post'''
		INSERT INTO @tranDomDetail
		EXEC (@sqlDom)				
		IF NOT EXISTS(SELECT 'X' FROM @tranDomDetail)
		BEGIN
			EXEC proc_errorHandler 1, 'No Transaction Found.', NULL
			RETURN
		END	
		
		
		--IF EXISTS(SELECT TOP 1 'A' FROM APPLICATIONUSERS(NOLOCK) WHERE UserName = @user and userType ='HO')
		IF @isHOPaid = 'Y'
		BEGIN
			SELECT @pAgentName = agentName,@pAgent = agentId,@pBranch = agentId,@pBranchName = agentName,@isHOPaid = 'Y'
			FROM AGENTMASTER(NOLOCK) WHERE agentId = 4680
		END	
		BEGIN TRAN
		UPDATE remitTran SET
			 tranStatus					= 'Paid'
			,payStatus					= 'Paid'
			,pAgent						= CASE @isHOPaid WHEN 'Y' THEN @pAgent ELSE pBank END
			,pAgentName					= CASE @isHOPaid WHEN 'Y' THEN @pAgentName ELSE pBankName END
			,pBranch					= CASE @isHOPaid WHEN 'Y' THEN @pBranch ELSE pBankBranch END
			,pBranchName				= CASE @isHOPaid WHEN 'Y' THEN @pBranchName ELSE pBankBranchName END
			,pSuperAgentComm			= 0
			,pSuperAgentCommCurrency	= 'NPR'
			,pAgentComm					= 0
			,pAgentCommCurrency			= 'NPR'
			,paidBy						= @user
			,paidDate					= dbo.FNAGetDateInNepalTZ()
			,paidDateLocal				= GETDATE()
		FROM remitTran rt WITH(NOLOCK)
		INNER JOIN @tranDomDetail td ON rt.id = td.tranId

		-- ## Update Accounting
		EXEC dbo.proc_payAcDepositAC
			 @flag				= 'payDom'
			,@user				= @user
			,@tranIds			= @tranIds
		
		COMMIT TRAN		
		EXEC proc_errorHandler 0, 'Transaction(s) paid successfully', NULL		
	END

IF @flag = 'payCooperative'
	BEGIN
		DECLARE @tranDomDetailCo TABLE(id INT IDENTITY(1,1),tranId VARCHAR(50),controlNo VARCHAR(50), controlNoEncInficare VARCHAR(20),sBranch INT,pAmt MONEY,pBranch INT,pAgentComm MONEY  ,pSuperAgentComm MONEY)
		DECLARE @sqlDomCo VARCHAR(MAX)
		SET @sqlDomCo = 'SELECT id, controlNo, controlNoEncInficare = dbo.encryptDbLocal(dbo.FNADecryptString(controlNo)) ,sBranch,pAmt,pBranch	,0 pAgentComm,0	pSuperAgentComm
							FROM dbo.remitTran WITH(NOLOCK) WHERE id IN (' + @tranIds + ')
							AND tranStatus = ''Payment'' AND payStatus = ''Post'''
		INSERT INTO @tranDomDetailCo
		EXEC (@sqlDomCo)
				
		IF NOT EXISTS(SELECT 'X' FROM @tranDomDetailCo)
		BEGIN
			EXEC proc_errorHandler 1, 'No Transaction Found.', NULL
			RETURN
		END
		
	
		  UPDATE t SET 
			t.pAgentComm=ISNULL(X.pAgentComm, 0),
			t.pSuperAgentComm=ISNULL(X.psAgentComm, 0)
		  from  @tranDomDetailCo t
		  CROSS APPLY dbo.FNAGetDomesticPayComm(t.sBranch,@pAgent,2,t.pAmt)X

		--------------------------------------------------------------
		BEGIN TRAN
		UPDATE remitTran SET
			 tranStatus					= 'Paid'
			,payStatus					= 'Paid'
			,pAgent						= pBank
			,pAgentName					= pBankName
			,pBranch					= pBankBranch
			,pBranchName				= pBankBranchName
			,pSuperAgentComm			= td.pSuperAgentComm
			,pSuperAgentCommCurrency	= 'NPR'
			,pAgentComm					= td.pAgentComm
			,pAgentCommCurrency			= 'NPR'
			,paidBy						= @user
			,paidDate					= dbo.FNAGetDateInNepalTZ()
			,paidDateLocal				= GETDATE()
		FROM remitTran rt WITH(NOLOCK)
		INNER JOIN @tranDomDetailCo td ON rt.id = td.tranId

		-- ## Update Accounting
		EXEC dbo.proc_payAcDepositAC
			 @flag				= 'payCooperative'
			,@user				= @user
			,@tranIds			= @tranIds
		
		COMMIT TRAN		
		EXEC proc_errorHandler 0, 'Transaction(s) paid successfully', NULL		
	END




GO
