﻿
CREATE function [dbo].[fds__getInvoiceCredits]
(
	@startdate date
	,@enddate date
)
RETURNS TABLE 
AS
RETURN 
(
	with b0 as (
		SELECT [taID]
			  --,[AccountIdentification]
			  ,[Amount]
			  --,[EntryDate]
			  ,[FundsCode]
			  ,[AccountNumberOfPayer]
			  ,[BankCodeOfPayer]
      
			  ,[EndToEndReference]
			  --,[JournalNumber]
			  ,[NameOfPayer]
      
			  ,'info' = [dbo].[fds__fn_creanupTransactionInfo]([SepaRemittanceInformation])
			  ,'e2e' = [dbo].[fds__fn_creanupTransactionInfo]([EndToEndReference])

			  --,[PayersReferenceParty]
			  --,[PostingText]
			  ,[SepaRemittanceInformation]
			  ,[TextKeyAddition]
			  ,[TransactionCode]
      
			  ,[DebitCreditMark]
			  ,[TransactionTypeIdCode]
			  ,[ValueDate]
		  FROM [dbo].[fds__bankingtransactions]
		  WHERE [DebitCreditMark] in ('C')
			AND (@startdate is null or CAST([valuedate] AS DATE) >= @startdate)
			AND (@enddate is null or CAST([valuedate] AS DATE) <= @enddate)
			AND NOT (ISNULL([AccountNumberOfPayer],'') IN ('DE52301502000002091478') AND ISNULL([SepaRemittanceInformation],'') like ('%umbuchung%'))
			AND NOT (ISNULL([AccountNumberOfPayer],'') IN ('DE23501108006161606386') AND ISNULL([SepaRemittanceInformation],'') like ('%ebay%'))
			AND NOT (ISNULL([SepaRemittanceInformation],'') like ('%Umsatzsteuer%'))
	)
	, b as (
		SELECT 
			*
		  ,'exact' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',[info])
		  ,'exact2' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',REPLACE([info], ' ', ''))
		  ,'test1' = PATINDEX('%202[0-9]%',[info])
		  ,'E2E_exact' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',[e2e])
		  ,'E2E_exact2' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',REPLACE([e2e], ' ', ''))
		  ,'E2E_test1' = PATINDEX('%202[0-9]%',[e2e])
		from b0
	), b2 as (
		SELECT 
			*
		  ,[info2] = REPLACE([info], ' ', '')
		  ,[info3] = CASE WHEN test1 > 0 THEN REPLACE(SUBSTRING([info], [test1] - 1, 10), ' ' , '-') ELSE '' END 
		  ,[info4] = REPLACE(REPLACE([info], 'RE ', 'NO '), 'RE-NR. ', 'NO ')
		  ,[e2e_info2] = REPLACE([e2e], ' ', '')
		  ,[e2e_info3] = CASE WHEN [E2E_test1] > 0 THEN REPLACE(SUBSTRING([e2e], [E2E_test1] - 1, 10), ' ' , '-') ELSE '' END 
		FROM b
	), c as (
	  select 
		  *
		  , 'lv' = CASE WHEN exact > 0 THEN 0 
						WHEN exact2 > 0 THEN 1 
						WHEN [test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN  2
						WHEN [test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) > 0  THEN 3
					
						WHEN [e2e_exact] > 0 THEN 10 
						WHEN [e2e_exact2] > 0 THEN 11
						WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0  THEN 12
						WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0  THEN 13

						WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) ) 
							THEN 21
						WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) ) 
							THEN 22
						ELSE 99 END
		  , 'Rg' = 
				CASe WHEN [exact] > 0 THEN REPLACE(SUBSTRING([info], [exact], 10), ' ' , '-')
					WHEN [exact2] > 0 THEN REPLACE(SUBSTRING([info2], [exact2], 10), ' ' , '-')
					WHEN [test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 'R' + SUBSTRING([info3], PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) ,9)
					WHEN [test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 'R' + SUBSTRING([info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) ,4) + '-' +  + SUBSTRING([info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) +4 ,4)
					WHEN [e2e_exact] > 0 THEN REPLACE(SUBSTRING([e2e], [e2e_exact], 10), ' ' , '-')
					WHEN [e2e_exact2] > 0 THEN REPLACE(SUBSTRING([e2e_info2], [e2e_exact2], 10), ' ' , '-')
					WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 'R' + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [e2e_info3]) ,9)
					WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 'R' + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) ,4) + '-' +  + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) +4 ,4)
				
					WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) ) 
						THEN (SELECT TOP(1) [invoiceid] FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4)  = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
					WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) ) 
						THEN (SELECT TOP(1) [invoiceid] FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4)  = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )

				
				
			ELSE NULL
			END
		from b2
	)
	SELECT 
		   [taID]
		 , [InvoiceId] = UPPER([rg])
		 , [info4]
		 , [Amount]
		  --,[EntryDate]
		 , [FundsCode]
		 , [AccountNumberOfPayer]
		 , [BankCodeOfPayer]
      
		 , [EndToEndReference]
		  --,[JournalNumber]
		 , [NameOfPayer]
      
	  
		  --,[PayersReferenceParty]
		  --,[PostingText]
		 , [SepaRemittanceInformation]
		 , [TextKeyAddition]
		 , [TransactionCode]
      
		 , [DebitCreditMark]
		 , [TransactionTypeIdCode]
		 , [ValueDate]
		--,ASCII(LEFT(RIGHT([info], 2),1))
	FROM c 
		--where lv >90--Rg is null
)