﻿    DECLARE @TEMP_DICTIONARYTYPE TABLE (
        ID INT, 
        C_CODENAME NVARCHAR(255) INDEX IX_CODENAME NONCLUSTERED, 
        C_NAME NVARCHAR(255), 
        C_ISCUSTOMITEMALLOWED NVARCHAR(1),
        C_ISCOLOREDITABLE NVARCHAR(1), 
        C_DESCRIPTION NVARCHAR(255)
    )

	INSERT INTO @TEMP_DICTIONARYTYPE (ID, C_CODENAME, C_NAME, C_ISCUSTOMITEMALLOWED, C_ISCOLOREDITABLE, C_DESCRIPTION) 
        VALUES
			(110, N'UzenetJogkorTipus', N'Üzenet jogkör típus', 'F', 'F', N'NULL')

DECLARE @TypeId int

DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT C_INTEZMENYID, C_TANEVID
		FROM T_INTEZMENYADATOK

DECLARE @intezmenyId int, @tanevId int

OPEN intezmenytanevekCursor  
FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId

WHILE @@FETCH_STATUS = 0 
BEGIN  
	PRINT N'INTÉZMÉNY ID: ' + CAST(@intezmenyId AS NVARCHAR(10)) + N', TANÉV ID: ' + CAST(@tanevId AS NVARCHAR(10))

	SET IDENTITY_INSERT T_DICTIONARYTYPE ON

	INSERT INTO T_DICTIONARYTYPE (ID, C_CODENAME, C_NAME, C_ISCUSTOMITEMALLOWED, C_ISCOLOREDITABLE, C_DESCRIPTION, C_INTEZMENYID, C_TANEVID)
        SELECT
            ID
            ,C_CODENAME
            ,C_NAME
            ,C_ISCUSTOMITEMALLOWED
            ,C_ISCOLOREDITABLE
            ,C_DESCRIPTION
            ,@IntezmenyId AS C_INTEZMENYID
            ,@TanevId AS C_TANEVID
        FROM @TEMP_DICTIONARYTYPE tdt
        WHERE NOT EXISTS (SELECT 1 FROM dbo.T_DICTIONARYTYPE dt WHERE dt.ID = tdt.ID AND dt.C_INTEZMENYID = @IntezmenyId AND dt.C_TANEVID = @TanevId)

    SET IDENTITY_INSERT T_DICTIONARYTYPE OFF

	IF (@TypeId IS NULL) BEGIN
		SELECT @TypeId = ID
		FROM T_DICTIONARYTYPE
		WHERE C_CODENAME = N'UzenetJogkorTipus'
	END

	SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] ON

		INSERT INTO dbo.T_DICTIONARYITEMBASE (ID, C_NAME, C_NAME_1, C_NAME_2, C_NAME_3, C_NAME_4, C_TYPE, C_PROTECTED, C_VISIBLE, C_ORDER, C_CODE, C_VALUE, C_COLOR, C_DICTIONARYTYPEID, C_INTEZMENYID, C_TANEVID, TOROLT, SERIAL, LASTCHANGED, CREATED, MODIFIER, CREATOR)
		VALUES
	        (6952, N'Tanuló', N'Tanuló', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 1, NULL, 1, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6953, N'Gondviselő', N'Gondviselő', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 2, NULL, 2, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6954, N'Korlátozott pedagógus', N'Korlátozott pedagógus', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 3, NULL, 3, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6955, N'Pedagógus', N'Pedagógus', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 4, NULL, 4, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6956, N'Osztályfőnök', N'Osztályfőnök', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 5, NULL, 5, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6957, N'Kiemelt pedagógus', N'Kiemelt pedagógus', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 6, NULL, 6, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6958, N'Vezető', N'Vezető', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 7, NULL, 7, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6959, N'Középfenntartó', N'Középfenntartó', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 8, NULL, 8, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL),
            (6960, N'Fenntartó', N'Fenntartó', NULL, NULL, NULL, N'UzenetJogkorTipus', N'T', N'T', 9, NULL, 9, NULL, @TypeId, @IntezmenyId, @TanevId, 'F', 0, GETDATE(), GETDATE(), NULL, NULL)

    SET IDENTITY_INSERT [dbo].[T_DICTIONARYITEMBASE] OFF

		INSERT INTO [dbo].[T_UZENETJOGKORTIPUS] ([ID], [C_ALINTEZMENYID], [C_ALTANEVID]) VALUES
			(6952, @IntezmenyId, @TanevId),
			(6953, @IntezmenyId, @TanevId),
			(6954, @IntezmenyId, @TanevId),
			(6955, @IntezmenyId, @TanevId),
			(6956, @IntezmenyId, @TanevId),
			(6957, @IntezmenyId, @TanevId),
			(6958, @IntezmenyId, @TanevId),
			(6959, @IntezmenyId, @TanevId),
			(6960, @IntezmenyId, @TanevId)

	FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor

GO
