﻿DROP PROCEDURE IF EXISTS [dev].[uspGenerateTanevAdatokFromExistingTanev]
GO

CREATE PROCEDURE [dev].[uspGenerateTanevAdatokFromExistingTanev]
	 @intezmenyId int
	,@tanevNeveNew nvarchar(20)
	,@tanevNeveSource nvarchar(20)
	,@kovetkezoTanev bit
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @intezmenyAzonosito nvarchar(30)
  SELECT @intezmenyAzonosito  = C_AZONOSITO FROM T_INTEZMENY WHERE ID = @intezmenyID
  -- print 'INTÉZMÉNY AZONOSITO: ' + @intezmenyAzonosito
  
	--------------------------- TANÉV LÉTREHOZÁSA ---------------------------
  DECLARE @tanevIdSource int 
  SELECT @tanevIdSource = ID FROM T_TANEV WHERE C_INTEZMENYID = @intezmenyID AND C_NEV = @tanevNeveSource AND TOROLT = 'F' AND C_AKTIV='T'
  
  IF @tanevIdSource IS NULL BEGIN
    -- print @tanevNeveSource + ' tanév nem létezik: intezmény ' + CAST(@intezmenyId as varchar(20))
    RETURN
  END
  IF EXISTS (SELECT 1 FROM T_TANEV WHERE C_INTEZMENYID = @intezmenyID AND C_NEV = @tanevNeveNew AND TOROLT = 'F' AND C_ISKIVALASZTHATO='T') BEGIN
    -- print @tanevNeveNew + ' tanév már létezik: intezmény ' + CAST(@intezmenyId as varchar(20))
    RETURN
  END
    
  DECLARE @tanevID INT
  SELECT @tanevID = ID FROM T_TANEV WHERE C_INTEZMENYID=@intezmenyId AND C_NEV = @tanevNeveNew AND TOROLT = 'F' AND C_ISKIVALASZTHATO='F'
  IF @tanevID IS NOT NULL
  BEGIN
    UPDATE T_TANEV SET C_KOVETKEZO='T', C_ISKIVALASZTHATO='T' WHERE ID = @tanevID
  END
  ELSE
	BEGIN
	INSERT INTO dbo.T_TANEV (
     C_AKTIV
    ,C_ELSOTANITASINAP
    ,C_KEZDONAP
    ,C_KOVETKEZO
    ,C_NEV
    ,C_SORSZAM
    ,C_UTOLSONAP
    ,C_UTOLSOTANITASINAP
    ,C_INTEZMENYID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,C_ISKIVALASZTHATO
  ) VALUES(
     'F'
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 1, 4) + '0901', 21)
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 1, 4) + '0901', 21)
    ,CASE WHEN @kovetkezoTanev = 1 THEN 'T' ELSE 'F' END
    ,@tanevNeveNew
    ,IIF(ISNUMERIC(SUBSTRING(@tanevNeveNew, 3, 2)) = 1, SUBSTRING(@tanevNeveNew, 3, 2) * 2, 0)
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 6, 4) + '0831', 21)
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 6, 4) + '0831', 21)
    ,@intezmenyID
    ,'F'
    ,0
    ,getdate()
    ,getdate()
    ,null
    ,null
    ,'T'
  )
	SET @tanevID = SCOPE_IDENTITY()
  END
     
	-- print @tanevNeveNew + ' tanév létrehozása sikeres!';
	--------------------------- TANÉV LÉTREHOZÁSA VÉGE ---------------------------
	--------------------------- SZÓTÁRELEMEK LÉTREHOZÁSA ---------------------------
 
  SET IDENTITY_INSERT T_DICTIONARYTYPE ON
  INSERT INTO T_DICTIONARYTYPE (
     ID
    ,C_CODENAME
    ,C_DESCRIPTION
    ,C_ISCOLOREDITABLE
    ,C_ISCUSTOMITEMALLOWED
    ,C_NAME
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  ) SELECT 
     ID
    ,dt.C_CODENAME AS C_CODENAME              -- C_CODENAME - nvarchar(30)
    ,dt.C_DESCRIPTION AS C_DESCRIPTION          -- C_DESCRIPTION - nvarchar(255)
    ,dt.C_ISCOLOREDITABLE  AS C_ISCOLOREDITABLE       -- C_ISCOLOREDITABLE - char(1)
    ,dt.C_ISCUSTOMITEMALLOWED  AS C_ISCUSTOMITEMALLOWED   -- C_ISCUSTOMITEMALLOWED - char(1)
    ,dt.C_NAME AS C_NAME                  -- C_NAME - nvarchar(50)
    ,dt.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,dt.TOROLT  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
  FROM T_DICTIONARYTYPE dt
  WHERE dt.C_TANEVID =  @tanevIdSource
    AND dt.C_INTEZMENYID = @intezmenyId
	  AND NOT EXISTS(SELECT 1 FROM T_DICTIONARYTYPE WHERE ID = dt.ID AND C_TANEVID=@tanevID)
  SET IDENTITY_INSERT T_DICTIONARYTYPE OFF
  
  SET IDENTITY_INSERT T_DICTIONARYITEMBASE ON
  INSERT INTO T_DICTIONARYITEMBASE (
     ID
    ,C_VALUE
    ,C_NAME
    ,C_NAME_1
    ,C_NAME_2
    ,C_NAME_3
    ,C_NAME_4
    ,C_VISIBLE
    ,C_CODE
    ,C_TYPE
    ,C_PROTECTED
    ,C_COLOR
    ,C_ORDER
    ,C_BGCOLOR
    ,C_DESCRIPTION
    ,C_ISACTIVE
    ,C_SHORTNAME
    ,C_DICTIONARYTYPEID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT
     ID
    ,C_VALUE AS C_VALUE                -- C_VALUE - int
    ,C_NAME AS C_NAME                  -- C_NAME - nvarchar(250)
    ,C_NAME_1 AS C_NAME_1               -- C_NAME_1 - nvarchar(250)
    ,C_NAME_2 AS C_NAME_2               -- C_NAME_2 - nvarchar(250)
    ,C_NAME_3 AS C_NAME_3               -- C_NAME_3 - nvarchar(250)
    ,C_NAME_4 AS C_NAME_4               -- C_NAME_4 - nvarchar(250)
    ,C_VISIBLE  AS C_VISIBLE               -- C_VISIBLE - char(1)
    ,C_CODE AS C_CODE                 -- C_CODE - nvarchar(20)
    ,C_TYPE AS C_TYPE                  -- C_TYPE - nvarchar(32)
    ,C_PROTECTED  AS C_PROTECTED             -- C_PROTECTED - char(1)
    ,C_COLOR AS C_COLOR                -- C_COLOR - nvarchar(6)
    ,C_ORDER AS C_ORDER                -- C_ORDER - int
    ,C_BGCOLOR AS C_BGCOLOR              -- C_BGCOLOR - nvarchar(6)
    ,C_DESCRIPTION AS C_DESCRIPTION          -- C_DESCRIPTION - nvarchar(255)
    ,C_ISACTIVE  AS C_ISACTIVE              -- C_ISACTIVE - char(1)
    ,C_SHORTNAME AS C_SHORTNAME            -- C_SHORTNAME - nvarchar(30)
    ,C_DICTIONARYTYPEID   AS C_DICTIONARYTYPEID      -- C_DICTIONARYTYPEID - int
    ,C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,TOROLT  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_DICTIONARYITEMBASE dib
  WHERE dib.C_TANEVID = @tanevIdSource
    AND dib.C_INTEZMENYID = @intezmenyId
	  AND NOT EXISTS(SELECT 1 FROM T_DICTIONARYITEMBASE WHERE ID = dib.ID AND C_TANEVID=@tanevID)
  SET IDENTITY_INSERT T_DICTIONARYITEMBASE OFF
	-- print 'Szótárelemek létrehozása sikeres!';
  INSERT INTO T_DICTIONARYITEMBASENYELV(
     C_NAME
    ,C_NAME_1
    ,C_NAME_2
    ,C_NAME_3
    ,C_NAME_4
    ,C_NYELVID
    ,C_SHORTNAME
    ,C_DICTIONARYITEMBASEID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  )
  SELECT
     dibNy.C_NAME
    ,dibNy.C_NAME_1
    ,dibNy.C_NAME_2
    ,dibNy.C_NAME_3
    ,dibNy.C_NAME_4
    ,dibNyelv.ID
    ,dibNy.C_SHORTNAME
    ,dibNew.ID
    ,@intezmenyId
    ,@tanevId
    ,'F'
    ,0
    ,GETDATE()
    ,GETDATE()
    ,NULL
    ,NULL
    ,dibNy.ID
  FROM T_DICTIONARYITEMBASENYELV dibNy
    INNER JOIN T_DICTIONARYITEMBASE dibNew ON dibNew.ELOZOTANEVIREKORDID=dibNy.C_DICTIONARYITEMBASEID
      AND dibNew.C_TANEVID = @tanevID AND dibNew.TOROLT='F'
    INNER JOIN T_DICTIONARYITEMBASE dibNyelv ON dibNyelv.ELOZOTANEVIREKORDID = dibNy.C_NYELVID AND dibNyelv.TOROLT='F'
  WHERE dibNy.C_TANEVID = @tanevIdSource AND dibNy.TOROLT='F' 
    AND NOT EXISTS (SELECT 1 FROM T_DICTIONARYITEMBASENYELV WHERE TOROLT='F' AND C_DICTIONARYITEMBASEID = dibNew.ID AND C_TANEVID = @tanevId AND TOROLT='F' AND C_NYELVID = dibNyelv.ID)
  INSERT INTO T_OKTNEVELESIKATEGORIA (
     ID
    ,C_FELADATCSOPORTTANULOOSZTALYK
    ,C_ALINTEZMENYID
    ,C_ALTANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,ELOZOTANEVIREKORDIDA
    ,NNID)
  SELECT
     ID
    ,C_FELADATCSOPORTTANULOOSZTALYK
    ,@intezmenyId
    ,@tanevID
    ,'F'
    ,0
    ,GETDATE()
    ,GETDATE()
    ,0
    ,0
    ,ID
    ,ID
    ,NNID
  FROM T_OKTNEVELESIKATEGORIA oktNev
  WHERE C_ALTANEVID=@tanevIdSource AND TOROLT='F'
	  AND NOT EXISTS(SELECT 1 FROM T_OKTNEVELESIKATEGORIA WHERE ELOZOTANEVIREKORDID = oktNev.ID AND C_ALTANEVID=@tanevID AND TOROLT='F')
    
  DECLARE tableCursor CURSOR FOR 
  SELECT DISTINCT OBJECT_NAME(parent_object_id) 
  FROM sys.foreign_key_columns 
  WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE') AND OBJECT_NAME(parent_object_id) NOT IN ('T_OKTNEVELESIKATEGORIA','T_DICTIONARYITEMBASENYELV')
  
  DECLARE @tableName nvarchar(80)
    ,@sql nvarchar(max)
    ,@columnList nvarchar(max)
    ,@insertList nvarchar(max)
    
  OPEN tableCursor
  FETCH NEXT FROM tableCursor into @tableName 
  WHILE @@FETCH_STATUS = 0 BEGIN
    
    SELECT @columnList = ISNULL(@columnList + ', ', '') + c.[name]
    FROM sys.tables t
      INNER JOIN sys.columns c ON c.[object_id] = t.[object_id]
    WHERE t.[name] = @tableName
    ORDER BY c.column_id
    
    SET @insertList = @columnList
    SET @insertList = REPLACE(@insertList, 'C_ALTANEVID', '@tanevID')
    SET @insertList = REPLACE(@insertList, 'SERIAL', '0')
    SET @insertList = REPLACE(@insertList, 'LASTCHANGED', 'GETDATE()')
    SET @insertList = REPLACE(@insertList, 'CREATED', 'GETDATE()')
    SET @insertList = REPLACE(@insertList, 'MODIFIER', 'NULL')
    SET @insertList = REPLACE(@insertList, 'CREATOR', 'NULL')
    SET @insertList = REPLACE(@insertList, 'ELOZOTANEVIREKORDIDA', 'x.ID')
    SET @insertList = REPLACE(@insertList, 'ELOZOTANEVIREKORDID', 'x.ID')
          
  
    SET @sql = N'
      INSERT INTO ' + @tableName + ' (
        ' + @columnList + ' 
      ) SELECT 
        ' + @insertList + '
      FROM ' + @tableName + ' x
      WHERE x.C_ALTANEVID = @tanevIdSource
        AND x.C_ALINTEZMENYID = @intezmenyId
        AND x.TOROLT = ''F''
		AND NOT EXISTS(SELECT 1 FROM ' + @tableName + ' WHERE ID = x.ID AND C_ALTANEVID=@tanevID)'
    -- print @sql
    EXEC sp_executesql @sql, N'@tanevIdSource int, @intezmenyId int, @tanevID int', @tanevIdSource, @intezmenyId, @tanevID
    SET @columnList = NULL
    FETCH NEXT FROM tableCursor into @tableName 
  END
  
  CLOSE tableCursor
  DEALLOCATE tableCursor
	--------------------------- SZÓTÁRELEMEK LÉTREHOZÁSA VÉGE ---------------------------
	--------------------------- INTÉZMÉNYI ADATOK LÉTREHOZÁSA ---------------------------
  INSERT INTO T_INTEZMENYADATOK (
     C_IGAZGATONEVE
    ,C_NEV
    ,C_OMKOD
    ,C_TELEFONSZAM
    ,C_EMAILCIM
    ,C_IRANYITOSZAM
    ,C_VAROS
    ,C_ROVIDNEV
    ,C_ADMINEMAILCIM
    ,C_KRETATIPUS
    ,C_ENGEDELYEZETTALLASHELYEK
    ,C_INTEZMENYHONLAPJA
    ,C_AJTO
    ,C_EMELET
    ,C_HAZSZAM
    ,C_KOZTERULETJELLEG
    ,C_KOZTERULETNEV
    ,C_DOKUMENTUMFEJLEC
    ,C_DOKUMENTUMLABLEC
    ,C_DOKUMENTUMFEJLECHTML
    ,C_DOKUMENTUMLABLECHTML
    ,C_TANEVID
    ,C_INTEZMENYID
    ,C_CIMERID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,C_ISDOKUMENTUMFEJLECMEGJELENIT
    ,C_ISDOKUMENTUMLABLECMEGJELENIT
    ,C_SAPAZONOSITO
    ,C_SNINEVELESIFORMATIPUSID
  ) SELECT 
     C_IGAZGATONEVE AS C_IGAZGATONEVE         -- C_IGAZGATONEVE - nvarchar(255)
    ,C_NEV AS C_NEV                   -- C_NEV - nvarchar(255)
    ,C_OMKOD AS C_OMKOD                 -- C_OMKOD - nvarchar(20)
    ,C_TELEFONSZAM AS C_TELEFONSZAM          -- C_TELEFONSZAM - nvarchar(20)
    ,C_EMAILCIM AS C_EMAILCIM             -- C_EMAILCIM - nvarchar(255)
    ,C_IRANYITOSZAM AS C_IRANYITOSZAM          -- C_IRANYITOSZAM - nvarchar(20)
    ,C_VAROS AS C_VAROS                 -- C_VAROS - nvarchar(255)
    ,C_ROVIDNEV AS C_ROVIDNEV             -- C_ROVIDNEV - nvarchar(255)
    ,C_ADMINEMAILCIM AS C_ADMINEMAILCIM        -- C_ADMINEMAILCIM - nvarchar(255)
    ,C_KRETATIPUS   AS C_KRETATIPUS            -- C_KRETATIPUS - int
    ,C_ENGEDELYEZETTALLASHELYEK AS C_ENGEDELYEZETTALLASHELYEK -- C_ENGEDELYEZETTALLASHELYEK - numeric(10, 2)
    ,C_INTEZMENYHONLAPJA AS C_INTEZMENYHONLAPJA    -- C_INTEZMENYHONLAPJA - nvarchar(300)
    ,C_AJTO AS C_AJTO                 -- C_AJTO - nvarchar(30)
    ,C_EMELET AS C_EMELET               -- C_EMELET - nvarchar(40)
    ,C_HAZSZAM AS C_HAZSZAM              -- C_HAZSZAM - nvarchar(30)
    ,C_KOZTERULETJELLEG AS C_KOZTERULETJELLEG     -- C_KOZTERULETJELLEG - int
    ,C_KOZTERULETNEV AS C_KOZTERULETNEV        -- C_KOZTERULETNEV - nvarchar(40)
    ,C_DOKUMENTUMFEJLEC AS C_DOKUMENTUMFEJLEC     -- C_DOKUMENTUMFEJLEC - nvarchar(MAX)
    ,C_DOKUMENTUMLABLEC AS C_DOKUMENTUMLABLEC     -- C_DOKUMENTUMLABLEC - nvarchar(MAX)
    ,C_DOKUMENTUMFEJLECHTML AS C_DOKUMENTUMFEJLECHTML -- C_DOKUMENTUMFEJLECHTML - nvarchar(MAX)
    ,C_DOKUMENTUMLABLECHTML AS C_DOKUMENTUMLABLECHTML -- C_DOKUMENTUMLABLECHTML - nvarchar(MAX)
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,NULL AS C_CIMERID              -- C_CIMERID - int
    ,'F' AS TOROLT                 -- TOROLT - char(1)
    ,0 AS SERIAL                 -- SERIAL - int
    ,GETDATE() AS LASTCHANGED            -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED                -- CREATED - datetime
    ,MODIFIER AS MODIFIER               -- MODIFIER - int
    ,CREATOR AS CREATOR                -- CREATOR - int
    ,ia.ID AS ELOZOTANEVIREKORDID
    ,ia.C_ISDOKUMENTUMFEJLECMEGJELENIT
    ,ia.C_ISDOKUMENTUMLABLECMEGJELENIT
    ,ia.C_SAPAZONOSITO
    ,ia.C_SNINEVELESIFORMATIPUSID
  FROM T_INTEZMENYADATOK ia
  WHERE ia.C_TANEVID = @tanevIdSource 
    AND ia.C_INTEZMENYID = @intezmenyId 
    AND ia.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_INTEZMENYADATOK WHERE ELOZOTANEVIREKORDID = ia.ID AND TOROLT='F' AND C_TANEVID = @tanevID)
  
	-- print 'Intézményi adatok létrehozása sikeres!';
	DECLARE @intezmenyAdatokID int = SCOPE_IDENTITY();
	--------------------------- INTÉZMÉNYI ADATOK LÉTREHOZÁSA VÉGE ---------------------------
	--------------------------- INTÉZMÉNYI ADATSZOLGÁLTATÁS LÉTREHOZÁSA ---------------------------
	INSERT INTO [dbo].[T_INTEZMENYADATSZOLGALTATAS](
     [C_INTEZMENYID]
    ,[C_TANEVID]
    ,[TOROLT]
    ,[SERIAL]
    ,[LASTCHANGED]
    ,[CREATED]
    ,[MODIFIER]
    ,[CREATOR]
  ) VALUES (
     @intezmenyID
    ,@tanevID
    ,'F'
    ,0
    ,getdate()
    ,getdate()
    ,null
    ,null
  )
  -- print 'INTEZMENYADATSZOLGALTATAS létrehozása sikeres!';
	--------------------------- INTÉZMÉNYI ADATSZOLGÁLTATÁS LÉTREHOZÁSA VÉGE ---------------------------
	--------------------------- TANÉV RENDJE LÉTREHOZÁSA --------------------------------
	EXEC dev.uspCreateTanevRendje @intezmenyId, @tanevId
	--------------------------- TANÉV RENDJE LÉTREHOZÁSA VÉGE ---------------------------
	--------------------------- CSENGETÉSI REND LÉTREHOZÁSA -----------------------------
	
  INSERT INTO T_CSENGETESIREND (
     C_AKTIV
    ,C_NEV
    ,C_MUKODESIHELYID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     C_AKTIV AS C_AKTIV                -- C_AKTIV - char(1)
    ,C_NEV AS C_NEV                   -- C_NEV - nvarchar(50)
    ,NULL AS C_MUKODESIHELYID       -- C_MUKODESIHELYID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,TOROLT  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_CSENGETESIREND
  WHERE C_TANEVID = @tanevIdSource
    AND C_INTEZMENYID = @intezmenyId
    AND TOROLT = 'F'
  INSERT INTO T_CSENGETESIRENDORA (
     C_KEZDETE
    ,C_ORASZAM
    ,C_VEGE
    ,C_CSENGETESIRENDID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     csro.C_KEZDETE AS C_KEZDETE         -- C_KEZDETE - datetime
    ,csro.C_ORASZAM   AS C_ORASZAM               -- C_ORASZAM - int
    ,csro.C_VEGE AS C_VEGE            -- C_VEGE - datetime
    ,csr.ID   AS C_CSENGETESIRENDID      -- C_CSENGETESIRENDID - int
    ,csro.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,csro.TOROLT  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,NULL AS NNID                   -- NNID - int
    ,csro.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_CSENGETESIRENDORA csro
    INNER JOIN T_CSENGETESIREND csr ON csro.C_CSENGETESIRENDID = csr.ELOZOTANEVIREKORDID AND csro.C_INTEZMENYID = csr.C_INTEZMENYID AND csr.C_TANEVID = @tanevId
  WHERE csro.C_TANEVID = @tanevIdSource
    AND csro.C_INTEZMENYID = @intezmenyId
    AND csro.TOROLT='F'
	-- print 'Csengetési rend létrehozása sikeres!';
	--------------------------- CSENGETÉSI REND LÉTREHOZÁSA VÉGE ------------------------
 
	--------------------------- ALAP FELHASZNÁLÓK LÉTREHOZÁSA ---------------------------
	-- Szuper admin létrehozása
	DECLARE @SUPERADMINID INT
	INSERT INTO T_FELHASZNALO (
     C_NEME
    ,C_SZULETESIDATUM
    ,C_SZULETESINEV
    ,C_ANYJANEVE
    ,C_SZULETESIHELY
    ,C_TAJSZAM
    ,C_ALLAMPOLGARSAGA
    ,C_LEIRAS
    ,C_KERESESINEV
    ,C_SZULETESIORSZAG
    ,C_SZULETESINEVSORREND
    ,C_ANYJANEVESORREND
    ,C_ALAPERTELMEZETTLCID
    ,C_LATASSERULTWEBHASZNALATA
    ,C_SMSSZAM
    ,C_SMSSZINKRONIZALT
    ,C_KELLADSZINKRONIZALAS
    ,C_SZULETESINEVELVALASZTO
    ,C_ANYJANEVEELVALASZTO
    ,C_DMSTARTTIME
    ,C_NYOMTATASINEV
    ,C_UTONEV
    ,C_VEZETEKNEV
    ,C_ADOSZAM
    ,C_ELOTAG
    ,C_ANYANYELVE
    ,C_IGAZOLVANYSZAM
    ,C_IGAZOLVANYTIPUSA
    ,C_NEVSORREND
    ,C_NYILVANTARTASISZAM
    ,C_NYILVANTARTASKEZDETE
    ,C_OKTATASIAZONOSITO
    ,C_OMKOD
    ,C_ALAIRAS
    ,C_PROFILKEP
    ,C_KRETAKOD
    ,C_KIRAZONOSITO
    ,C_ALLAMPOLGARSAGA2
    ,C_FENNTARTOAZONOSITO
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,FTIP
  ) SELECT 
     C_NEME   AS C_NEME                  -- C_NEME - int
    ,C_SZULETESIDATUM AS C_SZULETESIDATUM  -- C_SZULETESIDATUM - datetime
    ,C_SZULETESINEV AS C_SZULETESINEV          -- C_SZULETESINEV - nvarchar(64)
    ,C_ANYJANEVE AS C_ANYJANEVE             -- C_ANYJANEVE - nvarchar(64)
    ,C_SZULETESIHELY AS C_SZULETESIHELY        -- C_SZULETESIHELY - nvarchar(50)
    ,C_TAJSZAM AS C_TAJSZAM              -- C_TAJSZAM - nvarchar(9)
    ,C_ALLAMPOLGARSAGA   AS C_ALLAMPOLGARSAGA       -- C_ALLAMPOLGARSAGA - int
    ,C_LEIRAS AS C_LEIRAS               -- C_LEIRAS - nvarchar(2000)
    ,C_KERESESINEV AS C_KERESESINEV          -- C_KERESESINEV - nvarchar(61)
    ,C_SZULETESIORSZAG   AS C_SZULETESIORSZAG       -- C_SZULETESIORSZAG - int
    ,C_SZULETESINEVSORREND  AS C_SZULETESINEVSORREND   -- C_SZULETESINEVSORREND - char(1)
    ,C_ANYJANEVESORREND  AS C_ANYJANEVESORREND      -- C_ANYJANEVESORREND - char(1)
    ,NULL AS C_ALAPERTELMEZETTLCID  -- C_ALAPERTELMEZETTLCID - int
    ,C_LATASSERULTWEBHASZNALATA  AS C_LATASSERULTWEBHASZNALATA -- C_LATASSERULTWEBHASZNALATA - char(1)
    ,C_SMSSZAM AS C_SMSSZAM              -- C_SMSSZAM - int
    ,C_SMSSZINKRONIZALT AS C_SMSSZINKRONIZALT     -- C_SMSSZINKRONIZALT - char(1)
    ,C_KELLADSZINKRONIZALAS AS C_KELLADSZINKRONIZALAS  -- C_KELLADSZINKRONIZALAS - char(1)
    ,C_SZULETESINEVELVALASZTO   AS C_SZULETESINEVELVALASZTO -- C_SZULETESINEVELVALASZTO - int
    ,C_ANYJANEVEELVALASZTO   AS C_ANYJANEVEELVALASZTO   -- C_ANYJANEVEELVALASZTO - int
    ,C_DMSTARTTIME AS C_DMSTARTTIME          -- C_DMSTARTTIME - nvarchar(5)
    ,C_NYOMTATASINEV AS C_NYOMTATASINEV         -- C_NYOMTATASINEV - nvarchar(255)
    ,C_UTONEV AS C_UTONEV               -- C_UTONEV - nvarchar(255)
    ,C_VEZETEKNEV AS C_VEZETEKNEV           -- C_VEZETEKNEV - nvarchar(255)
    ,C_ADOSZAM AS C_ADOSZAM              -- C_ADOSZAM - nvarchar(10)
    ,C_ELOTAG AS C_ELOTAG               -- C_ELOTAG - nvarchar(20)
    ,C_ANYANYELVE AS C_ANYANYELVE           -- C_ANYANYELVE - int
    ,C_IGAZOLVANYSZAM AS C_IGAZOLVANYSZAM       -- C_IGAZOLVANYSZAM - nvarchar(50)
    ,C_IGAZOLVANYTIPUSA AS C_IGAZOLVANYTIPUSA     -- C_IGAZOLVANYTIPUSA - int
    ,C_NEVSORREND AS C_NEVSORREND           -- C_NEVSORREND - char(1)
    ,C_NYILVANTARTASISZAM AS C_NYILVANTARTASISZAM   -- C_NYILVANTARTASISZAM - nvarchar(20)
    ,C_NYILVANTARTASKEZDETE AS C_NYILVANTARTASKEZDETE -- C_NYILVANTARTASKEZDETE - datetime
    ,C_OKTATASIAZONOSITO AS C_OKTATASIAZONOSITO    -- C_OKTATASIAZONOSITO - nvarchar(20)
    ,C_OMKOD AS C_OMKOD                -- C_OMKOD - nvarchar(20)
    ,C_ALAIRAS AS C_ALAIRAS              -- C_ALAIRAS - nvarchar(MAX)
    ,C_PROFILKEP AS C_PROFILKEP            -- C_PROFILKEP - varbinary(MAX)
    ,C_KRETAKOD AS C_KRETAKOD             -- C_KRETAKOD - nvarchar(6)
    ,C_KIRAZONOSITO AS C_KIRAZONOSITO         -- C_KIRAZONOSITO - nvarchar(20)
    ,C_ALLAMPOLGARSAGA2 AS C_ALLAMPOLGARSAGA2     -- C_ALLAMPOLGARSAGA2 - int
    ,C_FENNTARTOAZONOSITO AS C_FENNTARTOAZONOSITO   -- C_FENNTARTOAZONOSITO - nvarchar(20)
    ,C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,TOROLT AS TOROLT                 -- TOROLT - char(1)
    ,0 AS SERIAL                 -- SERIAL - int
    ,GETDATE() AS LASTCHANGED            -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED                -- CREATED - datetime
    ,MODIFIER AS MODIFIER               -- MODIFIER - int
    ,CREATOR AS CREATOR                -- CREATOR - int
    ,NULL AS NNID                   -- NNID - int
    ,NULL AS FTIP                   -- FTIP - varchar(2)
  FROM T_FELHASZNALO 
  WHERE C_INTEZMENYID = @intezmenyId 
    AND C_TANEVID = @tanevIdSource
    AND C_SZULETESINEV = 'Kréta Adminisztrátor'
  SET @SUPERADMINID = SCOPE_IDENTITY()
  -- print 'Szuper admin létrehozása sikeres!';
  -- Szuperadmin e-mail címe
  INSERT INTO T_EMAIL (
     C_EMAILTIPUSA
    ,C_EMAILCIM
    ,C_ALAPERTELMEZETT
    ,C_GONDVISELOID
    ,C_FELHASZNALOID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     C_EMAILTIPUSA AS C_EMAILTIPUSA          -- C_EMAILTIPUSA - int
    ,C_EMAILCIM AS C_EMAILCIM              -- C_EMAILCIM - nvarchar(200)
    ,C_ALAPERTELMEZETT  AS C_ALAPERTELMEZETT       -- C_ALAPERTELMEZETT - char(1)
    ,C_GONDVISELOID AS C_GONDVISELOID         -- C_GONDVISELOID - int
    ,C_FELHASZNALOID AS C_FELHASZNALOID        -- C_FELHASZNALOID - int
    ,@intezmenyId   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,TOROLT AS TOROLT                 -- TOROLT - char(1)
    ,0 AS SERIAL                 -- SERIAL - int
    ,GETDATE() AS LASTCHANGED            -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED                -- CREATED - datetime
    ,MODIFIER AS MODIFIER               -- MODIFIER - int
    ,CREATOR AS CREATOR                -- CREATOR - int
    ,ID AS ELOZOTANEVIREKORDID
  FROM T_EMAIL
  WHERE C_INTEZMENYID = @intezmenyId 
    AND C_TANEVID = @tanevIdSource
    AND C_FELHASZNALOID = (SELECT TOP(1) ID FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevIdSource AND C_SZULETESINEV = 'Kréta Adminisztrátor' AND TOROLT = 'F')
-- Szuperadmin belépés
  INSERT INTO [dbo].[T_FELHASZNALOBELEPES] (
     [C_BEJELENTKEZESINEV]
		,[C_KOTELEZOVALTOZTATNI]
    ,[C_MEGHIUSULTBELEPESEK]
    ,[C_UTOLSOBELEPES]
    ,[C_JELSZO]
    ,[C_JELSZOVALTOZTATASIDEJE]
    ,[C_SO]
    ,[C_FELHASZNALOID]
		,[C_GONDVISELOID]
		,[C_INTEZMENYID]
		,[C_TANEVID]
    ,[SERIAL]
    ,[LASTCHANGED]
    ,[CREATED]
    ,[MODIFIER]
    ,[CREATOR]
	  ,C_GUID	
  ) SELECT 
     [C_BEJELENTKEZESINEV]
		,[C_KOTELEZOVALTOZTATNI]
    ,[C_MEGHIUSULTBELEPESEK]
    ,[C_UTOLSOBELEPES]
    ,[C_JELSZO]
    ,[C_JELSZOVALTOZTATASIDEJE]
    ,[C_SO]
    ,@SUPERADMINID
		,[C_GONDVISELOID]
		,[C_INTEZMENYID]
		,@tanevID
    ,0
    ,GETDATE()
    ,GETDATE()
    ,NULL
    ,NULL
	  ,C_GUID	
  FROM T_FELHASZNALOBELEPES
  WHERE C_INTEZMENYID = @intezmenyId 
    AND C_TANEVID = @tanevIdSource
    AND C_FELHASZNALOID = (SELECT TOP(1) ID FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevIdSource AND C_SZULETESINEV = 'Kréta Adminisztrátor' AND TOROLT = 'F')
  -- print 'Szuper admin belépés létrehozása sikeres!';
  
  
  INSERT INTO T_SZEREPKOR (
     C_SZEREPKORTIPUS
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     szk.C_SZEREPKORTIPUS AS C_SZEREPKORTIPUS       -- C_SZEREPKORTIPUS - int
    ,C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,'F'                 -- TOROLT - char(1)
    ,0 AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,NULL AS NNID                   -- NNID - int
    ,szk.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_SZEREPKOR szk
  WHERE szk.C_INTEZMENYID = @intezmenyId
    AND szk.C_TANEVID = @tanevIdSource
    AND szk.TOROLT = 'F'
    
  -- Rendszer beállítások alap adatok
  INSERT INTO T_RENDSZERBEALLITAS (
     C_ALAPERTEK
    ,C_BEALLITASTIPUS
    ,C_ERTEK
    ,C_ERTEKTIPUS
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     CASE 
      WHEN PATINDEX('%"Date"%', rb.C_ALAPERTEK) = 0 THEN rb.C_ALAPERTEK 
      ELSE REPLACE(REPLACE(rb.C_ALAPERTEK, LEFT(@tanevNeveSource, 4), LEFT(@tanevNeveNew, 4)), RIGHT(@tanevNeveSource, 4), RIGHT(@tanevNeveNew, 4))
     END AS C_ALAPERTEK            -- C_ALAPERTEK - nvarchar(MAX)
    ,rb.C_BEALLITASTIPUS   AS C_BEALLITASTIPUS        -- C_BEALLITASTIPUS - int
    ,CASE 
      WHEN PATINDEX('%"Date"%', rb.C_ERTEK) = 0 THEN rb.C_ERTEK 
      ELSE REPLACE(REPLACE(rb.C_ERTEK, LEFT(@tanevNeveSource, 4), LEFT(@tanevNeveNew, 4)), RIGHT(@tanevNeveSource, 4), RIGHT(@tanevNeveNew, 4))
     END AS C_ERTEK                 -- C_ERTEK - nvarchar(MAX)
    ,rb.C_ERTEKTIPUS   AS C_ERTEKTIPUS            -- C_ERTEKTIPUS - int
    ,rb.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,'F'                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,getdate() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,getdate() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,rb.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_RENDSZERBEALLITAS rb
  WHERE rb.C_INTEZMENYID = @intezmenyId
    AND rb.C_TANEVID = @tanevIdSource
    AND rb.TOROLT = 'F'
    
  
  -- Rendszer értesítések adminnak alapadatok
  INSERT INTO T_RENDSZERERTESITESEK (
    C_BEALLITASOKELLENORZESE,
    C_FRISSITESIDEJE,
    C_INTEZMENYHIANYOSADATOK,
    C_KELLFRISSITES,
    C_ORARENDTANARUTKOZES,
    C_ORARENDTANULOUTKOZESEK,
    C_ORARENDTEREMUTKOZES,
    C_TANAROKHIANYOSOKTATASIAZONOS,
    C_TANTARGYFELOSZTASOSZTALYOKNI,
    C_TANTARGYFELOSZTASTANARNINCSE,
    C_TANTARGYFELOSZTASTANTARGYNIN,
    C_TANULOHIANYOSOKTATASIAZONOSI,
    C_TANULOKOSZTALYBASOROLASA,
	  C_INTEZMENYID,
	  C_TANEVID,
    TOROLT,
    SERIAL,
    LASTCHANGED,
    CREATED,
    MODIFIER,
    CREATOR
  ) VALUES (
    'F', -- C_BEALLITASOKELLENORZESE - char
    GETDATE(), -- C_FRISSITESIDEJE - datetime
    'F', -- C_INTEZMENYHIANYOSADATOK - char
    'F', -- C_KELLFRISSITES - char
    0, -- C_ORARENDTANARUTKOZES - int
    0, -- C_ORARENDTANULOUTKOZESEK - int
    0, -- C_ORARENDTEREMUTKOZES - int
    0, -- C_TANAROKHIANYOSOKTATASIAZONOS - int
    0, -- C_TANTARGYFELOSZTASOSZTALYOKNI - int
    0, -- C_TANTARGYFELOSZTASTANARNINCSE - int
    0, -- C_TANTARGYFELOSZTASTANTARGYNIN - int
    0, -- C_TANULOHIANYOSOKTATASIAZONOSI - int
    0, -- C_TANULOKOSZTALYBASOROLASA - int
  	@intezmenyID, --	C_INTEZMENYID,
  	@tanevId, --C_TANEVID,
    'F', -- TOROLT - char
    0, -- SERIAL - int
    GETDATE(), -- LASTCHANGED - datetime
    GETDATE(), -- CREATED - datetime
    NULL, -- MODIFIER - int
    NULL -- CREATOR - int
  )
  INSERT INTO T_JOGOSULTSAG (
     C_JOG
    ,C_SZEREPKORTIPUS
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     j.C_JOG AS C_JOG                   -- C_JOG - nvarchar(50)
    ,j.C_SZEREPKORTIPUS   AS C_SZEREPKORTIPUS        -- C_SZEREPKORTIPUS - int
    ,j.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,@tanevID   AS C_TANEVID               -- C_TANEVID - int
    ,'F'  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,GETDATE() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,j.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_JOGOSULTSAG j
  WHERE j.C_INTEZMENYID = @intezmenyId
    AND j.C_TANEVID = @tanevIdSource
    AND j.TOROLT = 'F'
  IF EXISTS (SELECT 1 FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevId AND C_SZULETESINEV = 'Kréta Adminisztrátor' AND TOROLT = 'F') BEGIN
    INSERT INTO T_FELHASZNALO_SZEREPKOR (C_FELHASZNALOID, C_SZEREPKORID)
    VALUES ((
        SELECT TOP(1) ID 
        FROM T_FELHASZNALO 
        WHERE C_INTEZMENYID = @intezmenyId 
          AND C_TANEVID = @tanevId 
          AND C_SZULETESINEV = 'Kréta Adminisztrátor' 
          AND TOROLT = 'F'
      ), (
        SELECT TOP(1) ID 
        FROM dbo.T_SZEREPKOR 
        WHERE C_SZEREPKORTIPUS = 1758 -- Admin
          AND C_INTEZMENYID = @intezmenyID 
          AND C_TANEVID = @tanevID 
          AND TOROLT = 'F'
      )
    )
    INSERT INTO T_JOGOSULTSAG_FELHASZNALO (C_FELHASZNALOID, C_JOGOSULTSAGID)
    VALUES ((
        SELECT TOP(1) ID 
        FROM T_FELHASZNALO 
        WHERE C_INTEZMENYID = @intezmenyId 
          AND C_TANEVID = @tanevId 
          AND C_SZULETESINEV = 'Kréta Adminisztrátor' 
          AND TOROLT = 'F'
      ), (
        SELECT TOP(1) ID 
        FROM dbo.T_JOGOSULTSAG 
        WHERE C_SZEREPKORTIPUS = 1758 -- Szuperadmin
          AND C_JOG = 'Szuperadmin'
          AND C_INTEZMENYID = @intezmenyID 
          AND C_TANEVID = @tanevID 
          AND TOROLT = 'F'
      )
    ) 
  END 
  
  DECLARE @schemaName nvarchar(50)
  SELECT @schemaName = 'KR_' + C_AZONOSITO + '_Schema' FROM T_INTEZMENY WHERE ID = @intezmenyId
  
  -- á hét bé hét
--------------------------- NAPTÁRI HÉT LÉTREHOZÁSA --------------------------------
 -- DECLARE @sql nvarchar(max)
  SET @sql ='EXEC  ['+@schemaName+'].[sp_GenerateNaptariHetek] @intezmenyId = @intezmenyID, @tanevId = @tanevID '
  EXEC sp_executesql @sql, N'@intezmenyId INT, @tanevId INT', @intezmenyID, @tanevID
  -- print 'Naptári hetek létrehozása sikeres!';
  --------------------------- NAPTÁRI HÉT LÉTREHOZÁSA VÉGE --------------------------------
  --------------------------- NAPTÁRI NAP LÉTREHOZÁSA --------------------------------
  SET @sql ='EXEC  ['+@schemaName+'].[sp_GenerateNaptariNapok] @IntezmenyId = @intezmenyID, @TanevId = @tanevID '
  EXEC sp_executesql @sql, N'@intezmenyID INT, @tanevID INT', @intezmenyID, @tanevID
  -- print 'Naptári napok létrehozása sikeres!'
--------------------------- NAPTÁRI NAP LÉTREHOZÁSA VÉGE --------------------------------
  INSERT INTO T_EVFOLYAMTIPUS_OKTATASINEVELE (C_EVFOLYAMTIPUSID, C_OKTATASINEVELESIFELADATID, C_INTEZMENYID, C_TANEVID)
  SELECT C_EVFOLYAMTIPUSID, C_OKTATASINEVELESIFELADATID, C_INTEZMENYID, @tanevID 
  FROM T_EVFOLYAMTIPUS_OKTATASINEVELE
  WHERE C_TANEVID = @tanevIdSource
    AND C_INTEZMENYID = @intezmenyId
  -- print N'Oktatási-nevelési feladatok és ajánlott évfolyamok létrehozása sikeres!';
	
  --------------------------- Csoporttípus és foglalkozástípus összerendelés (kapcsolat) --------------------------------
  INSERT INTO T_CSOPORTTIPUS_FOGLALKOZASTIPU (C_FOGLALKOZASTIPUSID, C_CSOPORTTIPUSID, C_CSOPORTBONTOTT, C_INTEZMENYID, C_TANEVID) 
  SELECT C_FOGLALKOZASTIPUSID, C_CSOPORTTIPUSID, C_CSOPORTBONTOTT, C_INTEZMENYID, @tanevID 
  FROM T_CSOPORTTIPUS_FOGLALKOZASTIPU
  WHERE C_TANEVID = @tanevIdSource
    AND C_INTEZMENYID = @intezmenyId
 	
  -- print N'Csoporttípus és foglalkozástípus összerendelésének létrehozása sikeres!';
  -- Működési helyek migrációja
  INSERT INTO T_MUKODESIHELY (
     C_AJTO
    ,C_EMAILCIM
    ,C_EMELET
    ,C_FAX
    ,C_HAZSZAM
    ,C_INTEZMENYADATOKID
    ,C_INTEZMENYID
    ,C_IRANYITOSZAM
    ,C_KOZTERULETJELLEG
    ,C_KOZTERULETNEV
    ,C_MUKODESIHELYAZONOSITO
    ,C_MUKODESIHELYTIPUSA
	,C_FUNKCIOTERULET
    ,C_NEV
    ,C_SZEKHELY
    ,C_TAGINTEZMENY
    ,C_TAGINTEZMENYIKOD
    ,C_TANEVID
    ,C_TELEFONSZAM
    ,C_VAROS
    ,C_VEZETONEVE
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     C_AJTO AS C_AJTO
    ,C_EMAILCIM AS C_EMAILCIM
    ,C_EMELET AS C_EMELET
    ,C_FAX AS C_FAX
    ,C_HAZSZAM AS C_HAZSZAM
    ,(SELECT ID FROM T_INTEZMENYADATOK WHERE ELOZOTANEVIREKORDID = t.C_INTEZMENYADATOKID AND C_TANEVID = @tanevID AND C_INTEZMENYID = @intezmenyID) AS C_INTEZMENYADATOKID
    ,C_INTEZMENYID   AS C_INTEZMENYID
    ,C_IRANYITOSZAM AS C_IRANYITOSZAM
    ,IIF((SELECT COUNT(1) FROM T_KOZTERULETJELLEG WHERE C_ALTANEVID = @tanevID AND ID = t.C_KOZTERULETJELLEG) = 0,911,t.C_KOZTERULETJELLEG) AS C_KOZTERULETJELLEG
    ,C_KOZTERULETNEV AS C_KOZTERULETNEV
    ,C_MUKODESIHELYAZONOSITO AS C_MUKODESIHELYAZONOSITO
    ,IIF((SELECT COUNT(1) FROM T_MUKODESIHELYTIPUS WHERE C_ALTANEVID = @tanevID AND ID = t.C_MUKODESIHELYTIPUSA) = 0,5432,t.C_MUKODESIHELYTIPUSA) AS C_MUKODESIHELYTIPUSA
	  ,C_FUNKCIOTERULET
    ,C_NEV AS C_NEV
    ,C_SZEKHELY AS C_SZEKHELY
    ,C_TAGINTEZMENY AS C_TAGINTEZMENY
    ,C_TAGINTEZMENYIKOD AS C_TAGINTEZMENYIKOD
    ,@tanevID   AS C_TANEVID  
    ,C_TELEFONSZAM AS C_TELEFONSZAM
    ,C_VAROS AS C_VAROS
    ,C_VEZETONEVE AS C_VEZETONEVE
    ,'F' AS TOROLT        
    ,0 AS SERIAL             
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID    
    ,ID AS ELOZOTANEVIREKORDID
  FROM T_MUKODESIHELY t 
  WHERE C_TANEVID = @tanevIdSource AND t.TOROLT = 'F' AND t.C_INTEZMENYID=@intezmenyID
    AND NOT EXISTS (SELECT 1 FROM T_MUKODESIHELY WHERE TOROLT='F' AND C_TANEVID = @tanevId AND ELOZOTANEVIREKORDID = t.ID)
	-- print N'Működési helyek migrációja létrehozása sikeres!';
  -- Feladatellátási helyek migrációja
  INSERT INTO T_FELADATELLATASIHELY (
     C_OKTATASINEVELESIFELADATTIPUS
    ,C_ENGEDELYEZETTOSSZLETSZAM
    ,C_ENGEDELYEZETTOSZTALY
    ,C_FELADATELLATASIHELYKOD
    ,C_MUKODESIHELYID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_OKTATASINEVELESIFELADATTIPUS AS C_OKTATASINEVELESIFELADATTIPUS
    ,NULL AS C_ENGEDELYEZETTOSSZLETSZAM
    ,NULL AS C_ENGEDELYEZETTOSZTALY
    ,t.C_FELADATELLATASIHELYKOD
    ,mh.ID
    ,t.C_INTEZMENYID   AS C_INTEZMENYID
    ,@tanevID   AS C_TANEVID  
    ,'F' AS TOROLT        
    ,0 AS SERIAL             
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID    
    ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_FELADATELLATASIHELY t 
	  INNER JOIN T_MUKODESIHELY mh ON mh.ELOZOTANEVIREKORDID = t.C_MUKODESIHELYID AND mh.TOROLT='F' AND  mh.C_TANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource AND t.TOROLT = 'F' AND t.C_INTEZMENYID=@intezmenyID
    AND NOT EXISTS (SELECT 1 FROM T_FELADATELLATASIHELY WHERE TOROLT = 'F' AND ELOZOTANEVIREKORDID = t.ID AND C_TANEVID = @tanevId)
	-- print N'Ellátott feladatok migrációja létrehozása sikeres!';

  -- Alkalmazottak (Felhasználó) migrációja
  INSERT INTO T_FELHASZNALO (
  	 C_ADOSZAM
  	,C_ALAIRAS
  	,C_ALAPERTELMEZETTLCID
  	,C_ALLAMPOLGARSAGA
  	,C_ALLAMPOLGARSAGA2
  	,C_ANYANYELVE
  	,C_ANYJANEVE
  	,C_ANYJANEVEELVALASZTO
  	,C_ANYJANEVESORREND
  	,C_DMSTARTTIME
  	,C_ELOTAG
  	,C_FENNTARTOAZONOSITO
  	,C_IGAZOLVANYSZAM
  	,C_IGAZOLVANYTIPUSA
  	,C_INTEZMENYID
  	,C_KELLADSZINKRONIZALAS
  	,C_KERESESINEV
  	,C_KIRAZONOSITO
  	,C_KRETAKOD
  	,C_LATASSERULTWEBHASZNALATA
  	,C_LEIRAS
  	,C_NEME
  	,C_NEVSORREND
  	,C_NYILVANTARTASISZAM
  	,C_NYILVANTARTASKEZDETE
  	,C_NYOMTATASINEV
  	,C_OKTATASIAZONOSITO
  	,C_OMKOD
  	,C_PROFILKEP
  	,C_SMSSZAM
  	,C_SMSSZINKRONIZALT
  	,C_SZULETESIDATUM
  	,C_SZULETESIHELY
  	,C_SZULETESINEV
  	,C_SZULETESINEVELVALASZTO
  	,C_SZULETESINEVSORREND
  	,C_SZULETESIORSZAG
  	,C_TAJSZAM
  	,C_TANEVID
  	,C_UTONEV
  	,C_VEZETEKNEV
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
	  ,NNID
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
	   C_ADOSZAM AS C_ADOSZAM
	  ,C_ALAIRAS AS C_ALAIRAS
	  ,C_ALAPERTELMEZETTLCID AS C_ALAPERTELMEZETTLCID
	  ,C_ALLAMPOLGARSAGA AS C_ALLAMPOLGARSAGA
	  ,C_ALLAMPOLGARSAGA2 AS C_ALLAMPOLGARSAGA2
	  ,C_ANYANYELVE AS C_ANYANYELVE
	  ,C_ANYJANEVE AS C_ANYJANEVE
	  ,C_ANYJANEVEELVALASZTO AS C_ANYJANEVEELVALASZTO
	  ,C_ANYJANEVESORREND AS C_ANYJANEVESORREND
	  ,C_DMSTARTTIME AS C_DMSTARTTIME
	  ,C_ELOTAG AS C_ELOTAG
	  ,C_FENNTARTOAZONOSITO AS C_FENNTARTOAZONOSITO
	  ,C_IGAZOLVANYSZAM AS C_IGAZOLVANYSZAM
	  ,C_IGAZOLVANYTIPUSA
	  ,@intezmenyID AS C_INTEZMENYID
	  ,C_KELLADSZINKRONIZALAS AS C_KELLADSZINKRONIZALAS
	  ,C_KERESESINEV AS C_KERESESINEV
	  ,C_KIRAZONOSITO AS C_KIRAZONOSITO
	  ,C_KRETAKOD AS C_KRETAKOD
	  ,C_LATASSERULTWEBHASZNALATA AS C_LATASSERULTWEBHASZNALATA
	  ,C_LEIRAS AS C_LEIRAS
	  ,C_NEME AS C_NEME
	  ,C_NEVSORREND AS C_NEVSORREND
	  ,C_NYILVANTARTASISZAM AS C_NYILVANTARTASISZAM
	  ,C_NYILVANTARTASKEZDETE AS C_NYILVANTARTASKEZDETE
	  ,C_NYOMTATASINEV AS C_NYOMTATASINEV
	  ,C_OKTATASIAZONOSITO AS C_OKTATASIAZONOSITO
	  ,C_OMKOD AS C_OMKOD
	  ,C_PROFILKEP AS C_PROFILKEP
	  ,C_SMSSZAM AS C_SMSSZAM
	  ,C_SMSSZINKRONIZALT AS C_SMSSZINKRONIZALT
	  ,C_SZULETESIDATUM AS C_SZULETESIDATUM
	  ,C_SZULETESIHELY AS C_SZULETESIHELY
	  ,C_SZULETESINEV AS C_SZULETESINEV
	  ,C_SZULETESINEVELVALASZTO AS C_SZULETESINEVELVALASZTO
    ,C_SZULETESINEVSORREND AS C_SZULETESINEVSORREND
	  ,C_SZULETESIORSZAG AS C_SZULETESIORSZAG
	  ,C_TAJSZAM AS C_TAJSZAM
	  ,@tanevID AS C_TANEVID
    ,C_UTONEV AS C_UTONEV
    ,C_VEZETEKNEV AS C_VEZETEKNEV
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
    ,ID AS ELOZOTANEVIREKORDID
  FROM T_FELHASZNALO t 
  WHERE C_TANEVID = @tanevIdSource AND t.TOROLT = 'F'
    AND EXISTS (SELECT 1 FROM T_ALKALMAZOTT a 
					INNER JOIN T_MUNKAUGYIADATOK ma ON ma.C_ALKALMAZOTTID=a.ID 
					AND ISNULL(ma.C_ALKALMAZASMEGSZUNESE,'2200-01-01')>=(SELECT C_KEZDONAP FROM T_TANEV WHERE ID = @tanevID) WHERE a.ID = t.ID) 
  
  INSERT INTO T_ALKALMAZOTT (
  	 C_ALINTEZMENYID
	  ,C_KKKEPESITESID
  	,C_ALLANDOHELYETTESITESRE
  	,C_ALTANEVID
  	,C_FOALLAS
  	,C_TANARIAUTOMATAMEGAJANLASENGE
  	,C_TANARIERTEKELESHATARERTEK
    ,C_DIAKOLIMPIA
    ,C_SZAKERTOIVIZSGAELNOKI
    ,C_SZAKVIZSGA
    ,C_TOVABBKEPZES
    ,C_ORAKEGYHUZAMBAN
    ,C_KOZOSSEGISZOLGALATKEZELO
    ,C_ELFOGADOTTSZANDEKNYILATKOZAT
    ,C_SZTSZKOD
  	,ID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDIDA
    ,ELOZOTANEVIREKORDID
  ) SELECT 
	   @intezmenyID AS C_ALINTEZMENYID
	  ,(SELECT CASE WHEN DB_NAME() LIKE '%KRETA_KLIK%' THEN a.C_KKKEPESITESID ELSE NULL END)
	  ,C_ALLANDOHELYETTESITESRE AS C_ALLANDOHELYETTESITESRE
	  ,@tanevID AS C_ALTANEVID
	  ,C_FOALLAS AS C_FOALLAS
	  ,C_TANARIAUTOMATAMEGAJANLASENGE AS C_TANARIAUTOMATAMEGAJANLASENGE
	  ,C_TANARIERTEKELESHATARERTEK AS C_TANARIERTEKELESHATARERTEK
    ,C_DIAKOLIMPIA
    ,C_SZAKERTOIVIZSGAELNOKI
    ,C_SZAKVIZSGA
    ,C_TOVABBKEPZES
    ,C_ORAKEGYHUZAMBAN
    ,C_KOZOSSEGISZOLGALATKEZELO
    ,C_ELFOGADOTTSZANDEKNYILATKOZAT
    ,C_SZTSZKOD
	  ,f.ID AS ID
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,a.ID AS ELOZOTANEVIREKORDIDA
    ,a.ID AS ELOZOTANEVIREKORDID
  FROM T_ALKALMAZOTT a 
    INNER JOIN T_FELHASZNALO f ON f.ELOZOTANEVIREKORDID = a.ID AND f.C_TANEVID = @tanevID
	INNER JOIN T_MUNKAUGYIADATOK ma ON ma.C_ALKALMAZOTTID=a.ID AND ma.C_TANEVID=@tanevIdSource AND ISNULL(ma.C_ALKALMAZASMEGSZUNESE,'2200-01-01')>=(SELECT C_KEZDONAP FROM T_TANEV WHERE ID = @tanevID) AND ma.TOROLT = 'F'
  WHERE a.C_ALTANEVID = @tanevIdSource 
    AND a.TOROLT = 'F'

  INSERT INTO T_MUNKAUGYIADATOK(
  	 C_ALKALMAZASKEZDETE
  	,C_ALKALMAZASMEGSZUNESE
  	,C_ALKALMAZOTTID
  	,C_BESOROLASIFOKOZAT
  	,C_BETOLTETLENALLASHELY
  	,C_FELADATELLATASIHELYID
  	,C_FOGLALKOZTATASTIPUS
  	,C_INTEZMENYID
  	,C_KOTELEZOORASZAM
  	,C_MUNKAIDOKEDVEZMENYOKA
  	,C_MUNKAIDOKEDVEZMENYOKASZOVEG
  	,C_MUNKAIDOKEDVEZMENYORASZAM
  	,C_MUNKAKORTIPUSA
    ,C_CSOKKENTETTMUNKAIDOS
    ,C_SZABADALLASHELY
    ,C_SZAKERTOMESTERPEDAGOGUS
    ,C_UTAZOGYOGYPEDAGOGUS
    ,C_VEZETOIORASZAMOK
  	,C_MUNKAVISZONYTIPUSA
  	,C_NYUGDIJAS
  	,C_PEDAGOGUSSTATUSZA
  	,C_RESZMUNKAIDOSZAZALEK
  	,C_TANEVID
  	,C_TARTOSHELYETTESITES
    ,C_ATTANITO
    ,C_ATTANITOMASINTEZMENYNEL
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
	   t.C_ALKALMAZASKEZDETE AS C_ALKALMAZASKEZDETE
	  ,t.C_ALKALMAZASMEGSZUNESE AS C_ALKALMAZASMEGSZUNESE
	  ,a.ID AS C_ALKALMAZOTTID
	  ,t.C_BESOROLASIFOKOZAT AS C_BESOROLASIFOKOZAT
	  ,t.C_BETOLTETLENALLASHELY AS C_BETOLTETLENALLASHELY
	  ,fh.ID AS C_FELADATELLATASIHELYID
	  ,t.C_FOGLALKOZTATASTIPUS AS C_FOGLALKOZTATASTIPUS
	  ,@intezmenyID AS C_INTEZMENYID
	  ,t.C_KOTELEZOORASZAM AS C_KOTELEZOORASZAM
	  ,t.C_MUNKAIDOKEDVEZMENYOKA AS C_MUNKAIDOKEDVEZMENYOKA
	  ,t.C_MUNKAIDOKEDVEZMENYOKASZOVEG AS C_MUNKAIDOKEDVEZMENYOKASZOVEG
	  ,t.C_MUNKAIDOKEDVEZMENYORASZAM AS C_MUNKAIDOKEDVEZMENYORASZAM
	  ,t.C_MUNKAKORTIPUSA AS C_MUNKAKORTIPUSA
	  ,C_CSOKKENTETTMUNKAIDOS AS C_CSOKKENTETTMUNKAIDOS
	  ,C_SZABADALLASHELY AS C_SZABADALLASHELY
	  ,C_SZAKERTOMESTERPEDAGOGUS AS C_SZAKERTOMESTERPEDAGOGUS
    ,C_UTAZOGYOGYPEDAGOGUS AS C_UTAZOGYOGYPEDAGOGUS
    ,C_VEZETOIORASZAMOK AS C_VEZETOIORASZAMOK
	  ,t.C_MUNKAVISZONYTIPUSA AS C_MUNKAVISZONYTIPUSA
	  ,t.C_NYUGDIJAS AS C_NYUGDIJAS
	  ,t.C_PEDAGOGUSSTATUSZA AS C_PEDAGOGUSSTATUSZA
	  ,t.C_RESZMUNKAIDOSZAZALEK AS C_RESZMUNKAIDOSZAZALEK
	  ,@tanevID AS C_TANEVID
	  ,t.C_TARTOSHELYETTESITES AS C_TARTOSHELYETTESITES
	  ,t.C_ATTANITO
    ,t.C_ATTANITOMASINTEZMENYNEL
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_MUNKAUGYIADATOK t 
    INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA = t.C_ALKALMAZOTTID  
    INNER JOIN T_FELADATELLATASIHELY fh ON fh.ELOZOTANEVIREKORDID = t.C_FELADATELLATASIHELYID
  WHERE t.C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
    AND a.C_ALTANEVID = @tanevID
    AND fh.C_TANEVID = @tanevID    
  
  
  INSERT INTO T_TOVABBIMUNKAUGYIADATOK(
  	 C_ALKALMAZASKEZDETE
  	,C_ALKALMAZASMEGSZUNESE
  	,C_ALKALMAZOTTID
  	,C_BESOROLASIFOKOZAT
  	,C_FELADATELLATASIHELYID
  	,C_FOGLALKOZTATASTIPUS
  	,C_INTEZMENYID
  	,C_KOTELEZOORASZAM
  	,C_MUNKAIDOKEDVEZMENYOKA
  	,C_MUNKAIDOKEDVEZMENYOKASZOVEG
  	,C_MUNKAIDOKEDVEZMENYORASZAM
  	,C_MUNKAKORTIPUSA
  	,C_MUNKAVISZONYTIPUSA
  	,C_NYUGDIJAS
  	,C_RESZMUNKAIDOSZAZALEK
    ,C_VEZETOIORASZAMOK
  	,C_TANEVID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
	   t.C_ALKALMAZASKEZDETE AS C_ALKALMAZASKEZDETE
	  ,t.C_ALKALMAZASMEGSZUNESE AS C_ALKALMAZASMEGSZUNESE
	  ,a.ID AS C_ALKALMAZOTTID
	  ,t.C_BESOROLASIFOKOZAT AS C_BESOROLASIFOKOZAT
	  ,fh.ID AS C_FELADATELLATASIHELYID
	  ,t.C_FOGLALKOZTATASTIPUS AS C_FOGLALKOZTATASTIPUS
	  ,@intezmenyID AS C_INTEZMENYID
	  ,t.C_KOTELEZOORASZAM AS C_KOTELEZOORASZAM
	  ,t.C_MUNKAIDOKEDVEZMENYOKA AS C_MUNKAIDOKEDVEZMENYOKA
	  ,t.C_MUNKAIDOKEDVEZMENYOKASZOVEG AS C_MUNKAIDOKEDVEZMENYOKASZOVEG
	  ,t.C_MUNKAIDOKEDVEZMENYORASZAM AS C_MUNKAIDOKEDVEZMENYORASZAM
	  ,t.C_MUNKAKORTIPUSA AS C_MUNKAKORTIPUSA
	  ,t.C_MUNKAVISZONYTIPUSA AS C_MUNKAVISZONYTIPUSA
	  ,t.C_NYUGDIJAS AS C_NYUGDIJAS
	  ,t.C_RESZMUNKAIDOSZAZALEK AS C_RESZMUNKAIDOSZAZALEK
    ,t.C_VEZETOIORASZAMOK as C_VEZETOIORASZAMOK
	  ,@tanevID AS C_TANEVID
	  ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TOVABBIMUNKAUGYIADATOK t 
    INNER JOIN T_ALKALMAZOTT a ON t.C_ALKALMAZOTTID = a.ELOZOTANEVIREKORDIDA
    INNER JOIN T_FELADATELLATASIHELY fh ON fh.ELOZOTANEVIREKORDID = t.C_FELADATELLATASIHELYID
  WHERE t.C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
    AND a.C_ALTANEVID = @tanevID
    AND fh.C_TANEVID = @tanevID
  INSERT INTO T_CIM(
  	 C_AJTO
  	,C_ALAPERTELMEZETT
  	,C_CIMTIPUSA
  	,C_EMELET
  	,C_EPULET
  	,C_FELHASZNALOID
  	,C_HAZSZAM
  	,C_INTEZMENYID
  	,C_IRANYITOSZAM
  	,C_KOZTERULET
  	,C_KOZTERULETJELLEGE
  	,C_LEPCSOHAZ
  	,C_MEGYE
  	,C_ORSZAG
  	,C_OSSZETETTCIM
  	,C_TANEVID
  	,C_VAROS
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
	   C_AJTO AS C_AJTO
	  ,C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
	  ,t.C_CIMTIPUSA AS C_CIMTIPUSA
	  ,t.C_EMELET AS C_EMELET
	  ,t.C_EPULET AS C_EPULET
	  ,a.ID AS C_FELHASZNALOID
	  ,t.C_HAZSZAM AS C_HAZSZAM
	  ,@intezmenyID AS C_INTEZMENYID
	  ,t.C_IRANYITOSZAM AS C_IRANYITOSZAM
	  ,t.C_KOZTERULET AS C_KOZTERULET
	  ,t.C_KOZTERULETJELLEGE AS C_KOZTERULETJELLEGE
	  ,t.C_LEPCSOHAZ AS C_LEPCSOHAZ
	  ,t.C_MEGYE AS C_MEGYE
	  ,t.C_ORSZAG AS C_ORSZAG
	  ,t.C_OSSZETETTCIM AS C_OSSZETETTCIM
	  ,@tanevID AS C_TANEVID
	  ,t.C_VAROS AS C_VAROS
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
    ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_CIM t 
    INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA = t.C_FELHASZNALOID AND a.C_ALTANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource 
    AND t.TOROLT = 'F'
  INSERT INTO T_EMAIL(
   	 C_EMAILTIPUSA
  	,C_EMAILCIM
  	,C_ALAPERTELMEZETT
  	,C_FELHASZNALOID
    ,C_ISPUBLIC
  	,C_INTEZMENYID
  	,C_TANEVID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_EMAILTIPUSA AS C_EMAILTIPUSA
	  ,t.C_EMAILCIM AS C_EMAILCIM
	  ,t.C_ALAPERTELMEZETT AS C_ALAPERTELMEZETT
	  ,a.ID AS C_FELHASZNALOID
    ,t.C_ISPUBLIC AS C_ISPUBLIC
	  ,@intezmenyID AS C_INTEZMENYID
	  ,@tanevID AS C_TANEVID
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_EMAIL t 
    INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA = t.C_FELHASZNALOID AND a.C_ALTANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource
    AND t.TOROLT = 'F'
  INSERT INTO T_TELEFON(
     C_TELEFONTIPUSA
    ,C_TELEFONSZAM
    ,C_LEIRAS
    ,C_ALAPERTELMEZETT
    ,C_ISPUBLIC
    ,C_GONDVISELOID
    ,C_FELHASZNALOID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_TELEFONTIPUSA
    ,t.C_TELEFONSZAM
    ,t.C_LEIRAS
    ,t.C_ALAPERTELMEZETT
    ,t.C_ISPUBLIC
    ,NULL
    ,a.ID
    ,@intezmenyid
    ,@tanevID
    ,'F'
    ,0
    ,GETDATE()
    ,GETDATE()
    ,NULL
    ,NULL
    ,t.ID
  FROM T_TELEFON t 
    INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA = t.C_FELHASZNALOID AND a.C_ALTANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource
    AND t.TOROLT = 'F'
  INSERT INTO T_PEDAGOGUSELETPALYAMODELL(
     C_FOKOZAT
  	,C_MINOSITESDATUM
  	,C_PORTFOLIOSTATUSZ
  	,C_TANARID
  	,C_INTEZMENYID
  	,C_TANEVID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_FOKOZAT AS C_FOKOZAT
	  ,C_MINOSITESDATUM AS C_MINOSITESDATUM
	  ,t.C_PORTFOLIOSTATUSZ AS C_PORTFOLIOSTATUSZ
	  ,a.ID AS C_TANARID
	  ,@intezmenyID AS C_INTEZMENYID
	  ,@tanevID AS C_TANEVID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_PEDAGOGUSELETPALYAMODELL t 
    INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA = t.C_TANARID AND a.C_ALTANEVID = @tanevID
  WHERE C_TANEVID = @tanevIdSource  AND t.TOROLT = 'F'
  -- print N'Felhasználók létrehozása sikeres!';
  --Tantárgyak migrációja
  INSERT INTO dbo.T_TANTARGY (
     C_GYAKORLATI
    ,C_NEV
    ,C_ROVIDNEV
    ,C_TARGYKATEGORIA
    ,C_TANORANKIVULI
    ,C_IMPORTALT
    ,C_FOTARGYE
	  ,C_ALTANTARGYKENTNYOMTATVANYBAN
	  ,C_NEVNYOMTATVANYBAN
	  ,C_FOTARGYID
    ,C_ANGOLNEV
    ,C_NEMETNEV
    ,C_FONTOSSAG
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
	  ,NNID
	  ,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_GYAKORLATI AS C_GYAKORLATI
    ,t.C_NEV AS C_NEV
    ,t.C_ROVIDNEV AS C_ROVIDNEV
	  ,t.C_TARGYKATEGORIA AS C_TARGYKATEGORIA
	  ,t.C_TANORANKIVULI AS C_TANORANKIVULI
	  ,t.C_IMPORTALT AS C_IMPORTALT
	  ,'T' AS C_FOTARGYE
	  ,t.C_ALTANTARGYKENTNYOMTATVANYBAN AS C_ALTANTARGYKENTNYOMTATVANYBAN
	  ,t.C_NEVNYOMTATVANYBAN AS C_NEVNYOMTATVANYBAN
	  ,NULL AS C_FOTARGYID 
    ,t.C_ANGOLNEV AS C_ANGOLNEV
    ,t.C_NEMETNEV AS C_NEMETNEV
    ,t.C_FONTOSSAG AS C_FONTOSSAG
    ,@intezmenyID AS C_INTEZMENYID
    ,@tanevID AS C_TANEVID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TANTARGY t 
  WHERE t.C_FOTARGYE = 'T' 
    AND C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
    AND C_INTEZMENYID=@intezmenyID
	
  -- print N'Főtárgyak létrehozása sikeres!';
  INSERT INTO dbo.T_TANTARGY (
     C_GYAKORLATI
    ,C_NEV
    ,C_ROVIDNEV
    ,C_TARGYKATEGORIA
    ,C_TANORANKIVULI
    ,C_IMPORTALT
    ,C_FOTARGYE
  	,C_ALTANTARGYKENTNYOMTATVANYBAN
  	,C_NEVNYOMTATVANYBAN
  	,C_FOTARGYID
    ,C_ANGOLNEV
    ,C_NEMETNEV
    ,C_FONTOSSAG
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_GYAKORLATI AS C_GYAKORLATI
    ,t.C_NEV AS C_NEV
    ,t.C_ROVIDNEV AS C_ROVIDNEV
	  ,t.C_TARGYKATEGORIA AS C_TARGYKATEGORIA
	  ,t.C_TANORANKIVULI AS C_TANORANKIVULI
	  ,t.C_IMPORTALT AS C_IMPORTALT
	  ,'F' AS C_FOTARGYE
	  ,t.C_ALTANTARGYKENTNYOMTATVANYBAN AS C_ALTANTARGYKENTNYOMTATVANYBAN
	  ,t.C_NEVNYOMTATVANYBAN AS C_NEVNYOMTATVANYBAN
	  ,t2.ID AS C_FOTARGYID 
    ,t.C_ANGOLNEV AS C_ANGOLNEV
    ,t.C_NEMETNEV AS C_NEMETNEV
    ,t.C_FONTOSSAG AS C_FONTOSSAG
    ,@intezmenyID AS C_INTEZMENYID
    ,@tanevID AS C_TANEVID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TANTARGY t 
	  LEFT JOIN T_TANTARGY t2 ON t2.ELOZOTANEVIREKORDID = t.C_FOTARGYID AND t2.C_TANEVID = @tanevID
  WHERE t.C_FOTARGYE = 'F' 
    AND t.C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
  
  -- print N'Altárgyak létrehozása sikeres!';
  -- Tantervek migrációja
  INSERT INTO T_TANTERV (
  	 C_CSOPORTTIPUSA
  	,C_EVFOLYAMTOL
  	,C_EVFOLYAMIG
  	,C_KERETTANTERVREEPULO
  	,C_NEV
  	,C_MEGJEGYZES
  	,C_INTEZMENYID
  	,C_TANEVID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
    ,C_VEDETT
  	,ELOZOTANEVIREKORDID
  ) SELECT 
	   t.C_CSOPORTTIPUSA AS C_CSOPORTTIPUSA
	  ,t.C_EVFOLYAMTOL AS C_EVFOLYAMTOL
	  ,t.C_EVFOLYAMIG AS C_EVFOLYAMIG
	  ,C_KERETTANTERVREEPULO AS C_KERETTANTERVREEPULO
	  ,C_NEV AS C_NEV
	  ,C_MEGJEGYZES AS C_MEGJEGYZES
    ,@intezmenyID AS C_INTEZMENYID
    ,@tanevID AS C_TANEVID
    ,'F' AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
    ,t.C_VEDETT
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TANTERV t 
  WHERE t.C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
    AND NOT EXISTS(SELECT 1 FROM T_TANTERV WHERE ELOZOTANEVIREKORDID = t.ID AND C_TANEVID = @tanevID AND TOROLT='F')
  	-- print N'Tantervek létrehozása sikeres!';
  INSERT INTO T_ORATERV(
  	 C_EVFOLYAM
  	,C_INTEZMENYID
  	,C_NEV
  	,C_TANEVID
  	,C_TANTERVID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDID
  ) SELECT 
	   t.C_EVFOLYAM AS C_EVFOLYAM
    ,@intezmenyID AS C_INTEZMENYID
    ,t.C_NEV AS C_NEV
	  ,@tanevID AS C_TANEVID
	  ,tt.ID AS C_TANTERVID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_ORATERV t 
    INNER JOIN T_TANTERV tt ON tt.ELOZOTANEVIREKORDID = t.C_TANTERVID AND tt.C_TANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource  AND t.TOROLT = 'F'
  -- print N'Óratervek létrehozása sikeres!';
  INSERT INTO T_ORATERVTARGY(
     C_EVESORASZAM
    ,C_INTEZMENYID
    ,C_ORATERVID
    ,C_TANEVID
    ,C_TANTARGYID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     C_EVESORASZAM AS C_EVESORASZAM
    ,@intezmenyID AS C_INTEZMENYID
    ,ot.ID AS C_ORATERVID
	  ,@tanevID AS C_TANEVID
	  ,tt.ID AS C_TANTARGYID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_ORATERVTARGY t
    INNER JOIN T_ORATERV ot ON ot.ELOZOTANEVIREKORDID = t.C_ORATERVID AND ot.C_TANEVID = @tanevID
    INNER JOIN T_TANTARGY tt ON tt.ELOZOTANEVIREKORDID = t.C_TANTARGYID AND tt.C_TANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource  
    AND t.TOROLT = 'F'
  	-- print N'Óratervek tantárgyainak létrehozása sikeres!';
    --Érettségi tantárgy adatok migrációja
    IF NOT EXISTS (SELECT 1 FROM T_ERETTSEGITANTARGYADATOK WHERE C_TANEVID=@tanevId AND C_INTEZMENYID=@intezmenyId) BEGIN
    /*============INSERT BASED ON : KRETA_1306R==============*/
    INSERT INTO T_ERETTSEGITANTARGYADATOK 
      (C_ERETTSEGISZINT, C_ERETTSEGITANTARGY, C_GYAKORLATMAXPONTSZAM, C_IRASBELIMAXPONTSZAM, C_ISGYAKORLAT, C_ISIRASBELI, C_ISSZOBELI, C_OSSZMAXPONTSZAM, C_SZOBELIMAXPONTSZAM, C_TANEVID, C_INTEZMENYID) VALUES
      (6579, 6591, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6603, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6604, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6605, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6606, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6607, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6595, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId), 
      (6579, 6608, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6609, 60,  60, 'T', 'T', 'T', 150, 30, @tanevId, @intezmenyId),
      (6579, 6610, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6611, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6612, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6613, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6614, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId,@intezmenyId  ),
      (6579, 6615, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId,@intezmenyId  ),
      (6579, 6601, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6616, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6617, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6618, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6619, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6620, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6621, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId), 
      (6579, 6622, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId), 
      (6579, 6623, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId), 
      (6579, 6596, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6579, 6624, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6598, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6593, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6625, 0,  40, 'F', 'T', 'T', 150, 110, @tanevId, @intezmenyId),
      (6579, 6626, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6627, 0,  70, 'F', 'T', 'T', 150, 80, @tanevId, @intezmenyId ),
      (6579, 6628, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6629, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6630, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6631, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6632, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6633, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6599, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId, @intezmenyId),
      (6579, 6634, 120,  0, 'T', 'F', 'T', 150, 30, @tanevId, @intezmenyId),
      (6579, 6635, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6636, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6637, 0,  50, 'F', 'T', 'T', 100, 50, @tanevId,@intezmenyId  ),
      (6579, 6638, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6639, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6597, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6640, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6641, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6642, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6643, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6644, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6645, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6646, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6647, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6648, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6649, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6650, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6651, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6652, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6579, 6653, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6654, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6588, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6589, 0,  100, 'F', 'T', 'T', 100, 0, @tanevId, @intezmenyId ),
      (6579, 6655, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6656, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6602, 0,  50, 'F', 'T', 'T', 100, 50, @tanevId, @intezmenyId ),
      (6579, 6657, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6658, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6659, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6592, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6660, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6661, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6579, 6662, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6663, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6664, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6665, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6666, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6667, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6668, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6579, 6669, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6579, 6670, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6671, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6672, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6673, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6674, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6594, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6675, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6676, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6677, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6678, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6679, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6680, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6681, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6682, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6683, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6684, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId, @intezmenyId),
      (6579, 6685, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6686, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6579, 6687, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6579, 6688, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6579, 6689, 100,  0, 'T', 'F', 'T', 150, 50, @tanevId, @intezmenyId),
      (6579, 6690, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6579, 6600, 100,  0, 'T', 'F', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6590, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6691, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6692, 0,  117, 'F', 'T', 'T', 150, 33, @tanevId,@intezmenyId ),
      (6579, 6693, 50,  0, 'T', 'F', 'T', 100, 50, @tanevId, @intezmenyId ),
      (6579, 6694, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6695, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6696, 50,  100, 'T', 'T', 'F', 150, 0, @tanevId,@intezmenyId ),
      (6579, 6697, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6698, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6579, 6699, 75,  75, 'T', 'T', 'F', 150, 0, @tanevId, @intezmenyId ),
    
      -- EMELT
      (6580, 6591, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId, @intezmenyId),
      (6580, 6603, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6604, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6605, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId, @intezmenyId),
      (6580, 6606, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6607, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6595, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6608, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId,@intezmenyId  ),
      (6580, 6609, 50,  60, 'T', 'T', 'T', 150, 40, @tanevId, @intezmenyId),
      (6580, 6610, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6611, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6612, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6613, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId, @intezmenyId),
      (6580, 6614, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6580, 6615, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6580, 6601, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6616, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6617, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6618, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6619, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6620, 0,  80, 'F', 'T', 'T', 150, 70, @tanevId, @intezmenyId ),
      (6580, 6621, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6622, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6623, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6596, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6624, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6598, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6593, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId, @intezmenyId),
      (6580, 6700, 0,  40, 'F', 'T', 'T', 150, 110, @tanevId, @intezmenyId),
      (6580, 6701, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6626, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6627, 0,  70, 'F', 'T', 'T', 150, 80, @tanevId, @intezmenyId ),
      (6580, 6628, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6629, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6630, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6631, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6632, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6633, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6599, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6634, 120,  0, 'T', 'F', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6635, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6636, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6638, 0,  70, 'F', 'T', 'T', 150, 80, @tanevId, @intezmenyId ),
      (6580, 6639, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6597, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6640, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6641, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6642, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6643, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6644, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6645, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6646, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6647, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6648, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6649, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6650, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6651, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6652, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6580, 6653, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6654, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6588, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6589, 0,  115, 'F', 'T', 'T', 150, 35, @tanevId,@intezmenyId ),
      (6580, 6655, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6656, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6602, 60,  60, 'T', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6657, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6658, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6659, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6592, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6660, 0,  0, 'F', 'F', 'T', 50, 50, @tanevId, @intezmenyId   ),
      (6580, 6661, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6662, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6663, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6664, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6665, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6666, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6667, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6669, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6670, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6671, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6672, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6673, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6674, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6594, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6675, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6676, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6677, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6678, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6679, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6680, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6681, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6682, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6683, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6684, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6685, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6686, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6687, 0,  90, 'F', 'T', 'T', 150, 60, @tanevId, @intezmenyId ),
      (6580, 6688, 0,  75, 'F', 'T', 'T', 150, 75, @tanevId, @intezmenyId ),
      (6580, 6689, 100,  0, 'T', 'F', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6600, 100,  0, 'T', 'F', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6590, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6691, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6692, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId ),
      (6580, 6694, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6695, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6696, 50,  100, 'T', 'T', 'F', 150, 0, @tanevId,@intezmenyId ),
      (6580, 6697, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6698, 0,  100, 'F', 'T', 'T', 150, 50, @tanevId,@intezmenyId ),
      (6580, 6699, 0,  120, 'F', 'T', 'T', 150, 30, @tanevId,@intezmenyId )
  END
  --Érettségi tantárgy adatok létrehozása sikeres
  -- Termek migrációja
  INSERT INTO T_TEREM (
     C_AKTIV
    ,C_JELLEG
    ,C_KAPACITAS
    ,C_MUKODESIHELYID
    ,C_NEV
    ,C_TEREMFELELOSID
    ,C_TERULET
    ,C_TOBBORATLEHETTARTANI
    ,C_VIZSGAKAPACITAS
    ,C_WIFILEFEDETT
    ,C_AR
    ,C_BERBEADHATO
    ,C_FOGLALHATOIDOSZAKKEZDETE
    ,C_FOGLALHATOIDOSZAKVEGE
    ,C_LEIRAS
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     C_AKTIV AS C_AKTIV
	  ,t.C_JELLEG AS C_JELLEG
    ,C_KAPACITAS AS C_KAPACITAS
	  ,ISNULL (
		  mh.ID,
		  (SELECT TOP 1 ID FROM T_MUKODESIHELY WHERE C_TANEVID = @tanevID AND C_INTEZMENYID = @intezmenyID)
		 ) AS C_MUKODESIHELYID
	  ,t.C_NEV AS C_NEV
	  ,fh.ID AS C_TEREMFELELOSID
	  ,t.C_TERULET AS C_TERULET
	  ,ISNULL(t.C_TOBBORATLEHETTARTANI,'F') AS C_TOBBORATLEHETTARTANI
	  ,t.C_VIZSGAKAPACITAS AS C_VIZSGAKAPACITAS
	  ,t.C_WIFILEFEDETT AS C_WIFILEFEDETT
    ,t.C_AR
    ,t.C_BERBEADHATO
    ,t.C_FOGLALHATOIDOSZAKKEZDETE
    ,t.C_FOGLALHATOIDOSZAKVEGE
    ,t.C_LEIRAS
    ,@intezmenyID AS C_INTEZMENYID
    ,@tanevID AS C_TANEVID
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TEREM t 
    LEFT JOIN T_MUKODESIHELY mh ON mh.ELOZOTANEVIREKORDID = t.C_MUKODESIHELYID AND mh.C_TANEVID = @tanevID
    LEFT JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = t.C_TEREMFELELOSID AND fh.C_TANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource AND t.C_INTEZMENYID=@intezmenyID
    AND t.TOROLT = 'F'
  -- print N'Termek létrehozása sikeres!';
  -- Eszköz migrációja
  INSERT INTO T_ESZKOZ(
  	 C_BERELT
  	,C_BESZERZESDATUM
  	,C_BESZERZESIAR
  	,C_CELJA
  	,C_DARABSZAM
  	,C_FELELOSID
  	,C_FOKONYVISZAM
  	,C_GYARISZAM
  	,C_GYARTASIEV
  	,C_HALOZATIKAPCSOLAT
  	,C_HORDOZHATO
  	,C_INTERNETKAPCSOLAT
  	,C_INTEZMENYID
  	,C_JELLEGE
  	,C_KATEGORIA
  	,C_LELTARISZAM
  	,C_MEGJEGYZES
  	,C_MENNYISEGIEGYSEG
  	,C_MULTIMEDIAS
  	,C_NEMMUKODIK
  	,C_NEV
  	,C_PEDAGOGUSHOZZAFERHET
  	,C_SZALLITO
  	,C_TANEVID
  	,C_TANULOHOZZAFERHET
  	,C_TARTOZEK
  	,C_TARTOZEKHIVATKOZAS
  	,C_TEREMID
  	,C_TIPUS
  	,C_VONALKOD
    ,C_PROJEKT
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,NNID
    ,ELOZOTANEVIREKORDID
  ) SELECT 
     t.C_BERELT AS C_BERELT
	  ,t.C_BESZERZESDATUM AS C_BESZERZESDATUM
    ,t.C_BESZERZESIAR AS C_BESZERZESIAR
	  ,t.C_CELJA AS C_CELJA
	  ,t.C_DARABSZAM AS C_DARABSZAM
	  ,fh.ID AS C_FELELOSID
	  ,t.C_FOKONYVISZAM AS C_FOKONYVISZAM
	  ,t.C_GYARISZAM AS C_GYARISZAM
	  ,t.C_GYARTASIEV AS C_GYARTASIEV
	  ,t.C_HALOZATIKAPCSOLAT AS C_HALOZATIKAPCSOLAT
	  ,t.C_HORDOZHATO AS C_HORDOZHATO
	  ,t.C_INTERNETKAPCSOLAT AS C_INTERNETKAPCSOLAT
    ,@intezmenyID AS C_INTEZMENYID
	  ,t.C_JELLEGE AS C_JELLEGE
	  ,t.C_KATEGORIA AS C_KATEGORIA
	  ,t.C_LELTARISZAM AS C_LELTARISZAM
	  ,t.C_MEGJEGYZES AS C_MEGJEGYZES
	  ,t.C_MENNYISEGIEGYSEG AS C_MENNYISEGIEGYSEG
	  ,t.C_MULTIMEDIAS AS C_MULTIMEDIAS
	  ,t.C_NEMMUKODIK AS C_NEMMUKODIK
	  ,t.C_NEV AS C_NEV
	  ,t.C_PEDAGOGUSHOZZAFERHET AS C_PEDAGOGUSHOZZAFERHET
	  ,t.C_SZALLITO AS C_SZALLITO
    ,@tanevID AS C_TANEVID
    ,C_TANULOHOZZAFERHET AS C_TANULOHOZZAFERHET
    ,C_TARTOZEK AS C_TARTOZEK
	  ,C_TARTOZEKHIVATKOZAS AS C_TARTOZEKHIVATKOZAS
	  ,ISNULL(
		  tm.ID,
		  (SELECT TOP 1 ID FROM T_TEREM WHERE C_TANEVID = @tanevID AND C_INTEZMENYID = @intezmenyID)
		) AS C_TEREMID
	  ,IIF((SELECT COUNT(1) FROM T_ESZKOZTIPUS WHERE C_ALTANEVID = @tanevID AND ID = t.C_TIPUS) = 0,70,t.C_TIPUS) AS C_TIPUS
	  ,t.C_VONALKOD AS C_VONALKOD
	  ,t.C_PROJEKT AS C_PROJEKT
    ,t.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_ESZKOZ t 
    LEFT JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = t.C_FELELOSID AND fh.C_TANEVID = @tanevID
    LEFT JOIN T_TEREM tm ON tm.ELOZOTANEVIREKORDID = t.C_TEREMID AND tm.C_TANEVID = @tanevID
  WHERE t.C_TANEVID = @tanevIdSource 
    AND t.TOROLT = 'F' 
	-- print N'Eszközök létrehozása sikeres!';
	--Végzettség, képzettség
   INSERT INTO T_VEGZETTSEG (
     C_EGYEB
    ,C_VEGZETTSEGTIPUSID
    ,C_TANARID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     v.C_EGYEB AS C_EGYEB                -- C_EGYEB - nvarchar(255)
    ,v.C_VEGZETTSEGTIPUSID AS C_VEGZETTSEGTIPUSID    -- C_VEGZETTSEGTIPUSID - int
    ,fh.ID   AS C_TANARID               -- C_TANARID - int
    ,v.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,'F'  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,GETDATE() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,v.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_VEGZETTSEG v
    INNER JOIN T_FELHASZNALO fh ON fh.ELOZOTANEVIREKORDID = v.C_TANARID
    INNER JOIN T_TANEV te ON te.ID = v.C_TANEVID AND te.ID=@tanevIdSource  AND te.TOROLT = 'F'
    INNER JOIN T_TANEV teNext ON te.C_INTEZMENYID = teNext.C_INTEZMENYID AND teNext.ID=@tanevID  AND te.TOROLT = 'F'
  WHERE v.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_VEGZETTSEG WHERE ELOZOTANEVIREKORDID = v.ID)
  INSERT INTO T_KEPESITES (
     C_KEPESITESTIPUSID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     k.C_KEPESITESTIPUSID AS C_KEPESITESTIPUSID     -- C_KEPESITESTIPUSID - int
    ,k.C_INTEZMENYID   AS C_INTEZMENYID           -- C_INTEZMENYID - int
    ,teNext.ID   AS C_TANEVID               -- C_TANEVID - int
    ,'F'  AS TOROLT                  -- TOROLT - char(1)
    ,0   AS SERIAL                  -- SERIAL - int
    ,GETDATE() AS LASTCHANGED       -- LASTCHANGED - datetime
    ,GETDATE() AS CREATED           -- CREATED - datetime
    ,NULL AS MODIFIER               -- MODIFIER - int
    ,NULL AS CREATOR                -- CREATOR - int
    ,k.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KEPESITES k
    INNER JOIN T_TANEV te ON te.ID = k.C_TANEVID AND te.ID=@tanevIDSource AND te.TOROLT = 'F'
    INNER JOIN T_TANEV teNext ON te.C_INTEZMENYID = teNext.C_INTEZMENYID AND teNext.ID = @tanevID  AND te.TOROLT = 'F'
  WHERE k.TOROLT = 'F'
    AND NOT EXISTS (SELECT 1 FROM T_KEPESITES WHERE ELOZOTANEVIREKORDID = k.ID)
  INSERT INTO T_KEPESITES_VEGZETTSEG (
     C_KEPESITESID
    ,C_VEGZETTSEGID
    ,ELOZOTANEVIREKORDID
    ,NNID
  ) SELECT 
     k.ID   AS C_KEPESITESID           -- C_KEPESITESID - int
    ,v.ID   AS C_VEGZETTSEGID          -- C_VEGZETTSEGID - int
    ,NULL AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
    ,NULL AS NNID                   -- NNID - int
  FROM T_KEPESITES_VEGZETTSEG kv
    INNER JOIN T_VEGZETTSEG v ON v.ELOZOTANEVIREKORDID = kv.C_VEGZETTSEGID
    INNER JOIN T_KEPESITES k ON  k.ELOZOTANEVIREKORDID = kv.C_KEPESITESID
    INNER JOIN T_TANEV te ON te.ID = v.C_TANEVID AND te.ID = k.C_TANEVID AND te.ID = @tanevID  AND te.TOROLT = 'F'
  WHERE NOT EXISTS (SELECT 1 FROM T_KEPESITES_VEGZETTSEG kvs WHERE kvs.C_KEPESITESID = k.ID AND kvs.C_VEGZETTSEGID = v.ID)
  -- print N'Képzettség végzettség létrehozása sikeres!';
  --KKAMI végzettség képzettség adatok
  IF DB_NAME() LIKE '%KRETA_KLIK%'
  BEGIN
	INSERT INTO T_KKAMIVEGZETTSEG(
		 C_KKAMIVEGZETTSEGTIPUSID
		,C_ALKALMAZOTTID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT 
		 kav.C_KKAMIVEGZETTSEGTIPUSID
		,a.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,kav.ID
		,NULL
	FROM T_KKAMIVEGZETTSEG kav
		INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA=kav.C_ALKALMAZOTTID AND a.TOROLT='F' AND a.C_ALTANEVID=@tanevID
	WHERE kav.C_TANEVID=@tanevIDSource AND kav.TOROLT='F'
		AND NOT EXISTS(SELECT 1 FROM T_KKAMIVEGZETTSEG nkav WHERE nkav.ELOZOTANEVIREKORDID=kav.ID AND nkav.C_TANEVID=@tanevID)
	INSERT INTO T_KKGYOGYPEDVEGZETTSEG(
		 C_KKGYOGYPEDTERTIPUSID
		,C_ALKALMAZOTTID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT
		 kgv.C_KKGYOGYPEDTERTIPUSID
		,a.ID
		,@intezmenyId
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
    ,NULL
    ,kgv.ID
		,NULL
	FROM T_KKGYOGYPEDVEGZETTSEG kgv
		INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA=kgv.C_ALKALMAZOTTID AND a.TOROLT='F' AND a.C_ALTANEVID=@tanevID
	WHERE kgv.C_TANEVID=@tanevIDSource AND kgv.TOROLT='F'
		AND NOT EXISTS(SELECT 1 FROM T_KKGYOGYPEDVEGZETTSEG nkgv WHERE nkgv.ELOZOTANEVIREKORDID=kgv.ID AND nkgv.C_TANEVID=@tanevID)
	INSERT INTO T_KKTANARVEGZETTSEG(
		 C_EGYEB
		,C_KKTANARVEGZETTSEGID
		,C_ALKALMAZOTTID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT
		 ktv.C_EGYEB
		,C_KKTANARVEGZETTSEGID
		,a.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
    ,NULL
    ,ktv.ID
		,NULL
	FROM T_KKTANARVEGZETTSEG ktv
		INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA=ktv.C_ALKALMAZOTTID AND a.TOROLT='F' AND a.C_ALTANEVID=@tanevID 
	WHERE ktv.C_TANEVID=@tanevIDSource AND ktv.TOROLT='F'
		AND NOT EXISTS(SELECT 1 FROM T_KKGYOGYPEDVEGZETTSEG nktv WHERE nktv.ELOZOTANEVIREKORDID=ktv.ID AND nktv.C_TANEVID=@tanevID)
	INSERT INTO T_KKTANITOVEZGETTSEG(
		 C_KKTANIOTOMUVTERTIPUSID
		,C_ALKALMAZOTTID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT
		 ktnv.C_KKTANIOTOMUVTERTIPUSID
		,a.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
        ,ktnv.ID
		,NULL
	FROM T_KKTANITOVEZGETTSEG ktnv
		INNER JOIN T_ALKALMAZOTT a ON a.ELOZOTANEVIREKORDIDA=ktnv.C_ALKALMAZOTTID AND a.TOROLT='F' AND a.C_ALTANEVID=@tanevID 
	WHERE ktnv.C_TANEVID=@tanevIDSource AND ktnv.TOROLT='F'
		AND NOT EXISTS(SELECT 1 FROM T_KKTANITOVEZGETTSEG nktnv WHERE nktnv.ELOZOTANEVIREKORDID=ktnv.ID AND nktnv.C_TANEVID=@tanevID)
  
	INSERT INTO T_KKELEKTROAKUZENE(
		 C_ELEKTRAKUTIPUSID
		,C_KKAMIVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT 
		 keaz.C_ELEKTRAKUTIPUSID
		,kav.ID
		,@intezmenyId
		,@tanevId
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
    ,NULL
    ,keaz.ID
		,NULL
	FROM T_KKELEKTROAKUZENE keaz
		INNER JOIN T_KKAMIVEGZETTSEG kav ON kav.ELOZOTANEVIREKORDID=keaz.C_KKAMIVEGZETTSEGID AND kav.C_TANEVID=@tanevID
	WHERE keaz.C_TANEVID=@tanevIDSource AND keaz.TOROLT='F'
		
	INSERT INTO T_KKJAZZZENE(
		 C_JAZZZENETIPUSID
		,C_KKAMIVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT 
		 kjz.C_JAZZZENETIPUSID
		,kav.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,kjz.ID
		,NULL
	FROM T_KKJAZZZENE kjz
		INNER JOIN T_KKAMIVEGZETTSEG kav ON kav.ELOZOTANEVIREKORDID=kjz.C_KKAMIVEGZETTSEGID AND kav.C_TANEVID=@tanevID
	WHERE kjz.C_TANEVID=@tanevIDSource AND kjz.TOROLT='F'
	INSERT INTO T_KKKLASSZIKUSZENE(
		 C_KLASSZUKUSZENETIPUSID
		,C_KKAMIVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	)SELECT 
		 kkz.C_KLASSZUKUSZENETIPUSID
		,kav.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,kkz.ID
		,NULL
	FROM T_KKKLASSZIKUSZENE kkz
		INNER JOIN T_KKAMIVEGZETTSEG kav ON kav.ELOZOTANEVIREKORDID=kkz.C_KKAMIVEGZETTSEGID AND kav.C_TANEVID=@tanevID
	WHERE kkz.C_TANEVID=@tanevIDSource AND kkz.TOROLT='F'
  
	INSERT INTO T_KKNEPZENE(
		C_NEPZENETIPUSID
		,C_KKAMIVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID	
	) SELECT 
		 knz.C_NEPZENETIPUSID
		,kav.ID
		,@intezmenyID
		,@taneviD
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,knz.ID
		,NULL
	FROM T_KKNEPZENE knz
		INNER JOIN T_KKAMIVEGZETTSEG kav ON kav.ELOZOTANEVIREKORDID=knz.C_KKAMIVEGZETTSEGID AND kav.C_TANEVID=@tanevID
	WHERE knz.C_TANEVID=@tanevIDSource AND knz.TOROLT='F'
	INSERT INTO T_KKTANTARGYKATEGORIA(
		 C_KKSZAKTIPUSID
		,C_KKTANARVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT
		 kttk.C_KKSZAKTIPUSID
		,ktv.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,kttk.ID
		,NULL
	FROM T_KKTANTARGYKATEGORIA kttk
		INNER JOIN T_KKTANARVEGZETTSEG ktv ON ktv.ELOZOTANEVIREKORDID=kttk.C_KKTANARVEGZETTSEGID AND ktv.C_TANEVID=@tanevID
	WHERE kttk.C_TANEVID=@tanevIDSource AND kttk.TOROLT='F'
	INSERT INTO T_KKTERULET(
		 C_AMITERULETTIPUSID
		,C_KKAMIVEGZETTSEGID
		,C_INTEZMENYID
		,C_TANEVID
		,TOROLT
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
		,NNID
	) SELECT 
		 kt.C_AMITERULETTIPUSID
		,kav.ID
		,@intezmenyID
		,@tanevID
		,'F'
		,0
		,GETDATE()
		,GETDATE()
		,NULL
		,NULL
		,kt.ID
		,NULL
	FROM T_KKTERULET kt
		INNER JOIN T_KKAMIVEGZETTSEG kav ON kav.ELOZOTANEVIREKORDID=kt.C_KKAMIVEGZETTSEGID AND kav.C_TANEVID=@tanevID
	WHERE kt.C_TANEVID=@tanevIDSource AND kt.TOROLT='F'
  END
  -- print N'KKAMI Végzettség képzettség létrehozása sikeres';
  INSERT INTO T_LICENCE (
     C_DATE
    ,C_FULL
    ,C_FUNCTION
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,NNID
  )	SELECT
     l.C_DATE
    ,l.C_FULL
    ,l.C_FUNCTION
    ,l.C_INTEZMENYID
    ,@tanevID
    ,'F'
    ,0
    ,GETDATE()
    ,GETDATE()
    ,NULL
    ,NULL
    ,l.ID
    ,NULL
	FROM T_LICENCE l WHERE l.C_INTEZMENYID = @intezmenyID AND l.C_TANEVID = @tanevIdSource AND TOROLT='F'
	-- print N'Licence létrehozása sikeres!';
END
GO
