﻿


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[backup__fds__prepReminder]
	@InvId varchar(15)
	, @authuser varchar(100)
	, @type varchar(1)
	, @level int
AS
BEGIN
	SET NOCOUNT ON;
	IF [dbo].[fis_getModuleAuth]('fds_reminder', @authuser) < 2
		THROW 60000, N'not authorized', 1;  
	ELSE IF ISNULL(@type,'') NOT IN ('f','m','l') OR ISNULL(@level, 0) < 1 
		THROW 60000, N'inputs not valid', 1;  
	ELSE IF NOT EXISTS (select 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId) AND NOT EXISTS (select 0 FROM [dbo].[mfr__invoices] WHERE [id] = TRY_CAST(@InvId as bigint))
		THROW 60000, N'invoice not found', 1;
	
	INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
		VALUES ('fds__prepReminder' ,@authuser , (SELECT * FROM (VALUES(@InvId, @authuser, @type, @level)) as z ([InvId],[authuser],[type],[level]) FOR JSON PATH, INCLUDE_NULL_VALUES, WITHOUT_ARRAY_WRAPPER));
	
	
	DECLARE @now datetime = GETDATE();
	DECLARE @today date = CAST(@now as date);

	
	--output

	With inv1 as(
		select TOP(1) * FROM [dbo].[fds__invoices] WHERE [id] = @InvId
	), inv as(
		SELECT [Id] = cast([id] as varchar(15))
			,[PaymentTerm]
			,[SendToemail]
			,[SendToAddress]
			,[CustomerId]
			,[Invoiceid]
			,[DateFinalized]
			,[InvoiceBalance]
		FROM inv1
		UNION
		SELECT [Id] = cast([id] as varchar(15))
			,[PaymentTerm] = '10wd'
			,[SendToEmail] = sy.[SupportMail]
			,[SendToAddress] = (select TOP(1) sy.[name] + CHAR(10) + l.[addressString] + CHAR(10) + l.[postal] + ' ' + l.[city] + CHAR(10) + l.[country] from [dbo].[mfr__#locations] as l where l.[EntityId] = sy.[CustomerId])
			,[CustomerId]	
			,[Invoiceid]
			,[DateFinalized] = [DateOfCreation]
			,[InvoiceBalance]
		FROM [dbo].[mfr__invoices] as mfri
			LEFT JOIN (SELECT TOP(1) [invid] = p.[PartnerId], s.[CustomerId], cy.[supportmail], cy.[Name] 
							FROM dbo.[mfr__*PartnerSet] as p 
								JOIN [dbo].[mfr__servicerequests] as s on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices'
								JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
							WHERE p.PartnerId = TRY_CAST(@invId as bigint)) as sy on mfri.Id = sy.[invid]
		WHERE [id] = TRY_CAST(@invId as bigint) AND NOT EXISTS( SELECT 0 FROM inv1 )   --will only be used, if id not present in fds__invoices
			AND [Filetype] not in ('PdfCancelInvoice') 
			AND [invoiceState] not in ('eIsCancelled','eIsPaid')
	)
	SELECT TOP(1) 
			[today] = @today
			, [invid] = inv.[id]
			, [sender] = N'Sebastian Fuchs GmbH & Co. KG ● Germaniastraße 15 ● 40223 Düsseldorf'
			, [paymentterms]	= [PaymentTerm]
			, [invoiceemail]	= [SendToemail]
			, [invoiceaddress]	= [SendToAddress]
			, [CustomerId]		= [CustomerId]
			, [subject] = CASE WHEN @type = 'f' THEN N'Zahlungserinnerung'
								WHEN @type = 'm' THEN FORMAT(@level,'0','de-de') + N'. Mahnung' 
								WHEN @type = 'l' THEN N'Letzte außergerichtliche Mahnung' 
								ELSE N'Zahlungserinnerung'
							END
			, [type] = @type
			, [invoiceid] = inv.[Invoiceid]
			, [invoicedate] = inv.[DateFinalized]
			, [amount] = inv.[InvoiceBalance]
			, [amount_payed] = CAST(  ISNULL([dbo].[fds__fn_InvoicePaymentAmount](inv.[Id]),0.0)  as numeric(10,2))
		FROM inv;


END