USE FastMoneyPro_Remit
GO

-- EXEC proc_PartnerwiseStatement @flag = 'get-Statement',@partnerId='101941337',@fromDate='2019-12-01',@toDate='2020-01-31',@user='PRALHAD'

ALTER proc proc_PartnerwiseStatement
(
	@flag          varchar(50),
	@user          varchar(50),
	@partnerId     BIGINT      = null,
	@fromDate      varchar(10) = null,
	@toDate			varchar(30)= null,
	@pageSize	   VARCHAR(50) = NULL,
	@pageNumber	   VARCHAR(50) = NULL
)
AS

SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @sSettlingAgent int, @sSettlingAgentName varchar(150);

DECLARE @RESULT TABLE(tran_date DATE,paymentMethod VARCHAR(30),ContNo VARCHAR(30),refNo VARCHAR(30),tranStatus VARCHAR(20),controlNo VARCHAR(300),usd_amt MONEY
		,part_tran_type VARCHAR(5),ref_num VARCHAR(30),tran_particular VARCHAR(MAX),[Entry Date] DATE,tran_type CHAR(1),Currency VARCHAR(5))

BEGIN TRY
	IF @flag = 's-partner'
	BEGIN
	SELECT * FROM (
		SELECT acct_num AS [value],acct_name AS [text] 
		FROM FastMoneyPro_Account.DBO.ac_master(NOLOCK) 
		WHERE acct_rpt_code IN ('TP','TPU','TC')
		UNION ALL
		SELECT 100241027580 as [value],'Kwangju-CMS' as [text]
		UNION ALL
		SELECT 100241011536 as [value],'Kwangju-Wallet(345626)' as [text]
		UNION ALL
		SELECT 101715544 as [value],'Kasikorn Bank  USD' as [text]
		UNION ALL
		SELECT 100241008219 as [value],'Welcome Savings Bank-Customer Account(0678571)' as [text]
		UNION ALL
		SELECT 161874309 as [value],'Powercall Receivable' as [text]
	)X ORDER BY [text]
	END

	ELSE IF @flag = 'get-Statement'
	begin

		DECLARE @tempAccNo TABLE(accNumber VARCHAR(50))

		SET  @toDate = FORMAT(CONVERT(DATETIME,@toDate), 'yyyy-MM-dd')

		INSERT INTO @tempAccNo(accNumber)
		SELECT @partnerId

		SELECT @partnerId = agent_id FROM FastMoneyPro_Account.DBO.ac_master(NOLOCK) WHERE acct_num = @partnerId

	    SELECT @sSettlingAgent=agentId, @sSettlingAgentName = agentName ,@partnerId = parentId
		FROM dbo.agentMaster(NOLOCK) 
		WHERE agentId=@partnerId AND ISNULL(isSettlingAgent,'N')='Y' 
		AND ISNULL(isApiPartner,0)=1 AND ISNULL(isActive,'N')='Y' AND ISNULL(isDeleted,'N')='N'

		IF @partnerId IN (1036,1056,405488)
		begin
			SELECT @sSettlingAgent=agentId, @sSettlingAgentName = agentName 
			FROM dbo.agentMaster(NOLOCK) 
			WHERE agentId = @partnerId AND ISNULL(isActive,'N')='Y' 
		end
		
		IF @sSettlingAgent IS NULL
		BEGIN
				SELECT 
				 m.tran_date
				,'' AS paymentMethod,'' ContNo,'' refNo,'' tranStatus ,field1 controlNo
				,m.tran_amt
				,m.part_tran_type
				,M.ref_num
				,M.tran_particular
				,m.created_date as [Entry Date] 
			FROM FastMoneyPro_Account.dbo.VW_PostedAccountDetail(nolock) m
			INNER JOIN @tempAccNo A ON M.acc_num=A.accNumber
			WHERE M.tran_date  between @fromDate and @toDate

			RETURN
		END

		--INSERT INTO @tempAccNo(accNumber)
		--SELECT 
		--	acm.acct_num 
		--FROM FastMoneyPro_Account.dbo.ac_master(nolock) acm 
		--where agent_id = @sSettlingAgent 
		--AND acct_rpt_code in('TP','TPU','TC')

		select paymentMethod,ContNo,refNo = controlNo2,tranStatus,controlNo = controlNo
		into #temp
		from remitTran(nolock) where pagent = @sSettlingAgent
		and approvedDate between @fromDate and @toDate+' 23:59:59.88'

		select paymentMethod,ContNo,refNo = controlNo2,tranStatus,controlNo = controlNo
		into #tempCancel
		from remitTran(nolock) where pagent = @sSettlingAgent
		and cancelApprovedDate between @fromDate and @toDate+' 23:59:59.88'

		delete t from #temp t
		inner join #tempCancel c on c.controlNo = t.controlNo

		INSERT INTO #temp
		SELECT * FROM #tempCancel

		UPDATE #temp SET refNo = dbo.FNADecryptString(refNo),controlNo = dbo.FNADecryptString(controlNo)

		INSERT INTO @RESULT(tran_date,usd_amt,part_tran_type,ref_num,[Entry Date],tran_type,Currency,controlNo)
		SELECT 
			 m.tran_date,m.usd_amt,m.part_tran_type,M.ref_num,m.created_date as [Entry Date],M.tran_type,M.fcy_Curr,field1
		FROM FastMoneyPro_Account.dbo.tran_master(nolock) m
		--INNER JOIN FastMoneyPro_Account.dbo.tran_masterDETAIL D(NOLOCK) ON D.ref_num = M.ref_num AND M.tran_type = D.tran_type
		INNER JOIN @tempAccNo acc ON acc.accNumber = m.acc_num		
		--LEFT join  #temp t on field1 = t.controlNo
		WHERE M.tran_date  between @fromDate and @toDate

		UPDATE M SET M.tran_particular = D.tran_particular FROM @RESULT M
		INNER JOIN FastMoneyPro_Account.dbo.tran_masterDETAIL D(NOLOCK) ON D.ref_num = M.ref_num AND M.tran_type = D.tran_type

		INSERT INTO @RESULT(tran_date,usd_amt,part_tran_type,ref_num,[Entry Date],tran_type,Currency,controlNo)
		SELECT 
			 m.tran_date,m.usd_amt,m.part_tran_type,M.ref_num,m.created_date as [Entry Date],M.tran_type,M.fcy_Curr,field1
		FROM FastMoneyPro_Account.dbo.tran_master_POST(nolock) m
		--INNER JOIN FastMoneyPro_Account.dbo.tran_masterDETAIL_POST D(NOLOCK) ON D.ref_num = M.ref_num AND M.tran_type = D.tran_type
		INNER JOIN @tempAccNo acc ON acc.accNumber = m.acc_num		
		--LEFT join  #temp t on field1 = t.controlNo
		WHERE M.tran_date  between @fromDate and @toDate

		UPDATE M SET M.tran_particular = D.tran_particular FROM @RESULT M
		INNER JOIN FastMoneyPro_Account.dbo.tran_masterDETAIL_POST D(NOLOCK) ON D.ref_num = M.ref_num AND M.tran_type = D.tran_type

		UPDATE R SET R.paymentMethod=t.paymentMethod,R.ContNo=t.ContNo,R.refNo=t.refNo,R.tranStatus=t.tranStatus FROM @RESULT R 
		LEFT JOIN #temp T (NOLOCK) ON T.controlNo = R.controlNo

		SELECT * FROM @RESULT

		--AND ISNULL(field2,'') NOT IN ('TRADING GAINLOSS FCY','FOREIGN GAIN FCY','REVALUATION GAINLOSS FCY','TRADING GAINLOSS','FOREIGN GAIN','REVALUATION GAINLOSS')
	end

end try

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


