﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__createInvoice_Details]
	@InvId varchar(10)
	,@InvoiceService_net numeric(10,2)
	,@InvoiceService_VAT numeric(10,2)
	,@InvoiceOptions varchar(50)
	,@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;

	MERGE [dbo].[fds__invoice_details] AS TARGET
		USING (VALUES(@InvId,@InvoiceService_net,@InvoiceService_VAT,@InvoiceOptions)) SOURCE ([InvId],[InvoiceService_net],[InvoiceService_VAT],[InvoiceOptions]) ON TARGET.[InvID] = SOURCE.[InvID]
	WHEN MATCHED THEN
		UPDATE SET 
           [InvoiceService_net] = ISNULL(SOURCE.[InvoiceService_net], TARGET.[InvoiceService_net])
           ,[InvoiceService_VAT] = ISNULL(SOURCE.[InvoiceService_VAT], TARGET.[InvoiceService_VAT])
		   ,[InvoiceOptions] = ISNULL(SOURCE.[InvoiceOptions], TARGET.[InvoiceOptions])
	WHEN NOT MATCHED BY TARGET THEN
		INSERT 
				([InvId]
				,[InvoiceService_net]
				,[InvoiceService_VAT]
				,[InvoiceOptions])
		VALUES(
			SOURCE.[InvId]
			,SOURCE.[InvoiceService_net]
			,SOURCE.[InvoiceService_VAT]
			,SOURCE.[InvoiceOptions]
		)
	OUTPUT inserted.*
	;
    
END