﻿-- ===================================================================================================
-- KIRSTAT - Felhasználó Gyógytestnevelés
-- ===================================================================================================
-- 4. MUNKALAP FELSŐ - A02T11 - GYÓGYTESTNEVELÉS, NYITÓADATOK

DROP PROCEDURE IF EXISTS uspKIRstat_FelhasznaloGyogytestneveles
GO

CREATE PROCEDURE uspKIRstat_FelhasznaloGyogytestneveles
   @pFeladatellatasiHelyId int
  ,@pTanevId int
  ,@pIsKirstat bit = 1
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE 
     @statDatum datetime
  
  SET @statDatum = (SELECT LEFT(C_NEV, 4) FROM T_TANEV WHERE ID = @pTanevId) + IIF(@pIsKirstat = 1, '1001', '1008')

  SELECT 
     dk.ID AS TanuloId
    ,fh.C_NYOMTATASINEV AS TanuloNev
    ,d.C_NAME AS TestnevelesTipusa
    ,IIF(fh.C_NEME = 5, 'Leány','Fiú') AS Neme
    ,def.C_NAME_4 AS Evfolyam
  FROM T_FELHASZNALO fh
    INNER JOIN T_TANULO dk ON dk.ID = fh.ID AND dk.TOROLT = 'F'
    INNER JOIN T_DICTIONARYITEMBASE d ON d.ID = dk.C_TESTNEVELESTIPUSA AND d.C_TANEVID = dk.C_ALTANEVID AND d.TOROLT = 'F'
    INNER JOIN T_TANULOCSOPORT_OSSZES tcs ON tcs.C_TANULOID = fh.ID
      AND tcs.C_BELEPESDATUM <= @statDatum
      AND (tcs.C_KILEPESDATUM > @statDatum OR tcs.C_KILEPESDATUM IS NULL)
      AND tcs.TOROLT = 'F'
    INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs On ocs.ID = tcs.C_OSZTALYCSOPORTID 
      AND ocs.C_FELADATELLATASIHELYID = @pFeladatellatasiHelyId
      AND ocs.TOROLT = 'F'
    INNER JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
    INNER JOIN T_DICTIONARYITEMBASE_OSSZES def ON def.ID = ocs.C_EVFOLYAMTIPUSA 
      AND def.C_TANEVID = ocs.C_TANEVID 
      AND def.TOROLT = 'F'
  WHERE dk.C_TESTNEVELESTIPUSA BETWEEN 1092 AND 1094
    AND fh.TOROLT = 'F'
    AND EXISTS (SELECT 1 FROM T_OSZTALYCSOPORT nappalisOsztaly WHERE nappalisOsztaly.ID = ocs.ID AND nappalisOsztaly.C_KEPZESIFORMA = 1096)
    AND fh.C_TANEVID = @pTanevId

END
GO

