USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_CancelReport_Admin]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_CancelReport_Admin]
GO
/****** Object:  StoredProcedure [dbo].[proc_CancelReport_Admin]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_CancelReport_Admin]
	@user		VARCHAR(50),
	@sCountry	VARCHAR(50)	= NULL,
	@sAgent		VARCHAR(50)	= NULL,
	@sBranch	VARCHAR(50)	= NULL,
	@pCountry	VARCHAR(50)	= NULL,
	@rAgent		VARCHAR(50)	= NULL,
	@fromDate	VARCHAR(10)	,
	@toDate		VARCHAR(20) ,
	@cancelType	VARCHAR(50) = NULL,
	@pageSize	INT			= 100,
	@pageNumber	INT			= 1
	
AS
SET NOCOUNT ON;
	DECLARE @SQL VARCHAR(MAX)
	IF(DATEDIFF(D,@fromDate,@toDate))>31
	BEGIN
		EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
		EXEC proc_errorHandler '1', '<font color="red"><b>Report can view only 31 days</b></font>', NULL
		RETURN;
	END
	
	If OBJECT_ID('tempdb..#TempResult') is not null
		DROP TABLE #TempResult
	
	CREATE TABLE #TempResult(
		 [Sending_Country]		VARCHAR(50)
		,[Sending_Agent]		VARCHAR(200)
		,[Sending_Branch]		VARCHAR(200)
		,[BRN]					VARCHAR(200)
		,[Sender/Receiver]		VARCHAR(300)
		,[Receiving_Country]	VARCHAR(50)
		,[Receiving_Agent]		VARCHAR(200)
		,[TXN Date]				VARCHAR(50)
		,[Deposit Amt]			MONEY
		,[Payout Amt]			MONEY
		,[Cancel Request_User]			VARCHAR(50)
		,[Cancel Request_Date]			VARCHAR(50)
		,[Cancel Approve_User]			VARCHAR(50)
		,[Cancel Approve_Date]			VARCHAR(50)
		,[Cancel Refund_User]			VARCHAR(50)
		,[Cancel Refund_Date]			VARCHAR(50)
		,[Cancel Reason]				VARCHAR(500)
		,[Txn<br/>Generated By] VARCHAR(50)
	)
	
	SET @SQL='
		INSERT INTO #TempResult
		SELECT * FROM (
			SELECT   
					 [Sending_Country]		= ISNULL(sCountry,''-'') 
					,[Sending_Agent]		= ISNULL(sAgentName,''-'') 
					,[Sending_Branch]		= ISNULL(sBranchName,''-'') 
					,[BRN]					= ''<a href = "#" onclick="ViewTranDetail(''+CAST(RT.id AS VARCHAR)+'')">''+dbo.FNADecryptString(RT.controlNo) +''</a>''   
					,[Sender/Receiver]		= rt.senderName+''/''+rt.receiverName
					,[Receiving_Country]	= RT.pCountry 
					,[Receiving_Agent]		= RT.pAgentName 
					,[TXN Date]				= RT.createdDate
					,[Deposit Amt]			= RT.cAmt	
					,[Payout Amt]			= RT.pAmt

					,[Cancel Request_User]		= RT.cancelRequestBy
					,[Cancel Request_Date]		= RT.cancelRequestDate					
					,[Cancel Approve_User]		= RT.cancelApprovedBy
					,[Cancel Approve_Date]		= RT.cancelApprovedDate
					,[Cancel Refund_User]		= RC.teller
					,[Cancel Refund_Date]		= RC.refundDate
					,[Cancel Reason]			= ISNULL(''Request :''+RC.cancelReason,'''')+''<br/>''+ISNULL(''Approve :''+RC.approvedRemarks,'''')
					,[Txn<br/>Generated By]		= RT.createdBy
			FROM remitTran RT WITH (NOLOCK)
			LEFT JOIN tranCancelrequest  RC WITH (NOLOCK) ON RT.controlNo=RC.controlNo
			WHERE ISNULL(RC.cancelStatus, '''') <> ''Rejected'' '

	IF @sCountry IS NOT NULL
		SET @SQL = @SQL +' AND RT.sCountry = '''+@sCountry+''''

	IF @sAgent IS NOT NULL
		SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
	
	IF @sBranch IS NOT NULL
		SET @SQL = @SQL +' AND RT.sBranch =''' +CAST(@sBranch AS VARCHAR)+ ''''
		
	IF @pCountry IS NOT NULL AND @pCountry<>'ALL'
		SET @SQL = @SQL +' AND RT.pCountry ='''+@pCountry+''''

	IF @rAgent IS NOT NULL 
		SET @SQL = @SQL +' AND RT.pAgent ='''+@rAgent+''''

	-->> Report Type 
	IF @cancelType IS NULL
		SET @SQL = @SQL +' AND RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and ISNULL(RC.cancelStatus, '''') <> ''CancelRequest'' '

	IF @cancelType ='denied'
		SET @SQL = @SQL +' AND 1=2 '

	IF  @cancelType ='Approved'
		SET @SQL = @SQL +'  AND RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and ISNULL(RC.cancelStatus, '''') not in (''CancelRequest,Rejected'') '

	IF @cancelType ='Refunded'
		SET @SQL = @SQL +'  AND RC.refundDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and RC.cancelStatus = ''Refunded'' '	

	IF @cancelType ='RefundPending'
		SET @SQL = @SQL +'  AND RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' 
				and RC.cancelStatus = ''Approved'' and RC.tranStatus in (''Payment'',''Paid'',''Post'') and RT.sAgent = 13410 ' 		

	IF @cancelType ='PaidCancel'
		SET @SQL = @SQL +'  AND RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and RT.payStatus = ''Paid'''		

	IF @cancelType ='PostCancel'
		SET @SQL = @SQL +'  AND RT.cancelApprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and RT.payStatus = ''Post'''	

	IF @cancelType ='Rejected'
		SET @SQL = @SQL +'  AND RC.approvedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'' and RC.tranStatus = ''Rejected'''	
												
	IF  @cancelType ='denied' OR @cancelType IS NULL
	BEGIN
		SET @SQL=@SQL + '  
			UNION ALL
				SELECT 
					 [Sending_Country]		= ISNULL(RT.sCountry,''-'') 
					,[Sending_Agent]		= ISNULL(RT.sAgentName,''-'') 
					,[Sending_Branch]		= ISNULL(RT.sBranchName,''-'') 
					,[BRN]					= ''<a href = "#" onclick="ViewCancelTxnByControlNo(''+CAST(dbo.FNADecryptString(RT.controlNo) AS VARCHAR)+'')">''+dbo.FNADecryptString(RT.controlNo) +''</a>''   
					,[Sender/Receiver]		= rt.senderName+''/''+rt.receiverName
					,[Receiving_Country]	= RT.pCountry 
					,[Receiving_Agent]		= RT.pAgentName 
					,[TXN Date]				= RT.createdDate
					,[Deposit Amt]			= RT.cAmt	
					,[Payout Amt]			= RT.pAmt
					,[Cancel Request_User]		= RT.cancelRequestBy
					,[Cancel Request_Date]		= RT.cancelRequestDate	
									
					,[Cancel Approve_User]		= RT.cancelApprovedBy
					,[Cancel Approve_Date]		= RT.cancelApprovedDate

					,[Cancel Refund_User]		= RC.teller
					,[Cancel Refund_Date]		= RC.refundDate

					,[Cancel Reason]			= ISNULL(''Request :''+RC.cancelReason,'''')+''<br/>''+ISNULL(''Approve :''+RC.approvedRemarks,'''')
					,[Txn Generated By]			= RT.createdBy
				FROM cancelTranHistory RT  WITH (NOLOCK)
				LEFT JOIN tranCancelrequest  RC WITH (NOLOCK) ON RT.controlNo=RC.controlNo
				WHERE RC.cancelStatus <> ''Rejected'' and cancelapprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
				
		IF @sCountry IS NOT NULL
			SET @SQL = @SQL +' AND RT.sCountry = '''+@sCountry+''''

		IF @sAgent IS NOT NULL
			SET @SQL = @SQL +' AND RT.sAgent = '''+@sAgent+''''
		
		IF @sBranch IS NOT NULL
			SET @SQL = @SQL +' AND RT.sBranch =''' +CAST(@sBranch AS VARCHAR)+ ''''
			
		IF @pCountry IS NOT NULL AND @pCountry<>'ALL'
			SET @SQL = @SQL +' AND RT.pCountry ='''+@pCountry+''''

		IF @rAgent IS NOT NULL 
			SET @SQL = @SQL +' AND RT.pAgent ='''+@rAgent+''''
			
	END
	SET @SQL =  @SQL +')x'
	
	PRINT @SQL		
	EXEC(@SQL)

	ALTER TABLE #TempResult ADD rowId INT IDENTITY(1,1)
	
	SELECT 
			COUNT(*) AS TXNCOUNT
			,@pageSize PAGESIZE
			,@pageNumber PAGENUMBER					
	FROM #TempResult
	
	SELECT 
		 rowId [S.N.]
		,[Sending_Country]		
		,[Sending_Agent]		
		,[Sending_Branch]		
		,[BRN]	
		,[Sender/Receiver]		
		,[Receiving_Country]	
		,[Receiving_Agent]		
		,[TXN Date]				
		,[Deposit Amt]				
		,[Payout Amt]
		,[Cancel Reason]	
		,[Cancel Request_User]		
		,[Cancel Request_Date]							
		,[Cancel Approve_User]		
		,[Cancel Approve_Date]		
		,[Cancel Refund_User]		
		,[Cancel Refund_Date]		
				
		,[Txn<br/>Generated By]	
	FROM #TempResult
	WHERE rowId BETWEEN CAST(((@pageNumber - 1) * @pageSize + 1) AS VARCHAR)  AND  CAST(@pageNumber * @pageSize AS VARCHAR)

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

SELECT 'Sending Country' head,isnull(@sCountry,'All') VALUE 
UNION ALL
SELECT 'Receiving Country' head,isnull(@pCountry,'All') VALUE 
UNION ALL
SELECT 'Sending Agent' head,isnull(@sAgent,'All') VALUE 
UNION ALL
SELECT 'Receiving Agent' head,isnull(@rAgent,'All') VALUE 
UNION ALL
SELECT 'Branch' head,case when @sBranch is null then 'All' else (SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=@sBranch) end VALUE
UNION ALL
SELECT '<br/>From Date' head,@fromDate value
UNION ALL
SELECT 'To Date' head,	@toDate
UNION ALL
SELECT 'Cancel Type' head,	ISNULL(UPPER(@cancelType),'All')

SELECT 'Cancel Transaction Report' title

	

GO
