﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__getInvoiceReminder]
	@InvId varchar(15)
	, @include_drafts bit = 0
	,@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;

	With inv as (
		SELECT TOP(1) 
			[Id] = CAST([id] as varchar(15)), [InvoiceId], [DocumentName]
		FROM [dbo].[fds__invoices] 
		WHERE [id] = @InvId
	) 
	SELECT rem.[Id]
			,rem.[DateSent]
			,[status]		= CAST(CASE WHEN rem.[isSent] = 1 THEN 'versandt' WHEN rem.[isfinal] = 1 THEN 'fertig' WHEN rem.isfinal = 0 THEN 'Entwurf' ELSE ''  END AS  varchar(50))
			,rem.[subject]
			,rem.[DocumentName]
			,rem.[amount_open]
			,rem.[InvId]
			,[InvoiceId]	= inv.[InvoiceId]
			--,[InvoiceDocumentName] = inv2.[DocumentName]
			,[hasFile]		= cast( IIF(rem.[file] is null, 0, 1) as bit)
		FROM [dbo].[fds__reminder] as rem 
			LEFT JOIN inv on rem.[InvId] = inv.[Id]
		WHERE [invId] = @InvId;

END