﻿
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__setReminderFinal]
	@Id varchar(8),
	@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;
	IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 2
		THROW 60000, N'not authorized', 1;  
	

	
	DECLARE @now datetime = GETUTCDATE();

	DECLARE @ids as TABLE([Id] varchar(10));

	

    UPDATE [dbo].[fds__reminder] SET [DateFinalized] = @now, [UserFinalized] = @authuser, [DateModified] = @now, [UserModified] = @authuser, [version] = (ISNULL([version],0) + 1)
	OUTPUT inserted.[id]
		INTO @ids
	WHERE [Id] = @Id AND @Id is not null and [isFinal] = 0 and [isSent] = 0;

	SELECT rem.*
			, [InvoiceFile] = (SELECT TOP(1) [file] FROM [fds__invoices] as i where i.InvoiceId = rem.[InvId] and i.IsFinal = 1 and i.[file] is not null and ISNULL(i.[DocumentName],'') <> '')
			, [InvoiceFileName] = (SELECT TOP(1) [DocumentName] FROM [fds__invoices] as i where i.InvoiceId = rem.[InvId] and i.IsFinal = 1 and i.[file] is not null and ISNULL(i.[DocumentName],'') <> '')
		FROM [dbo].[fds__reminder] as rem 
			JOIN @ids as ids on rem.[id] = ids.Id;   --this is necessary to get calculated fields also

END