﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

DROP PROCEDURE IF EXISTS [dbo].[sp_GetMunkaidoElszamoloLap]
DROP PROCEDURE IF EXISTS [dbo].[uspGetMunkaidoElszamoloLap]
GO

CREATE PROCEDURE [dbo].[uspGetMunkaidoElszamoloLap]
  @pIntezmenyId	INT,
  @pTanarId		INT,
  @tanevId		INT,
  @honapId		INT,
  @isKlebersberg	BIT,
  @feladatKategoriaId INT = NULL
  ,@szakszeruHelyettesitesSzorzo decimal(3,2) = 1
  ,@nemSzakszeruHelyettesitesSzorzo decimal(3,2) = 1
  ,@oraOsszevonasSzorzo decimal(3,2) = 1
  ,@osszevontFelugyeletSzorzo decimal(3,2) = 1
  ,@bontas int = 0 -- 0: napi bontás, 1: heti bontás, 2: havi bontás
  ,@elmeletGyakorlatKulon bit = 0
  ,@felnottKepzesKulon bit = 0
AS 
  BEGIN 
	 SET NOCOUNT ON;

  DECLARE @backDate datetime

	SELECT
		ID      pedagogusId
		,Nev    pedagogusNev
	FROM  fnGetDokumentumIntezmenyPedagogusok (@pIntezmenyId, @tanevId )
	WHERE ID = @pTanarId


	DECLARE @Honap INT = (
		SELECT TOP 1
			dictionaryItemBase.C_VALUE
		FROM
			T_DICTIONARYITEMBASE_OSSZES dictionaryItemBase
		WHERE
			dictionaryItemBase.ID = @honapId
			AND dictionaryItemBase.C_TANEVID = @tanevId
			AND dictionaryItemBase.TOROLT = 'F'
	)

  CREATE TABLE #EvHonap (Ev int, Honap int, HonapNeve nvarchar(50))
  INSERT INTO #EvHonap
	SELECT TOP 1
		FORMAT(C_NAPDATUMA, 'yyyy')		Ev,
		FORMAT(C_NAPDATUMA, 'MM')		Honap,
		d.C_NAME		HonapNeve
	FROM
		T_NAPTARINAP_OSSZES naptariNap
    INNER JOIN T_DICTIONARYITEMBASE_OSSZES d ON d.C_VALUE = @Honap AND d.C_TYPE='Honapok' AND d.C_TANEVID = @tanevId AND d.TOROLT = 'F'
	WHERE
		DATEPART(MONTH,naptariNap.C_NAPDATUMA) = @Honap
		AND naptariNap.C_TANEVID = @tanevId
		AND naptariNap.TOROLT = 'F'
  SELECT * FROM #EvHonap

  SET @backDate = IIF(@bontas <> 2
    ,DATEADD(MONTH, 1, DATEFROMPARTS((SELECT Ev FROM #EvHonap), (SELECT Honap FROM #EvHonap), 1))
    ,(SELECT C_UTOLSONAP FROM T_TANEV_OSSZES WHERE TOROLT = 'F' AND ID = @tanevId)
  )
--===================================
--FEJLÉC
--===================================
  SELECT
     f.ID									    pedagogusId
    ,f.C_NYOMTATASINEV							TanarNev
    ,f.C_OKTATASIAZONOSITO						OktatasiAzonosito
    ,munkakor.C_NAME							Munkakor
    ,dbo.fnGetDokumentumPedagogusOraszam(f.id, @isKlebersberg) 	KotelezoOraszam
    ,ISNULL(CONVERT(NVARCHAR(MAX),m.C_MUNKAIDOKEDVEZMENYORASZAM),'-')	MunkaidoKedvezmenyOraszam
    ,ISNULL(munkaidoKedvezmenyOka.C_NAME,'-')	MunkaidoKedvezmenyOka
    ,ia.C_NEV + ' (' + ia.C_OMKOD +')'			IntezmenyNeve
    ,ia.C_VAROS AS IntezmenyVaros
    ,ia.C_IGAZGATONEVE							Igazgato
    ,m.C_FELADATELLATASIHELYID AS FeladatEllatasiHelyId
    ,m.C_FOGLALKOZTATASTIPUS AS FoglalkoztatasTipusId
    ,t.C_NEV AS Tanev
  FROM T_FELHASZNALO_OSSZES f
    INNER JOIN T_MUNKAUGYIADATOK_OSSZES m ON m.C_ALKALMAZOTTID = f.Id
    LEFT JOIN T_DICTIONARYITEMBASE_OSSZES munkakor ON munkakor.Id = m.C_MUNKAKORTIPUSA AND munkakor.C_TANEVID = m.C_TANEVID
    LEFT JOIN T_DICTIONARYITEMBASE_OSSZES munkaidoKedvezmenyOka ON munkaidoKedvezmenyOka.Id = m.C_MUNKAIDOKEDVEZMENYOKA AND munkaidoKedvezmenyOka.C_TANEVID = m.C_TANEVID
    INNER JOIN T_INTEZMENYADATOK_OSSZES ia ON ia.C_TANEVID = f.C_TANEVID
    LEFT JOIN T_TANEV_OSSZES t ON t.ID = @tanevId
  WHERE ia.TOROLT = 'F'
    AND m.TOROLT = 'F'
    AND f.ID = @pTanarId

--===================================
-- Tanítási napok meghatározása a színezéshez
--===================================
  CREATE TABLE #TanitasiNapTabla (Nap int PRIMARY KEY, IsTanitasiNap char(1))
  INSERT INTO #TanitasiNapTabla (Nap, IsTanitasiNap) 
  SELECT DATEPART(DAY, nn.C_NAPDATUMA)
    ,CASE
      WHEN tr.C_NAPTIPUSA IN (1385, 1395, 1402, 1394, 1406) THEN 'T'
      WHEN tr.C_NAPTIPUSA IS NOT NULL THEN 'F'
      WHEN nn.C_ALAPNAPTIPUSA = 1385 THEN 'T'
      ELSE 'F'
    END
  FROM T_NAPTARINAP_OSSZES nn
  LEFT JOIN T_TANEVRENDJE_OSSZES tr ON tr.C_DATUM = nn.C_NAPDATUMA AND tr.C_TANEVID = nn.C_TANEVID AND C_OSSZESCSOPORTRAVONATKOZIK = 'T'
  WHERE nn.C_TANEVID = @tanevId
    AND DATEPART(MONTH, nn.C_NAPDATUMA) = @Honap
    AND nn.TOROLT = 'F'
--===================================
-- Hetek sorszámának meghatározása a hónapon belül
--===================================
  CREATE TABLE #Hetek (Sorszam int, KezdoNap datetime, UtolsoNap datetime, IsTanitasiHet char(1))
  INSERT INTO #Hetek
  SELECT
    ROW_NUMBER() OVER ( ORDER BY nh.C_HETKEZDONAPJA )
    ,CASE DATEPART(MONTH, nh.C_HETKEZDONAPJA)
      WHEN @Honap THEN nh.C_HETKEZDONAPJA
      ELSE (SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, nh.C_HETKEZDONAPJA)+1, 0))
    END
    ,CASE DATEPART(MONTH, nh.C_HETUTOLSONAPJA)
      WHEN @Honap THEN nh.C_HETUTOLSONAPJA
      ELSE (SELECT DATEADD(DAY,-1,DATEADD(MONTH, DATEDIFF(MONTH,0,nh.C_HETUTOLSONAPJA),0)))
    END
    ,'T'
  FROM T_NAPTARIHET_OSSZES nh
  WHERE nh.C_TANEVID = @tanevId 
    AND nh.TOROLT = 'F'
    AND (@Honap = DATEPART(MONTH, nh.C_HETKEZDONAPJA) OR @Honap = DATEPART(MONTH, nh.C_HETUTOLSONAPJA));

  WITH TanitasiHetek AS (
  SELECT 
    ROW_NUMBER() OVER ( PARTITION BY h.Sorszam, h.KezdoNap, h.UtolsoNap, n.IsTanitasiNap ORDER BY h.Sorszam) rn
    ,h.Sorszam, h.KezdoNap, h.UtolsoNap, n.IsTanitasiNap 
  FROM #Hetek h
  INNER JOIN #TanitasiNapTabla n ON n.Nap BETWEEN DATEPART(DAY,h.KezdoNap) AND DATEPART(DAY,h.UtolsoNap)
  )
  UPDATE h SET h.IsTanitasiHet = th.IsTanitasiNap
  FROM #Hetek AS h
  INNER JOIN TanitasiHetek AS th On th.Sorszam = h.Sorszam
  WHERE th.rn = (DATEDIFF(DAY, th.KezdoNap, th.UtolsoNap) + 1)
--===================================
--MEGTARTOTT TANÓRÁK, HELYETTESÍTÉSEK
--===================================
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #MegtartottOrakHelyesitesek_tmp
	FROM
	(
	SELECT
		T_TANITASIORA_OSSZES.C_ORATULAJDONOSID   AS		pedagogusId
		,CASE @bontas
      WHEN 0 THEN DATEPART(DAY, T_TANITASIORA_OSSZES.C_DATUM)
      WHEN 1 THEN h.Sorszam
      WHEN 2 THEN IIF(DATEPART(MONTH, T_TANITASIORA_OSSZES.C_DATUM) <> 8
        ,(DATEPART(MONTH, T_TANITASIORA_OSSZES.C_DATUM) + 4) % 12
        ,12) 
      END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, T_TANITASIORA_OSSZES.C_DATUM)
      END	AS Honap
		,T_OSZTALYCSOPORT_OSSZES.ID						OsztalyCsoportID
		,T_OSZTALYCSOPORT_OSSZES.ID						ocsId
		,T_OSZTALYCSOPORT_OSSZES.C_NEV					OsztalyCsoportNev
		,IIF(T_TANITASIORA_OSSZES.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / 45, 2)
			)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
      END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
      END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES
		INNER JOIN T_OSZTALYCSOPORT_OSSZES on T_TANITASIORA_OSSZES.C_OSZTALYCSOPORTID = T_OSZTALYCSOPORT_OSSZES.ID AND (T_OSZTALYCSOPORT_OSSZES.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES ON T_CSOPORT_OSSZES.Id = T_OSZTALYCSOPORT_OSSZES.ID
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog ON fog.ID = C_FOGLALKOZASID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = T_OSZTALYCSOPORT_OSSZES.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = T_TANITASIORA_OSSZES.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON T_TANITASIORA_OSSZES.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE 
			T_TANITASIORA_OSSZES.C_TANEVID = @tanevId
		AND T_TANITASIORA_OSSZES.C_ORATULAJDONOSID = @pTanarId
		AND (@bontas = 2 OR DATEPART(MONTH, T_TANITASIORA_OSSZES.C_DATUM) = @Honap)
		AND T_TANITASIORA_OSSZES.TOROLT = 'F' 
		AND T_TANITASIORA_OSSZES.C_MEGTARTOTT = 'T'
		AND T_TANITASIORA_OSSZES.C_HELYETTESITOTANARID IS NULL
		AND (T_CSOPORT_OSSZES.Id IS NULL OR T_CSOPORT_OSSZES.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)))
		AND (T_TANITASIORA_OSSZES.C_PARHUZAMOSORA = 'F' OR T_TANITASIORA_OSSZES.C_PARHUZAMOSORA IS NULL)
		AND (fog.C_FOGLALKOZASTIPUSA IS NULL OR fog.C_FOGLALKOZASTIPUSA <> 1371)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]) 
	) OsszTanora 

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#MegtartottOrakHelyesitesek_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--szakszerű helyettesítés
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     ,IsFelnottoktatas
     ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #SzakszeruHelyetesites_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12) 
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
			)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES  cs ON cs.ID = ocs.ID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE 
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_HELYETTESITOTANARID = @pTanarId	 
		AND tao.TOROLT = 'F' 
		AND tao.C_HELYETTESITESTIPUSA = 1494 
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) SzakszeruHelyettesites

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#SzakszeruHelyetesites_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--nem szakszerű helyettesítés
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
     ,IsFelnottoktatas
     ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #NemSzakszeruHelyetesites_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE 
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_HELYETTESITOTANARID = @pTanarId	 
		AND tao.TOROLT = 'F' 
		AND tao.C_HELYETTESITESTIPUSA = 1495 
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) NemSzakszeruHelyettesites

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#NemSzakszeruHelyetesites_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--óraösszevonás
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #OraOsszevonas_tmp
	FROM
	(
	SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.ID=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE  
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_HELYETTESITOTANARID = @pTanarId	 
		AND tao.TOROLT = 'F' 
		AND tao.C_HELYETTESITESTIPUSA = 1496 
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) Oraosszevonas

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#OraOsszevonas_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--összevont felügyelet
	SELECT 
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + 
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) + 
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) + 
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #OsszevontFelugyelet_tmp
	FROM
	(
	SELECT 
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL, 
				1, 
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.ID=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE  
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_HELYETTESITOTANARID = @pTanarId	 
		AND tao.TOROLT = 'F' 
		AND tao.C_HELYETTESITESTIPUSA = 7367 
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
 
	) OsszevontFelugyelet
  
  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#OsszevontFelugyelet_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--tanóránkívüli
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #TanoranKivuli_tmp
	FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS		pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV						OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID			oraid
		,tao.C_OSZTALYCSOPORTID			OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / 60, 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog on fog.id=tao.C_FOGLALKOZASID 
		INNER JOIN T_CSOPORT_OSSZES ON T_CSOPORT_OSSZES.Id = ocs.ID
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE 
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_ORATULAJDONOSID = @pTanarId	 
		AND tao.C_HELYETTESITOTANARID IS NULL
		AND tao.TOROLT = 'F'
		AND tao.C_TANEVID = @tanevId
		AND T_CSOPORT_OSSZES.C_TIPUSA NOT IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId))
		AND tao.C_MEGTARTOTT = 'T'
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
		AND (fog.C_FOGLALKOZASTIPUSA IS NULL OR fog.C_FOGLALKOZASTIPUSA <> 1371)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) TanoranKivuli

    ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#TanoranKivuli_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--egyéni foglalkozas
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
		INTO #EgyeniFoglalkozas_tmp
  FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL, 
				1, 
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.Id = ocs.Id
		INNER JOIN T_FOGLALKOZAS_OSSZES fog on fog.id=tao.C_FOGLALKOZASID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE 
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
			AND tao.C_ORATULAJDONOSID = @pTanarId	 
			AND fog.C_FOGLALKOZASTIPUSA = 1371
			AND tao.C_HELYETTESITOTANARID IS NULL
			AND tao.TOROLT = 'F'
			AND tao.C_TANEVID = @tanevId
			AND tao.C_MEGTARTOTT = 'T'
			AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) EgyeniFoglalkozas

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#EgyeniFoglalkozas_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

--============================================
--NEVELÉSSEL-OKTATÁSSAL LE NEM KÖTÖTT MUNKAIDŐ
--============================================
--ÖSSZESEN
	SELECT
   		 pedagogusId
		,Honap
		 ,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1],0)+ISNULL([2],0)+ISNULL([3],0)+ISNULL([4],0)+ISNULL([5],0)+
		 ISNULL([6],0)+ISNULL([7],0)+ISNULL([8],0)+ISNULL([9],0)+ISNULL([10],0)+
		 ISNULL([11],0)+ISNULL([12],0)+ISNULL([13],0)+ISNULL([14],0)+ISNULL([15],0)+
		 ISNULL([16],0)+ISNULL([17],0)+ISNULL([18],0)+ISNULL([19],0)+ISNULL([20],0)+
		 ISNULL([21],0)+ISNULL([22],0)+ISNULL([23],0)+ISNULL([24],0)+ISNULL([25],0)+
		 ISNULL([26],0)+ISNULL([27],0)+ISNULL([28],0)+ISNULL([29],0)+ISNULL([30],0)+ISNULL([31],0) Ossz
    ,'F' AS IsFelnottoktatas
    ,'-' AS IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #LeNemKotottMunkaido_tmp
	FROM
	(
	SELECT
		T_NEMKOTOTTMUNKAIDO_OSSZES.C_TANARID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, C_KEZDETE)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, C_KEZDETE) <> 8
        ,(DATEPART(MONTH, C_KEZDETE) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, C_KEZDETE)
    END	AS Honap
		,ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_VEGE - C_KEZDETE) * 60 + DATEPART(MINUTE, C_VEGE - C_KEZDETE))) / 60, 2) Oraszam
	FROM T_NEMKOTOTTMUNKAIDO_OSSZES
    LEFT JOIN #Hetek h ON C_KEZDETE BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE
			C_MEGTARTOTT = 'T'
		AND T_NEMKOTOTTMUNKAIDO_OSSZES.C_TANARID = @pTanarId
		AND TOROLT = 'F'
		AND (@bontas = 2 OR (DATEPART(MONTH, C_KEZDETE)	= @Honap 
		AND DATEPART(MONTH, C_VEGE)		= @Honap))
	) Orak
	PIVOT
	(
	SUM(Oraszam)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
	) Tevekenyseg

--sorokra (tipusokra) lebontva
	SELECT
	 pedagogusId
	 ,Honap
	 ,TipusId				TipusId
	 ,Tipus					Tipus
	 ,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
 	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
	 ISNULL([1],0)+ISNULL([2],0)+ISNULL([3],0)+ISNULL([4],0)+ISNULL([5],0)+
	 ISNULL([6],0)+ISNULL([7],0)+ISNULL([8],0)+ISNULL([9],0)+ISNULL([10],0)+
	 ISNULL([11],0)+ISNULL([12],0)+ISNULL([13],0)+ISNULL([14],0)+ISNULL([15],0)+
	 ISNULL([16],0)+ISNULL([17],0)+ISNULL([18],0)+ISNULL([19],0)+ISNULL([20],0)+
	 ISNULL([21],0)+ISNULL([22],0)+ISNULL([23],0)+ISNULL([24],0)+ISNULL([25],0)+
	 ISNULL([26],0)+ISNULL([27],0)+ISNULL([28],0)+ISNULL([29],0)+ISNULL([30],0)+ISNULL([31],0) Ossz
    ,'F' AS IsFelnottoktatas
    ,'-' AS IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
	INTO #NemKotottSorok_tmp
	FROM
	(
	SELECT
		nemKotottMunkaido.C_TANARID   AS				pedagogusId
		,nemKotottMunkaido.C_TEVEKENYSEGTIPUSA			TipusId
		,tipusNeve.C_NAME								Tipus
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, nemKotottMunkaido.C_KEZDETE)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, nemKotottMunkaido.C_KEZDETE) <> 8
        ,(DATEPART(MONTH, nemKotottMunkaido.C_KEZDETE) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, nemKotottMunkaido.C_KEZDETE)
    END	AS Honap
		,ROUND(CONVERT(FLOAT,(DATEPART(HOUR, nemKotottMunkaido.C_VEGE - nemKotottMunkaido.C_KEZDETE) * 60 + DATEPART(MINUTE, nemKotottMunkaido.C_VEGE - nemKotottMunkaido.C_KEZDETE))) / 60, 2) Oraszam
	FROM T_NEMKOTOTTMUNKAIDO_OSSZES	nemKotottMunkaido
  	INNER JOIN (SELECT DISTINCT ID, C_NAME FROM T_DICTIONARYITEMBASE) tipusNeve ON tipusNeve.ID=nemKotottMunkaido.C_TEVEKENYSEGTIPUSA
    LEFT JOIN #Hetek h ON nemKotottMunkaido.C_KEZDETE BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE
			C_MEGTARTOTT='T'
		AND nemKotottMunkaido.C_TANARID = @pTanarId
		AND TOROLT = 'F'
		AND (@bontas = 2 OR (DATEPART(MONTH, C_KEZDETE)	= @Honap 
		AND DATEPART(MONTH, C_VEGE)	= @Honap))
	) Orak
	PIVOT
	(
	SUM(Oraszam)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
	) Soronkent

-- Túlóra
	SELECT
		pedagogusId
		,OsztalyCsoportID
		,OsztalyCsoportNev
		,Honap
		,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
		 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
		 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31],
		 ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) +
		 ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) +
		 ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([13], 0) + ISNULL([14], 0) + ISNULL([15], 0) +
		 ISNULL([16], 0) + ISNULL([17], 0) + ISNULL([18], 0) + ISNULL([19], 0) + ISNULL([20], 0) +
		 ISNULL([21], 0) + ISNULL([22], 0) + ISNULL([23], 0) + ISNULL([24], 0) + ISNULL([25], 0) +
		 ISNULL([26], 0) + ISNULL([27], 0) + ISNULL([28], 0) + ISNULL([29], 0) + ISNULL([30], 0) + ISNULL([31], 0) Ossz
    ,IsFelnottoktatas
    ,IsGyakorlat
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 1) = 'T' OR [1] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [1b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 2) = 'T' OR [2] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [2b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 3) = 'T' OR [3] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [3b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 4) = 'T' OR [4] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [4b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 5) = 'T' OR [5] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [5b]
    ,CASE @bontas
      WHEN 0 THEN IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 1 THEN IIF((SELECT h.IsTanitasiHet FROM #Hetek h WHERE h.Sorszam = 6) = 'T' OR [6] IS NOT NULL, 0, 1)
      WHEN 2 THEN 0
      END AS [6b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 7) = 'T' OR [7] IS NOT NULL, 0, 1)
      END AS [7b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 8) = 'T' OR [8] IS NOT NULL, 0, 1)
      END AS [8b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 9) = 'T' OR [9] IS NOT NULL, 0, 1)
      END AS [9b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 10) = 'T' OR [10] IS NOT NULL, 0, 1)
      END AS [10b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 11) = 'T' OR [11] IS NOT NULL, 0, 1)
      END AS [11b]
    ,CASE @bontas
      WHEN 2 THEN 0
      ELSE IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 12) = 'T' OR [12] IS NOT NULL, 0, 1)
      END AS [12b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 13) = 'T' OR [13] IS NOT NULL, 0, 1) AS [13b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 14) = 'T' OR [14] IS NOT NULL, 0, 1) AS [14b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 15) = 'T' OR [15] IS NOT NULL, 0, 1) AS [15b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 16) = 'T' OR [16] IS NOT NULL, 0, 1) AS [16b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 17) = 'T' OR [17] IS NOT NULL, 0, 1) AS [17b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 18) = 'T' OR [18] IS NOT NULL, 0, 1) AS [18b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 19) = 'T' OR [19] IS NOT NULL, 0, 1) AS [19b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 20) = 'T' OR [20] IS NOT NULL, 0, 1) AS [20b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 21) = 'T' OR [21] IS NOT NULL, 0, 1) AS [21b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 22) = 'T' OR [22] IS NOT NULL, 0, 1) AS [22b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 23) = 'T' OR [23] IS NOT NULL, 0, 1) AS [23b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 24) = 'T' OR [24] IS NOT NULL, 0, 1) AS [24b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 25) = 'T' OR [25] IS NOT NULL, 0, 1) AS [25b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 26) = 'T' OR [26] IS NOT NULL, 0, 1) AS [26b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 27) = 'T' OR [27] IS NOT NULL, 0, 1) AS [27b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 28) = 'T' OR [28] IS NOT NULL, 0, 1) AS [28b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 29) = 'T' OR [29] IS NOT NULL, 0, 1) AS [29b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 30) = 'T' OR [30] IS NOT NULL, 0, 1) AS [30b]
    ,IIF((SELECT tnt.IsTanitasiNap FROM #TanitasiNapTabla tnt WHERE tnt.Nap = 31) = 'T' OR [31] IS NOT NULL, 0, 1) AS [31b]
     INTO #Tulora_tmp
	FROM
	(
	SELECT
		tao.C_ORATULAJDONOSID   AS  pedagogusId
		,CASE @bontas
       WHEN 0 THEN DATEPART(DAY, tao.C_DATUM)
       WHEN 1 THEN h.Sorszam
       WHEN 2 THEN IIF(DATEPART(MONTH, tao.C_DATUM) <> 8
        ,(DATEPART(MONTH, tao.C_DATUM) + 4) % 12
        ,12)
    END AS Nap
		,CASE @bontas
      WHEN 2 THEN @Honap
      ELSE DATEPART(Month, tao.C_DATUM)
    END	AS Honap
		,ocs.C_NEV					OsztalyCsoportNev
		,tao.C_OSZTALYCSOPORTID		oraid
		,tao.C_OSZTALYCSOPORTID		OsztalyCsoportID
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
		)SumOra
    ,CASE
      WHEN @felnottKepzesKulon = 1 AND feh.C_OKTATASINEVELESIFELADATTIPUS IN (6463, 1176) THEN 'T'
      ELSE 'F'
    END AS IsFelnottoktatas
    ,CASE
      WHEN @elmeletGyakorlatKulon = 1 THEN tant.C_GYAKORLATI
      ELSE '-'
    END AS IsGyakorlat
	FROM T_TANITASIORA_OSSZES tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES ocs on ocs.ID=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES cs ON cs.ID = ocs.ID
		LEFT JOIN T_FOGLALKOZAS_OSSZES fog ON fog.ID = C_FOGLALKOZASID
    LEFT JOIN T_FELADATELLATASIHELY_OSSZES feh ON feh.ID = ocs.C_FELADATELLATASIHELYID AND feh.TOROLT = 'F'
    LEFT JOIN T_TANTARGY_OSSZES tant ON tant.ID = tao.C_TANTARGYID AND tant.TOROLT = 'F'
    LEFT JOIN #Hetek h ON tao.C_DATUM BETWEEN h.KezdoNap AND h.UtolsoNap
	WHERE  
			(@bontas = 2 OR DATEPART(MONTH, tao.C_DATUM) = @Honap)
		AND tao.C_ORATULAJDONOSID = @pTanarId	 
		AND tao.TOROLT = 'F' 
		AND tao.C_TANEVID = @tanevId
		AND tao.C_TULORA = 'T'
		AND tao.C_MEGTARTOTT = 'T'
	)a
	PIVOT
	(
	SUM(SumOra)
	FOR Nap IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	 [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	 [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])

	) Tulora

  ----ITT ADJUK HOZZÁ AZ ÜRES SOROKAT, HOGY AZ FRX MEG TUDJA JELENÍTENI
  exec uspInsertMunkaidoElszamoloLapUresSorokUresTablazatokhoz
     @pTableName = '#Tulora_tmp'
	  ,@pFelnottkepzesKulon = @felnottKepzesKulon
	  ,@pElmeletGyakorlatKulon = @elmeletGyakorlatKulon
    ,@pTanarId = @pTanarId
    ,@pHonap = @Honap
    ,@pBontas = @bontas

  ----ITT ADOM VISSZA A TABLAT=======================================
  SELECT * FROM #Tulora_tmp
  SELECT
  	pedagogusId
	,SUM([1]) AS [1], SUM([2]) AS [2], SUM([3]) AS [3], SUM([4]) AS [4], SUM([5]) AS [5], SUM([6]) AS [6], SUM([7]) AS [7], SUM([8]) AS [8], SUM([9]) AS [9], SUM([10]) AS [10],
	SUM([11]) AS [11], SUM([12]) AS [12], SUM([13]) AS [13], SUM([14]) AS [14], SUM([15]) AS [15], SUM([16]) AS [16], SUM([17]) AS [17], SUM([18]) AS [18], SUM([19]) AS [19], SUM([20]) AS [20],
	SUM([21]) AS [21], SUM([22]) AS [22], SUM([23]) AS [23], SUM([24]) AS [24], SUM([25]) AS [25], SUM([26]) AS [26], SUM([27]) AS [27], SUM([28]) AS [28], SUM([29]) AS [29], SUM([30]) AS [30], SUM([31]) AS [31]
  	,SUM(Ossz) as Ossz
	  ,AVG([1b]) AS [1b], AVG([2b]) AS [2b], AVG([3b]) AS [3b], AVG([4b]) AS [4b], AVG([5b]) AS [5b], AVG([6b]) AS [6b], AVG([7b]) AS [7b], AVG([8b]) AS [8b], AVG([9b]) AS [9b], AVG([10b]) AS [10b]
	  ,AVG([11b]) AS [11b], AVG([12b]) AS [12b], AVG([13b]) AS [13b], AVG([14b]) AS [14b], AVG([15b]) AS [15b], AVG([16b]) AS [16b], AVG([17b]) AS [17b], AVG([18b]) AS [18b], AVG([19b]) AS [19b], AVG([20b]) AS [20b]
	  ,AVG([21b]) AS [21b], AVG([22b]) AS [22b], AVG([23b]) AS [23b], AVG([24b]) AS [24b], AVG([25b]) AS [25b], AVG([26b]) AS [26b], AVG([27b]) AS [27b], AVG([28b]) AS [28b], AVG([29b]) AS [29b], AVG([30b]) AS [30b], AVG([31b]) AS [31b]
  FROM #Tulora_tmp
  GROUP BY
	pedagogusId

  ----KUMULÁLT HELYETTESÍTÉSEK
  CREATE TABLE #helyettesitesek (pedagogusId int, Nap int, Honap int, SumOra float, HelyettesitesTipusa int)
  INSERT INTO #helyettesitesek
  SELECT
		tao.C_HELYETTESITOTANARID   AS  pedagogusId
		,DATEPART(DAY, tao.C_DATUM)	AS	Nap
		,DATEPART(MONTH,tao.C_DATUM) AS	Honap
		,IIF(tao.C_CSENGETESIRENDORAID IS NOT NULL,
				1,
				ROUND(CONVERT(FLOAT,(DATEPART(HOUR, C_ORAVEGE - C_ORAKEZDETE) * 60 + DATEPART(MINUTE, C_ORAVEGE - C_ORAKEZDETE))) / IIF(cs.Id IS NULL OR cs.C_TIPUSA IN (SELECT ID FROM fnGetTanoraiCeluCsoportTipusok(@tanevId)), 45, 60), 2)
			) AS SumOra
    ,tao.C_HELYETTESITESTIPUSA AS HelyettesitesTipusa
	FROM T_TANITASIORA_OSSZES AS tao
		INNER JOIN T_OSZTALYCSOPORT_OSSZES AS ocs ON ocs.id=tao.C_OSZTALYCSOPORTID AND (ocs.C_FELADATKATEGORIAID = @feladatKategoriaId OR @feladatKategoriaId IS NULL)
		LEFT JOIN T_CSOPORT_OSSZES AS cs ON cs.ID = ocs.ID
	WHERE
			tao.C_DATUM < @backDate
		AND tao.C_HELYETTESITOTANARID = @pTanarId
		AND tao.TOROLT = 'F'
		AND tao.C_MEGTARTOTT = 'T'
		AND tao.C_TANEVID = @tanevId
		AND (tao.C_PARHUZAMOSORA = 'F' OR tao.C_PARHUZAMOSORA IS NULL)

  ----ITT ADOM VISSZA A KUMULÁLT HELYETTESÍTÉSEK TÁBLÁT
  SELECT
		pedagogusId,
    HelyettesitesTipusa
		,[9],[10],[11],[12],[1],[2],[3],[4],[5],[6],[7],[8]
    ,ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0) + ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) AS Ossz
  FROM (
	  SELECT
		  pedagogusId
		  ,Honap
		  ,SumOra
      ,d.C_NAME AS HelyettesitesTipusa
	  FROM #helyettesitesek AS h
      INNER JOIN T_DICTIONARYITEMBASE_OSSZES AS d ON h.HelyettesitesTipusa = d.ID AND d.C_TANEVID = @tanevId AND d.C_INTEZMENYID = @pIntezmenyId AND d.TOROLT = 'F'
    )a
  PIVOT (
    SUM(SumOra) FOR Honap IN ([9],[10],[11],[12],[1],[2],[3],[4],[5],[6],[7],[8])
  ) piv

  ----HETEK TÁBLA
  SELECT * FROM #Hetek ORDER BY Sorszam
  DROP TABLE #Hetek
  DROP TABLE #TanitasiNapTabla

  ----ÉV-HÓNAPOK TÁBLA
  SELECT DISTINCT
     FORMAT(nn.C_NAPDATUMA, 'yyyy.MM.')
  FROM T_NAPTARINAP_OSSZES nn
  WHERE nn.C_TANEVID = @tanevId
    AND nn.TOROLT = 'F'

  ----MINDEN TEVÉKENYSÉG EGYBEN VISSZAADÁSA
  SELECT *, 0 AS TevekenysegTipus FROM #MegtartottOrakHelyesitesek_tmp
  UNION ALL
  SELECT *, 1 AS TevekenysegTipus FROM #TanoranKivuli_tmp
  UNION ALL
  SELECT *, 2 AS TevekenysegTipus FROM #EgyeniFoglalkozas_tmp
  UNION ALL
  SELECT *, 3 AS TevekenysegTipus FROM #SzakszeruHelyetesites_tmp
  UNION ALL
  SELECT *, 4 AS TevekenysegTipus FROM #NemSzakszeruHelyetesites_tmp
  UNION ALL
  SELECT *, 5 AS TevekenysegTipus FROM #OraOsszevonas_tmp
  UNION ALL
  SELECT *, 6 AS TevekenysegTipus FROM #OsszevontFelugyelet_tmp
  UNION ALL
  SELECT pedagogusId, TipusId, Tipus
    ,Honap
    ,[1], [2], [3], [4], [5], [6], [7], [8], [9], [10],
	   [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],
	   [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
     ,Ossz, IsFelnottoktatas, IsGyakorlat
     ,[1b], [2b], [3b], [4b], [5b], [6b], [7b], [8b], [9b], [10b],
	   [11b], [12b], [13b], [14b], [15b], [16b], [17b], [18b], [19b], [20b],
	   [21b], [22b], [23b], [24b], [25b], [26b], [27b], [28b], [29b], [30b], [31b]
  , 7 AS TevekenysegTipus FROM #NemKotottSorok_tmp

END
GO