-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2025, 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_BelText_Silent')
  DROP PROCEDURE [dbo].[up_Append_BelText_Silent]
GO
CREATE PROCEDURE [dbo].[up_Append_BelText_Silent]
   @Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_TextArt char(1)
  ,@Param_RefKey char(6)
  ,@Param_LfdNr char(6)
  ,@Param_Abschluss tinyint
  ,@Param_SoftBreak tinyint
  ,@Param_LineFeeds integer
  ,@Param_CntChar integer
  ,@Param_Daten varchar(55)
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_BelText()
      INSERT INTO [dbo].[ut_BelText] (RecId
        ,[PrjNr]
        ,[BelArt]
        ,[BelNr]
        ,[TextArt]
        ,[RefKey]
        ,[LfdNr]
        ,[Abschluss]
        ,[SoftBreak]
        ,[LineFeeds]
        ,[CntChar]
        ,[Daten]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_PrjNr))
        ,UPPER(RTRIM(@Param_BelArt))
        ,UPPER(RTRIM(@Param_BelNr))
        ,UPPER(RTRIM(@Param_TextArt))
        ,UPPER(RTRIM(@Param_RefKey))
        ,UPPER(RTRIM(@Param_LfdNr))
        ,@Param_Abschluss
        ,@Param_SoftBreak
        ,@Param_LineFeeds
        ,@Param_CntChar
        ,RTRIM(@Param_Daten)
          )
      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

--------------------------------------------------------------------------------------------------------
-- Belegtext abrufen
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_BelText_get2')
  DROP PROCEDURE [dbo].[itcproc_BelText_get2]
GO
CREATE PROCEDURE [dbo].[itcproc_BelText_get2]
  @Param_TextArt varchar(1)
 ,@Param_PrjNr varchar(8)
 ,@Param_BelArt varchar(2)
 ,@Param_BelNr varchar(8)
 ,@Param_RefKey varchar(6)
AS
  SET NOCOUNT ON
  SELECT [RecId], [PrjNr], [BelArt], [BelNr], [TextArt], [RefKey], [LfdNr], [Abschluss], [SoftBreak], [LineFeeds], [CntChar], [Daten] FROM [dbo].[ut_BelText]
  WHERE PrjNr = @Param_PrjNr AND BelArt = @Param_BelArt AND BelNr = @Param_BelNr  AND TextArt = @Param_TextArt AND RefKey = @Param_RefKey
  ORDER BY LfdNr
GO

--------------------------------------------------------------------------------------------------------
-- Belegtext speichern
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_BelText_put2')
  DROP PROCEDURE [dbo].[itcproc_BelText_put2]
GO
CREATE PROCEDURE [dbo].[itcproc_BelText_put2]
   @Param_RecId	varchar(10)
  ,@Param_PrjNr char(8)
  ,@Param_BelArt char(2)
  ,@Param_BelNr char(8)
  ,@Param_TextArt char(1)
  ,@Param_RefKey char(6)
  ,@Param_LfdNr char(6)
  ,@Param_Abschluss tinyint
  ,@Param_SoftBreak varchar(10)
  ,@Param_LineFeeds varchar(10)
  ,@Param_CntChar   varchar(10)
  ,@Param_Daten varchar(55)
AS
  DECLARE @m_RecId integer = dbo.itcfunc_integer(@Param_RecId)
  DECLARE @m_LfdNr char(6) = RIGHT('00000' + LTRIM(STR(dbo.itcfunc_integer(@Param_LfdNr),6)),6)
  DECLARE @m_Abschluss integer = dbo.itcfunc_integer(@Param_Abschluss)
  DECLARE @m_SoftBreak integer = dbo.itcfunc_integer(@Param_SoftBreak)
  DECLARE @m_LineFeeds integer = dbo.itcfunc_integer(@Param_LineFeeds)
  DECLARE @m_CntChar integer = dbo.itcfunc_integer(@Param_CntChar)

  SET NOCOUNT ON

  If @m_RecId = 0
	EXECUTE [dbo].[up_Append_BelText_Silent] @Param_PrjNr, @Param_BelArt, @Param_BelNr, @Param_TextArt, @Param_RefKey, @m_LfdNr, @m_Abschluss, @m_SoftBreak, @m_LineFeeds, @m_CntChar, @Param_Daten
  else
	EXECUTE [dbo].[up_Upd_BelText] @m_RecId, @Param_PrjNr, @Param_BelArt, @Param_BelNr, @Param_TextArt, @Param_RefKey, @m_LfdNr, @m_Abschluss, @m_SoftBreak, @m_LineFeeds, @m_CntChar, @Param_Daten

  IF @m_Abschluss = 1
    DELETE FROM dbo.ut_BelText WHERE PrjNr = @Param_PrjNr AND BelArt = @Param_BelArt AND BelNr = @Param_BelNr  AND TextArt = @Param_TextArt AND RefKey = @Param_RefKey AND LfdNr > @Param_LfdNr
GO

--------------------------------------------------------------------------------------------------------
-- Belegtext loeschen
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P' and [name] = 'itcproc_BelText_delete')
  DROP PROCEDURE [dbo].[itcproc_BelText_delete]
GO
CREATE PROCEDURE [dbo].[itcproc_BelText_delete]
  @Param_TextArt varchar(1)
 ,@Param_PrjNr varchar(8)
 ,@Param_BelArt varchar(2)
 ,@Param_BelNr varchar(8)
 ,@Param_RefKey varchar(6)
AS
  SET NOCOUNT ON
  DELETE FROM ut_BelText WHERE PrjNr = @Param_PrjNr AND BelArt = @Param_BelArt AND BelNr = @Param_BelNr  AND TextArt = @Param_TextArt AND RefKey = @Param_RefKey
GO
