USE [FastMoneyPro_Remit]
GO

--EXEC proc_settlement_v2   @flag = 'S', @user = 'admin', @pCountry = 'SOUTH KOREA', @sAgent = NULL, @sBranch = null, @fromDate = '09/10/2017', @toDate = '01/15/2018', @pageNumber = '1', @pageSize = '100'

ALTER PROCEDURE [dbo].[proc_settlement_v2]
	@flag				VARCHAR(20) = NULL,
	@pCountry			VARCHAR(20)	= NULL,
	@sAgent				VARCHAR(50),
	@sBranch			VARCHAR(20)	= NULL,
	@fromDate			VARCHAR(30) = NULL,
	@toDate				VARCHAR(50)	= NULL,
	@pageNumber			VARCHAR(50)	= NULL,
	@pageSize			VARCHAR(50)	= NULL,
	@user				VARCHAR(50) = NULL

AS 
SET NOCOUNT ON;
SET ANSI_NULLS ON;

IF @pCountry = 'All'
	SET @pCountry = NULL


IF @flag='s'
BEGIN
		IF OBJECT_ID(N'tempdb..#SETTLEMENT') IS NOT NULL
		DROP TABLE #SETTLEMENT

		declare @sql varchar(max),@fxGain varchar(1),@agentCountry varchar(200),@isAgentUUM char(1) = null,@cutOffDate VARCHAR(20) = '2014-10-20'

		SELECT   id,controlNo,holdTranId
				,sCurrCostRate,sCurrHoMargin,sCurrAgentMargin
				,pCurrCostRate,pCurrHoMargin,pCurrAgentMargin
				,customerRate,sAgentSettRate
				,pDateCostRate,agentFxGain
				,serviceCharge = CASE WHEN PAGENT = 221227 THEN 0 ELSE serviceCharge END 
				,handlingFee,sAgentComm,sAgentCommCurrency,pAgentComm,pAgentCommCurrency
				,senderName,receiverName,sCountry,sAgent,sAgentName,sBranch,sBranchName
				,pCountry ,pAgent,pAgentName,pBranch,pBranchName
				,collCurr,tAmt,cAmt,pAmt,payoutCurr,tranStatus,payStatus
				,createdDate
				,approvedDate = CAST(approvedDate AS DATE)
				,paidDate
				,cancelApprovedDate = CAST(cancelApprovedDate AS DATE)
		INTO #SETTLEMENT
		FROM remitTran WITH (NOLOCK) 
		WHERE 1=1 
		AND 
		( 
				(SAGENT=ISNULL(@sAgent, SAGENT) AND createdDate BETWEEN @fromDate AND @toDate + ' 23:59:59' AND sCountry = isnull(@pCountry,sCountry) AND sBranch = isnull(@sBranch,sBranch))
			OR  (SAGENT=ISNULL(@sAgent, SAGENT) AND CANCELAPPROVEDDATE BETWEEN @fromDate AND @toDate + ' 23:59:59' AND sCountry = isnull(@pCountry,sCountry) AND sBranch = isnull(@sBranch,sBranch))
			OR  (PAGENT=ISNULL(@sAgent, PAGENT) AND PAIDDATE BETWEEN @fromDate AND @toDate + ' 23:59:59' AND pCountry  = isnull(@pCountry,pCountry) AND pBranch = isnull(@sBranch,pBranch))
		) 

		CREATE TABLE #SETTLEMENT_MAIN
		(
			REMARKS VARCHAR(100)  NULL,
			[DATE]  DATE  NULL,
			TOTTRAN INT NULL,
			CURR	VARCHAR(10) NULL,
			LCYCA	MONEY NULL,
			LCYSC	MONEY NULL,
			LCYTA	MONEY NULL,
			LCYCOMM MONEY NULL,
			LCYFX	MONEY NULL,
			LCYSETTL MONEY NULL,
			USDCA	MONEY NULL,
			USDSC	MONEY NULL,
			USDTA	MONEY NULL,
			USDCOMM MONEY NULL,
			USDSETTL MONEY NULL,
			SUMPA	MONEY NULL
		)
		
	
		--INSERT INTO #SETTLEMENT_MAIN(REMARKS,[DATE],TOTTRAN,LCYCA,LCYSC,LCYTA,LCYCOMM,LCYFX,LCYSETTL,USDCA,USDSC,USDTA,USDCOMM,USDSETTL,SUMPA)
			SELECT * FROM 
			(
				SELECT 
					 [Remarks] = '<a href="Reports.aspx?reportName=irhSettDrilDwn&sAgent='+isnull(@sAgent, '')+'&pCountry='+
					 ISNULL(@pCountry,'')+'&sBranch='+ISNULL(CAST(@sBranch AS VARCHAR),'')+'&fromDate='+
					 convert(varchar,createdDate,101)+'&toDate='+convert(varchar,createdDate,101)+'&flag=Send"> '+ 'Remittance Send(+)' +' </a>' 
					,[DATE]											= convert(varchar,createdDate,101)
					,[Total Trans]									= COUNT(id) 
					,[IN COLLECTION CURRENCY_Total Collection]		= SUM(cAmt) 
					,[IN COLLECTION CURRENCY_Total Charge]			= SUM(ISNULL(serviceCharge,0)) 
					,[IN COLLECTION CURRENCY_Principal Amount]		= SUM(tAmt)
					,[IN COLLECTION CURRENCY_Agent<br /> Commission]= SUM(sAgentComm)
					,[IN COLLECTION CURRENCY_Settlement Amount]		= SUM(tAmt + ISNULL(sAgentComm,0))
					,[IN COLLECTION CURRENCY_Fx. Gain]				= SUM(agentFxGain)					
					,[IN USD_Total Collection]						= SUM(cAmt/(sCurrCostRate+isnull(sCurrHoMargin,0)))														
					,[IN USD_Total Charge]							= SUM(serviceCharge/sCurrCostRate)
					,[IN USD_Principal Amount]						= SUM(tAmt/(sCurrCostRate+isnull(sCurrHoMargin,0)))
					,[IN USD_Agent <br /> Commission]				= SUM(sAgentComm/sCurrCostRate)
					,[IN USD_Settlement Amount]						= SUM((cAmt-sAgentComm)/(sCurrCostRate+isnull(sCurrHoMargin,0)))
					,[IN Paying_Principal Amount]					= SUM(pAmt)
			
				FROM #SETTLEMENT
				WHERE SAGENT = ISNULL(@sAgent, SAGENT) AND createdDate BETWEEN @fromDate AND @toDate + ' 23:59:59'
				GROUP BY convert(varchar,createdDate,101)

				UNION ALL

				SELECT 
					 [Remarks]	='<a href="Reports.aspx?reportName=irhSettDrilDwn&sAgent='+isnull(@sAgent, '')+'&pCountry='+
					 ISNULL(@pCountry,'')+'&sBranch='+ISNULL(CAST(@sBranch AS VARCHAR),'')+'&fromDate='+
					 convert(varchar,cancelApprovedDate,101)+'&toDate='+convert(varchar,cancelApprovedDate,101)+'&flag=cancel"> '+ 'Remittance Cancel(-)'+' </a>'			 
					,[DATE]											= convert(varchar,cancelApprovedDate,101)
					,[Nos]											= COUNT(id) * -1
					,[IN COLLECTION CURRENCY_Total Collection]		= SUM(cAmt) * -1
					,[IN COLLECTION CURRENCY_Total Charge]			= SUM(ISNULL(serviceCharge,0)) * -1
					,[IN COLLECTION CURRENCY_Principal Amount]		= SUM(tAmt)* -1
					,[IN COLLECTION CURRENCY_Agent<br /> Commission]= SUM(sAgentComm)* -1
					,[IN COLLECTION CURRENCY_Settlement Amount]		= SUM(tAmt + ISNULL(sAgentComm,0))* -1
					,[IN COLLECTION CURRENCY_Fx. Gain]				= SUM(agentFxGain)* -1
					,[IN USD_Total Collection]						= SUM(cAmt/(sCurrCostRate+isnull(sCurrHoMargin,0)))* -1														
					,[IN USD_Total Charge]							= SUM(serviceCharge/(sCurrCostRate+isnull(sCurrHoMargin,0)))* -1
					,[IN USD_Principal Amount]						= SUM(tAmt/(sCurrCostRate+isnull(sCurrHoMargin,0)))* -1
					,[IN USD_Agent <br /> Commission]				= SUM(sAgentComm/(sCurrCostRate+isnull(sCurrHoMargin,0)))* -1
					,[IN USD_Sett. Amount]							= SUM((cAmt-sAgentComm)/(sCurrCostRate+isnull(sCurrHoMargin,0)))* -1
					,[IN Paying_Principal Amount]					= SUM(pAmt)* -1
					FROM #SETTLEMENT
					WHERE 
						SAGENT = ISNULL(@sAgent, SAGENT)  
						AND cancelApprovedDate BETWEEN @fromDate AND @toDate + ' 23:59:59'
					GROUP BY  convert(varchar,cancelApprovedDate,101)
			) x ORDER BY CAST([DATE] AS DATE)
 END	

ELSE IF @flag='a'
BEGIN
	CREATE TABLE #PartnerResult([Remarks] VARCHAR(50),[DATE] VARCHAR(500),[Total Trans] INT,[IN COLLECTION CURRENCY_Total Collection] MONEY
	,[IN COLLECTION CURRENCY_Total Charge] MONEY,[IN COLLECTION CURRENCY_Principal Amount] MONEY,[IN COLLECTION CURRENCY_Agent<br /> Commission] MONEY
	,[IN COLLECTION CURRENCY_Settlement Amount] MONEY,[IN USD_Principal Amount] MONEY,[IN USD_Agent Comm] MONEY,[IN USD_Settlement Amount] MONEY
	,[IN Paying_Principal Amount] MONEY)
	
	----#### ONLY FOR PULLING TRANSACTION PARTNER
	IF @sAgent IN (1036,405488) 
	BEGIN
			INSERT INTO #PartnerResult
			SELECT 
				[Remarks] = 'Post Transactions(+)' 
				,[DATE] = '<a href="Reports.aspx?reportName=irhSettDrilDwn&sAgent='+@sAgent+'&pCountry='+ISNULL(@pCountry,'')
					+'&sBranch='+ISNULL(CAST(@sBranch AS VARCHAR),'')+'&fromDate='+convert(varchar,approvedDate,101)+'&toDate='
					+convert(varchar,approvedDate,101)+'&flag=Post"> '+convert(varchar,approvedDate,101)+' </a>' 
				--,[DATE]											= convert(varchar,postedDate,101)
				,[Total Trans]									= COUNT(id) 
				,[IN COLLECTION CURRENCY_Total Collection]		= SUM(cAmt) 
				,[IN COLLECTION CURRENCY_Total Charge]			= SUM(ISNULL(serviceCharge,0)) 
				,[IN COLLECTION CURRENCY_Principal Amount]		= SUM(tAmt)
				,[IN COLLECTION CURRENCY_Agent<br /> Commission]= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)
				,[IN COLLECTION CURRENCY_Settlement Amount]		= 0 ----SUM(tAmt + case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)
				,[IN USD_Principal Amount]						= SUM(pAmt/pCurrCostRate)														
				,[IN USD_Agent Comm]							= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/pCurrCostRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0) else ISNULL(pAgentComm,0)/(sCurrCostRate+isnull(sCurrHoMargin,0)) end)
				,[IN USD_Settlement Amount]						= 0 ----SUM(pAmt/pCurrCostRate)+SUM(ISNULL(pAgentComm,0)/case when pagentcommcurrency='USD' then 1 else pCurrCostRate end)
				,[IN Paying_Principal Amount]					= SUM(pAmt)
			
			FROM remitTran WITH (NOLOCK) 
			WHERE 1=1 AND pAgent = @sAgent --AND pCountry = @pCountry
			AND postedDate BETWEEN @fromDate AND @toDate+' 23:59:59'
			GROUP BY  CONVERT(varchar,approvedDate,101)
	 END
	 ELSE
	 BEGIN
			INSERT INTO #PartnerResult
			SELECT 
					 [Remarks] = 'Post Transactions(+)' 
					,[DATE] = '<a href="Reports.aspx?reportName=irhSettDrilDwn&sAgent='+@sAgent+'&pCountry='+ISNULL(@pCountry,'')
					 +'&sBranch='+ISNULL(CAST(@sBranch AS VARCHAR),'')+'&fromDate='+convert(varchar,approvedDate,101)+'&toDate='
					 +convert(varchar,approvedDate,101)+'&flag=Post"> '+convert(varchar,approvedDate,101)+' </a>' 
					--,[DATE]											= convert(varchar,postedDate,101)
					,[Total Trans]									= COUNT(id) 
					,[IN COLLECTION CURRENCY_Total Collection]		= SUM(cAmt) 
					,[IN COLLECTION CURRENCY_Total Charge]			= SUM(ISNULL(serviceCharge,0)) 
					,[IN COLLECTION CURRENCY_Principal Amount]		= SUM(tAmt)
					,[IN COLLECTION CURRENCY_Agent<br /> Commission]= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)
					,[IN COLLECTION CURRENCY_Settlement Amount]		= 0 ----SUM(tAmt + case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)
					,[IN USD_Principal Amount]						= SUM(pAmt/pCurrCostRate)														
					,[IN USD_Agent Comm]							= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/pCurrCostRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0) else ISNULL(pAgentComm,0)/(sCurrCostRate+isnull(sCurrHoMargin,0)) end)
					,[IN USD_Settlement Amount]						= 0 ----SUM(pAmt/pCurrCostRate)+SUM(ISNULL(pAgentComm,0)/case when pagentcommcurrency='USD' then 1 else pCurrCostRate end)
					,[IN Paying_Principal Amount]					= SUM(pAmt)
			
				FROM remitTran WITH (NOLOCK) 
				WHERE 1=1 AND pAgent = @sAgent --AND pCountry = @pCountry
				AND approvedDate BETWEEN @fromDate AND @toDate+' 23:59:59'
				GROUP BY  CONVERT(varchar,approvedDate,101)
	END

				INSERT INTO #PartnerResult
				SELECT 
					 [Remarks] = 'Reverse Transactions(-)' 
					,[DATE] = '<a href="Reports.aspx?reportName=irhSettDrilDwn&sAgent='+@sAgent+'&pCountry='+ISNULL(@pCountry,'')
					 +'&sBranch='+ISNULL(CAST(@sBranch AS VARCHAR),'')+'&fromDate='+convert(varchar,cancelapprovedDate,101)+'&toDate='
					 +convert(varchar,cancelapprovedDate,101)+'&flag=CANCEL"> '+convert(varchar,cancelapprovedDate,101)+' </a>' 
					--,[DATE]											= convert(varchar,cancelapprovedDate,101)
					,[Total Trans]									= COUNT(id)*-1
					,[IN COLLECTION CURRENCY_Total Collection]		= SUM(cAmt)*-1 
					,[IN COLLECTION CURRENCY_Total Charge]			= SUM(ISNULL(serviceCharge,0)) *-1
					,[IN COLLECTION CURRENCY_Principal Amount]		= SUM(tAmt)*-1
					,[IN COLLECTION CURRENCY_Agent<br /> Commission]= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)*-1
					,[IN COLLECTION CURRENCY_Settlement Amount]		= 0 ----SUM(tAmt + case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/customerRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0)*(sCurrCostRate+isnull(sCurrHoMargin,0)) else ISNULL(pAgentComm,0) end)*-1
					,[IN USD_Principal Amount]						= SUM(pAmt/pCurrCostRate)	*-1													
					,[IN USD_Agent Comm]							= SUM(case when pagentcommcurrency='LKR' then ISNULL(pAgentComm,0)/pCurrCostRate when pagentcommcurrency='USD' then ISNULL(pAgentComm,0) else ISNULL(pAgentComm,0)/(sCurrCostRate+isnull(sCurrHoMargin,0)) end)*-1
					,[IN USD_Settlement Amount]						= 0 ----SUM(pAmt/pCurrCostRate)+SUM(ISNULL(pAgentComm,0)/case when pagentcommcurrency='USD' then 1 else pCurrCostRate end)*-1
					,[IN Paying_Principal Amount]					= SUM(pAmt)*-1
			
				FROM remitTran WITH (NOLOCK) 
				WHERE 1=1 AND pAgent = @sAgent  --AND pCountry = @pCountry
				AND cancelapprovedDate BETWEEN @fromDate AND @toDate+' 23:59:59'
				GROUP BY  CONVERT(varchar,cancelapprovedDate,101)
			----) x order by [Remarks],[DATE]
			
			UPDATE #PartnerResult 
				SET [IN COLLECTION CURRENCY_Settlement Amount] = [IN COLLECTION CURRENCY_Principal Amount]+[IN COLLECTION CURRENCY_Agent<br /> Commission]
				,[IN USD_Settlement Amount] = [IN USD_Principal Amount]+[IN USD_Agent Comm]
			
			SELECT * FROM #PartnerResult ORDER BY [Remarks],[DATE]
			----## if paying agent wise settlement report then check from posted date rest approve date
 END	

ELSE IF @flag='PartnerD'  --##
BEGIN
		SELECT 
			[DATE]											= convert(varchar,postedDate,101)
			,[GME NO]										= dbo.FNADecryptString(controlNo)
			,[IN COLLECTION CURRENCY_Total Collection]		= (cAmt) 
			,[IN COLLECTION CURRENCY_Total Charge]			= (ISNULL(serviceCharge,0)) 
			,[IN COLLECTION CURRENCY_Principal Amount]		= (tAmt)
			,[IN COLLECTION CURRENCY_Agent<br /> Commission]= (pAgentComm)
			,[IN COLLECTION CURRENCY_Settlement Amount]		= (tAmt + ISNULL(pAgentComm,0))
			,[IN USD_Principal Amount]						= (pAmt/pCurrCostRate)														
			,[IN USD_Agent Comm]							= (ISNULL(pAgentComm,0)/(sCurrCostRate+isnull(sCurrHoMargin,0)))
			,[IN USD_Settlement Amount]						= (pAmt/pCurrCostRate)+(ISNULL(pAgentComm,0)/pCurrCostRate)
			,[IN Paying_Principal Amount]					= (pAmt)
		FROM remitTran WITH (NOLOCK) 
		WHERE 1=1 AND pAgent = @sAgent AND pCountry = @pCountry
		AND postedDate BETWEEN @fromDate AND @toDate+' 23:59:59'
		ORDER BY postedDate
			----## if paying agent wise settlement report then check from posted date rest approve date
 END

EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL

SELECT 'Receiving Country' head,isnull(@pCountry,'All') value
UNION ALL
SELECT 'Sending Branch' head,case when @sBranch is null then 'All' else
									(SELECT agentName FROM agentMaster WITH (NOLOCK)  WHERE agentId=@sBranch) end VALUE
UNION ALL

SELECT 'From Date' head,@fromDate VALUE
UNION ALL
SELECT 'To Date' head,@toDate VALUE

IF @flag='s'			
	SELECT 'Settlement Report(Sending Agent)' title
ELSE IF @flag='a'
	SELECT 'Settlement Report(Paying Agent)' title
ELSE IF @flag='a'
	SELECT 'Settlement Detail(Paying Agent)' title