﻿

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__createCredit_simple]
	@InvId varchar(20)
	, @authuser varchar(100)
AS
BEGIN
	SET NOCOUNT ON;

	IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
		THROW 60000, N'not authorized', 1;  
	
	INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
		VALUES ('fds__createCredit_simple' ,@authuser , 'invid: ' + ISNULL(@InvId, ''));

	DECLARE @now datetime = GETDATE();
	DECLARE @today date = CAST(@now as date);
	
	DECLARE @newid varchar(10) = [dbo].[fds__fn_invoice_id]();


	DECLARE @inv_DocumentName varchar(50), @inv_DateFinalized datetime, @inv_vat numeric(5,2), @inv_net numeric(10,3);

	IF EXISTS (SELECT 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1)
	BEGIN

			SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateFinalized], @inv_vat = [InvoiceVAT_1], @inv_net = 1
			FROM [dbo].[fds__invoices] WHERE [id] = @InvId
	
			INSERT INTO [dbo].[fds__invoices]
				   ([Id]
				   ,[Version]
				   ,[InvoiceId]
				   ,[InvoiceType]
				   ,[InvoiceTitle]
				   ,[DocumentName]
				   ,[InvoiceBalance]
				   ,[InvoiceBalance_net]
				   ,[InvoiceVAT_net1]
				   ,[InvoiceVAT_1]
				   ,[InvoiceVAT_net2]
				   ,[InvoiceVAT_2]
				   ,[PaymentTerm]
				   ,[DueDate]
				   ,[CustomerId]
				   ,[SendToAddress]
				   ,[SendToEmail]
				   ,[ProvisionPeriod]
				   ,[ProvisionLocation]
				   ,[PaymentStatus]
				   ,[IsPayed]
				   ,[IsSent]
				   ,[Replaces_InvId]
				   ,[CustomValues]
				   ,[DateSent]
				   ,[UserSent]
				   ,[DateFinalized]
				   ,[UserFinalized]
				   ,[DateCancelled]
				   ,[UserCancelled]
				   ,[DateCreated]
				   ,[UserCreated]
				   ,[DateModified]
				   ,[UserModified]
				   ,[ExternalId]
				   ,[isExternal])
			 SELECT
				   [Id] = @newid
				   ,[Version]			= 0		
				   ,[InvoiceId]			= NULL	
				   ,[InvoiceType]		= 'g'	
				   ,[InvoiceTitle]		= 'Gutschrift'
				   ,[DocumentName]		= NULL
				   ,[InvoiceBalance]	= (@inv_net * -1) * (1 + 0.01 * ISNULL(@inv_vat,19))
				   ,[InvoiceBalance_net] =  (@inv_net * -1)
				   ,[InvoiceVAT_net1]	= (@inv_net * -1) * (0.01 * ISNULL(@inv_vat,19))
				   ,[InvoiceVAT_1]		= @inv_net
				   ,[InvoiceVAT_net2]	= null
				   ,[InvoiceVAT_2]		= null
				   ,[PaymentTerm]
				   ,[DueDate]			= @today
				   ,[CustomerId]
				   ,[SendToAddress]		= ISNULL([SendToAddress], [dbo].[fds__getCompanyNameAddress]([customerid]))
				   ,[SendToEmail]		= ISNULL([SendToEmail], [dbo].[fds__getCompanyNameAddress]([customerid]))
				   ,[ProvisionPeriod]
				   ,[ProvisionLocation]
				   ,[PaymentStatus]		= NULL
				   ,[IsPayed]			= 0
				   ,[IsSent]			= 0	
				   ,[Replaces_InvId]	= @InvId
				   ,[CustomValues]		 
				   ,[DateSent]			= NULL
				   ,[UserSent]			= @authuser
				   ,[DateFinalized]		= NULL
				   ,[UserFinalized]		= NULL
				   ,[DateCancelled]		= NULL
				   ,[UserCancelled]		= NULL
				   ,[DateCreated]		= @now
				   ,[UserCreated]		= @authuser
				   ,[DateModified]		= @now
				   ,[UserModified]		= @authuser
				   ,[ExternalId]		= NULL
				   ,[isExternal]		= 0
			FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1;
	END
	

	-- continue creating storno

	DECLARE @inv_srq [dbo].[fds__tt__invoice_servicerequests];
	INSERT INTO [dbo].[fds__invoice_servicerequests]
           ([InvId]
           ,[mfr__servicerequest]
           ,[title]
           ,[value_net]
           ,[SortOrder])
	OUTPUT
		inserted.* INTO @inv_srq 
     SELECT
           [InvId] = [id]
           ,NULL --<mfr__servicerequest, bigint,>
           ,'Gutschrift'
           ,(@inv_net * -1)
           ,0
	FROM [dbo].[fds__invoices] WHERE [id] = @newid; 


	INSERT INTO [dbo].[fds__invoice_items]
           ([InvId]
           ,[InvRqId]
           ,[mfr__item]
           ,[reference_InvId]
           ,[Type]
           ,[Position]
           ,[Quantity]
           ,[Text]
           ,[value]
           ,[value_total]
           ,[vat]
           ,[value_service]
           ,[det]
           ,[SortOrder])
     SELECT
           @newid --<InvId, varchar(10),>
           ,[id]--<InvRqId, bigint,>
           ,NULL --<mfr__item, bigint,>
           ,@InvId --<reference_InvId, varchar(20),>
           ,'CreditInvoice'	--<Type, nvarchar(255),>
           ,NULL --<Position, varchar(5),>
           ,'1' ---<Quantity, nvarchar(25),>
           ,[Text]			= '<p>' + 'Gutschrift zu der ' + REPLACE(@inv_DocumentName, '.pdf','') + ' vom ' + FORMAT(@inv_dateFinalized, 'dd.MM.yyyy', 'de-de') + '</p>'
           ,[value]			= (@inv_net * -1)
           ,[value_total]	= (@inv_net * -1)
           ,[vat] = @inv_vat
           ,0 --<value_service, numeric(10,3),>
           ,1 --<det, bit,>
           ,1 --<SortOrder, tinyint,>)
	FROM @inv_srq;


	INSERT INTO [dbo].[fds__invoice_details]
           ([InvId]
           ,[InvoiceService_net]
           ,[InvoiceService_VAT]
           ,[AllocatedTo_InvId]
           ,[StornoTo_InvId])
     VALUES(
           @newid
           ,NULL --[InvoiceService_net]
           ,NULL --[InvoiceService_VAT]
           ,NULL --[AllocatedTo_InvId]
           ,@InvId --[StornoTo_InvId]
	);

	EXECUTE [dbo].[fds__getInvoice] @newid, @authuser;


END