-- =============================================
-- Author:    Dőrr Tamás
-- Create date: 
-- Description:  
-- =============================================
DROP PROCEDURE IF EXISTS [dbo].[sp_JelszoResetGondviselo]
GO

CREATE PROCEDURE [dbo].[sp_JelszoResetGondviselo]
   @xml xml
  ,@tanevID int
  ,@intezmenyID int
  ,@userId int
  ,@modositoUserId int
AS
BEGIN
  BEGIN TRY
    SET NOCOUNT ON
    SET XACT_ABORT ON
  
    DECLARE @tbl TABLE (
       C_ID int
      ,C_PASSWORD nvarchar(50)
      ,C_GONDVISELOID int
      ,C_SO nvarchar(50))
    INSERT INTO @tbl
      SELECT 
       sor.value('number((Id)[1])', 'int') AS C_ID
      ,sor.value('(Password)[1]', 'nvarchar(50)') AS C_PASSWORD
      ,sor.value('number((GondviseloId)[1])', 'int') AS C_GONDVISELOID
      ,sor.value('(Salt)[1]', 'nvarchar(50)') AS C_SO
    FROM @xml.nodes('/BelepesiAdatok/BelepesiAdatokLista/BelepesiAdat') as sorok(sor)
    
  BEGIN TRANSACTION
   
    UPDATE [T_FELHASZNALOBELEPES] SET
       [C_NEPTUNNAPLOJELSZO] = NULL
      ,[C_SO] = x.C_SO
      ,[C_JELSZO] = x.C_PASSWORD
      ,[C_JELSZOVALTOZTATASIDEJE] = GETDATE()
      ,[C_KOTELEZOVALTOZTATNI] = 'T'
      ,[SERIAL] = SERIAL + 1
      ,[LASTCHANGED] = GETDATE()
      ,[MODIFIER] = @modositoUserId
    FROM [T_FELHASZNALOBELEPES] fb
      INNER JOIN @tbl x ON x.C_GONDVISELOID = fb.C_GONDVISELOID
    WHERE fb.TOROLT = 'F'
   
  COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 AND XACT_STATE() <> 0 
      ROLLBACK TRANSACTION;
    THROW
  END CATCH
END
GO
