UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.8.2';
--DROP TABLE TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE
CREATE TABLE TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE
(
	GUID            INT			IDENTITY(1,1),              		 
	SPECNAME	   	VARCHAR(100)	NOT NULL,	
	Row_Caption		VARCHAR(250)	NOT NULL,
	Area			VARCHAR(100)	NOT NULL,
	xPosition		INTEGER			NOT NULL	DEFAULT 10,
	--yPosition		INTEGER			NOT NULL,
	SequenceItem	INTEGER			NOT NULL	DEFAULT 1,
	Y_eq_lastrow	BIT				NOT NULL	DEFAULT 0,
	Display			BIT				NOT NULL	DEFAULT 1,
	Active			BIT				NOT NULL	DEFAULT 1,
	AddedWho		VARCHAR(50)		DEFAULT		'DD_ECM DEFAULT'	NOT NULL,
	AddedWhen		DATETIME 		DEFAULT GETDATE(),
	ChangedWho		VARCHAR(50),
	ChangedWhen		DATETIME,
	CONSTRAINT		PK_TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE		PRIMARY KEY (GUID),
	CONSTRAINT		UNIQUE_TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE	UNIQUE(SPECNAME)
)
GO
------------------------------------------------------------------------------

CREATE TRIGGER TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE_AFT_UPD ON TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE
FOR UPDATE
AS
  UPDATE TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE SET ChangedWhen = GETDATE() FROM INSERTED WHERE TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE.GUID = INSERTED.GUID
GO
CREATE VIEW VWDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE_ITEMS AS 
SELECT 
	A.SPEC_NAME,
	A.ITEM_VALUE,
	B.Area,
	B.Row_Caption,
	B.xPosition,
	B.Y_eq_lastrow, 
	B.SequenceItem,
	B.Display,
	A.REFERENCE_GUID
FROM
	(SELECT * FROM TBEDMI_ITEM_VALUE ) A INNER JOIN
	(SELECT * FROM TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE WHERE Active = 1) B ON A.SPEC_NAME = B.SPECNAME
GO
CREATE FUNCTION [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_TAX] (@pMSGID VARCHAR(100))
RETURNS @TABLE TABLE(SPEC_NAME VARCHAR(100),ITEM_VALUE VARCHAR(250),Area VARCHAR(60),Row_Caption Varchar(100), SequenceItem INT)
AS
BEGIN
	DECLARE @GROUP_COUNTER INT,@INDEX INT = 60
	DECLARE cursGroupCounter CURSOR FOR 
		select GROUP_COUNTER from TBEDMI_ITEM_VALUE WHERE REFERENCE_GUID = @pMSGID And SPEC_NAME = 'INVOICE_TAXPOS_RATE' ORDER BY GROUP_COUNTER
	OPEN cursGroupCounter 
		FETCH NEXT FROM cursGroupCounter INTO @GROUP_COUNTER
		WHILE @@FETCH_STATUS = 0
		BEGIN
			DECLARE @SPEC_NAME VARCHAR(100),@ITEM_VALUE VARCHAR(100),@Row_Caption VARCHAR(100)
			DECLARE cursPosItems CURSOR FOR 
				select A.SPEC_NAME,ITEM_VALUE, B.Row_Caption from TBEDMI_ITEM_VALUE A INNER JOIN TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE B ON A.SPEC_NAME = B.SPECNAME WHERE REFERENCE_GUID = @pMSGID And A.SPEC_NAME IN ('INVOICE_TAXPOS_AMOUNT',
				'INVOICE_TAXPOS_RATE','INVOICE_TAXPOS_TYPE') AND GROUP_COUNTER = @GROUP_COUNTER ORDER BY B.SequenceItem
			OPEN cursPosItems 
				FETCH NEXT FROM cursPosItems INTO @SPEC_NAME,@ITEM_VALUE,@Row_Caption
				WHILE @@FETCH_STATUS = 0
				BEGIN
					SET @INDEX += 1
					INSERT INTO @TABLE (SPEC_NAME,ITEM_VALUE,Area,Row_Caption,SequenceItem) VALUES
					(@SPEC_NAME,@ITEM_VALUE,'TAXPOS',@Row_Caption,@INDEX)
				FETCH NEXT FROM cursPosItems INTO @SPEC_NAME,@ITEM_VALUE, @Row_Caption
			END
			CLOSE cursPosItems
			DEALLOCATE cursPosItems
		FETCH NEXT FROM cursGroupCounter INTO @GROUP_COUNTER
	END
	CLOSE cursGroupCounter
	DEALLOCATE cursGroupCounter
RETURN
END
GO

CREATE FUNCTION [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_POSITIONS] (@pMSGID VARCHAR(100))
RETURNS @TABLE TABLE(SPEC_NAME VARCHAR(100),ITEM_VALUE VARCHAR(250),Area VARCHAR(60),SequenceItem INT)
AS
BEGIN
	DECLARE @GROUP_COUNTER INT,@INDEX INT = 15
	DECLARE cursGroupCounter CURSOR FOR 
		select GROUP_COUNTER from TBEDMI_ITEM_VALUE WHERE REFERENCE_GUID = @pMSGID And SPEC_NAME = 'INVOICE_POSITION_AMOUNT' ORDER BY GROUP_COUNTER
	OPEN cursGroupCounter 
		FETCH NEXT FROM cursGroupCounter INTO @GROUP_COUNTER
		WHILE @@FETCH_STATUS = 0
		BEGIN
			DECLARE @SPEC_NAME VARCHAR(100),@ITEM_VALUE VARCHAR(100)
			DECLARE cursPosItems CURSOR FOR 
				select A.SPEC_NAME,ITEM_VALUE from TBEDMI_ITEM_VALUE A INNER JOIN TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE B ON A.SPEC_NAME = B.SPECNAME WHERE REFERENCE_GUID = @pMSGID And A.SPEC_NAME IN ('INVOICE_POSITION_AMOUNT',
				'INVOICE_TAXPOS_TAX_RATE','INVOICE_POSITION_ARTICLE') AND GROUP_COUNTER = @GROUP_COUNTER ORDER BY B.SequenceItem
			OPEN cursPosItems 
				FETCH NEXT FROM cursPosItems INTO @SPEC_NAME,@ITEM_VALUE 
				WHILE @@FETCH_STATUS = 0
				BEGIN
					SET @INDEX += 1
					INSERT INTO @TABLE (SPEC_NAME,ITEM_VALUE,Area,SequenceItem) VALUES
					(@SPEC_NAME,@ITEM_VALUE,'POSITION',@INDEX)
				FETCH NEXT FROM cursPosItems INTO @SPEC_NAME,@ITEM_VALUE 
			END
			CLOSE cursPosItems
			DEALLOCATE cursPosItems
		FETCH NEXT FROM cursGroupCounter INTO @GROUP_COUNTER
	END
	CLOSE cursGroupCounter
	DEALLOCATE cursGroupCounter
RETURN
END
GO

CREATE FUNCTION [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_CUST_RESULT] (@pMSGID VARCHAR(100))
RETURNS TABLE
AS RETURN 
	(
		SELECt A.SPEC_NAME,A.ITEM_VALUE,A.Area,A.Row_Caption,A.xPosition,A.Y_eq_lastrow,A.SequenceItem ORDER_SEQ,A.Display FROM VWDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE_ITEMS A 
		WHERE REFERENCE_GUID = @pMSGID
		AND A.SPEC_NAME NOT IN ('INVOICE_POSITION_AMOUNT',
						'INVOICE_TAXPOS_TAX_RATE','INVOICE_POSITION_ARTICLE','INVOICE_TAXPOS_AMOUNT',
						'INVOICE_TAXPOS_RATE','INVOICE_TAXPOS_TYPE')
		UNION
		SELECT SPEC_NAME,ITEM_VALUE,Area,'' Row_Caption,10 xPosition, 0 Y_eq_lastrow,SequenceItem ORDER_SEQ, 1 Display FROM [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_POSITIONS] (@pMSGID)
		UNION
		SELECT SPEC_NAME,ITEM_VALUE,Area,Row_Caption Row_Caption,10 xPosition, 0 Y_eq_lastrow,SequenceItem ORDER_SEQ, 1 Display FROM [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_TAX] (@pMSGID)
	)
GO
DELETE FROM TBDD_SQL_COMMANDS WHERE TITLE = 'VWDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE_ITEMS';
INSERT INTO TBDD_SQL_COMMANDS (TITLE,SQL_COMMAND) VALUES
('VWDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE_ITEMS','
SELECT * FROM [dbo].[FNDD_ZUGFERD_VIEW_RECEIPT_CUST_RESULT] (''@MSG_ID'') ORDER BY ORDER_SEQ')

DELETE FROM TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE;
Insert INTO TBDD_ZUGFERD_VIEW_RECEIPT_TEMPLATE 
(SPECNAME,				Row_Caption,							Area,		xPosition,	SequenceItem,	Y_eq_lastrow,	Display) values 
('INVOICE_TYPE',		'',										'TYPE',		10,			1,				0,				0),
('INVOICE_CURRENCY',	'',										'TYPE',		10,			2,				0,				0),
('INVOICE_NUMBER',		'Rechnungsnummer/Invoice-No:',			'HEAD',		10,			3,				0,				1),
('INVOICE_DATE',		'Datum/Date:',							'HEAD',		10,			4,				0,				1),
('INVOICE_SERVICE_DATE','Leistungsdatum/Service date:',			'HEAD',		10,			5,				0,				1),
('INVOICE_SELLER_NAME',	'',										'SELLER',	10,			6,				0,				1),
('INVOICE_SELLER_ADRESS',		'',								'SELLER',	10,			7,				0,				1),
('INVOICE_SELLER_POSTALCODE',	'',								'SELLER',	10,			8,				0,				1),
('INVOICE_SELLER_CITY',			'',								'SELLER',	35,			9,				1,				1),
('INVOICE_SELLER_TAX_ID',	'USt-ID/Seller tax ID:',			'SELLER',	10,			10,				0,				1),
('INVOICE_BUYER_NAME',			'',								'BUYER',	10,			11,				0,				1),
('INVOICE_BUYER_ADRESS',		'',								'BUYER',	10,			12,				0,				1),
('INVOICE_BUYER_POSTALCODE',	'',								'BUYER',	10,			13,				0,				1),
('INVOICE_BUYER_CITY',			'',								'BUYER',	35,			14,				1,				1),
('INVOICE_POSITION_AMOUNT',		'',								'POSITION',	10,			15,				0,				0),
('INVOICE_POSITION_ARTICLE',	'',								'POSITION',	10,			16,				0,				0),
('INVOICE_TAXPOS_TAX_RATE',		'',								'POSITION',	10,			17,				0,				1),
('INVOICE_TOTAL_NET',	'Nettobetrag/Net amount:',				'AMOUNT',	10,			58,				0,				1),
('INVOICE_TOTAL_GROSS',	'Bruttobetrag/Gross amount:',			'AMOUNT',	10,			59,				0,				1),
('INVOICE_TAXPOS_RATE',			'',								'TAXPOS',	10,			60,				1,				0),
('INVOICE_TAXPOS_AMOUNT',		'',								'TAXPOS',	10,			61,				0,				0),
('INVOICE_TAXPOS_TYPE',			'',								'TAXPOS',	10,			62,				1,				1),
('INVOICE_PAYMENT_TERMS','',									'PAYMENT',	10,			100,			0,				1),
('INVOICE_PAYMENT_IBAN','IBAN:',								'PAYMENT',	10,			101,			0,				1),
('INVOICE_PAYMENT_BIC','BIC:',									'PAYMENT',	10,			102,			0,				1);
GO



