-- =============================================
-- Author:		Dőrr Tamás
-- Create date: 
-- Description:	
-- =============================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON

GO

IF OBJECT_ID('[dbo].[sp_JelszoResetTanulo]') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_JelszoResetTanulo]
END

GO

CREATE PROCEDURE [dbo].[sp_JelszoResetTanulo]
   @xml xml
  ,@tanevID int
  ,@intezmenyID int
  ,@userId 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'
		FROM [T_FELHASZNALOBELEPES] fb
		  INNER JOIN @tbl x ON x.C_ID = fb.C_FELHASZNALOID AND x.C_GONDVISELOID =-1 AND fb.C_GONDVISELOID IS NULL
		WHERE fb.TOROLT = 'F'
   
  COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
    IF @@TRANCOUNT > 0 AND XACT_STATE() <> 0 
      ROLLBACK TRANSACTION;
    THROW
  END CATCH
END
