﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_DocumentName]
(
	@InvoiceId varchar(10)
	, @InvoiceType char(1)
	, @InvoiceTitle varchar(100)
	, @DateFinalized datetime
)
RETURNS varchar(50)
AS
BEGIN
	DECLARE @doc varchar(50);

	SET @doc = CASE WHEN @DateFinalized is null THEN ''
					WHEN ISNULL(@InvoiceTitle,'') <> '' THEN @InvoiceTitle + ' ' + @InvoiceId
					WHEN @InvoiceType = 'a' THEN 'Abschlagsrechnung ' + @InvoiceId
					WHEN @InvoiceType = 'c' THEN 'Stornorechnung ' + @InvoiceId
					WHEN @InvoiceType = 'f' THEN 'Schlussrechnung ' + @InvoiceId
				ELSE 'Rechnung ' + @InvoiceId
				END;

	RETURN @doc + '.pdf';

END