-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2023, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Dimensionstexte_Silent')
  DROP PROCEDURE [dbo].[up_Append_Dimensionstexte_Silent]
GO
CREATE PROCEDURE [dbo].[up_Append_Dimensionstexte_Silent]
   @Param_LKZ char(2)
  ,@Param_ARTNR char(18)
  ,@Param_LFDNR char(2)
  ,@Param_KENNUNG char(1)
  ,@Param_LKZTEXTNR char(2)
  ,@Param_TEXTNR char(18)
  ,@Param_TEXTZEILE char(55)
  ,@Param_ABSCHLUSS tinyint
AS
  SET NOCOUNT ON
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Dimensionstexte()
      INSERT INTO [dbo].[ut_Dimensionstexte] (RecId
        ,[LKZ]
        ,[ARTNR]
        ,[LFDNR]
        ,[KENNUNG]
        ,[LKZTEXTNR]
        ,[TEXTNR]
        ,[TEXTZEILE]
        ,[ABSCHLUSS]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_LKZ))
        ,UPPER(RTRIM(@Param_ARTNR))
        ,RTRIM(@Param_LFDNR)
        ,UPPER(RTRIM(@Param_KENNUNG))
        ,UPPER(RTRIM(@Param_LKZTEXTNR))
        ,UPPER(RTRIM(@Param_TEXTNR))
        ,RTRIM(@Param_TEXTZEILE)
        ,@Param_ABSCHLUSS
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
GO

--------------------------------------------------------------------------------------------------------
-- Dimensionstext abrufen
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_Dimensionstextzeilen_get2')
  DROP PROCEDURE [dbo].[itcproc_Dimensionstextzeilen_get2]
GO
CREATE PROCEDURE [dbo].[itcproc_Dimensionstextzeilen_get2]
  @Param_Lkz varchar(2)
 ,@Param_Artnr varchar(18)
AS
  SET NOCOUNT ON
  SELECT RecId, LKZ, ARTNR, LFDNR, KENNUNG, LKZTEXTNR, TEXTNR, TEXTZEILE, ABSCHLUSS FROM ut_Dimensionstexte
  WHERE LKZ = @Param_Lkz AND ARTNR = @Param_Artnr
  ORDER BY LFDNR
GO

--------------------------------------------------------------------------------------------------------
-- Dimensionstext speichern
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_Dimensionstextzeilen_put2')
  DROP PROCEDURE [dbo].[itcproc_Dimensionstextzeilen_put2]
GO
CREATE PROCEDURE [dbo].[itcproc_Dimensionstextzeilen_put2]
  @Param_RecId	   varchar(10)
 ,@Param_LKZ       varchar(2)
 ,@Param_ARTNR     varchar(18)
 ,@Param_LFDNR     varchar(2)
 ,@Param_KENNUNG   varchar(1)
 ,@Param_LKZTEXTNR varchar(2)
 ,@Param_TEXTNR    varchar(18)
 ,@Param_TEXTZEILE varchar(55)
 ,@Param_Abschluss varchar(1)
AS
  DECLARE @m_RecId integer = dbo.itcfunc_integer(@Param_RecId)
  DECLARE @m_Abschluss integer = dbo.itcfunc_integer(@Param_Abschluss)

  SET NOCOUNT ON

  If @m_RecId = 0
	EXECUTE [dbo].[up_Append_Dimensionstexte_Silent] @Param_LKZ, @Param_ARTNR, @Param_LFDNR, @Param_KENNUNG, @Param_LKZTEXTNR, @Param_TEXTNR, @Param_TEXTZEILE, @m_Abschluss
  else
	EXECUTE [dbo].[up_Upd_Dimensionstexte] @m_RecId, @Param_LKZ, @Param_ARTNR, @Param_LFDNR, @Param_KENNUNG, @Param_LKZTEXTNR, @Param_TEXTNR, @Param_TEXTZEILE, @m_Abschluss

  IF @m_Abschluss = 1
    DELETE FROM dbo.ut_Dimensionstexte WHERE LKZ = @Param_LKZ AND ARTNR = @Param_ARTNR AND LFDNR > @Param_LFDNR
GO

--------------------------------------------------------------------------------------------------------
-- Dimensionstext loeschen
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_Dimensionstext_delete')
  DROP PROCEDURE [dbo].[itcproc_Dimensionstext_delete]
GO
CREATE PROCEDURE [dbo].[itcproc_Dimensionstext_delete]
  @Param_Lkz varchar(2)
 ,@Param_Artnr varchar(18)
AS
  SET NOCOUNT ON
  DELETE FROM ut_Dimensionstexte WHERE LKZ = @Param_Lkz AND ARTNR = @Param_Artnr
GO
