USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_cancelRpt]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_cancelRpt]
GO
/****** Object:  StoredProcedure [dbo].[proc_cancelRpt]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procEDURE [dbo].[proc_cancelRpt]
	@user		VARCHAR(50),
	@pCountry	VARCHAR(50)	= NULL,
	@sBranch	INT			,
	@fromDate	VARCHAR(10)	,
	@toDate		VARCHAR(20) ,
	@cancelType	VARCHAR(10) = NULL
	
AS
SET NOCOUNT ON;
	IF(DATEDIFF(D,@fromDate,GETDATE())>90 )
    BEGIN
    	EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
    	EXEC proc_errorHandler '1', '<font color="red"><b>Date Range is not valid, You can only view transaction upto 90 days.</b></font>', NULL
    	RETURN;
    END
		
	IF(DATEDIFF(D,@fromDate,@toDate))>32 
	BEGIN
		EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
		EXEC proc_errorHandler '1', '<font color="red"><b>Date Range is not valid, Please select date range of 32 days.</b></font>', NULL
		RETURN;
	END

	DECLARE @SQL VARCHAR(MAX)
	DECLARE @sAgent INT
	SELECT @sAgent = parentId FROM applicationUsers au WITH(NOLOCK) 
	INNER JOIN agentMaster am WITH(NOLOCK) ON au.agentId = am.agentId
	WHERE userName = @user
	
	SET @SQL='SELECT [SN]  = ROW_NUMBER() over (ORDER BY [Branch] ),* FROM (
			SELECT [Cancel Date]			= '+CASE WHEN @cancelType ='Refunded' THEN ' RC.refundDate ' ELSE ' RC.approvedDate'  END +'
					,[Branch]				= ISNULL(sBranchName,''-'') 
					,[BRN]	=''<a href = "#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'AgentPanel/SearchTxnReport/SearchTransaction.aspx?commentFlag=N&showBankDetail=N&tranId=''+CAST(RT.id AS VARCHAR)+'''''')">''+dbo.FNADecryptString(RT.controlNo)+''</a>''   
					,[Sender/Receiver]		= rt.senderName+''/''+rt.receiverName
					,[Receiving Country]	= RT.pCountry 
					,[TXN Date]				= RT.createdDate
					,[Deposit Amt]			= RT.cAmt	
					,[Payout Amt]			= RT.pAmt
					,[Requested By]			= RC.createdBy
					,'+CASE WHEN @cancelType = 'Refunded' THEN '[Refunded By]' ELSE '[Cancelled By]' END +'	= RC.ApprovedBy
					,[Reason]				= ISNULL(''Request :''+RC.cancelReason,'''')+''<br/>''+ISNULL(''Approve :''+RC.approvedRemarks,'''')
					,[Txn <br/>Generated By]		= RT.createdBy
			FROM remitTran RT WITH (NOLOCK)
			INNER JOIN tranCancelrequest  RC WITH (NOLOCK) ON RT.id=RC.tranId
			WHERE 1=1 '
	
	IF @sAgent IS NOT NULL
		SET @SQL = @SQL + ' AND RT.sAgent = ''' + CAST(@sAgent AS VARCHAR) + ''''
		
	IF @sBranch IS NOT NULL
		SET @SQL = @SQL +' AND RT.sBranch =''' +CAST(@sBranch AS VARCHAR)+ ''''
		
	IF @pCountry IS NOT NULL 
		SET @SQL = @SQL +' AND pCountry ='''+@pCountry+''''

	IF  @cancelType ='Approved'
		SET @SQL = @SQL +' AND RC.cancelStatus =''Approved'' AND RC.approvedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
	
	IF  @cancelType ='Rejected'
		SET @SQL = @SQL +' AND RC.cancelStatus =''Rejected'' AND RC.approvedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''

	IF @cancelType ='deny'
		SET @SQL = @SQL +' AND 1=2  '
	IF @cancelType IS  NULL
		SET @SQL = @SQL +' AND RC.cancelStatus =''Approved''  AND RT.cancelapprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
					
	IF  @cancelType ='deny' OR @cancelType IS  NULL
	BEGIN
		SET @SQL=@SQL + '  
			UNION ALL
					SELECT 
					[Cancel Date]			= RT.cancelapprovedDate
					,[Branch]				= ISNULL(RT.sBranchName,''-'') 
					,[BRN]	= ''<a href = "#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'AgentPanel/SearchTxnReport/SearchTransaction.aspx?commentFlag=N&showBankDetail=N&tranId=''+CAST(RT.id AS VARCHAR)+'''''')">''+dbo.FNADecryptString(RT.controlNo)+''</a>''   
					,[Sender/Receiver]		= rt.senderName+''/''+rt.receiverName
					,[Receiving Country]	= RT.pCountry 
					,[TXN Date]				= RT.createdDate
					,[Deposit Amt]			= RT.cAmt	
					,[Payout Amt]			= RT.pAmt
					,[Cancel Requested By]	= RT.cancelRequestBy
					,[Cancel Cancelled By]	= RT.cancelApprovedBy
					,[Cancel Reason]		= ISNULL(''Request :''+RT.cancelReason,'''')+''<br/>''+ISNULL(''Approve :''+RC.approvedRemarks,'''')
					,[Txn Generated By]		= RT.createdBy
				FROM cancelTranHistory RT  WITH (NOLOCK)
				LEFT JOIN tranCancelrequest  RC WITH (NOLOCK) ON RT.tranId=RC.tranId
				WHERE cancelapprovedDate BETWEEN '''+@fromDate+''' AND '''+@toDate+' 23:59:59'''
				
		IF @pCountry IS NOT NULL
			SET @SQL = @SQL +' AND pCountry ='''+@pCountry+''''
	
	IF @sAgent IS NOT NULL
		SET @SQL = @SQL + ' AND RT.sAgent = ''' + CAST(@sAgent AS VARCHAR) + ''''
		
	IF @sBranch IS NOT NULL
		SET @SQL = @SQL +' AND RT.sBranch =''' +CAST(@sBranch AS VARCHAR)+ ''''
	
			
	END
	SET @SQL =  @SQL +')x'
	
	PRINT @SQL		
	EXEC(@SQL)

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

	SELECT 'Beneficiary' head,isnull(@pCountry,'All') VALUE 
	UNION ALL
	SELECT 'Branch' head,(SELECT agentName FROM agentMaster WITH(NOLOCK) WHERE agentid=@sBranch) VALUE
	UNION ALL
	SELECT 'From Date' head,@fromDate value
	UNION ALL
	SELECT 'To Date' head,	@toDate
	UNION ALL
	SELECT 'Cancel Type' head,	ISNULL(CASE WHEN @cancelType ='deny' THEN 'Deny' WHEN @cancelType ='Approved' THEN 'Approve Cancel' 
									 WHEN @cancelType ='Refunded' THEN 'Refund'  END,'ALL')

	SELECT 'Cancel Report' title

	




GO
