﻿DROP PROCEDURE IF EXISTS uspGetNemkotottMunkaidoByHelyettesites
GO

CREATE PROCEDURE uspGetNemkotottMunkaidoByHelyettesites
   @GroupId int = NULL
  ,@HelyettesitesId int = NULL
  ,@TanevId int
AS BEGIN
  SET NOCOUNT ON;

SELECT 
   nkido.ID as NemKotottMunkaidoId
  ,nkido.C_GROUPID as NemKotottMunkaidoGroupId
  ,nkido.C_KEZDETE as NemKotottMunkaidoKezdete
  ,nkido.C_VEGE as NemKotottMunkaidoVege
  ,hido.Id as HelyettesitesId
  ,hido.C_GROUPID as HelyettesitesGroupId
  ,hido.C_HELYETTESITESNAPJA as HelyettesitesNapjaDate
FROM T_HELYETTESITESIIDOSZAK hido
INNER JOIN T_ORARENDIORA oo ON oo.ID = hido.C_HELYETTESITETTORARENDID and oo.C_TANEVID = @TanevId and oo.TOROLT = 'F'
INNER JOIN T_NEMKOTOTTMUNKAIDO nkido ON 
  nkido.C_TANARID = oo.C_TANARID
  and nkido.C_KEZDETE <= CAST(hido.C_HELYETTESITESNAPJA + oo.C_ORAKEZDETE AS datetime) 
  and nkido.C_VEGE >= CAST(hido.C_HELYETTESITESNAPJA + oo.C_ORAVEGE AS datetime)
  and nkido.C_TANEVID = @TanevId
  and nkido.TOROLT = 'F'
WHERE hido.C_TANEVID = @TanevId and hido.TOROLT = 'F' and ((@GroupId IS NULL and (@HelyettesitesId IS NULL or hido.ID = @HelyettesitesId)) or hido.C_GROUPID = @GroupId)

END
GO
