﻿DECLARE intezmenytanevekCursor CURSOR LOCAL FOR 
	SELECT IntezmenyAdatok.C_INTEZMENYID, IntezmenyAdatok.C_TANEVID
		FROM T_INTEZMENYADATOK IntezmenyAdatok
    INNER JOIN T_TANEV Tanev ON Tanev.ID = C_TANEVID
      AND Tanev.TOROLT = 'F'
      AND Tanev.C_AKTIV = 'T'

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))

  DECLARE @ETKEZESIADMINID INT = NULL

  SELECT TOP 1 @ETKEZESIADMINID = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 7703 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ETKEZESIADMINID IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR
  			   ([C_SZEREPKORTIPUS]
  			   ,[C_INTEZMENYID]
  			   ,[C_TANEVID]
  			   ,[SERIAL]
  			   ,[LASTCHANGED]
  			   ,[CREATED]
  			   ,[MODIFIER]
  			   ,[CREATOR])
  		 VALUES
  			   (7703  -- Étkezés Admin
  			   ,@intezmenyId
  			   ,@tanevId
  			   ,0
  			   ,GETDATE()
  			   ,GETDATE()
  			   ,NULL
  			   ,1)
  	SET @ETKEZESIADMINID = SCOPE_IDENTITY()	
  END

  DECLARE @ETKEZESIELLENORZOID INT = NULL

  SELECT TOP 1 @ETKEZESIELLENORZOID = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 7704 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ETKEZESIELLENORZOID IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR
  			   ([C_SZEREPKORTIPUS]
  			   ,[C_INTEZMENYID]
  			   ,[C_TANEVID]
  			   ,[SERIAL]
  			   ,[LASTCHANGED]
  			   ,[CREATED]
  			   ,[MODIFIER]
  			   ,[CREATOR])
  		 VALUES
  			   (7704  -- Étkezés Ellenőrző
  			   ,@intezmenyId
  			   ,@tanevId
  			   ,0
  			   ,GETDATE()
  			   ,GETDATE()
  			   ,NULL
  			   ,1)
  	SET @ETKEZESIELLENORZOID = SCOPE_IDENTITY()	
  END

  DECLARE @TEREMBERLETJELENLETELLENORZOID INT = NULL

  SELECT TOP 1 @TEREMBERLETJELENLETELLENORZOID = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 7705 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@TEREMBERLETJELENLETELLENORZOID IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR
  			   ([C_SZEREPKORTIPUS]
  			   ,[C_INTEZMENYID]
  			   ,[C_TANEVID]
  			   ,[SERIAL]
  			   ,[LASTCHANGED]
  			   ,[CREATED]
  			   ,[MODIFIER]
  			   ,[CREATOR])
  		 VALUES
  			   (7705  -- Terembérlet Jelenlét Ellenőrző
  			   ,@intezmenyId
  			   ,@tanevId
  			   ,0
  			   ,GETDATE()
  			   ,GETDATE()
  			   ,NULL
  			   ,1)
  	SET @TEREMBERLETJELENLETELLENORZOID = SCOPE_IDENTITY()	
  END

  DECLARE @ESLID INT = NULL

  SELECT TOP 1 @ESLID = ID FROM dbo.T_SZEREPKOR WHERE C_SZEREPKORTIPUS = 7702 AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ESLID IS NULL)  BEGIN
  	INSERT INTO [dbo].T_SZEREPKOR
  			   ([C_SZEREPKORTIPUS]
  			   ,[C_INTEZMENYID]
  			   ,[C_TANEVID]
  			   ,[SERIAL]
  			   ,[LASTCHANGED]
  			   ,[CREATED]
  			   ,[MODIFIER]
  			   ,[CREATOR])
  		 VALUES
  			   (7702  -- ESL
  			   ,@intezmenyId
  			   ,@tanevId
  			   ,0
  			   ,GETDATE()
  			   ,GETDATE()
  			   ,NULL
  			   ,1)
  	SET @ESLID = SCOPE_IDENTITY()	
  END

    DECLARE @TEREMBERLETJELENLETELLENORZOJOGID INT = NULL

  SELECT TOP 1 @TEREMBERLETJELENLETELLENORZOJOGID = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'TeremberletJelenletEllenorzo' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@TEREMBERLETJELENLETELLENORZOJOGID IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'TeremberletJelenletEllenorzo', -- C_JOG - nvarchar
  		7705, -- TeremberletJelenletEllenorzo
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @TEREMBERLETJELENLETELLENORZOJOGID = SCOPE_IDENTITY()
  END

  DECLARE @ETKEZESIADMINJOGID INT = NULL

  SELECT TOP 1 @ETKEZESIADMINJOGID = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'EtkezesiAdmin' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ETKEZESIADMINJOGID IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'EtkezesiAdmin', -- C_JOG - nvarchar
  		7703, -- TeremberletJelenletEllenorzo
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @ETKEZESIADMINJOGID = SCOPE_IDENTITY()
  END

  DECLARE @ETKEZESIELLENORZOJOGID INT = NULL

  SELECT TOP 1 @ETKEZESIELLENORZOJOGID = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'EtkezesiEllenorzo' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ETKEZESIELLENORZOJOGID IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'EtkezesiEllenorzo', -- C_JOG - nvarchar
  		7704, -- TeremberletJelenletEllenorzo
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @ETKEZESIELLENORZOJOGID = SCOPE_IDENTITY()
  END

  DECLARE @ESLJOGID INT = NULL

  SELECT TOP 1 @ESLJOGID = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'ESL' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@ESLJOGID IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'ESL', -- C_JOG - nvarchar
  		7702, -- TeremberletJelenletEllenorzo
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @ESLJOGID = SCOPE_IDENTITY()
  END

  DECLARE @GAZDASAGIUGYINTEZOJOGID INT = NULL

  SELECT TOP 1 @GAZDASAGIUGYINTEZOJOGID = ID FROM dbo.T_JOGOSULTSAG WHERE C_JOG = 'GazdasagiUgyintezo' AND C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId
  IF(@GAZDASAGIUGYINTEZOJOGID IS NULL)
  BEGIN
  	INSERT INTO dbo.T_JOGOSULTSAG
  	(
  		C_JOG,  C_SZEREPKORTIPUS,[C_INTEZMENYID] , [C_TANEVID],  TOROLT,  SERIAL,  CREATED,  CREATOR 
  	)
  	VALUES
  	(
  		N'GazdasagiUgyintezo', -- C_JOG - nvarchar
  		7698, -- TeremberletJelenletEllenorzo
  		@intezmenyId , @tanevId,
  		'F', -- TOROLT - char
  		0, -- SERIAL - int
  		GETDATE(), -- CREATED - datetime
  		0 -- CREATOR - int
  	)

  	SET @GAZDASAGIUGYINTEZOJOGID = SCOPE_IDENTITY()
  END

  FETCH NEXT FROM intezmenytanevekCursor INTO @intezmenyId, @tanevId
END  

CLOSE intezmenytanevekCursor  
DEALLOCATE intezmenytanevekCursor
