﻿DROP PROCEDURE IF EXISTS uspUpdateFelhasznalokCovidAdatok
GO

CREATE PROCEDURE uspUpdateFelhasznalokCovidAdatok
   @pTanevId int
  ,@pTajSzamokJson nvarchar(max)
  ,@pDatum datetime = NULL
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @datum datetime
    ,@updatedToT int
    ,@updatedToF int

  SET @datum = ISNULL(@pDatum, GETDATE())

  UPDATE f
  SET C_ISCOVIDFERTOZOTT = 'T'
    ,C_COVIDFERTOZESKEZDETEDATE = @datum
    ,SERIAL = SERIAL + 1
    ,MODIFIER = 0
    ,LASTCHANGED = GETDATE()
  FROM T_FELHASZNALO_OSSZES f
    INNER JOIN (
      SELECT value as C_TAJSZAM
      FROM OPENJSON(@pTajSzamokJson)
    ) t on t.C_TAJSZAM = f.C_TAJSZAM
  WHERE f.TOROLT = 'F'
    AND f.C_TANEVID = @pTanevId
    AND f.C_ISCOVIDFERTOZOTT = 'F'

  SET @updatedToT = @@ROWCOUNT

  UPDATE T_FELHASZNALO_OSSZES SET C_ISCOVIDFERTOZOTT = 'F'
    ,SERIAL = SERIAL + 1
    ,MODIFIER = 0
    ,LASTCHANGED = GETDATE()
  WHERE CAST(C_COVIDFERTOZESKEZDETEDATE as date) < DATEADD(day, -13, CAST(@datum AS date))
    AND TOROLT = 'F'
    AND C_TANEVID = @pTanevId

  SET @updatedToF = @@ROWCOUNT

  SELECT @updatedToT as Fertozott, @updatedToF as MarNemFertozott
END
GO