DROP PROCEDURE IF EXISTS uspHasValidMunkaviszony
GO
  CREATE PROCEDURE uspHasValidMunkaviszony
     @pUserId INT
    ,@pTanevId INT
  AS
  BEGIN
    SET NOCOUNT ON;

    IF EXISTS (
        SELECT 1
        FROM T_ALKALMAZOTT_OSSZES a
        JOIN T_TANEV_OSSZES t ON t.ID = a.C_ALTANEVID
          AND t.C_AKTIV = 'T'
          AND t.TOROLT = 'F'
        JOIN T_MUNKAUGYIADATOK_OSSZES ma ON ma.C_ALKALMAZOTTID = a.ID
          AND GETDATE() BETWEEN C_ALKALMAZASKEZDETE
            AND ISNULL(C_ALKALMAZASMEGSZUNESE, GETDATE())
          AND ma.TOROLT = 'F'
        WHERE a.ID = @pUserId
          AND a.C_ALTANEVID = @pTanevId
          AND a.TOROLT = 'F'
        )
      SELECT 1
    ELSE
      SELECT 0
  END
GO
