﻿--Migration
INSERT INTO T_EMAIL (
	 C_EMAILTIPUSA
	,C_EMAILCIM
	,C_ALAPERTELMEZETT
	,C_ISPUBLIC
	,C_GONDVISELOID
	,C_FELHASZNALOID
	,C_INTEZMENYID
	,C_TANEVID
	,TOROLT
	,SERIAL
	,LASTCHANGED
	,CREATED
	,MODIFIER
	,CREATOR
	,ELOZOTANEVIREKORDID
	,C_GUID
	,C_ISHIBASANMEGADVA
	,C_ISMOBILROGZITETT
) SELECT --f1.ID, f2.ID, e1.ID, te1.ID, f1.C_INTEZMENYID, i1.C_AZONOSITO, f1.C_NYOMTATASINEV, *
   e1.C_EMAILTIPUSA
	,e1.C_EMAILCIM
	,e1.C_ALAPERTELMEZETT
	,e1.C_ISPUBLIC
	,e1.C_GONDVISELOID
	,f2.ID          --e1.C_FELHASZNALOID
	,e1.C_INTEZMENYID
	,te2.ID         --e1.C_TANEVID
	,'F'            --e1.TOROLT
	,0              --e1.SERIAL
	,GetDate()      --e1.LASTCHANGED
	,GetDate()      --e1.CREATED
	,0              --e1.MODIFIER
	,0              --e1.CREATOR
	,e1.ID          --e1.ELOZOTANEVIREKORDID
	,e1.C_GUID --??????????? vagy null kell ide???
	,e1.C_ISHIBASANMEGADVA
	,e1.C_ISMOBILROGZITETT
FROM T_FELHASZNALO f1
  INNER JOIN T_ALKALMAZOTT a1 ON a1.ID = f1.ID AND a1.TOROLT = 'F'
  INNER JOIN T_EMAIL e1 ON e1.C_FELHASZNALOID = f1.ID AND e1.C_TANEVID = f1.C_TANEVID AND e1.TOROLT = 'F'
  INNER JOIN T_TANEV te1 ON te1.ID = f1.C_TANEVID AND te1.C_AKTIV = 'T' AND te1.C_NEV = '2020/2021' AND te1.TOROLT = 'F'
  INNER JOIN T_INTEZMENY i1 ON i1.ID = f1.C_INTEZMENYID AND i1.TOROLT = 'F'
  INNER JOIN T_FELHASZNALO f2 ON f2.ELOZOTANEVIREKORDID = f1.ID AND f2.TOROLT = 'F'
  INNER JOIN T_TANEV te2 ON te2.ID = f2.C_TANEVID AND te2.C_KOVETKEZO = 'T' AND te2.C_NEV = '2021/2022' AND te2.TOROLT = 'F'
WHERE f1.TOROLT = 'F'
  AND NOT EXISTS (
    SELECT 1
    FROM T_EMAIL e2
    WHERE e2.C_FELHASZNALOID = f2.ID
			AND e2.C_TANEVID = f2.C_TANEVID
			AND e2.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
  ,C_ISMOBILROGZITETT
) SELECT --f1.ID, f2.ID, t1.ID, te1.ID, f1.C_INTEZMENYID, i1.C_AZONOSITO, f1.C_NYOMTATASINEV, *
   t1.C_TELEFONTIPUSA
  ,t1.C_TELEFONSZAM
  ,t1.C_LEIRAS
  ,t1.C_ALAPERTELMEZETT
  ,t1.C_ISPUBLIC
  ,t1.C_GONDVISELOID
  ,f2.ID               --t1.C_FELHASZNALOID
  ,t1.C_INTEZMENYID
  ,te2.ID              --t1.C_TANEVID
  ,'F'                 --t1.TOROLT
  ,0                   --t1.SERIAL
  ,GETDATE()           --t1.LASTCHANGED
  ,GETDATE()           --t1.CREATED
  ,0                   --t1.MODIFIER
  ,0                   --t1.CREATOR
  ,t1.ID               --t1.ELOZOTANEVIREKORDID
  ,t1.C_ISMOBILROGZITETT

FROM T_FELHASZNALO f1
  INNER JOIN T_ALKALMAZOTT a1 ON a1.ID = f1.ID AND a1.TOROLT = 'F'
  INNER JOIN T_TELEFON t1 ON t1.C_FELHASZNALOID = f1.ID AND t1.C_TANEVID = f1.C_TANEVID AND t1.TOROLT = 'F'
  INNER JOIN T_TANEV te1 ON te1.ID = f1.C_TANEVID AND te1.C_AKTIV = 'T' AND te1.C_NEV = '2020/2021' AND te1.TOROLT = 'F'
  INNER JOIN T_INTEZMENY i1 ON i1.ID = f1.C_INTEZMENYID AND i1.TOROLT = 'F'
  INNER JOIN T_FELHASZNALO f2 ON f2.ELOZOTANEVIREKORDID = f1.ID AND f2.TOROLT = 'F'
  INNER JOIN T_TANEV te2 ON te2.ID = f2.C_TANEVID AND te2.C_KOVETKEZO = 'T' AND te2.C_NEV = '2021/2022' AND te2.TOROLT = 'F'
WHERE f1.TOROLT = 'F'
  AND NOT EXISTS (
    SELECT 1
    FROM T_TELEFON t2
    WHERE t2.C_FELHASZNALOID = f2.ID
		  AND t2.C_TANEVID = f2.C_TANEVID
			AND t2.TOROLT = 'F'
  )

-----------------------------------------------------------------------------------------

INSERT INTO T_CIM (
   C_CIMTIPUSA
  ,C_ORSZAG
  ,C_IRANYITOSZAM
  ,C_VAROS
  ,C_MEGYE
  ,C_KOZTERULET
  ,C_HAZSZAM
  ,C_EPULET
  ,C_LEPCSOHAZ
  ,C_EMELET
  ,C_AJTO
  ,C_OSSZETETTCIM
  ,C_ALAPERTELMEZETT
  ,C_FELHASZNALOID
  ,C_GONDVISELOID
  ,C_INTEZMENYID
  ,C_TANEVID
  ,TOROLT
  ,SERIAL
  ,LASTCHANGED
  ,CREATED
  ,MODIFIER
  ,CREATOR
  ,ELOZOTANEVIREKORDID
  ,CORE_PUBLIKUS
  ,C_KOZTERULETJELLEGENEV
) SELECT --f1.ID, f2.ID, c1.ID, te1.ID, f1.C_INTEZMENYID, i1.C_AZONOSITO, f1.C_NYOMTATASINEV, *
   c1.C_CIMTIPUSA
  ,c1.C_ORSZAG
  ,c1.C_IRANYITOSZAM
  ,c1.C_VAROS
  ,c1.C_MEGYE
  ,c1.C_KOZTERULET
  ,c1.C_HAZSZAM
  ,c1.C_EPULET
  ,c1.C_LEPCSOHAZ
  ,c1.C_EMELET
  ,c1.C_AJTO
  ,c1.C_OSSZETETTCIM
  ,c1.C_ALAPERTELMEZETT
  ,f2.ID               --c1.C_FELHASZNALOID
  ,NULL                --c1.C_GONDVISELOID
  ,c1.C_INTEZMENYID
  ,te2.ID              --c1.C_TANEVID
  ,'F'                 --c1.TOROLT
  ,0                   --c1.SERIAL
  ,GETDATE()           --c1.LASTCHANGED
  ,GETDATE()           --c1.CREATED
  ,0                   --c1.MODIFIER
  ,0                   --c1.CREATOR
  ,c1.ID               --c1.ELOZOTANEVIREKORDID
  ,c1.CORE_PUBLIKUS
  ,c1.C_KOZTERULETJELLEGENEV
FROM T_FELHASZNALO f1
  INNER JOIN T_ALKALMAZOTT a1 ON a1.ID = f1.ID AND a1.TOROLT = 'F'
  INNER JOIN T_CIM c1 ON c1.C_FELHASZNALOID = f1.ID AND c1.C_TANEVID = f1.C_TANEVID AND c1.TOROLT = 'F'
  INNER JOIN T_TANEV te1 ON te1.ID = f1.C_TANEVID AND te1.C_AKTIV = 'T' AND te1.C_NEV = '2020/2021' AND te1.TOROLT = 'F'
  INNER JOIN T_INTEZMENY i1 ON i1.ID = f1.C_INTEZMENYID AND i1.TOROLT = 'F'
  INNER JOIN T_FELHASZNALO f2 ON f2.ELOZOTANEVIREKORDID = f1.ID AND f2.TOROLT = 'F'
  INNER JOIN T_TANEV te2 ON te2.ID = f2.C_TANEVID AND te2.C_KOVETKEZO = 'T' AND te2.C_NEV = '2021/2022' AND te2.TOROLT = 'F'
WHERE f1.TOROLT = 'F'
  AND NOT EXISTS (
    SELECT 1
    FROM T_CIM c2
    WHERE c2.C_FELHASZNALOID = f2.ID
			AND c2.C_TANEVID = f2.C_TANEVID
			AND c2.TOROLT = 'F'
  )

-----------------------------------------------------------------------------------------
