USE [FastMoneyPro_Remit]
GO
/****** Object:  StoredProcedure [dbo].[proc_approveOFACCompliance]    Script Date: 7/4/2019 11:35:48 AM ******/
DROP PROCEDURE [dbo].[proc_approveOFACCompliance]
GO
/****** Object:  StoredProcedure [dbo].[proc_approveOFACCompliance]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE proc [dbo].[proc_approveOFACCompliance]
	 @flag                              VARCHAR(50)		= NULL
	,@user                              VARCHAR(200)	= NULL
	,@trnId								VARCHAR(30)		= NULL
	,@controlNo							VARCHAR(100)	= NULL
	,@sCountry							VARCHAR(50)		= NULL
	,@sAgentName						VARCHAR(50)     = NULL
	,@branchName						VARCHAR(50)		= NULL
	,@createdBy							VARCHAR(50)		= NULL
	,@createdDate						VARCHAR(20)     = NULL
	,@type								VARCHAR(10)		= NULL
	,@sortBy                            VARCHAR(50)		= NULL
	,@sortOrder                         VARCHAR(5)		= NULL
	,@pageSize                          INT				= NULL
	,@pageNumber                        INT				= NULL
	,@Msg						 VARCHAR(20)		= NULL

AS

SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @controlNoEncrypted VARCHAR(20)
SELECT @controlNoEncrypted = dbo.FNAEncryptString(@controlNo)

BEGIN TRY

	CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)
	DECLARE
		 @sql				VARCHAR(MAX)
		,@oldValue			VARCHAR(MAX)
		,@newValue			VARCHAR(MAX)
		,@module			  VARCHAR(10)
		,@tableAlias		  VARCHAR(100)
		,@logIdentifier	  VARCHAR(50)
		,@logParamMod		VARCHAR(100)
		,@logParamMain		VARCHAR(100)
		,@table				VARCHAR(MAX)
		,@select_field_list	VARCHAR(MAX)
		,@extra_field_list	VARCHAR(MAX)
		,@sql_filter		VARCHAR(MAX)		
		,@id				VARCHAR(10)
		,@modType			VARCHAR(6)
		,@ApprovedFunctionId INT
		,@tranAmount		MONEY

	SELECT
		 @ApprovedFunctionId = 20123030
		,@logIdentifier = 'trnId'
		,@logParamMain = 'remitTranCompliance'
		,@logParamMod = 'remitTranOfac'
		,@module = '20'
		,@tableAlias = 'Approve OFAC Compliance'
	
	IF @flag='s'
	BEGIN

		SET @table = '(
		
					select 
						
						 tranId=ISNULL(b.holdTranId,b.id)
						,controlNo=dbo.FNADecryptString(b.controlNo) 
						,b.sCountry
						,b.sAgentName
						,branchName=b.sBranchName
						,b.createdBy 
						,b.createdDate
						,b.cAmt
						,type = (SELECT dbo.FNAGetOfacComplianceReason(ISNULL(holdTranId, id)))
						,receiverName = receiverName
						,senderName = senderName
						,hasChanged = ''''
						from remitTran b with(nolock)
						WHERE (B.tranStatus	= ''Compliance'' OR B.tranStatus	= ''OFAC'' OR B.tranStatus = ''OFAC/Compliance'')
			) '


		IF @sortBy IS NULL
			SET @sortBy = 'tranId'
		IF @sortOrder IS NULL
			SET @sortOrder = 'ASC'
		
		--print @table 
		--return;
		SET @table = '( 
			select tranId 
						--,controlNo= ''<a href="#" onclick="OpenInNewWindow('''''+dbo.FNAGetURL()+'Remit/Transaction/Reports/SearchTransaction.aspx?controlNo='' + main.controlNo + '''''')">'' + main.controlNo + ''</a>''
						,controlNo= ''<a href="'+dbo.FNAGetURL()+'Remit/Compliance/ApproveOFACandComplaince/Manage.aspx?controlNo='' + main.controlNo + ''">'' + main.controlNo + ''</a>''
						,branchName
						,type 
						,receiverName
						,senderName 
						,hasChanged
						,sCountry
						,sAgentName
						,createdBy
						,createdDate
						,cAmt
					
				FROM ' + @table + ' main
				
				) x
	
				'
					
		SET @sql_filter = ''
		
		IF @controlNo IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND controlNo LIKE ''%' + @controlNo + '%'''
			
		IF @sCountry IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND sCountry = ''' + @sCountry + ''''
			
		IF @sAgentName IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND sAgentName = ''' + @sAgentName + ''''
		
		IF @branchName IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND senderName = ''' + @branchName + ''''
			
		IF @createdBy IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND createdBy = ''' + @createdBy + ''''
			
		IF @createdDate IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND CAST(createdDate AS DATE) = ''' + @createdDate + ''''
			
		IF @type IS NOT NULL
			SET @sql_filter = @sql_filter + ' AND type LIKE ''' + @type + '%'''
			
		SET @select_field_list ='
			 tranId
			,controlNo
			,branchName
			,type
			,receiverName
			,senderName
			,hasChanged
			,sCountry
			,sAgentName
			,createdBy
			,createdDate
			,cAmt
			'
			
		EXEC dbo.proc_paging
			 @table
			,@sql_filter
			,@select_field_list
			,@extra_field_list
			,@sortBy
			,@sortOrder
			,@pageSize
			,@pageNumber
	END

/*
	ELSE IF @flag = 'c'			-- ## search transaction for Errorneously Pay 
	BEGIN

		IF NOT EXISTS(SELECT 'X' FROM remitTran WITH(NOLOCK) 
			  WHERE controlNo = @controlNoEncrypted and tranStatus = 'Paid' )
		BEGIN

			EXEC proc_errorHandler 1, 'Paid Transaction Not Found', @controlNoEncrypted
			RETURN	
		END
		IF EXISTS(SELECT 'X' FROM errPaidTran WITH(NOLOCK) 
			  WHERE controlNo = (select controlNo from errPaidTran where controlNo=@controlNo and tranStatus='Paid'))
		BEGIN

			EXEC proc_errorHandler 1, 'Transaction Already Errorneously Paid!', @controlNoEncrypted
			RETURN	
		END
		IF EXISTS(SELECT 'X' FROM errPaidTran WITH(NOLOCK) 
			  WHERE controlNo = (select controlNo from errPaidTran where controlNo=@controlNo 
			  and tranStatus is null and ISNULL(isDeleted,'N')<>'Y'))
		BEGIN

			EXEC proc_errorHandler 1, 'Transaction Already Requested For Errorneously Pay!', @controlNoEncrypted
			RETURN	
		END
		
		DECLARE @agentId INT
		SELECT @agentId = agentId FROM applicationUsers WHERE userName = @user
		IF @agentId = 1
		BEGIN
			EXEC proc_errorHandler 0, 'Transaction Found', @controlNoEncrypted
			RETURN
		END
		
		IF NOT EXISTS(SELECT 'X' FROM remitTran WITH(NOLOCK) 
				WHERE controlNo = @controlNoEncrypted AND 
				(pBranch = (SELECT agentId FROM applicationUsers WITH(NOLOCK) WHERE userName = @user))
				)
		BEGIN
			EXEC proc_errorHandler 1, 'You are not authorized to view this transaction', @controlNoEncrypted
			RETURN
		END
		EXEC proc_errorHandler 0, 'Transaction Found', @controlNo
	END
	
	*/
END TRY
BEGIN CATCH

     IF @@TRANCOUNT > 0
     ROLLBACK TRANSACTION
     DECLARE @errorMessage VARCHAR(MAX)
     SET @errorMessage = ERROR_MESSAGE()
     EXEC proc_errorHandler 1, @errorMessage, @trnId

END CATCH
GO
