﻿DROP PROCEDURE IF EXISTS [dev].[uspGenerateEpjUtalasiAllomany]
GO

CREATE PROCEDURE [dev].[uspGenerateEpjUtalasiAllomany]
   @pKifizetesDatuma datetime = NULL
AS
BEGIN

  DECLARE @tanevId nvarchar(100)

    IF @pKifizetesDatuma IS NOT NULL BEGIN​
      DECLARE cur CURSOR LOCAL FOR
      SELECT ia.C_TANEVID
      FROM T_INTEZMENY i
        INNER JOIN T_INTEZMENYADATOK ia ON i.ID = ia.C_INTEZMENYID
        INNER JOIN T_TANEV tv ON tv.ID = ia.C_TANEVID AND tv.C_AKTIV = 'T' AND tv.TOROLT = 'F'
      WHERE i.TOROLT = 'F'
        AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'
        AND i.C_ISARCHIV = 'F'
      ​
      OPEN cur
      FETCH NEXT FROM cur INTO @tanevId
      WHILE @@FETCH_STATUS = 0 BEGIN
        EXEC uspUpdateTanuloEpJuttatas
          @pTanevId = @tanevId
          ,@pMinErdemjegy = 2.0
          ,@pJuttatasAlap = 100000
          ,@pTanuloId = NULL
          ,@pFelhasznaloId = 0
          ,@pKifizetesDatuma = @pKifizetesDatuma

        FETCH NEXT FROM cur INTO @tanevId
      END
      CLOSE cur
      DEALLOCATE cur
    END

    SELECT
        i.C_AZONOSITO as 'KRÉTA Azonosító'
        ,ia.C_NEV as 'Intézmény neve'
        ,ia.C_OMKOD as 'OM kód'
        ,fn.C_AZONOSITO as 'Fenntartó'
        ,ISNULL(fn.C_NEV, 'Ismeretlen fenntartó') AS 'Fenntartónév'
        ,ISNULL(lmszf.C_NEV, 'Ismeretlen legmagasabb szintű fenntartó') AS 'Legmagasabb szintű fenntartó'
        ,f.C_OKTATASIAZONOSITO as 'Oktatási azonosító'
        ,f.C_NYOMTATASINEV as 'Diák neve'
        ,f.C_SZULETESIDATUM as 'Születési dátum'
        ,f.C_SZULETESIHELY as 'Születési hely'
        ,f.C_ANYJANEVE as 'Anyja neve'
        ,tel.C_TELEFONSZAM as 'Telefonszám'
        ,em.C_EMAILCIM as 'E-mail cím'
        ,x.C_NEV as 'Gondviselő neve'
        ,x.C_TELEFONSZAM as 'Telefonszám'
        ,x.C_EMAILCIM as 'E-mail cím'
        ,x2.C_NEV as 'Gondviselő neve 2'
        ,x2.C_TELEFONSZAM as 'Telefonszám 2'
        ,x2.C_EMAILCIM as 'E-mail cím 2'
        ,cim.C_CIM AS 'Cím'
        ,dib.c_name as 'Évfolyam'
        ,epj.C_ERDEMJEGY as 'Érdemjegy'
        ,epj.C_BIZONYITVANYSZAMA as 'Bizonyítvány száma'
        ,epj.C_VIZSGADATUM as 'Vizsga dátuma'
        ,j.C_OSSZEG as 'Ösztöndíj összege'
        ,'''' + tt.C_BANKSZAMLASZAM as 'Számlaszáma'
        ,tt.C_BANKSZAMLATULAJNEVE as 'Számla tulajdonosának neve'
        ,dib2.c_name as 'Számla típusa'
    FROM t_intezmeny i
        INNER JOIN T_TANEV t on t.C_INTEZMENYID = i.id and t.C_AKTIV = 'T' and t.C_NEV = '2021/2022' and t.TOROLT = 'F'
        INNER JOIN T_INTEZMENYADATOK ia on ia.C_TANEVID = t.ID and ia.TOROLT = 'F'
        INNER JOIN T_JUTTATAS j on j.C_TANEVId = t.id and j.C_ISAKTIV = 'T' and j.TOROLT = 'F' and j.C_JUTTATASTIPUSID = 8617
        INNER JOIN T_FELHASZNALO f on f.id = j.C_TANULOID and f.torolt  ='F'
        INNER JOIN T_TANULOCSOPORT tcs on tcs.C_TANULOID = f.id and tcs.torolt ='F' and tcs.C_TANEVID = t.ID
        LEFT JOIN T_FENNTARTO fn ON fn.TOROLT = 'F' AND fn.ID = i.C_FENNTARTOID
        LEFT JOIN T_LEGMAGASABBSZINTUFENNTARTO lmszf ON lmszf.TOROLT = 'F' AND fn.C_LEGMAGASABBSZINTUFENNTARTOID = lmszf.Id
        LEFT JOIN (T_TANULOCSOPORT tcs2
          INNER JOIN T_OSZTALYCSOPORT ocs2 on ocs2.id = tcs2.C_OSZTALYCSOPORTID and ocs2.torolt = 'F' and ocs2.C_FELADATKATEGORIAID = 7553
          INNER JOIN T_OSZTALY o2 ON o2.id = ocs2.ID AND o2.TOROLT = 'F' AND o2.C_ALTANEVID = ocs2.C_TANEVID
        ) on tcs2.C_TANULOID = f.ID
            AND (tcs.C_KILEPESDATUM < tcs2.C_KILEPESDATUM OR (tcs.C_KILEPESDATUM IS NOT NULL AND tcs2.C_KILEPESDATUM IS NULL))
            AND tcs2.TOROLT = 'F' AND tcs2.C_TANEVID = t.ID
        INNER JOIN t_osztaly o on o.id = tcs.C_OSZTALYCSOPORTID and o.torolt = 'F' and o.C_ALTANEVID = t.ID
        INNER JOIN T_OSZTALYCSOPORT ocs on ocs.id = o.id and ocs.C_TANEVID = t.id and ocs.torolt = 'F'
            AND ocs.C_FELADATKATEGORIAID = 7553
        INNER JOIN T_DICTIONARYITEMBASE dib on dib.id = ocs.C_EVFOLYAMTIPUSA and dib.C_TANEVID = t.id
        INNER JOIN t_tanulo tt on tt.id = f.id and tt.torolt = 'F' and tt.C_ALTANEVID = t.ID AND tt.C_ISOSZTONDIJBOLKIZARVA = 'F'
        LEFT JOIN T_DICTIONARYITEMBASE dib2 on dib2.id = tt.C_BANKSZAMLATULAJDONOSTIPUSID and dib2.C_TANEVID = t.id
        OUTER APPLY (
            SELECT TOP(1) C_TELEFONSZAM
            FROM T_TELEFON x
            WHERE x.C_FELHASZNALOID = tt.ID
                and x.C_ALAPERTELMEZETT = 'T'
                and x.TOROLT = 'F'
                and x.C_GONDVISELOID is null) tel
        OUTER APPLY (
            SELECT TOP(1) C_EMAILCIM
            FROM T_EMAIL x
            WHERE x.C_FELHASZNALOID = tt.ID
                and x.C_ALAPERTELMEZETT = 'T'
                and x.TOROLT = 'F'
                and x.C_GONDVISELOID is null) em
        OUTER APPLY (
            SELECT TOP(1) * FROM  T_GONDVISELO g
                OUTER APPLY (
                    SELECT TOP(1) C_EMAILCIM
                    FROM T_EMAIL x
                    WHERE x.C_FELHASZNALOID = tt.ID
                        and x.C_ALAPERTELMEZETT = 'T'
                        and x.TOROLT = 'F'
                        and x.C_GONDVISELOID = g.ID
                        and x.C_TANEVID = t.ID) em2
                OUTER APPLY (
                    SELECT TOP(1) C_TELEFONSZAM
                    FROM T_TELEFON x
                    WHERE x.C_FELHASZNALOID = tt.ID
                        and x.C_ALAPERTELMEZETT = 'T'
                        and x.TOROLT = 'F'
                        and x.C_GONDVISELOID = g.ID
                        and x.C_TANEVID = t.ID) tel2
            WHERE g.TOROLT = 'F'
                AND g.C_TANULOID = tt.ID and g.C_TANEVID = t.ID
            ORDER BY g.CREATED DESC  ) x
        OUTER APPLY (
            SELECT TOP(1) *FROM  T_GONDVISELO g
                OUTER APPLY (
                    SELECT TOP(1) C_EMAILCIM
                    FROM T_EMAIL x
                    WHERE x.C_FELHASZNALOID = tt.ID
                        and x.C_ALAPERTELMEZETT = 'T'
                        and x.TOROLT = 'F'
                        and x.C_GONDVISELOID = g.ID
                        and x.C_TANEVID = t.ID) em2
                OUTER APPLY (
                    SELECT TOP(1) C_TELEFONSZAM
                    FROM T_TELEFON x
                    WHERE x.C_FELHASZNALOID = tt.ID
                        and x.C_ALAPERTELMEZETT = 'T'
                        and x.TOROLT = 'F'
                        and x.C_GONDVISELOID = g.ID
                        and x.C_TANEVID = t.ID) tel2
            WHERE g.TOROLT = 'F'
                AND g.C_TANULOID = tt.ID and g.C_TANEVID = t.ID
            ORDER BY g.CREATED DESC
        ) x2
    OUTER APPLY (
        SELECT TOP(1) CONCAT(x.C_IRANYITOSZAM, ' ', x.C_VAROS, ', ', x.C_KOZTERULET, ' ', x.C_KOZTERULETJELLEGENEV, ' ', x.C_HAZSZAM, ', ' + x.C_EMELET + '. emelet', ' ' + x.C_AJTO + '. ajtó') AS C_CIM
        FROM T_CIM x
        WHERE x.C_FELHASZNALOID = tt.ID
            and x.C_ALAPERTELMEZETT = 'T'
            and x.TOROLT = 'F'
            and x.C_GONDVISELOID IS NULL
            and x.C_TANEVID = t.ID
         ORDER BY x.C_ALAPERTELMEZETT DESC) cim
    LEFT JOIN T_EPJTANULOIADAT epj ON epj.C_TANULOID = f.ID AND epj.C_ISAKTIV = 'T' AND epj.TOROLT = 'F'
    WHERE i.torolt = 'F'
      AND fn.C_ISTESZTFENNTARTO = 'F'
      AND j.C_ISJOGOSULT = 'T'
      AND tcs2.ID IS NULL
      AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'
      AND i.C_ISARCHIV = 'F'

    ORDER BY i.C_AZONOSITO, f.C_NYOMTATASINEV

    IF @pKifizetesDatuma IS NOT NULL BEGIN​
      DECLARE cur CURSOR LOCAL FOR
      SELECT ia.C_TANEVID
      FROM T_INTEZMENY i
        INNER JOIN T_INTEZMENYADATOK ia ON i.ID = ia.C_INTEZMENYID
        INNER JOIN T_TANEV tv ON tv.ID = ia.C_TANEVID AND tv.C_AKTIV = 'T' AND tv.TOROLT = 'F'
      WHERE i.TOROLT = 'F'
        AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'
        AND i.C_ISARCHIV = 'F'
      ​
      OPEN cur
      FETCH NEXT FROM cur INTO @tanevId
      WHILE @@FETCH_STATUS = 0 BEGIN
        EXEC uspUpdateTanuloEpJuttatas
          @pTanevId = @tanevId
          ,@pMinErdemjegy = 2.0
          ,@pJuttatasAlap = 100000
          ,@pTanuloId = NULL
          ,@pFelhasznaloId = 0
          ,@pKifizetesDatuma = NULL

        FETCH NEXT FROM cur INTO @tanevId
      END
      CLOSE cur
      DEALLOCATE cur
    END
END
GO