﻿DROP PROCEDURE IF EXISTS uspSzirStatTanuloEvEleje
GO

CREATE PROCEDURE uspSzirStatTanuloEvEleje
   @pTanevId int
  ,@pIntezmenyid int
AS BEGIN
  DECLARE @datum datetime, @IsSniKezelo bit

  SELECT @datum = LEFT(tv.C_NEV, 4) + '1001' FROM T_TANEV tv WHERE ID = @pTanevId
  SET @IsSniKezelo = dbo.fnGetRendszerbeallitasEnumBool(1666, @pIntezmenyid, @pTanevId)

  CREATE TABLE #EmeltSzintTanulo (C_TANULOID int PRIMARY KEY, Targy1 nvarchar(200), Targy2 nvarchar(200), Targy3 nvarchar(200))

  CREATE TABLE #EmeltOraszam (TantargyKategoriaId int NOT NULL, Evfolyam nvarchar(2) NOT NULL, Oraszam int NOT NULL, PRIMARY KEY(TantargyKategoriaId, Evfolyam))
  INSERT INTO #EmeltOraszam (TantargyKategoriaId, Evfolyam, Oraszam) VALUES
    (1198, '9', 3) ,(1198, '10', 4) ,(1198, '11', 4) ,(1198, '12', 4) ,(1210, '9', 3) ,(1210, '10', 3) ,(1210, '11', 3) ,(1210, '12', 3) ,(6754, '9', 2) ,(6754, '10', 2)
   ,(6754, '11', 3) ,(6754, '12', 3) ,(1293, '12', 1) ,(6727, '9', 3) ,(6727, '10', 3) ,(6727, '11', 4) ,(6727, '12', 4) ,(6731, '9', 3) ,(6731, '10', 3) ,(6731, '11', 4)
   ,(6731, '12', 4) ,(6733, '9', 3) ,(6733, '10', 3) ,(6733, '11', 4) ,(6733, '12', 4) ,(6729, '9', 3) ,(6729, '10', 3) ,(6729, '11', 4) ,(6729, '12', 4) ,(6732, '9', 3)
   ,(6732, '10', 3) ,(6732, '11', 4) ,(6732, '12', 4) ,(6734, '9', 3) ,(6734, '10', 3) ,(6734, '11', 4) ,(6734, '12', 4) ,(6730, '9', 3) ,(6730, '10', 3) ,(6730, '11', 4)
   ,(6730, '12', 4) ,(6728, '9', 3) ,(6728, '10', 3) ,(6728, '11', 4) ,(6728, '12', 4) ,(7748, '9', 3) ,(7748, '10', 3) ,(7748, '11', 4)
   ,(7748, '12', 4) ,(7749, '9', 3) ,(7749, '10', 3) ,(7749, '11', 4) ,(7749, '12', 4) ,(7750, '9', 3) ,(7750, '10', 3) ,(7750, '11', 4) ,(7750, '12', 4) ,(7751, '9', 3)
   ,(7751, '10', 3) ,(7751, '11', 4) ,(7751, '12', 4) ,(7752, '9', 3) ,(7752, '10', 3) ,(7752, '11', 4) ,(7752, '12', 4) ,(7753, '9', 3) ,(7753, '10', 3) ,(7753, '11', 4)
   ,(7753, '12', 4) ,(7754, '9', 3) ,(7754, '10', 3) ,(7754, '11', 4) ,(7754, '12', 4) ,(6737, '9', 3) ,(6737, '10', 3) ,(6737, '11', 3) ,(6737, '12', 3) ,(6741, '9', 3)
   ,(6741, '10', 3) ,(6741, '11', 3) ,(6741, '12', 3) ,(6743, '9', 3) ,(6743, '10', 3) ,(6743, '11', 3) ,(6743, '12', 3) ,(6739, '9', 3) ,(6739, '10', 3) ,(6739, '11', 3)
   ,(6739, '12', 3) ,(6742, '9', 3) ,(6742, '10', 3) ,(6742, '11', 3) ,(6742, '12', 3) ,(6744, '9', 3) ,(6744, '10', 3) ,(6744, '11', 3) ,(6744, '12', 3) ,(6740, '9', 3)
   ,(6740, '10', 3) ,(6740, '11', 3) ,(6740, '12', 3) ,(6738, '9', 3) ,(6738, '10', 3) ,(6738, '11', 3) ,(6738, '12', 3) ,(1219, '9', 2) ,(1219, '10', 3) ,(1220, '9', 1)
   ,(1220, '10', 2) ,(1221, '9', 3) ,(1221, '10', 2) ,(8272, '9', 2) ,(8272, '10', 1) ,(7747, '11', 2) ,(1224, '9', 1) ,(1224, '10', 1) ,(1226, '9', 1) ,(1226, '10', 1)
   ,(1294, '11', 1) ,(1228, '12', 1) ,(1235, 'NY', 5) ,(1235, '9', 5) ,(1235, '10', 5) ,(1235, '11', 5) ,(1235, '12', 5), (8274, 'NY', 5), (8274, '9', 5), (8274, '10', 5)
   ,(8274, '11', 5), (8274, '12', 5) ,(1248, 'NY', 1), (1248, '9', 1), (1248, '10', 1), (1248, '11', 1), (1248, '12', 1) ,(1230, 'NY', 3), (1230, '9', 2), (1230, '10', 1)
   ,(1230, '11', 2) ,(6727, 'NY', 18) ,(6731, 'NY', 18) ,(6733, 'NY', 18) ,(6729, 'NY', 18) ,(6732, 'NY', 18) ,(6734, 'NY', 18) ,(6730, 'NY', 18) ,(6728, 'NY', 18)

   INSERT INTO #EmeltSzintTanulo (C_TANULOID, Targy1, Targy2, Targy3)
   SELECT pvt.C_TANULOID,  [1] AS Targy1, [2] AS Targy2, [3] AS Targy3
      FROM (
        SELECT y.C_TANULOID,  y.C_FELADATELLATASIHELYID, y.C_TARGYKATEGORIANEV, y.RN2
        FROM (
          SELECT x.C_TANULOID, x.C_TARGYKATEGORIANEV, x.C_ORASZAM, x.C_TANTARGYID, x.C_FELADATELLATASIHELYID, ROW_NUMBER() OVER(PARTITION BY x.C_TANULOID, x.C_FELADATELLATASIHELYID ORDER BY x.C_ORASZAM DESC) AS RN2
          FROM (
            SELECT 
               tcs.C_TANULOID
              ,dib.ID AS C_TARGYKATEGORIAID
              ,tgy.ID AS C_TANTARGYID
              ,fhely.ID AS C_FELADATELLATASIHELYID
              ,MAX(IIF(ocsO.C_EVFOLYAMTIPUSA = 7366, IIF(tta.C_TANULOEVFOLYAMTIPUSID IN (7869, 7905, 7127, 1333, 7870, 7128), 'NY', efTta.C_NAME_4), IIF(tta.C_TANULOEVFOLYAMTIPUSID IN (7869, 7905, 7127, 1333, 7870, 7128), 'NY', efO.C_NAME_4))) AS C_EVFOLYAM
              ,MAX(dib.C_NAME) AS C_TARGYKATEGORIANEV
              ,SUM(f.C_ORASZAM) AS C_ORASZAM
              ,ROW_NUMBER() OVER(PARTITION BY tcs.C_TANULOID, dib.ID, fhely.ID ORDER BY SUM(f.C_ORASZAM) DESC) AS RN
            FROM T_TANULOCSOPORT tcs
              INNER JOIN T_FOGLALKOZAS f on f.C_INTEZMENYID = tcs.C_INTEZMENYID
                AND f.C_TANEVID = tcs.C_TANEVID
                AND f.C_OSZTALYCSOPORTID = tcs.C_OSZTALYCSOPORTID AND tcs.TOROLT = 'F'
              INNER JOIN T_TANTARGY tgy on tgy.ID = f.C_TANTARGYID and tgy.C_INTEZMENYID = f.C_INTEZMENYID and tgy.C_TANEVID = f.C_TANEVID AND tgy.TOROLT = 'F'
              LEFT JOIN T_DICTIONARYITEMBASE dib ON dib.C_TANEVID = tgy.C_TANEVID AND dib.ID = tgy.C_TARGYKATEGORIA AND dib.C_INTEZMENYID = tgy.C_INTEZMENYID AND dib.TOROLT = 'F'
              INNER JOIN T_TANULOCSOPORT tcsO ON tcsO.C_TANULOID = tcs.C_TANULOID AND tcsO.TOROLT = 'F'
              INNER JOIN T_OSZTALYCSOPORT ocsO ON ocsO.ID = tcsO.C_OSZTALYCSOPORTID AND ocsO.TOROLT = 'F'
              INNER JOIN T_TANULOTANUGYIADATOK tta ON tta.C_TANULOCSOPORTID = tcsO.ID AND tta.TOROLT = 'F'
              LEFT JOIN T_DICTIONARYITEMBASE efO ON efO.ID = ocsO.C_EVFOLYAMTIPUSA AND efO.C_TANEVID = ocsO.C_TANEVID AND efO.C_INTEZMENYID = ocsO.C_INTEZMENYID AND efO.TOROLT = 'F'
              LEFT JOIN T_DICTIONARYITEMBASE efTta ON efTta.ID = tta.C_TANULOEVFOLYAMTIPUSID AND efTta.C_TANEVID = tta.C_TANEVID AND efTta.C_INTEZMENYID = tta.C_INTEZMENYID AND efTta.TOROLT = 'F'
              INNER JOIN T_FELADATELLATASIHELY fhely ON fhely.ID = ocsO.C_FELADATELLATASIHELYID AND fhely.TOROLT = 'F'
            WHERE f.TOROLT = 'F'
              AND tcs.C_BELEPESDATUM <= @datum 
              AND (tcs.C_KILEPESDATUM IS NULL OR tcs.C_KILEPESDATUM > @datum)
              AND tcsO.C_BELEPESDATUM <= @datum 
              AND (tcsO.C_KILEPESDATUM IS NULL OR tcsO.C_KILEPESDATUM > @datum)   
              AND f.C_TANEVID = @pTanevId
              AND f.C_INTEZMENYID = @pIntezmenyid     
              AND fhely.C_OKTATASINEVELESIFELADATTIPUS IN (1160, 7764)
            GROUP BY tcs.C_TANULOID, fhely.ID, tgy.ID, dib.ID
          ) x
            INNER JOIN #EmeltOraszam eo ON eo.Evfolyam COLLATE Hungarian_CI_AI = x.C_EVFOLYAM COLLATE Hungarian_CI_AI  AND eo.TantargyKategoriaId  = x.C_TARGYKATEGORIAID         
          WHERE x.C_ORASZAM > eo.Oraszam
            AND x.RN = 1
        ) y
        WHERE y.RN2 <= 3
      ) z PIVOT 
        ( MAX(z.C_TARGYKATEGORIANEV)
          FOR RN2 IN ([1], [2], [3])
        ) AS pvt 

  CREATE TABLE #Idegennyelv (C_TANULOID int, Nyelv1 nvarchar(100), Nyelv2 nvarchar(100), Nyelv3 nvarchar(100));
  INSERT INTO #Idegennyelv(C_TANULOID, Nyelv1, Nyelv2, Nyelv3)
  SELECT pvt.C_TANULOID, [1] AS Nyelv1, [2] AS Nyelv2, [3] AS Nyelv3
  FROM (
    SELECT x.C_TANULOID, x.C_TARGYKATEGORIANEV, x.RN
    FROM (
      SELECT 
         C_TANULOID
        ,C_TARGYKATEGORIANEV
        ,ROW_NUMBER() OVER(PARTITION BY C_TANULOID ORDER BY SUM_C_ORASZAM DESC, C_TARGYKATEGORIANEV ASC) AS RN
      FROM (
        SELECT 
           tcs.C_TANULOID
          ,dib.C_NAME AS C_TARGYKATEGORIANEV
          ,SUM(f.C_ORASZAM) SUM_C_ORASZAM
        FROM T_TANULOCSOPORT tcs
        INNER JOIN T_FOGLALKOZAS f ON f.C_INTEZMENYID = tcs.C_INTEZMENYID AND tcs.TOROLT = 'F'
        AND f.C_TANEVID = tcs.C_TANEVID
        AND f.C_OSZTALYCSOPORTID = tcs.C_OSZTALYCSOPORTID
        INNER JOIN T_TANTARGY tgy ON tgy.ID = f.C_TANTARGYID and tgy.C_INTEZMENYID = f.C_INTEZMENYID and tgy.C_TANEVID = f.C_TANEVID AND tgy.TOROLT = 'F'
        INNER JOIN T_DICTIONARYITEMBASE dib ON dib.C_TANEVID = tgy.C_TANEVID AND dib.ID = tgy.C_TARGYKATEGORIA AND dib.C_INTEZMENYID = tgy.C_INTEZMENYID AND dib.TOROLT = 'F'
        WHERE tgy.C_TARGYKATEGORIA in (1202, 1203, 1207, 1204, 1205, 1206, 1208, 1209, 1201, 7748, 7749, 7750, 7751, 7752, 7753, 7754, 6727, 6731, 6733, 6729, 6732, 6734, 6730, 6728, 6737, 6741, 6743, 6739, 6742, 6744, 6740, 6738)
        AND f.TOROLT = 'F'
        AND tcs.C_TANEVID = @pTanevId
        AND tcs.C_INTEZMENYID = @pIntezmenyid  
        GROUP BY tcs.C_TANULOID, dib.C_NAME
     ) sub1
   ) x
   WHERE x.RN <= 3
   ) z 
   PIVOT (MAX(z.C_TARGYKATEGORIANEV) FOR RN IN ([1], [2], [3])) AS pvt

  SELECT 
     fh.ID  AS TanuloId
    ,CONCAT(fh.C_NYOMTATASINEV, ' (', fh.C_OKTATASIAZONOSITO ,')') AS TanuloNev
    ,ia.C_OMKOD AS IntezmenyOmAzonosito
    ,ia.C_NEV AS IntezmenyNev
    ,ia.C_VAROS AS IntezenyTelepules
    ,ia.C_IRANYITOSZAM AS IntezmenyIrszam
    ,RTRIM(CONCAT(ia.C_KOZTERULETNEV, ' ', ia.C_KOZTERULETJELLEGENEV,' ', ia.C_HAZSZAM, ' ', ia.C_EMELET, ' ', ia.C_AJTO)) AS IntezmenyCim
    ,ISNULL(NULLIF(ia.C_EMAILCIM, ''), '-') AS IntezmenyEmailcim
    ,CONCAT(ia.C_OMKOD, '/', fhely.C_FELADATELLATASIHELYKOD) AS FelhelyAzonosito
    ,mhely.C_NEV AS FelhelyNev
    ,mhely.C_VAROS AS FelhelyTelepules
    ,mhely.C_IRANYITOSZAM AS FelhelyIrszam
    ,RTRIM(CONCAT(mhely.C_KOZTERULETNEV, ' ', mhely.C_KOZTERULETJELLEGENEV,' ', mhely.C_HAZSZAM, ' ', mhely.C_EMELET, ' ', mhely.C_AJTO)) AS FelhelyCim
    ,ISNULL(NULLIF(mhely.C_EMAILCIM, ''), '-') AS FelhelyEmailcim
    ,fhelytip.C_NAME AS FelhelyTipus
    ,ISNULL(cimTanulo.C_VAROS, '-') AS TanuloTelepules
    ,COALESCE(cimSzulo.C_VAROS, cimTanulo.C_VAROS, '-') AS SzuloTelepules
    ,IIF(fh.C_ALLAMPOLGARSAGA <> 217 AND fh.C_ALLAMPOLGARSAGA2 <> 217, c2.C_VAROS, '-') AS TanuloKulfoldiTelepules
    ,DATEDIFF(YEAR, fh.C_SZULETESIDATUM, @datum) AS TanuloKorev
    ,ISNULL(jogviszony.C_NAME, '-')  AS TanuloJogviszony
    ,ISNULL(nem.C_NAME, '-') AS TanuloNem
    ,ISNULL(tantevijellemzotta.C_NAME, '-') AS TanuloTanterviJellemzo
    ,LTRIM(RTRIM(REPLACE(ISNULL(munkarend.C_NAME, '-'), '(*)', ''))) AS TanuloMunkarend
    ,ISNULL(IIF(ocs.C_EVFOLYAMTIPUSA <> 7366, evfolyamOcs.C_NAME, evfolyamTta.C_NAME), '-') AS TanuloEvfolyam
    ,IIF(fhely.C_OKTATASINEVELESIFELADATTIPUS NOT IN (5435,7664,7894,7895,7896,1161,1162,6463,1176,7765), '-', COALESCE(NULLIF(agazatNkt.C_NAME, 'na'), NULLIF(agazatOkj.C_NAME, 'na'), NULLIF(agazatSzkt.C_NAME, 'na'), '-')) AS TanuloAgazat
    ,IIF(fhely.C_OKTATASINEVELESIFELADATTIPUS NOT IN (5435,7664,7894,7895,7896,1161,1162,6463,1176,7765), '-', IIF(PATINDEX ( '% - [1234567890]%' , COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-') ) = 0, '-', SUBSTRING(COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-'), PATINDEX ( '% - [1234567890]%' , COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-') ) + 3, 100))) AS TanuloSzakkepesitesAzon
    ,IIF(fhely.C_OKTATASINEVELESIFELADATTIPUS NOT IN (5435,7664,7894,7895,7896,1161,1162,6463,1176,7765), '-', IIF(PATINDEX ( '% - [1234567890]%' , COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-') ) = 0, COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-'), LEFT(COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-'), PATINDEX ( '% - [1234567890]%' , COALESCE(NULLIF(ISNULL(szktTta.C_NAME, szktO.C_NAME), 'na'), NULLIF(ISNULL(nktTta.C_NAME, nktO.C_NAME), 'na'), NULLIF(okj.C_NAME, 'na'), '-') )))) AS TanuloSzakkepesitesNev
    ,ISNULL(allampolg.C_NAME, '-') AS TanuloAllampolgarsag
    ,ISNULL(IIF(fh.C_ALLAMPOLGARSAGA = fh.C_ALLAMPOLGARSAGA2, '-', allampolg2.C_NAME), '-') AS TanuloAllampolgarsag2
    ,IIF(@IsSniKezelo = 1, IIF(dk.C_SAJATOSNEVELESU = 'T', 'Igen', 'Nem'), '-') AS TanuloSni
    ,IIF(@IsSniKezelo = 1, IIF(dk.C_SAJATOSNEVELESU = 'T' AND ocs.C_ISGYOGYPEDAGOGIAILOGOPEDIAI = 'T', 'Igen', 'Nem'), '-') AS TanuloSniGyogypedNevelt
    ,IIF(@IsSniKezelo = 1 AND dk.C_SAJATOSNEVELESU = 'T', IIF(integralt.INTEGRALT = 'T', 'Igen', 'Nem'), '-') AS TanuloSniIntegraltanNevelt
    ,IIF(@IsSniKezelo = 1 AND snizavar.ID <> 8796 AND snizavar.ID IS NOT NULL, snizavar.C_NAME, '-') AS TanuloSniZavar
    ,IIF(tta.C_ISKIEMELTENTEHETSEGES = 'T', 'Igen', 'Nem') AS TanuloKiemeltTehetseg
    ,IIF(tta.C_EVISMETLO = 'T', 'Igen', 'Nem') AS TanuloEvfolyamIsmetlo
    ,IIF(tta.C_MAGANTANULO = 'T' AND (tta.C_MAGANTANULOSAGVEGE IS NULL OR tta.C_MAGANTANULOSAGVEGE > @datum), 'Igen', 'Nem') AS TanuloEgyeniTanrend
    ,IIF(tta.C_BEJARO = 'T', 'Igen', 'Nem') AS TanuloBejaro
    ,IIF(dk.C_KOLLEGIUMIELLATASOS = 'T', 'Igen', 'Nem') AS TanuloKollegista
    ,IIF(dk.C_ISEXTERNATUSIELLATASBAN = 'T', 'Igen', 'Nem') AS TanuloExternatus
    ,IIF(dk.C_SZOCIALISTAMOGATAS = 'T', 'Igen', 'Nem') AS TanuloSzocOsztondij
    ,IIF(etkezes.C_NAME = 'na' OR dk.C_ETKEZESIKEDVEZMENY IS NULL, '-', etkezes.C_NAME) AS TanuloEtkeztetes
    ,CASE
      WHEN tta.C_TANULOSZERZODESES = 'T' THEN 'Tanulószerződés'
      WHEN tta.C_EGYUTTMUKODESES = 'T' THEN 'Együttműködési megállapodásos'
      WHEN tta.C_ISSZAKKEPZESIMUNKASZERZODESS = 'T' THEN 'Szakképzési munkaszerződéssel'
      ELSE '-'
     END AS TanuloGyakJogviszony
    ,IIF(@IsSniKezelo = 1, IIF(dk.C_BTMPROBLEMAS = 'T', 'Igen', 'Nem'), '-') AS TanuloBtm
    ,IIF(dk.C_ISNAPKOZIOTTHONIELLATASBAN = 'T', 'Igen', 'Nem') AS TanuloNapkozi
    ,IIF(dk.C_HATRANYOSHELYZETU IS NULL OR dk.C_HATRANYOSHELYZETU IN (1120, 1121), 'Nem', 'Igen') AS TanuloHatranyosHelyzet
    ,IIF(dk.C_HATRANYOSHELYZETU IS NULL OR dk.C_HATRANYOSHELYZETU IN (1120, 1121, 1122), 'Nem', 'Igen') AS TanuloHalmozottanHatranyos
    ,IIF(dk.C_ISRENDSZERESGYERMEKVEDELMI = 'T', 'Igen', 'Nem') AS TanuloGyermekvedelmiTamogatas
    ,IIF(tta.C_ISSZAMITOGEPETOKTCELRAHASZNA = 'T', 'Igen', 'Nem') AS TanuloSzamitogepOktatasra
    ,IIF(targyak.Hittan = 1, 'Igen', 'Nem') AS TanuloHittan
    ,IIF(targyak.Etika  = 1, 'Igen', 'Nem') AS TanuloEtika
    ,IIF(idegennyelv.Nyelv1 IS NULL AND idegennyelv.Nyelv2 IS NULL AND idegennyelv.Nyelv3 IS NULL, 'Nem', 'Igen') AS TanuloNyelvoktatas
    ,IIF(felmentes.C_TANULOID IS NOT NULL, 'Igen', 'Nem') AS TanuloNyelvoktatasFelmentes
    ,IIF(tta.C_TANDIJATFIZETO = 'T', 'Igen', 'Nem') AS TanuloTandijas
    ,IIF(tta.C_TERITESIDIJATFIZETO = 'T', 'Igen', 'Nem') AS TanuloTeritesiDijas
    ,IIF(ocs.C_EVFOLYAMTIPUSA IN (1303,1311,7111,7871,7872,7906,8396,7112,7113,7114,1332,7869,7905,7127,1333,7870,7128,3019,3018,3020,7904,7868,1316,7862,1317,1319,7883,3016,7866,6702,6818,7902), 'Igen', 'Nem') AS TanuloKezdoEvfolyam
    ,IIF(tta.C_DIAKSPORTKOROS = 'T', 'Igen', 'Nem') AS TanuloDiaksportKor
    ,IIF(tta.C_ISSZABOKYADOLFSZAKKEPODRESZE = 'T', 'Igen', 'Nem') AS TanuloSzabokyAdolf
    ,IIF(o.C_AJPROGRAM = 'T', IIF(o.C_ARANYJANOSPROGRAMTIPUSID IS NULL, 'Igen', aranyjanos.C_NAME), 'Nem') AS TanuloAranyJanos
    ,IIF(emeltSzint.Targy1 IS NULL AND emeltSzint.Targy2 IS NULL AND emeltSzint.Targy3 IS NULL, 'Nem', 'Igen') AS TanuloEmeltSzint
    ,ISNULL(emeltSzint.Targy1, '-') AS TanuloEmeltSzintTargy1
    ,ISNULL(emeltSzint.Targy2, '-') AS TanuloEmeltSzintTargy2
    ,ISNULL(emeltSzint.Targy3, '-') AS TanuloEmeltSzintTargy3
    ,ISNULL(idegennyelv.Nyelv1, '-') AS TanuloIdegenNyelv1
    ,ISNULL(idegennyelv.Nyelv2, '-') AS TanuloIdegenNyelv2
    ,ISNULL(idegennyelv.Nyelv3, '-') AS TanuloIdegenNyelv3
    ,IIF(o.C_NEMZETISEGI = 'T', 'Igen', 'Nem') AS TanuloNemzetisegiOktatas
    ,IIF(fhely.C_OKTATASINEVELESIFELADATTIPUS IN (1158, 1178, 7763), IIF(tta.C_ISEGESZNAPOSISKOKTRESZESUL = 'T', 'Igen', 'Nem'), '-') AS TanuloEgeszNaposOktatas
    ,IIF(tta.C_IS9KOZUL8ELOZOTANEVBENVEGEZT = 'T', 'Igen', 'Nem') AS TanuloNyolcEvfolyamElozoTanevben
    ,IIF(dk.C_ISOTTHONIELLATAS = 'T', 'Igen', 'Nem') AS TanuloOtthoniEllatas
  FROM T_FELHASZNALO fh 
    INNER JOIN T_TANULO dk ON dk.ID = fh.ID
    INNER JOIN T_INTEZMENYADATOK ia ON ia.C_TANEVID = fh.C_TANEVID AND ia.TOROLT = 'F'
    INNER JOIN T_INTEZMENY i ON ia.C_INTEZMENYID = i.ID AND i.TOROLT = 'F'
    INNER JOIN T_FELADATELLATASIHELY fhely ON fhely.C_TANEVID = ia.C_TANEVID AND fhely.TOROLT = 'F'
    INNER JOIN T_MUKODESIHELY mhely ON mhely.ID = fhely.C_MUKODESIHELYID AND mhely.TOROLT = 'F'
    INNER JOIN T_DICTIONARYITEMBASE fhelytip ON fhelytip.ID = fhely.C_OKTATASINEVELESIFELADATTIPUS AND fhelytip.C_TANEVID = fhely.C_TANEVID AND fhelytip.TOROLT = 'F'
    INNER JOIN T_DICTIONARYITEMBASE nem ON fh.C_NEME = nem.ID AND nem.C_TANEVID = fh.C_TANEVID
    INNER JOIN T_TANULOCSOPORT tcs ON tcs.C_TANULOID = dk.ID AND tcs.TOROLT = 'F'
    INNER JOIN T_TANULOTANUGYIADATOK tta ON tta.C_TANULOCSOPORTID = tcs.ID AND tta.TOROLT = 'F'
    INNER JOIN T_OSZTALYCSOPORT ocs ON ocs.ID = tcs.C_OSZTALYCSOPORTID AND ocs.C_FELADATELLATASIHELYID = fhely.ID AND ocs.TOROLT = 'F'
    INNER JOIN T_OSZTALY o ON o.ID = ocs.ID AND o.TOROLT = 'F'
    LEFT JOIN T_TANULOSNI sni ON sni.C_TANULOID = dk.ID AND sni.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE muvag ON ocs.C_MUVESZETIAGID = muvag.ID AND muvag.C_TANEVID = ocs.C_TANEVID AND muvag.C_INTEZMENYID = ocs.C_INTEZMENYID AND muvag.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE mufajoszt ON mufajoszt.ID = o.C_MUFAJTIPUSID AND mufajoszt.C_TANEVID = o.C_ALTANEVID AND mufajoszt.C_INTEZMENYID = o.C_ALINTEZMENYID AND mufajoszt.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE mufajtta ON mufajtta.ID = tta.C_MUFAJTIPUSID AND mufajtta.C_TANEVID = tta.C_TANEVID AND mufajtta.C_INTEZMENYID = tta.C_INTEZMENYID AND mufajtta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE tanszakoszt ON tanszakoszt.ID = o.C_TANSZAKTIPUSID AND tanszakoszt.C_TANEVID = o.C_ALTANEVID AND tanszakoszt.C_INTEZMENYID = o.C_ALINTEZMENYID AND tanszakoszt.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE tanszaktta ON tanszaktta.ID = tta.C_TANSZAKTIPUSID AND tanszaktta.C_TANEVID = tta.C_TANEVID AND tanszaktta.C_INTEZMENYID = tta.C_INTEZMENYID AND tanszaktta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE snizavar ON sni.C_FOGYATEKOSSAGTIPUSID = snizavar.ID AND snizavar.C_TANEVID = sni.C_TANEVID AND snizavar.C_INTEZMENYID = sni.C_INTEZMENYID AND snizavar.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE aranyjanos ON o.C_ARANYJANOSPROGRAMTIPUSID = aranyjanos.ID AND aranyjanos.C_TANEVID = o.C_ALTANEVID AND aranyjanos.C_INTEZMENYID = o.C_ALINTEZMENYID AND aranyjanos.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE tantevijellemzotta ON tantevijellemzotta.ID = tta.C_TANTERVIJELLEMZOID AND tantevijellemzotta.C_TANEVID = tta.C_TANEVID AND tantevijellemzotta.C_INTEZMENYID = tta.C_INTEZMENYID AND tantevijellemzotta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE munkarend ON munkarend.ID = tta.C_TANTERVIJELLEGID AND munkarend.C_TANEVID = ocs.C_TANEVID AND munkarend.C_INTEZMENYID = ocs.C_INTEZMENYID AND munkarend.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE evfolyamOcs ON evfolyamOcs.ID = ocs.C_EVFOLYAMTIPUSA AND evfolyamOcs.C_TANEVID = ocs.C_TANEVID AND evfolyamOcs.C_INTEZMENYID = ocs.C_INTEZMENYID AND evfolyamOcs.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE evfolyamTta ON evfolyamTta.ID = tta.C_TANULOEVFOLYAMTIPUSID AND evfolyamTta.C_TANEVID = tta.C_TANEVID AND evfolyamTta.C_INTEZMENYID = tta.C_INTEZMENYID AND evfolyamTta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE agazatOkj ON agazatOkj.ID = tta.C_AGAZATID AND agazatOkj.C_TANEVID = tta.C_TANEVID AND agazatOkj.C_INTEZMENYID = tta.C_INTEZMENYID AND agazatOkj.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE agazatNkt ON agazatNkt.ID = tta.C_TANULMANYITERULETNKTTIPUSID AND agazatNkt.C_TANEVID = tta.C_TANEVID AND agazatNkt.C_INTEZMENYID = tta.C_INTEZMENYID AND agazatNkt.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE agazatSzkt ON agazatSzkt.ID = tta.C_AGAZATUJSZKTTIPUSID AND agazatSzkt.C_TANEVID = tta.C_TANEVID AND agazatSzkt.C_INTEZMENYID = tta.C_INTEZMENYID AND agazatSzkt.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE szktTta ON szktTta.ID = tta.C_SZAKMATIPUSID AND szktTta.C_TANEVID = tta.C_TANEVID AND szktTta.C_INTEZMENYID = tta.C_INTEZMENYID AND szktTta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE szktO ON szktO.ID = o.C_SZAKMATIPUSID AND szktO.C_TANEVID = o.C_ALTANEVID AND szktO.C_INTEZMENYID = o.C_ALINTEZMENYID AND szktO.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE nktTta ON nktTta.ID = tta.C_SZAKKEPESITESNKTTIPUSID AND nktTta.C_TANEVID = tta.C_TANEVID AND nktTta.C_INTEZMENYID = tta.C_INTEZMENYID AND nktTta.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE nktO ON nktO.ID = o.C_SZAKKEPESITESNKTTIPUSID AND nktO.C_TANEVID = o.C_ALTANEVID AND nktO.C_INTEZMENYID = o.C_ALINTEZMENYID AND nktO.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE okj ON okj.ID = tta.C_SZAKKEPESITESID AND okj.C_TANEVID = tta.C_TANEVID AND okj.C_INTEZMENYID = tta.C_INTEZMENYID AND okj.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE jogviszony ON jogviszony.ID = tcs.C_JOGVISZONYTIPUSID AND jogviszony.C_TANEVID = tcs.C_TANEVID AND jogviszony.C_INTEZMENYID = tcs.C_INTEZMENYID AND jogviszony.TOROLT = 'F'  
    LEFT JOIN T_DICTIONARYITEMBASE allampolg ON allampolg.ID = fh.C_ALLAMPOLGARSAGA AND allampolg.C_TANEVID = fh.C_TANEVID AND allampolg.C_INTEZMENYID = fh.C_INTEZMENYID AND allampolg.TOROLT = 'F'
    LEFT JOIN T_DICTIONARYITEMBASE allampolg2 ON allampolg2.ID = fh.C_ALLAMPOLGARSAGA2 AND allampolg2.C_TANEVID = fh.C_TANEVID AND allampolg2.C_INTEZMENYID = fh.C_INTEZMENYID AND allampolg2.TOROLT = 'F'  
    LEFT JOIN T_DICTIONARYITEMBASE etkezes ON etkezes.ID = dk.C_ETKEZESIKEDVEZMENY AND etkezes.C_TANEVID = dk.C_ALTANEVID AND etkezes.C_INTEZMENYID = dk.C_ALINTEZMENYID AND etkezes.TOROLT = 'F' 
    LEFT JOIN (
      SELECT DISTINCT tm.C_TANULOID 
      FROM T_TANULOMENTESSEG tm
        INNER JOIN T_TANTARGY tgy ON tgy.ID = tm.C_TANTARGYID AND tgy.TOROLT = 'F'
      WHERE tgy.C_TARGYKATEGORIA IN (1202, 1203, 1207, 1204, 1205, 1206, 1208, 1209, 1201, 7748, 7749, 7750, 7751, 7752, 7753, 7754, 6727, 6731, 6733, 6729, 6732, 6734, 6730, 6728, 6737, 6741, 6743, 6739, 6742, 6744, 6740, 6738)
        AND tm.TOROLT = 'F'
        AND (tm.C_KEZDETE IS NULL OR tm.C_KEZDETE <= @datum)
        AND (tm.C_VEGE IS NULL OR tm.C_VEGE >= @datum)
        AND tm.C_TANEVID = @pTanevId
        AND tm.C_INTEZMENYID = @pIntezmenyid  
    ) felmentes ON felmentes.C_TANULOID = dk.ID
    LEFT JOIN (
      SELECT 
         tcs.C_TANULOID
        ,MAX(case when t.C_TARGYKATEGORIA in (1292, 8790 ) then 1 else 0 end) as Etika
        ,MAX(case when t.C_TARGYKATEGORIA in (8789, 1291) then 1 else 0 end) as Hittan
      FROM T_TANULOCSOPORT tcs
        INNER JOIN T_FOGLALKOZAS f on f.C_INTEZMENYID = tcs.C_INTEZMENYID
          AND f.C_TANEVID = tcs.C_TANEVID
          AND f.C_OSZTALYCSOPORTID = tcs.C_OSZTALYCSOPORTID AND f.TOROLT = 'F'
        INNER JOIN T_TANTARGY t on t.ID = f.C_TANTARGYID and t.C_INTEZMENYID = f.C_INTEZMENYID and t.C_TANEVID = f.C_TANEVID
      WHERE t.C_TARGYKATEGORIA in (1291, 8789, 1292, 8790)
        AND f.TOROLT = 'F'
        AND tcs.C_TANEVID = @pTanevId
        AND tcs.C_INTEZMENYID = @pIntezmenyid  
      GROUP by tcs.C_TANULOID
    ) targyak ON targyak.C_TANULOID = dk.ID
    LEFT JOIN #Idegennyelv idegennyelv ON idegennyelv.C_TANULOID = dk.ID
    OUTER APPLY (
      SELECT TOP(1) c.C_VAROS
      FROM T_CIM c
      WHERE c.C_FELHASZNALOID = dk.ID 
        AND c.C_GONDVISELOID IS NULL 
        AND c.TOROLT = 'F'
        AND c.C_TANEVID = @pTanevId
        AND c.C_INTEZMENYID = @pIntezmenyid  
      ORDER BY 
        CASE c.C_CIMTIPUSA
          WHEN 907 THEN 1
          WHEN 909 THEN 2
          WHEN 908 THEN 3
          WHEN 907 THEN 4
          WHEN 910 THEN 5
          ELSE 6
        END
    ) cimTanulo
    OUTER APPLY (
      SELECT TOP(1) c.C_VAROS
      FROM T_CIM c
        INNER JOIN T_GONDVISELO gv ON gv.ID = c.C_GONDVISELOID
      WHERE c.C_FELHASZNALOID = dk.ID
        AND gv.C_ISCSOKKENTETTGONDVISELO = 'F'
        AND c.TOROLT = 'F'
        AND c.C_TANEVID = @pTanevId
        AND c.C_INTEZMENYID = @pIntezmenyid   
      ORDER BY 
         gv.C_ISTORVENYESKEPVISELO DESC
        ,CASE gv.C_ROKONSAGFOKA WHEN 973 THEN 1 WHEN 974 THEN 2 ELSE 3 END
        ,gv.CREATED DESC        
        ,CASE c.C_CIMTIPUSA
          WHEN 907 THEN 1
          WHEN 909 THEN 2
          WHEN 908 THEN 3
          WHEN 907 THEN 4
          WHEN 910 THEN 5
          ELSE 6
        END
    ) cimSzulo
    LEFT JOIN T_CIM c2 ON dk.ID = c2.C_FELHASZNALOID AND c2.C_GONDVISELOID IS NULL AND c2.C_CIMTIPUSA = 909 AND c2.TOROLT = 'F'
    LEFT JOIN #EmeltSzintTanulo AS emeltSzint ON emeltSzint.C_TANULOID = dk.ID
    OUTER APPLY (
      SELECT TOP(1) 'T' AS INTEGRALT 
      FROM T_TANULOCSOPORT_OSSZES tcs2
        INNER JOIN T_TANULO_OSSZES dk2 ON dk2.ID = tcs2.C_TANULOID AND dk.TOROLT = 'F'
      WHERE tcs2.C_OSZTALYCSOPORTID = o.ID
        AND tcs2. C_BELEPESDATUM <= @datum 
        AND (tcs2.C_KILEPESDATUM >= @datum OR tcs2.C_KILEPESDATUM IS NULL)
        AND dk2.C_SAJATOSNEVELESU = 'F'
        AND tcs2.TOROLT = 'F'
        AND @IsSniKezelo = 1
        AND tcs2.C_TANEVID = @pTanevId
        AND tcs2.C_INTEZMENYID = @pIntezmenyid
    ) integralt
  WHERE fh.TOROLT = 'F'
    AND tcs.C_BELEPESDATUM <= @datum 
    AND (tcs.C_KILEPESDATUM IS NULL OR tcs.C_KILEPESDATUM > @datum)
    AND  ocs.C_FELADATKATEGORIAID <> '7555'
    AND fh.C_TANEVID = @pTanevId
    AND fh.C_INTEZMENYID = @pIntezmenyid
END
GO

