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

CREATE proc [dbo].[proc_KJDepositReport]
@flag varchar(10),
@startDate varchar(10),
@endDate varchar(20),
@user	varchar(40),
@searchType varchar(30) = null,
@searchValue varchar(150) = null

as
set nocount on
if @flag = 's'
begin
	select [Bank Name] = k.BankName,[Sum Amount] = sum(d.amount),[No of Txn] = count(1)
	from TblVirtualBankDepositDetail d(nolock)
	LEFT join KoreanBankList k on k.bankCode = d.institution
	where d.logDate between @startDate and @endDate+' 23:59:59'
	group by k.BankName
end
if @flag = 'd'
begin
	select [Bank Name] =k.BankName,[Received On] = d.receivedOn
		,[Customer Name] = CASE WHEN d.customerName ='Nnull' THEN D.depositor ELSE d.customerName END
		,[Primary Ac No] = d.no,[Virtual Ac No] = d.virtualAccountNo,[Amount] = d.amount
	from TblVirtualBankDepositDetail d(nolock)
	LEFT join KoreanBankList k on k.bankCode = d.institution
	where d.logDate between @startDate and @endDate+' 23:59:59'
end
if @flag = 'statement'
begin
	DECLARE @customerId BIGINT 
	DECLARE @table TABLE (customerId BIGINT, email VARCHAR(150))
	if @searchValue is not null
	begin
		DECLARE @sql VARCHAR(150) = 'SELECT customerId, email FROM customerMaster CM (NOLOCK) WHERE 1 = 1 '
		DECLARE @sqlFilter VARCHAR(200) = ''
		IF ISNULL(@searchValue,'')<>'' AND ISNULL(@searchType,'')<>''
		BEGIN 
			IF @searchType='idNumber'
			BEGIN
				--IF ISNUMERIC(@searchValue)<>1
				--	SET @searchValue='-1'	--to ignore string value for datatype integer/customerID
				--SET @sql_Filter=@sql_Filter + ' AND customerId = ''' +@searchValue+''''
				SET @sqlFilter=@sqlFilter + ' AND REPLACE(idNumber, ''-'', '''') = ''' +REPLACE(@searchValue, '-', '')+''''
			END
			ELSE IF @searchType='emailId'
				SET @sqlFilter=@sqlFilter + ' AND email like ''' +@searchValue+'%'''
			ELSE IF @searchType='customerName'
				SET @sqlFilter=@sqlFilter + ' AND fullName like ''' +@searchValue+'%'''
			ELSE IF @searchType='mobile'
				SET @sqlFilter=@sqlFilter + ' AND mobile = ''' +@searchValue+''''
			ELSE IF @searchType='walletAccountNo'
				SET @sqlFilter=@sqlFilter + ' AND walletAccountNo = ''' +@searchValue+''''
			ELSE IF @searchType='bankAccountNo'
				SET @sqlFilter=@sqlFilter + ' AND bankAccountNo = ''' +@searchValue+''''
		END

		INSERT INTO @table(customerId, email)
		EXEC(@sql+@sqlFilter)
	end

	

	select sum(amount) amount,virtualAccountNo,c.email,(c.availableBalance) availableBalance ,c.FullName
	into #temp
	from TblVirtualBankDepositDetail t(nolock)
	inner join customermaster c(nolock) on c.walletAccountNo = t.virtualAccountNo
	WHERE T.logDate between @startDate and @endDate+' 23:59:59'
	group by virtualAccountNo,c.email,c.availableBalance,c.FullName

	select createdBy,sum(camt) camt into #txn 
	from remittran(nolock) where tranStatus <> 'cancel' 
	AND createdDate between @startDate and @endDate+' 23:59:59'
	group by createdBy

	if @searchValue is not null
	begin
		select FullName,t.email,T.virtualAccountNo,sum(t.amount)'Deposited Amount', sum(camt) 'Send Amount',sum(t.amount-camt) 'Available Balance'
			,(t.availableBalance) 'Balance Seen In System' 
		from #temp t
		left join #txn x on x.createdBy = t.email
		inner join @table tmp on tmp.email = t.email
		group by t.email,T.virtualAccountNo,t.availableBalance,FullName
	end
	else
	begin
		select FullName,t.email,T.virtualAccountNo,sum(t.amount)'Deposited Amount', sum(camt) 'Send Amount',sum(t.amount-camt) 'Available Balance'
			,(t.availableBalance) 'Balance Seen In System' 
		from #temp t
		left join #txn x on t.email = x.createdBy
		group by t.email,T.virtualAccountNo,t.availableBalance,FullName
		--having sum(t.amount-x.camt-T.availableBalance) >0
		--having sum(t.amount-camt) <> (t.availableBalance)
	end
end
 EXEC proc_errorHandler '0', 'Report has been prepared successfully.', NULL
			
SELECT  'From Date ' head,@startDate value union all
SELECT  'To Date ' head,@endDate value 
		
SELECT 'Bank Deposit Report' title
GO
