﻿--Migration
-- C_ISOSZTONDIJBOLKIZARVA javítás
UPDATE dk SET
  C_ISOSZTONDIJBOLKIZARVA = 'F', dk.LASTCHANGED = GETDATE(), dk.MODIFIER = 0, dk.SERIAL += 1 
FROM T_INTEZMENY i
  INNER JOIN T_TANEV tv ON tv.C_INTEZMENYID = i.ID AND tv.C_NEV = '2021/2022' AND tv.TOROLT = 'F'
  INNER JOIN T_TANULO dk ON dk.C_ALTANEVID = tv.ID AND dk.TOROLT = 'F'
  INNER JOIN T_INTEZMENYADATOK ia ON ia.C_TANEVID = tv.ID AND ia.TOROLT = 'F'
WHERE i.TOROLT = 'F'
  AND dk.C_ISOSZTONDIJBOLKIZARVA = 'T'
  AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'

-- C_ISMULASZTASMIATTKIZART javítás
UPDATE dk SET
  C_ISMULASZTASMIATTKIZART = 'F', dk.LASTCHANGED = GETDATE(), dk.MODIFIER = 0, dk.SERIAL += 1 
FROM T_INTEZMENY i
  INNER JOIN T_TANEV tv ON tv.C_INTEZMENYID = i.ID AND tv.C_NEV = '2021/2022' AND tv.TOROLT = 'F'
  INNER JOIN T_TANULO dk ON dk.C_ALTANEVID = tv.ID AND dk.TOROLT = 'F'
  INNER JOIN T_INTEZMENYADATOK ia ON ia.C_TANEVID = tv.ID AND ia.TOROLT = 'F'
WHERE i.TOROLT = 'F'
  AND dk.C_ISMULASZTASMIATTKIZART = 'F'
  AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'
GO

-- Átlag számítás (csak akik 2021-09-16 00:00 után váltottak tanévet)
DECLARE tanevCursor CURSOR LOCAL FOR 
SELECT tv.ID AS TanevId, tvElozo.ID AS ElozoTanevId
FROM T_TANEV tv
  INNER JOIN T_TANEV tvElozo ON tvElozo.C_INTEZMENYID = tv.C_INTEZMENYID AND tvElozo.TOROLT = 'F'
  INNER JOIN T_INTEZMENYADATOK ia ON ia.C_TANEVID = tv.ID AND ia.TOROLT = 'F'
WHERE tv.C_AKTIV = 'T' 
  AND tv.C_SORSZAM = 42
  AND tv.TOROLT = 'F'
  AND tvElozo.C_SORSZAM = tv.C_SORSZAM - 2
  AND tv.C_AKTIVALVA > '20210916 00:00'
  AND ia.C_ISSZAKKEPZOJUTTATAS = 'T'

DECLARE @tanevId int
  ,@elozoTanevId int

OPEN tanevCursor
FETCH NEXT FROM tanevCursor INTO @tanevId, @elozoTanevId
WHILE @@FETCH_STATUS = 0 BEGIN

  UPDATE dk SET dk.C_SZAMITOTTATLAG = x.ATLAG
    ,dk.MODIFIER = 0
    ,dk.LASTCHANGED = GETDATE()
    ,dk.SERIAL = dk.SERIAL + 1
  FROM T_TANULO_OSSZES dk
  INNER JOIN (
    SELECT er.C_TANULOID, AVG(ISNULL(erpot.C_ERTEKELESOSZTALYZATID - 1500, er.C_ERTEKELESOSZTALYZATID - 1500) * 1.0) AS ATLAG 
    FROM T_TANULOERTEKELES_OSSZES er
      INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs ON ocs.ID = er.C_OSZTALYCSOPORTID AND ocs.C_FELADATKATEGORIAID = 7553 AND ocs.TOROLT = 'F'
      LEFT JOIN T_TANULOERTEKELES_OSSZES erpot ON er.C_TANULOID = erpot.C_TANULOID AND er.TOROLT = 'F' AND erpot.C_TANTARGYID = er.C_TANTARGYID AND erpot.C_TIPUSID = 6917 
      INNER JOIN T_TANTARGY_OSSZES tgy ON tgy.ID = er.C_TANTARGYID AND tgy.TOROLT = 'F'
      LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID  AND cs.TOROLT = 'F'
      LEFT JOIN T_CSOPORTTIPUS_OSSZES cst ON cst.ID = cs.C_TIPUSA AND cs.C_ALTANEVID = cst.C_ALTANEVID  AND cst.TOROLT = 'F'
    WHERE er.C_TIPUSID = 1520 
      AND er.TOROLT = 'F' 
      AND er.C_ISMAGATARTASSZORGALOM = 'F'
      AND er.C_TANEVID = @elozoTanevId
      AND tgy.C_ISOSZTALYNAPLOBANEMLATSZIK = 'F' AND tgy.C_ISTANULMANYIATLAGBANEMSZAMIT = 'F'
      AND ((cs.ID IS NULL OR cst.C_ISTANORAICELU = 'T' AND tgy.C_FOTARGYE = 'T') OR (tgy.C_FOTARGYE = 'F' AND tgy.C_ALTANTARGYKENTNYOMTATVANYBAN = 'T'))
      AND  er.C_ERTEKELESOSZTALYZATID IS NOT NULL
    GROUP BY er.C_TANULOID
  ) x ON x.C_TANULOID = dk.ELOZOTANEVIREKORDID
  WHERE dk.TOROLT = 'F'
    AND dk.C_ALTANEVID = @tanevId

    
  FETCH NEXT FROM tanevCursor INTO @tanevId, @elozoTanevId
END

CLOSE tanevCursor
DEALLOCATE tanevCursor
GO


DECLARE 
   @pLekerdezesDatuma nvarchar(12) = CONVERT(nvarchar, GETDATE(), 23)
DECLARE 
   @pGeneraltHonap int = NULL
  ,@pVisszaallitasDatuma datetime = NULL

DECLARE @tanevId nvarchar(100)
IF @pLekerdezesDatuma 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 tv.C_NEV = '2021/2022'
		
  OPEN cur
  FETCH NEXT FROM cur INTO @tanevId
  WHILE @@FETCH_STATUS = 0 BEGIN
	  EXEC uspUpdateTanuloSzakkepzesiJuttatasok
         @pTanevId = @tanevId
	    ,@pMaxIgazolatlanokSzama = 6
	    ,@pMinOsztondijAtlag = 2.0
	    ,@pOsztondijAlap = 167400
	    ,@pLekerdezesDatuma = @pLekerdezesDatuma
	    ,@pTanuloId = NULL
	    ,@pTanuloIdList = NULL
	    ,@pFelhasznaloId = 0
	    ,@pGeneraltHonap = @pGeneraltHonap
	    ,@pVisszaallitasDatuma = @pVisszaallitasDatuma
	  FETCH NEXT FROM cur INTO @tanevId
  END
  CLOSE cur
  DEALLOCATE cur
END
GO