﻿DROP PROCEDURE IF EXISTS sp_FollowUpTantargy
GO

CREATE PROCEDURE sp_FollowUpTantargy
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @tantargyId int
AS
BEGIN

DECLARE
	 @kovetkezoTantargyId int
  ,@tantargyNev nvarchar(510)=(SELECT C_NEV FROM T_TANTARGY_OSSZES WHERE ID = @tantargyId)

  IF EXISTS(SELECT 1 FROM T_TANTARGY_OSSZES WHERE C_TANEVID=@kovetkezoTanevId AND TOROLT='F' AND C_NEV=@tantargyNev AND ISNULL(ELOZOTANEVIREKORDID,0)<>@tantargyId) BEGIN
    UPDATE T_TANTARGY_OSSZES SET ELOZOTANEVIREKORDID = NULL WHERE ELOZOTANEVIREKORDID = @tantargyId
    UPDATE T_TANTARGY_OSSZES SET ELOZOTANEVIREKORDID = @tantargyId WHERE C_NEV = @tantargyNev AND TOROLT='F' AND C_TANEVID = @kovetkezoTanevId
  END

  SELECT  @kovetkezoTantargyId = ID FROM T_TANTARGY_OSSZES WHERE ELOZOTANEVIREKORDID = @tantargyId AND C_TANEVID=@kovetkezoTanevId

	IF (SELECT TOROLT FROM T_TANTARGY_OSSZES WHERE ID = @tantargyId) = 'T' OR (SELECT TOROLT FROM T_TANTARGY_OSSZES WHERE ID = @kovetkezoTantargyId) = 'T' BEGIN
		RETURN
	END

IF (@kovetkezoTantargyId IS NULL) BEGIN
	INSERT INTO T_TANTARGY_OSSZES (
		 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
		,SERIAL
		,LASTCHANGED
		,CREATED
		,MODIFIER
		,CREATOR
		,ELOZOTANEVIREKORDID
  ) SELECT 
		 C_GYAKORLATI AS C_GYAKORLATI
		,C_NEV AS C_NEV
		,C_ROVIDNEV AS C_ROVIDNEV
		,tkt.ID AS C_TARGYKATEGORIA
		,C_TANORANKIVULI AS C_TANORANKIVULI
		,C_IMPORTALT AS C_IMPORTALT
		,C_FOTARGYE AS C_FOTARGYE
		,C_ALTANTARGYKENTNYOMTATVANYBAN AS C_ALTANTARGYKENTNYOMTATVANYBAN
		,C_NEVNYOMTATVANYBAN AS C_NEVNYOMTATVANYBAN
		,(SELECT ID FROM T_TANTARGY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_FOTARGYID) AS C_FOTARGYID 
		,C_ANGOLNEV
		,C_NEMETNEV
		,C_FONTOSSAG
		,@intezmenyID AS C_INTEZMENYID
		,@kovetkezoTanevId AS C_TANEVID
		,0 AS SERIAL
		,t.LASTCHANGED AS LASTCHANGED
		,t.LASTCHANGED AS CREATED
		,t.MODIFIER AS MODIFIER
		,t.MODIFIER AS CREATOR
		,t.ID AS ELOZOTANEVIREKORDID
    FROM T_TANTARGY_OSSZES t 
      INNER JOIN T_TARGYKATEGORIATIPUS_OSSZES tkt ON tkt.ELOZOTANEVIREKORDID=t.C_TARGYKATEGORIA AND tkt.TOROLT='F' AND tkt.C_ALTANEVID=@kovetkezoTanevId
    WHERE t.ID=@tantargyId
      
	END
ELSE
	BEGIN
		UPDATE kovetkezo SET
			 kovetkezo.C_GYAKORLATI = aktiv.C_GYAKORLATI
			,kovetkezo.C_NEV = aktiv.C_NEV
			,kovetkezo.C_ROVIDNEV = aktiv.C_ROVIDNEV
			,kovetkezo.C_TARGYKATEGORIA = tkt.ID
			,kovetkezo.C_TANORANKIVULI = aktiv.C_TANORANKIVULI
			,kovetkezo.C_IMPORTALT = aktiv.C_IMPORTALT
			,kovetkezo.C_FOTARGYE = aktiv.C_FOTARGYE
			,kovetkezo.C_ALTANTARGYKENTNYOMTATVANYBAN = aktiv.C_ALTANTARGYKENTNYOMTATVANYBAN
			,kovetkezo.C_NEVNYOMTATVANYBAN = aktiv.C_NEVNYOMTATVANYBAN
			,kovetkezo.C_FOTARGYID = (SELECT ID FROM T_TANTARGY_OSSZES WHERE ELOZOTANEVIREKORDID = aktiv.C_FOTARGYID)
			,kovetkezo.C_ANGOLNEV = aktiv.C_ANGOLNEV
			,kovetkezo.C_NEMETNEV = aktiv.C_NEMETNEV
			,kovetkezo.C_FONTOSSAG = aktiv.C_FONTOSSAG
			,kovetkezo.MODIFIER = aktiv.MODIFIER
			,kovetkezo.SERIAL += 1
			,kovetkezo.LASTCHANGED = aktiv.LASTCHANGED
		FROM T_TANTARGY_OSSZES kovetkezo
			INNER JOIN T_TANTARGY_OSSZES aktiv ON aktiv.ID = @tantargyId AND aktiv.ID=kovetkezo.ELOZOTANEVIREKORDID
      INNER JOIN T_TARGYKATEGORIATIPUS_OSSZES tkt ON tkt.ELOZOTANEVIREKORDID=aktiv.C_TARGYKATEGORIA AND tkt.TOROLT='F' AND tkt.C_ALTANEVID=@kovetkezoTanevId
   END
END
GO