ALTER PROC Proc_Recon_WithdrawDeposit (
		@flag varchar(10),
		@User	 varchar(30) = null,
		@Date  DATE =null,
		@ddlType varchar(10) ='Withdraw',
		@tranAmt  varchar(30) =null,
		@walletAccountNo varchar(20)=NULL,
		@customerId varchar(20)=null,
		@rowId  varchar(10) =null,
		@pageNumber varchar(30)='1', 
		@pageSize varchar(30)='30',
		@sortOrder varchar(30) ='ASC',
		@sortBy varchar(30) ='rowId'

) as 
DECLARE  
	  @table    VARCHAR(MAX)  
	  ,@select_field_list VARCHAR(MAX)  
	  ,@extra_field_list VARCHAR(MAX)  
	  ,@sql_filter  VARCHAR(MAX)  
	  ,@errorMsg   VARCHAR(MAX)  
BEGIN TRANSACTION
  BEGIN TRY 
  If @flag='ddl'
  Begin 
	select  [value]='' ,[text]='Select'
	UNION ALL	
	select  [value]='Widthdraw' ,[text]='Widthdraw'
	UNION ALL
    select [value]='Deposit', [text]='Deposit'
	 
  END	
  if @flag='generate'
  Begin
		
	declare @query varchar(max) 
	SET @query ='exec [dbo].[proc_WithdrawBalance_Autodebit] @tranId = '''+@rowId+''''	
		
	EXEC (@query)
		if  @@TRANCOUNT <>0
		begin
		  
		EXEC dbo.proc_errorHandler '0' ,'voucher generate sucessfully ',@rowId
		end
	else 
	begin
		EXEC dbo.proc_errorHandler '1' ,'voucher can not generate generate  ',@rowId
	end
  END 
 IF @flag='L'---list -----
	 BEGIN
		IF OBJECT_ID('tempdb..#temp3') IS NOT NULL
			drop table #temp1
		IF OBJECT_ID('tempdb..#temp2') IS NOT NULL
			drop table #temp2
		IF OBJECT_ID('tempdb..#temp3') IS NOT NULL
			drop table #temp3 
		select processId,amount,virtualAccountNo into #temp1 from TblVirtualBankDepositDetail(nolock) 
		where left(depositor,14)='OT-CMSwithdraw' 

		select rowId,tranAmt,customerId,dpsPrintContent, @ddlType as ddlType into #temp2 from KFTC_CUSTOMER_TRANSFER t(nolock) 
		where bankTranDate  <>  FORMAT( @Date,'yyyyMMdd')

		select ref_num,tran_amt,field1,acc_num into #temp3 from FastMoneyPro_Account.dbo.VW_PostedAccountDetail
	 where field2='OT-CMSwithdraw' and tran_date   <> FORMAT(@Date,'yyyy-MM-dd') and gl_sub_head_code='79'


	   set @table='(
				 select 
					T2.*,C.walletAccountNo 
					
					from #temp2 t2
					INNER JOIN customerMaster C(NOLOCK) ON T2.customerId = C.customerId
					left join #temp1 t1 on t2.rowId = t1.processId
					where t1.processId is null
				)x'
		SET @sql_filter = ''  
		IF @walletAccountNo is not null
		begin
		SET @sql_Filter = @sql_Filter + ' and walletAccountNo = ''' +@walletAccountNo+''''
		end
		IF @tranAmt is not null
		begin
		set @sql_filter = @sql_filter + ' and tranAmt =''' + @tranAmt +''''
		end
		print(@table)
	   SET @select_field_list ='rowId,tranAmt,customerId,dpsPrintContent,walletAccountNo,ddlType'
	  EXEC dbo.proc_paging
					@table
				   ,@sql_filter
				   ,@select_field_list
				   ,@extra_field_list
				   ,@sortBy
				   ,@sortOrder
				   ,@pageSize
				   ,@pageNumber 
	
	 END
	 	COMMIT TRANSACTION
 END TRY
    BEGIN CATCH
	 IF @@TRANCOUNT > 0  
     ROLLBACK TRANSACTION  
     DECLARE @errorMessage VARCHAR(MAX)  
     SET @errorMessage = ERROR_MESSAGE() 
     EXEC proc_errorHandler 1, @errorMessage, null  
 END CATCH



