﻿-- =============================================
-- Author:		Telek Ákos
-- Create date: 2017.05.31.
-- Description:	A tanévhez tartozó alapadatokat és sémákat generálja le.
-- Modified:	Telek Ákos
-- Modify date:	2018.04.24.
-- =============================================

IF OBJECT_ID('dev.uspGenerateTanevAdatokFromExistingTanev') IS NOT NULL BEGIN
  DROP PROCEDURE [dev].[uspGenerateTanevAdatokFromExistingTanev]
END  
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
  
  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) BEGIN
    PRINT @tanevNeveNew + ' tanév már létezik: intezmény ' + CAST(@intezmenyId as varchar(20))
  END
    
	DECLARE @tanevID INT
	
	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
  ) 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
    ,(SELECT ISNULL(MAX(C_SORSZAM), 0) + 1 FROM T_TANEV WHERE T_TANEV.C_INTEZMENYID = @intezmenyID)
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 6, 4) + '0831', 21)
    ,convert(datetime, SUBSTRING(@tanevNeveNew, 6, 4) + '0831', 21)
    ,@intezmenyID
    ,'F'
    ,0
    ,getdate()
    ,getdate()
    ,null
    ,null
  )
		   
	PRINT @tanevNeveNew + ' tanév létrehozása sikeres!';

	SET @tanevID = SCOPE_IDENTITY()

	--------------------------- 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
  
  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
  SET IDENTITY_INSERT T_DICTIONARYITEMBASE OFF
	PRINT 'Szótárelemek létrehozása sikeres!';
  

    
  DECLARE tableCursor CURSOR FOR 
  SELECT DISTINCT OBJECT_NAME(parent_object_id) 
  FROM sys.foreign_key_columns 
  WHERE referenced_object_id = OBJECT_ID('T_DICTIONARYITEMBASE') 
  
  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'''
    
    --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
  ) 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
    ,T_INTEZMENYADATOK.ID AS ELOZOTANEVIREKORDID
  FROM T_INTEZMENYADATOK
  WHERE C_TANEVID = @tanevIdSource 
    AND C_INTEZMENYID = @intezmenyId 
    AND TOROLT = 'F'
  

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

	SET @sql = N'dev.uspCreateTanevRendje' + REPLACE(@tanevNeveNew, '/', '') + ' @intezmenyId, @tanevId'
	EXEC sp_executesql @sql, N'@intezmenyId int, @tanevId int', @intezmenyId, @tanevId
	
  PRINT @sql
	PRINT 'Tanév rendje létrehozása sikeres!';

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

	PRINT 'Csengetési rend létrehozása sikeres!';

	--------------------------- CSENGETÉSI REND LÉTREHOZÁSA VÉGE ------------------------
 
	--------------------------- ALAP FELHASZNÁLÓK LÉTREHOZÁSA ---------------------------

	-- Fenntartó--
	DECLARE @fenntartoId 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 Fenntartó'

	SET @fenntartoId = SCOPE_IDENTITY()
	PRINT 'Fenntartó létrehozása sikeres!';

	-- 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 ID FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevIdSource AND C_SZULETESINEV = 'Kréta Adminisztrátor')


--Fenntartó(ready only user)		   
  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]
    ,@fenntartoId
		,[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 ID FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevIdSource AND C_SZULETESINEV = 'Kréta Fenntartó')

  PRINT 'Fenntartó belépés létrehozása sikeres!';

-- 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 ID FROM T_FELHASZNALO WHERE C_INTEZMENYID = @intezmenyId AND C_TANEVID = @tanevIdSource AND C_SZULETESINEV = 'Kréta Adminisztrátor')

  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
    ,szk.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
    ,NULL 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
    ,rb.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 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 dbo.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
    ,j.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
    ,j.ID AS ELOZOTANEVIREKORDID    -- ELOZOTANEVIREKORDID - int
  FROM T_JOGOSULTSAG j
  WHERE j.C_INTEZMENYID = @intezmenyId
    AND j.C_TANEVID = @tanevIdSource
    AND j.TOROLT = 'F'


  INSERT INTO T_FELHASZNALO_SZEREPKOR (C_FELHASZNALOID, C_SZEREPKORID)
  VALUES ((
      SELECT C_FELHASZNALOID 
      FROM T_FELHASZNALOBELEPES
        WHERE C_BEJELENTKEZESINEV = 'admin'
        AND TOROLT = 'F'
        AND C_TANEVID = @tanevID
        AND C_INTEZMENYID = @intezmenyID 
    ), (
      SELECT ID 
      FROM dbo.T_SZEREPKOR 
      WHERE C_SZEREPKORTIPUS = 1758 -- Admin
        AND C_INTEZMENYID = @intezmenyID 
        AND C_TANEVID = @tanevID 
        AND TOROLT = 'F'
    )
  )
  
  INSERT INTO T_FELHASZNALO_SZEREPKOR (C_FELHASZNALOID, C_SZEREPKORID)
  VALUES ((
      SELECT C_FELHASZNALOID 
      FROM T_FELHASZNALOBELEPES
        WHERE C_BEJELENTKEZESINEV = 'fenntarto'
        AND TOROLT = 'F'
        AND C_TANEVID = @tanevID
        AND C_INTEZMENYID = @intezmenyID 
    ), (
      SELECT ID 
      FROM dbo.T_SZEREPKOR 
      WHERE C_SZEREPKORTIPUS = 1757 -- Fenntartó
        AND C_INTEZMENYID = @intezmenyID 
        AND C_TANEVID = @tanevID 
        AND TOROLT = 'F'
    )
  )  
  
  INSERT INTO T_JOGOSULTSAG_FELHASZNALO (C_FELHASZNALOID, C_JOGOSULTSAGID)
  VALUES ((
      SELECT C_FELHASZNALOID 
      FROM T_FELHASZNALOBELEPES
        WHERE C_BEJELENTKEZESINEV = 'fenntarto'
        AND TOROLT = 'F'
        AND C_TANEVID = @tanevID
        AND C_INTEZMENYID = @intezmenyID 
    ), (
      SELECT 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'
    )
  ) 
  
   
  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_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_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
    ,TOROLT AS TOROLT        
    ,0 AS SERIAL             
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,MODIFIER AS MODIFIER
    ,MODIFIER AS CREATOR
    ,NULL AS NNID    
    ,ID AS ELOZOTANEVIREKORDID
  FROM T_MUKODESIHELY t WHERE C_TANEVID = @tanevIdSource AND t.TOROLT = 'F'

	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_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
    ,(SELECT ID FROM T_MUKODESIHELY WHERE ELOZOTANEVIREKORDID = t.C_MUKODESIHELYID AND C_TANEVID = @tanevID AND C_INTEZMENYID = @intezmenyID) AS C_MUKODESIHELYID
    ,C_INTEZMENYID   AS C_INTEZMENYID
    ,@tanevID   AS C_TANEVID  
    ,TOROLT 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_FELADATELLATASIHELY t WHERE C_TANEVID = @tanevIdSource AND t.TOROLT = 'F'

	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
    ,TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,MODIFIER AS MODIFIER
    ,MODIFIER AS CREATOR
    ,NULL AS NNID
    ,ID AS ELOZOTANEVIREKORDID
  FROM T_FELHASZNALO t 
  WHERE C_TANEVID = @tanevIdSource 
    AND EXISTS (SELECT 1 FROM T_ALKALMAZOTT WHERE T_ALKALMAZOTT.ID = t.ID)  AND t.TOROLT = 'F'
  
  INSERT INTO T_ALKALMAZOTT (
  	 C_ALINTEZMENYID
  	,C_ALLANDOHELYETTESITESRE
  	,C_ALTANEVID
  	,C_FOALLAS
  	,C_TANARIAUTOMATAMEGAJANLASENGE
  	,C_TANARIERTEKELESHATARERTEK
  	,ID
  	,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
  	,NNID
  	,ELOZOTANEVIREKORDIDA
  ) SELECT 
	   @intezmenyID AS C_ALINTEZMENYID
	  ,C_ALLANDOHELYETTESITESRE AS C_ALLANDOHELYETTESITESRE
	  ,@tanevID AS C_ALTANEVID
	  ,C_FOALLAS AS C_FOALLAS
	  ,C_TANARIAUTOMATAMEGAJANLASENGE AS C_TANARIAUTOMATAMEGAJANLASENGE
	  ,C_TANARIERTEKELESHATARERTEK AS C_TANARIERTEKELESHATARERTEK
	  ,f.ID AS ID
    ,a.TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,NULL AS CREATOR
    ,NULL AS NNID
	  ,a.ID AS ELOZOTANEVIREKORDIDA
  FROM T_ALKALMAZOTT a 
    INNER JOIN T_FELHASZNALO f ON f.ELOZOTANEVIREKORDID = a.ID AND f.C_TANEVID = @tanevID
  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_MUNKAVISZONYTIPUSA
  	,C_NYUGDIJAS
  	,C_PEDAGOGUSSTATUSZA
  	,C_RESZMUNKAIDOSZAZALEK
  	,C_TANEVID
  	,C_TARTOSHELYETTESITES
  	,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
	  ,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.TOROLT 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_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
	  ,@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
    ,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_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_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
	  ,@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_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_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_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 
    ,@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'
	
  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_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 
    ,@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 
	  INNER 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
  	,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
    ,TOROLT AS TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,MODIFIER AS MODIFIER
    ,MODIFIER AS CREATOR
    ,NULL AS NNID
	  ,t.ID AS ELOZOTANEVIREKORDID
  FROM T_TANTERV t 
  WHERE t.C_TANEVID = @tanevIdSource  
    AND t.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!';

  -- 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_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
	  ,t.C_TOBBORATLEHETTARTANI AS C_TOBBORATLEHETTARTANI
	  ,t.C_VIZSGAKAPACITAS AS C_VIZSGAKAPACITAS
	  ,t.C_WIFILEFEDETT AS C_WIFILEFEDETT
    ,@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.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
  	,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.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!';
  
END
GO
