﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__createInvoice]
	@InvoiceType char(1),
	@InvoiceTitle varchar(100),
	@InvoiceBalance numeric(10,2),
	@InvoiceBalance_net numeric(10,2),
	@InvoiceVAT_net1 numeric(10,2),
	@InvoiceVAT_1 numeric(5,2),
	@PaymentTerm varchar(5),
	@CustomerId bigint,
	@SendToAddress nvarchar(1000),
	@SendToEmail nvarchar(255),
	@ProvisionPeriod varchar(50), 
	@CustomValues nvarchar(max),
	@authuser varchar(25),
	@Id varchar(10) OUT,
	@SendToAddressJson nvarchar(max) = NULL
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__createInvoice' ,@authuser , '');
	

	DECLARE @now datetime = GETUTCDATE();
	DECLARE @today date = @now;
	SET @PaymentTerm = ISNULL(@PaymentTerm, '10wd');
	DECLARE @days tinyint = ISNULL(TRY_PARSE(REPLACE(REPLACE(@PaymentTerm, 'w',''),'d','') as tinyint), 10);
	DECLARE @duedate date = CASE WHEN RIGHT(@PaymentTerm, 2) = 'wd' THEN DATEADD(weekday, @days, @today) ELSE DATEADd(DAY, @days, @today) END;

	DECLARE @newid varchar(10) = [dbo].[fds__fn_invoice_id]();
	
	DECLARE @out TABLE (
		[Id] [varchar](10) NULL,
		[Version] [int] NULL,
		[InvoiceId] [nvarchar](25) NULL,
		[InvoiceType] [char](1) NULL,
		[InvoiceTitle] [varchar](100) NULL,
		[DocumentName] [nvarchar](255) NULL,
		[InvoiceBalance] [numeric](10, 2) NULL,
		[InvoiceBalance_net] [numeric](10, 2) NULL,
		[InvoiceVAT_net1] [numeric](10, 2) NULL,
		[InvoiceVAT_1] [numeric](5, 2) NULL,
		[InvoiceVAT_net2] [numeric](10, 2) NULL,
		[InvoiceVAT_2] [numeric](5, 2) NULL,
		[PaymentTerm] [varchar](5) NULL,
		[DueDate] [date] NULL,
		[CustomerId] [bigint] NULL,
		[SendToAddress] [nvarchar](1000) NULL,
		[SendToEmail] [nvarchar](255) NULL,
		[ProvisionPeriod] [varchar](50) NULL,
		[ProvisionLocation] [nvarchar](1000) NULL,
		[PaymentStatus] [char](1) NULL,
		[IsPayed] [bit] NULL,
		[IsSent] [bit] NULL,
		[IsFinal] [bit] NULL,
		[IsCanceled] [bit] NULL,
		[Replaces_InvId] [varchar](10) NULL,
		[CustomValues] [nvarchar](max) NULL,
		[SendToAddressJson] [nvarchar](max) NULL,
		[DateSent] [datetime] NULL,
		[UserSent] [varchar](25) NULL,
		[DateFinalized] [datetime] NULL,
		[UserFinalized] [varchar](25) NULL,
		[DateCancelled] [datetime] NULL,
		[UserCancelled] [varchar](25) NULL,
		[DateCreated] [datetime] NULL,
		[UserCreated] [varchar](25) NULL,
		[DateModified] [datetime] NULL,
		[UserModified] [varchar](25) NULL,
		[ExternalId] [varchar](25) NULL,
		[isExternal] [bit] NULL,
		[file] [varbinary](max) NULL,
		[file_guid] uniqueidentifier null
	);


    INSERT INTO [dbo].[fds__invoices]
           ([Id]
		   ,[Version]
           ,[InvoiceId]
		   ,[InvoiceType]
		   ,[InvoiceTitle]
           ,[InvoiceBalance]
		   ,[InvoiceBalance_net]
           ,[InvoiceVAT_net1]
           ,[InvoiceVAT_1]
           ,[InvoiceVAT_net2]
           ,[InvoiceVAT_2]
		   ,[PaymentTerm]
           ,[DueDate]
           ,[CustomerId]
           ,[SendToAddress]
           ,[SendToEmail]
           ,[ProvisionPeriod]
           ,[ProvisionLocation]
		   ,[PaymentStatus]
           ,[IsPayed]
           ,[IsSent]
           ,[CustomValues]
           ,[SendToAddressJson]
		   ,[DateSent]
		   ,[UserSent]
           ,[DateFinalized]
           ,[UserFinalized]
           ,[DateCancelled]
           ,[UserCancelled]
           ,[DateCreated]
           ,[UserCreated]
           ,[DateModified]
           ,[UserModified]
		   ,[ExternalId]
		   ,[isExternal]
		   ,[file]
		   ,[file_guid])
	 OUTPUT inserted.*
		INTO @out
     VALUES
           (@newid
		   ,0  --Version
           ,NULL  --InvoiceID
		   ,ISNULL(@InvoiceType,'r')
		   ,ISNULL(@InvoiceTitle,'Rechnung')
           ,@InvoiceBalance
		   ,@InvoiceBalance_net
           ,@InvoiceVAT_net1
           ,@InvoiceVAT_1
           ,NULL --<InvoiceVAT_net2, numeric(10,2),>
           ,NULL --<InvoiceVAT_2, numeric(5,2),>
		   ,@PaymentTerm
           ,@DueDate
           ,@CustomerId
		   ,@SendToAddress
           ,@SendToEmail
           ,@ProvisionPeriod
		   ,NULL --[ProvisionLocation]
		   ,NULL --[PaymentStatus]
           ,0 --<IsPayed, bit,>
           ,0 --<IsSent, bit,>
           ,@CustomValues
           ,@SendToAddressJson
		   , NULL  --[DateSent]
		   , NULL  --[UserSent]
           ,NULL --<DateFinalized, datetime,>
           ,NULL --<UserFinalized, varchar(25),>
           ,NULL --<DateCancelled, datetime,>
           ,NULL --<UserCancelled, varchar(25),>
           ,@now
           ,@authuser
            ,@now
           ,@authuser
		   ,NULL--ExternalId
		   ,0	--isExternal
		   ,NULL --file
		   ,NEWID() --file_guid
		  );

	SELECT TOP(1) @Id = [id] FROM @out;

	SELECT TOP(1) *
	  ,[hasFile] = CAST (  CASE WHEN [file] is null THEN 0 ELSE 1 END as bit)
	  ,[UserNameFinalized] =  [dbo].[fis_admin_getUserName_byID]([UserFinalized])
	  ,[UserEmailFinalized] =  [dbo].[fis_admin_getUserEmail_byID]([UserFinalized])
	from @out

END