DROP PROCEDURE IF EXISTS uspGetRogzitettOrak
GO

CREATE PROCEDURE uspGetRogzitettOrak
   @pTanarId int
  ,@pStart datetime
  ,@pEnd datetime
AS BEGIN
  
  SELECT
    ROUND(SUM(dbo.fnGetOrahossz(tao.C_CSENGETESIRENDORAID, tao.C_ORAKEZDETE, tao.C_ORAVEGE, ocs.C_FELADATKATEGORIAID, cs.Id, feh.C_OKTATASINEVELESIFELADATTIPUS, cst.C_ORAPERC)), 2) AS SumOra
  FROM T_TANITASIORA tao
    INNER JOIN T_OSZTALYCSOPORT ocs on ocs.ID = tao.C_OSZTALYCSOPORTID
    LEFT JOIN T_CSOPORT cs ON cs.ID = ocs.ID
    LEFT JOIN T_CSOPORTTIPUS cst ON cst.ID = cs.C_TIPUSA AND cst.C_ALTANEVID = cs.C_ALTANEVID
    LEFT JOIN T_FELADATELLATASIHELY feh ON feh.ID = ocs.C_FELADATELLATASIHELYID
  WHERE tao.C_MEGTARTOTT = 'T'
    AND tao.C_TANARID = @pTanarId
    AND tao.C_HELYETTESITOTANARID IS NULL
    AND tao.C_DATUM BETWEEN @pStart AND @pEnd

  SELECT
    ROUND(SUM(CONVERT(FLOAT,(DATEDIFF(minute, C_KEZDETE, C_VEGE) + IIF(DATEPART(hour, C_VEGE) = 23 AND DATEPART(minute, C_VEGE) = 59, 1, 0))) / 60), 2) AS SumOra
  FROM T_NEMKOTOTTMUNKAIDO
  WHERE C_MEGTARTOTT = 'T'
    AND C_TANARID = @pTanarId
    AND CAST(C_KEZDETE AS DATE) BETWEEN @pStart AND @pEnd

END
GO