SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
DROP PROCEDURE IF EXISTS uspInsertOrUpdateHittan
GO

CREATE PROCEDURE uspInsertOrUpdateHittan
   @pCsoportId INT
  ,@pHittanTipusId INT
  ,@pDarabszam INT
  ,@pLetszam INT
  ,@pStartDate DATETIME
  ,@pEndDate DATETIME
  ,@pForJanuar BIT
  ,@pIntezmenyId INT
  ,@pTanevId INT
  ,@pUserId INT
AS
BEGIN
  SET NOCOUNT ON
  DECLARE @Id INT = 0

  SELECT @Id = ID
  FROM T_HITTANADATSZOLGALTATAS
  WHERE TOROLT = 'F' AND C_OSZTALYCSOPORTID = @pCsoportId AND CREATED >= @pStartDate AND CREATED <= @pEndDate

  IF (@Id IS NOT NULL AND @ID > 0)
	BEGIN
		UPDATE T_HITTANADATSZOLGALTATAS
		SET
			 C_EDDIGIORASZAM = @pDarabszam
			,C_HITTANTIPUSID = @pHittanTipusId
			,C_CSOPORTLETSZAM = @pLetszam
			,LASTCHANGED = GETDATE()
			,MODIFIER = @pUserId
		WHERE ID = @Id
	END
  ELSE
	BEGIN
		INSERT INTO T_HITTANADATSZOLGALTATAS(
			C_EDDIGIORASZAM
           ,C_HITTANTIPUSID
           ,C_CSOPORTLETSZAM
           ,C_SORSZAM
           ,C_OSZTALYCSOPORTID
           ,C_INTEZMENYID
           ,C_TANEVID
           ,TOROLT
           ,SERIAL
           ,LASTCHANGED
           ,CREATED
           ,MODIFIER
           ,CREATOR
		)
		VALUES(
			 @pDarabszam
			,@pHittanTipusId
			,@pLetszam
			,IIF(@pForJanuar = 1, 1, 0)
			,@pCsoportId
			,@pIntezmenyId
			,@pTanevId
			,'F'
			,0
			,IIF(@pForJanuar = 1, DATEFROMPARTS(YEAR(@pStartDate), 1, 15),  DATEFROMPARTS(YEAR(@pStartDate), 10, 1))
			,IIF(@pForJanuar = 1, DATEFROMPARTS(YEAR(@pStartDate), 1, 15),  DATEFROMPARTS(YEAR(@pStartDate), 10, 1))
			,@pUserId
			,@pUserId
		)
	END
 
END
GO
