﻿DROP PROCEDURE IF EXISTS sp_FollowUpAlkalmazottPEP
GO

CREATE PROCEDURE sp_FollowUpAlkalmazottPEP
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @PEPId int
AS
BEGIN
  DECLARE @kovPEPId int
  
  SELECT @kovPEPId = ID
  FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE ELOZOTANEVIREKORDID = @PEPId 
  
  IF (SELECT TOROLT FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE ID = @PEPId) = 'T' OR (SELECT TOROLT FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES WHERE ID = @kovPEPId) = 'T' BEGIN
    RETURN
  END

  IF @kovPEPId IS NOT NULL BEGIN
	UPDATE kov set
		 kov.C_FOKOZAT = akt.C_FOKOZAT
		,kov.C_MINOSITESDATUM = akt.C_MINOSITESDATUM
		,kov.C_PORTFOLIOSTATUSZ = akt.C_PORTFOLIOSTATUSZ
		,kov.SERIAL += 1
		,kov.LASTCHANGED = akt.LASTCHANGED
		,kov.MODIFIER = akt.MODIFIER
	FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES kov
		INNER JOIN T_PEDAGOGUSELETPALYAMODELL_OSSZES akt ON kov.ELOZOTANEVIREKORDID = akt.ID AND kov.ID=@kovPEPId
  END
  ELSE BEGIN
	INSERT INTO T_PEDAGOGUSELETPALYAMODELL_OSSZES(
		 C_FOKOZAT
		,C_MINOSITESDATUM
		,C_PORTFOLIOSTATUSZ
		,C_TANARID
		,C_INTEZMENYID
		,C_TANEVID
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
	) SELECT 
		 akt.C_FOKOZAT AS C_FOKOZAT
		,akt.C_MINOSITESDATUM AS C_MINOSITESDATUM
		,akt.C_PORTFOLIOSTATUSZ AS C_PORTFOLIOSTATUSZ
		,fh.ID AS C_TANARID
		,@intezmenyId AS C_INTEZMENYID
		,@kovetkezoTanevId AS C_TANEVID
		,0 AS SERIAL
		,akt.LASTCHANGED AS LASTCHANGED
		,akt.LASTCHANGED AS CREATED
		,akt.MODIFIER AS MODIFIER
		,akt.MODIFIER AS CREATOR
		,akt.ID AS ELOZOTANEVIREKORDID
  FROM T_PEDAGOGUSELETPALYAMODELL_OSSZES akt
	INNER JOIN T_FELHASZNALO_OSSZES fh ON fh.ELOZOTANEVIREKORDID=akt.C_TANARID
  WHERE akt.ID=@PEPId
  END

END
GO

