IF OBJECT_ID('sp_FollowUpTerem') IS NOT NULL BEGIN
  DROP PROCEDURE sp_FollowUpTerem
END;
GO

CREATE PROCEDURE sp_FollowUpTerem
  @intezmenyId int,
  @aktTanevId int,
  @kovetkezoTanevId int,
  @teremId int
AS
BEGIN

IF (@teremId = 0)
	BEGIN
		INSERT INTO T_TEREM_OSSZES (
	 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
	  ,IIF((SELECT COUNT(1) FROM T_TEREMTIPUS_OSSZES WHERE C_ALTANEVID = @kovetkezoTanevId AND ID = t.C_JELLEG) = 0,180,t.C_JELLEG) AS C_JELLEG
      ,C_KAPACITAS AS C_KAPACITAS
	  ,ISNULL(
		(SELECT ID FROM T_MUKODESIHELY_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_MUKODESIHELYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID),
		(SELECT TOP 1 ID FROM T_MUKODESIHELY_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID)
		) AS C_MUKODESIHELYID
	  ,C_NEV AS C_NEV
	  ,(SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = t.C_TEREMFELELOSID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID) AS C_TEREMFELELOSID
	  ,C_TERULET AS C_TERULET
	  ,C_TOBBORATLEHETTARTANI AS C_TOBBORATLEHETTARTANI
	  ,C_VIZSGAKAPACITAS AS C_VIZSGAKAPACITAS
	  ,C_WIFILEFEDETT AS C_WIFILEFEDETT
      ,@intezmenyID AS C_INTEZMENYID
      ,@kovetkezoTanevId 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_TEREM_OSSZES t WHERE C_TANEVID = @aktTanevId AND C_INTEZMENYID = @intezmenyId AND TOROLT='F'
		AND 0 = (SELECT COUNT(1) FROM T_TEREM_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyId AND ELOZOTANEVIREKORDID = t.ID)
	END
ELSE
	BEGIN
		UPDATE kovetkezo SET
	 kovetkezo.C_AKTIV = aktiv.C_AKTIV
	,kovetkezo.C_JELLEG = aktiv.C_JELLEG
	,kovetkezo.C_KAPACITAS = aktiv.C_KAPACITAS
	,kovetkezo.C_MUKODESIHELYID = ISNULL(
		(SELECT ID FROM T_MUKODESIHELY_OSSZES WHERE ELOZOTANEVIREKORDID = aktiv.C_MUKODESIHELYID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID),
		(SELECT TOP 1 ID FROM T_MUKODESIHELY_OSSZES WHERE C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID)
		)
	,kovetkezo.C_NEV = aktiv.C_NEV
	,kovetkezo.C_TEREMFELELOSID = (SELECT ID FROM T_FELHASZNALO_OSSZES WHERE ELOZOTANEVIREKORDID = aktiv.C_TEREMFELELOSID AND C_TANEVID = @kovetkezoTanevId AND C_INTEZMENYID = @intezmenyID)
	,kovetkezo.C_TERULET = aktiv.C_TERULET
	,kovetkezo.C_TOBBORATLEHETTARTANI = aktiv.C_TOBBORATLEHETTARTANI
	,kovetkezo.C_VIZSGAKAPACITAS = aktiv.C_VIZSGAKAPACITAS
	,kovetkezo.C_WIFILEFEDETT = aktiv.C_WIFILEFEDETT
	,kovetkezo.TOROLT = aktiv.TOROLT
	FROM T_TEREM_OSSZES kovetkezo
	INNER JOIN T_TEREM_OSSZES aktiv ON aktiv.C_INTEZMENYID = kovetkezo.C_INTEZMENYID AND aktiv.C_TANEVID = @aktTanevId AND aktiv.ID = @teremId AND aktiv.TOROLT='F'
	WHERE kovetkezo.C_INTEZMENYID = @intezmenyId AND kovetkezo.C_TANEVID = @kovetkezoTanevId AND kovetkezo.ELOZOTANEVIREKORDID = @teremId AND kovetkezo.TOROLT='F'
	END
END
GO
