UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.0.1'
GO

CREATE TABLE [dbo].[TBDD_3RD_PARTY_MODULES](
	[GUID] [int] IDENTITY(1,1) NOT NULL,
	[ACTIVE] [bit] NOT NULL,
	[NAME] [varchar](50) NOT NULL,
	[DESCRIPTION] [varchar](500) NULL,
	[LICENSE] [varchar](MAX) NOT NULL,
	[VERSION] [varchar](20) NOT NULL,
	[STRING1] [varchar](100) NULL,
	[STRING2] [varchar](100) NULL,
	[STRING3] [varchar](100) NULL,
	[STRING4] [varchar](100) NULL,
	[STRING5] [varchar](100) NULL,
	[STRING6] [varchar](100) NULL,
	[STRING7] [varchar](100) NULL,
	[STRING8] [varchar](100) NULL,
	[STRING9] [varchar](100) NULL,
	[STRING10] [varchar](100) NULL,
	[ADDED_WHO] [varchar](50) NULL,
	[ADDED_WHEN] [datetime] NULL,
	[CHANGED_WHO] [varchar](50) NULL,
	[CHANGED_WHEN] [datetime] NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[TBDD_3RD_PARTY_MODULES] ADD  CONSTRAINT [DF_TBDD_3RD_PARTY_MODULES_ACTIVE]  DEFAULT ((0)) FOR [ACTIVE]
GO

ALTER TABLE [dbo].[TBDD_3RD_PARTY_MODULES] ADD  CONSTRAINT [DF_TBDD_3RD_PARTY_MODULES_VERSION]  DEFAULT ('1.0.0.0') FOR [VERSION]
GO

CREATE TRIGGER [dbo].[TBDD_3RD_PARTY_MODULES_AFT_INS] ON [dbo].[TBDD_3RD_PARTY_MODULES]
FOR INSERT
AS
	UPDATE TBDD_3RD_PARTY_MODULES SET ADDED_WHEN = GETDATE() FROM INSERTED WHERE TBDD_3RD_PARTY_MODULES.GUID = INSERTED.GUID
GO

CREATE TRIGGER [dbo].[TBDD_3RD_PARTY_MODULES_AFT_UPD] ON [dbo].[TBDD_3RD_PARTY_MODULES]
FOR UPDATE
AS
	UPDATE TBDD_3RD_PARTY_MODULES SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBDD_3RD_PARTY_MODULES.GUID = INSERTED.GUID
GO

INSERT INTO [dbo].[TBDD_3RD_PARTY_MODULES]
           ([ACTIVE]
           ,[NAME]
           ,[LICENSE]
           ,[VERSION])
     VALUES
           (1
           ,'GDPICTURE'
           ,'21182889975216572111813147150675976632'
           ,'1.0.0.0' )
GO


CREATE PROCEDURE PRDD_CHECK_REL_DOCTYPE_MODULE (@MODULE_SHORT VARCHAR(20),@DOCTYPE_ID INTEGER)
AS
DECLARE @MODULEID INTEGER
SELECT @MODULEID = GUID FROM TBDD_MODULES WHERE SHORT_NAME = 'GLOBIX'

If not exists(select guid from TBDD_DOKUMENTART_MODULE where DOKART_ID = @DOCTYPE_ID and MODULE_ID = @MODULEID)
	INSERT INTO TBDD_DOKUMENTART_MODULE (DOKART_ID,MODULE_ID) VALUES (@DOCTYPE_ID,@MODULEID)
GO
IF OBJECT_ID('TBDD_USER_DOKTYPE', 'U') IS  NULL 
BEGIN
	CREATE TABLE [dbo].[TBDD_USER_DOKTYPE](
		[GUID] [int] IDENTITY(1,1) NOT NULL,
		[DOCTYPE_ID] [int] NOT NULL,
		[USER_ID] [int] NOT NULL,
		[ADDED_WHO] [varchar](50) NOT NULL,
		[ADDED_WHEN] [datetime] NULL,
		[CHANGED_WHEN] [datetime] NULL,
	 CONSTRAINT [PKTBDD_USER_DOKTYPE] PRIMARY KEY CLUSTERED 
	(
		[GUID] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
	) ON [PRIMARY];

	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE] ADD  DEFAULT ('Digital Data') FOR [ADDED_WHO];

	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE] ADD  DEFAULT (getdate()) FOR [ADDED_WHEN];
	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE]  WITH CHECK ADD  CONSTRAINT [FK_TBDD_USER_DOKTYPE_DOCTYPE_ID] FOREIGN KEY([DOCTYPE_ID])
	REFERENCES [dbo].[TBDD_DOKUMENTART] ([GUID]);
	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE] CHECK CONSTRAINT [FK_TBDD_USER_DOKTYPE_DOCTYPE_ID];
	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE]  WITH CHECK ADD  CONSTRAINT [FK_TBDD_USER_DOKTYPE_USER_ID] FOREIGN KEY([USER_ID])
	REFERENCES [dbo].[TBDD_USER] ([GUID]);
	ALTER TABLE [dbo].[TBDD_USER_DOKTYPE] CHECK CONSTRAINT [FK_TBDD_USER_DOKTYPE_USER_ID];
END




