DROP PROCEDURE IF EXISTS uspCloneGondviselo
GO

CREATE PROCEDURE uspCloneGondviselo 
   @gondviseloId int
  ,@trgTanevId int
  ,@intezmenyId int
  ,@srcTanevId int
  ,@felhasznaloId int
AS BEGIN
  SET NOCOUNT ON;
  DECLARE @newId int
    ,@trgTanuloId int
  SELECT @newId = ID FROM T_GONDVISELO_OSSZES WHERE ELOZOTANEVIREKORDID = @gondviseloId AND C_TANEVID = @trgTanevId AND TOROLT = 'F'

  IF @newId IS NOT NULL BEGIN
    SELECT @newId AS [newId]
    RETURN
  END
  SELECT @trgTanuloId = trg_fh.ID
  FROM T_GONDVISELO_OSSZES src_g
    INNER JOIN T_FELHASZNALO_OSSZES src_fh ON src_fh.ID =  src_g.C_TANULOID AND src_fh.TOROLT = 'F' AND src_fh.C_TANEVID = @srcTanevId
    INNER JOIN T_FELHASZNALO_OSSZES trg_fh ON trg_fh.ELOZOTANEVIREKORDID = src_fh.ID AND trg_fh.TOROLT = 'F' AND trg_fh.C_TANEVID = @trgTanevId
    WHERE src_g.ID = @gondviseloId

  INSERT INTO T_GONDVISELO (
     C_AKTIV
    ,C_GONDVISELESKEZDETE
    ,C_GONDVISELESVEGE
    ,C_GYERMEKETEGYEDULNEVELO
    ,C_NEV
    ,C_NYUGDIJAS
    ,C_ROKONSAGFOKA
    ,C_ISEMAILERTESITES
    ,C_ISTORVENYESKEPVISELO
    ,C_ISCSOKKENTETTGONDVISELO
    ,C_TANULOID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,C_EGYEDIAZONOSITO
    ,C_IMPORTALT
    ,C_IDPEGYEDIAZONOSITO
    ,C_COVIDBEJELENTESDATUMA
    ,C_ISCOVIDBEJELENTO
    ,C_ANYJAUTONEV
    ,C_ANYJAVEZETEKNEV
    ,C_ELOTAG
    ,C_SZULETESIDATUM
    ,C_SZULETESIHELY
    ,C_SZULETESIUTONEV
    ,C_SZULETESIVEZETEKNEV
    ,C_UTONEV
    ,C_VEZETEKNEV
    ,C_FELTARELFOGADASDATUMA
    ,C_ISFELTARELFOGADOTT
  ) SELECT
     g.C_AKTIV
    ,g.C_GONDVISELESKEZDETE
    ,g.C_GONDVISELESVEGE
    ,g.C_GYERMEKETEGYEDULNEVELO
    ,g.C_NEV
    ,g.C_NYUGDIJAS
    ,g.C_ROKONSAGFOKA
    ,g.C_ISEMAILERTESITES
    ,g.C_ISTORVENYESKEPVISELO
    ,g.C_ISCSOKKENTETTGONDVISELO
    ,@trgTanuloId AS C_TANULOID
    ,g.C_INTEZMENYID
    ,@trgTanevId AS C_TANEVID
    ,g.TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,@felhasznaloId AS CREATOR
    ,g.ID AS ELOZOTANEVIREKORDID
    ,g.C_EGYEDIAZONOSITO
    ,g.C_IMPORTALT
    ,g.C_IDPEGYEDIAZONOSITO
    ,g.C_COVIDBEJELENTESDATUMA
    ,g.C_ISCOVIDBEJELENTO
    ,g.C_ANYJAUTONEV
    ,g.C_ANYJAVEZETEKNEV
    ,g.C_ELOTAG
    ,g.C_SZULETESIDATUM
    ,g.C_SZULETESIHELY
    ,g.C_SZULETESIUTONEV
    ,g.C_SZULETESIVEZETEKNEV
    ,g.C_UTONEV
    ,g.C_VEZETEKNEV
    ,g.C_FELTARELFOGADASDATUMA
    ,g.C_ISFELTARELFOGADOTT
  FROM T_GONDVISELO_OSSZES g
  WHERE g.ID = @gondviseloId AND g.TOROLT = 'F'

  SET @newId = SCOPE_IDENTITY()

  --T_FELHASZNALOBELEPES
  INSERT INTO T_FELHASZNALOBELEPES (
     C_KOTELEZOVALTOZTATNI
    ,C_MEGHIUSULTBELEPESEK
    ,C_UTOLSOBELEPES
    ,C_JELSZO
    ,C_JELSZOVALTOZTATASIDEJE
    ,C_SO
    ,C_BEJELENTKEZESINEV
    ,C_NEPTUNNAPLOJELSZO
    ,C_ELFOGADOTTGDPRNYILATKOZAT
    ,C_GDPRELFOGADASDATUMA
    ,C_IMPORTALT
    ,C_FELHASZNALOID
    ,C_GONDVISELOID
    ,C_INTEZMENYID
    ,C_TANEVID
    ,TOROLT
    ,SERIAL
    ,LASTCHANGED
    ,CREATED
    ,MODIFIER
    ,CREATOR
    ,ELOZOTANEVIREKORDID
    ,C_UTOLSOBELEPESMOBIL
  ) SELECT
     fb.C_KOTELEZOVALTOZTATNI
    ,fb.C_MEGHIUSULTBELEPESEK
    ,fb.C_UTOLSOBELEPES
    ,fb.C_JELSZO
    ,fb.C_JELSZOVALTOZTATASIDEJE
    ,fb.C_SO
    ,fb.C_BEJELENTKEZESINEV
    ,fb.C_NEPTUNNAPLOJELSZO
    ,fb.C_ELFOGADOTTGDPRNYILATKOZAT
    ,fb.C_GDPRELFOGADASDATUMA
    ,fb.C_IMPORTALT
    ,@trgTanuloId AS C_FELHASZNALOID
    ,@newId AS C_GONDVISELOID
    ,fb.C_INTEZMENYID
    ,@trgTanevId AS C_TANEVID
    ,fb.TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,@felhasznaloId AS CREATOR
    ,fb.ID AS ELOZOTANEVIREKORDID
    ,NULL AS C_UTOLSOBELEPESMOBIL
  FROM T_FELHASZNALOBELEPES_OSSZES fb 
  WHERE fb.C_GONDVISELOID = @gondviseloId
  AND 
  NOT EXISTS 
  (
    SELECT ID 
    FROM T_FELHASZNALOBELEPES_OSSZES
    WHERE C_TANEVID = @trgTanevId
      AND C_INTEZMENYID = fb.C_INTEZMENYID
      AND C_BEJELENTKEZESINEV = fb.C_BEJELENTKEZESINEV
      AND TOROLT = 'F'
  )

  --T_CIM
  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
     c.C_CIMTIPUSA
    ,c.C_ORSZAG
    ,c.C_IRANYITOSZAM
    ,c.C_VAROS
    ,c.C_MEGYE
    ,c.C_KOZTERULET
    ,c.C_HAZSZAM
    ,c.C_EPULET
    ,c.C_LEPCSOHAZ
    ,c.C_EMELET
    ,c.C_AJTO
    ,c.C_OSSZETETTCIM
    ,c.C_ALAPERTELMEZETT
    ,@trgTanuloId AS C_FELHASZNALOID
    ,@newId AS C_GONDVISELOID
    ,c.C_INTEZMENYID
    ,@trgTanevId AS C_TANEVID
    ,c.TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,@felhasznaloId AS CREATOR
    ,c.ID AS ELOZOTANEVIREKORDID
    ,c.CORE_PUBLIKUS
    ,c.C_KOZTERULETJELLEGENEV
  FROM T_CIM_OSSZES c 
  WHERE c.C_GONDVISELOID = @gondviseloId

  --T_TELEFON
  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
     t.C_TELEFONTIPUSA
    ,t.C_TELEFONSZAM
    ,t.C_LEIRAS
    ,t.C_ALAPERTELMEZETT
    ,t.C_ISPUBLIC
    ,@newId AS C_GONDVISELOID
    ,@trgTanuloId AS C_FELHASZNALOID
    ,t.C_INTEZMENYID
    ,@trgTanevId AS C_TANEVID
    ,t.TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,@felhasznaloId AS CREATOR
    ,t.ID AS ELOZOTANEVIREKORDID
    ,'F' AS C_ISMOBILROGZITETT
  FROM T_TELEFON_OSSZES t 
  WHERE t.C_GONDVISELOID = @gondviseloId

  --T_EMAIL
  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
     e.C_EMAILTIPUSA
    ,e.C_EMAILCIM
    ,e.C_ALAPERTELMEZETT
    ,e.C_ISPUBLIC
    ,@newId AS C_GONDVISELOID
    ,@trgTanuloId AS C_FELHASZNALOID
    ,e.C_INTEZMENYID
    ,@trgTanevId AS C_TANEVID
    ,e.TOROLT
    ,0 AS SERIAL
    ,GETDATE() AS LASTCHANGED
    ,GETDATE() AS CREATED
    ,NULL AS MODIFIER
    ,@felhasznaloId AS CREATOR
    ,e.ID AS ELOZOTANEVIREKORDID
    ,e.C_GUID
    ,e.C_ISHIBASANMEGADVA
    ,'F' ASC_ISMOBILROGZITETT
  FROM T_EMAIL_OSSZES e 
  WHERE e.C_GONDVISELOID = @gondviseloId
  

END
GO

