﻿USE [DD_ECM]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/* TBEDI_XML_TEMPLATES */

DROP TABLE [dbo].[TBEDI_XML_TEMPLATES]

CREATE TABLE [dbo].[TBEDI_XML_TEMPLATES](
	[GUID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[NAME] [nvarchar](20) NOT NULL,
	[DESCRIPTION] [nvarchar](max) NULL
) ON [PRIMARY]
GO

INSERT INTO [dbo].[TBEDI_XML_TEMPLATES] (NAME, DESCRIPTION) VALUES ('EXIM-VRG_orders', 'Schaum EDI Aufträge') 

/* TBEDI_XML_TYPES */

DROP TABLE [dbo].[TBEDI_XML_TYPES]

CREATE TABLE [dbo].[TBEDI_XML_TYPES](
	[GUID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[NAME] [nvarchar](20) NOT NULL
) ON [PRIMARY]
GO

INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('STRING') 
INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('INTEGER') 
INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('DATE') 
INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('BOOLEAN') 
INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('DECIMAL')

INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('ACCOUNT')
INSERT INTO [dbo].[TBEDI_XML_TYPES] (NAME) VALUES ('ARTICLE')

/* TBEDI_XML_NODES */

DROP TABLE [dbo].[TBEDI_XML_FUNCTIONS]

CREATE TABLE [dbo].[TBEDI_XML_FUNCTIONS](
	[GUID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[NAME] [nvarchar](max) NOT NULL,
	[DESCRIPTION] [nvarchar](max) NOT NULL,
	[PARAMETERS] [nvarchar](max) NULL,
) ON [PRIMARY]
GO 

INSERT INTO [dbo].[TBEDI_XML_FUNCTIONS] (NAME, DESCRIPTION) VALUES ('GLN', 'Finds an WinLine Account Number by GLN') 
INSERT INTO [dbo].[TBEDI_XML_FUNCTIONS] (NAME, DESCRIPTION) VALUES ('EAN', 'Finds an WinLine Article Number by EAN') 

/* TBEDI_XML_NODES */

DROP TABLE [dbo].[TBEDI_XML_NODES]

CREATE TABLE [dbo].[TBEDI_XML_NODES](
	[GUID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[NAME] [nvarchar](max) NOT NULL,
	[TEMPLATE_ID] [int] NOT NULL,
	[IS_HEAD] [bit] NOT NULL
) ON [PRIMARY]
GO 

ALTER TABLE [dbo].[TBEDI_XML_NODES]
	ADD CONSTRAINT DF_TBEDI_XML_NODES_IS_HEAD
		DEFAULT 0 FOR IS_HEAD

INSERT INTO [dbo].[TBEDI_XML_NODES] (NAME, IS_HEAD, TEMPLATE_ID) VALUES ('EXIM-VRG_ordersT025', 1, 1) 
INSERT INTO [dbo].[TBEDI_XML_NODES] (NAME, IS_HEAD, TEMPLATE_ID) VALUES ('EXIM-VRG_ordersT026', 0, 1)

/* TBEDI_XML_ITEMS */

DROP TABLE [dbo].[TBEDI_XML_ITEMS]

CREATE TABLE [dbo].[TBEDI_XML_ITEMS](
	[GUID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[XML_NAME] [nvarchar](50) NOT NULL,
	[XML_NODE_ID] [int] NOT NULL,
	[XML_TYPE_ID] [int] NOT NULL,
	[IS_READ_ONLY] [bit] NOT NULL,
	[IS_VISIBLE] [bit] NOT NULL,
	[FUNCTION_ID] [int] NULL
) ON [PRIMARY]
GO
		
ALTER TABLE [dbo].[TBEDI_XML_ITEMS]
	ADD CONSTRAINT FK_TBEDI_XML_TYPES FOREIGN KEY (XML_TYPE_ID)
		REFERENCES [dbo].[TBEDI_XML_ITEMS] (GUID)
		
ALTER TABLE [dbo].[TBEDI_XML_ITEMS]
	ADD CONSTRAINT FK_TBEDI_XML_NODES FOREIGN KEY (XML_NODE_ID)
		REFERENCES [dbo].[TBEDI_XML_ITEMS] (GUID)

ALTER TABLE [dbo].[TBEDI_XML_ITEMS]
	ADD CONSTRAINT DF_TBEDI_XML_ITEMS_IS_READ_ONLY
		DEFAULT 0 FOR IS_READ_ONLY


DROP VIEW [dbo].[VWEDI_XML_ITEMS]
GO

CREATE VIEW [dbo].[VWEDI_XML_ITEMS]
AS
	SELECT DISTINCT
		[TBITEMS].[XML_NAME]
		,[TBNODES].[NAME] [XML_ROOT]
		,[TBTEMPLATES].[NAME] [TEMPLATE_NAME]
		,[TBTYPES].[NAME] [DATA_TYPE]
		,[TBNODES].[IS_HEAD] [IS_HEAD]
		,[TBFUNCTIONS].[GUID] [FUNCTION_ID]
		,[TBFUNCTIONS].[NAME] [FUNCTION_NAME]
		,[TBFUNCTIONS].[PARAMETERS] [FUNCTION_PARAMETERS]
		,[TBITEMS].[IS_READ_ONLY]
		,[TBITEMS].[IS_VISIBLE]
	FROM
		[dbo].[TBEDI_XML_ITEMS] [TBITEMS]
		INNER JOIN [dbo].[TBEDI_XML_NODES] [TBNODES] ON [TBITEMS].[XML_NODE_ID] = [TBNODES].[GUID]
		INNER JOIN [dbo].[TBEDI_XML_TYPES] [TBTYPES] ON [TBITEMS].[XML_TYPE_ID] = [TBTYPES].[GUID]
		INNER JOIN [dbo].[TBEDI_XML_TEMPLATES] [TBTEMPLATES] ON [TBNODES].[TEMPLATE_ID] = [TBTEMPLATES].[GUID]
		LEFT OUTER JOIN [dbo].[TBEDI_XML_FUNCTIONS] [TBFUNCTIONS] ON [TBITEMS].[FUNCTION_ID] = [TBFUNCTIONS].[GUID]
		
GO


/* Kopfdaten */	

INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('BELEGKEY', 1, 1, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Fakt_Kontonummer', 1, 1, 1, 1, 1)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Laufnummer', 1, 1, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Bestellt_von', 1, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Lief_Kontonummer', 1, 1, 1, 1, 1)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Belegart', 1, 2, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Datum_Auftrag-Bestellung', 1, 3, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Auftrags-Bestellnummer', 1, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Leistungsdatum', 1, 3, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Infotext', 1, 1, 1, 0, NULL)

/* Positionsdaten */	

INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('BELEGKEY', 2, 1, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Zeilennummer', 2, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Datentyp', 2, 2, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Artikelnummer', 2, 1, 1, 1, 2)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Bezeichnung', 2, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Lieferantenartikelnummer', 2, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Menge_bestellt', 2, 5, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Menge_geliefert', 2, 5, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Colli', 2, 1, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Einzelpreis', 2, 5, 1, 1, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Zeilenrabatt1', 2, 5, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Zeilenrabatt2', 2, 5, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Zeilenrabatt3', 2, 5, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Zeilenrabatt4', 2, 5, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('Umsatzsteuerprozent_Zeile', 2, 2, 1, 0, NULL)
INSERT [dbo].[TBEDI_XML_ITEMS] (XML_NAME, XML_NODE_ID, XML_TYPE_ID, IS_READ_ONLY, IS_VISIBLE, FUNCTION_ID)
VALUES ('EDI_Infotext', 2, 1, 1, 0, NULL)