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

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_PaymentTerms')
BEGIN
CREATE TABLE [dbo].[ut_PaymentTerms]
(
  [RecId]  integer NOT NULL
 ,[Code] char(10) DEFAULT ''
 ,[Descr] varchar(50) DEFAULT ''
 ,[Faellig] integer DEFAULT 0
 ,[SkontoTage] integer DEFAULT 0
 ,[SkontoSatz] float DEFAULT 0
  CONSTRAINT [Pk_PaymentTerms]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_PaymentTerms_KeyDel')
  DROP TRIGGER [dbo].[tr_PaymentTerms_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_PaymentTerms_KeyUpd')
  DROP TRIGGER [dbo].[tr_PaymentTerms_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_PaymentTerms_Keys')
  DROP TABLE [dbo].[ut_PaymentTerms_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Code')
BEGIN
  ALTER TABLE [dbo].[ut_PaymentTerms] ADD [Code] char(10) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Descr')
BEGIN
  ALTER TABLE [dbo].[ut_PaymentTerms] ADD [Descr] varchar(50) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Faellig')
BEGIN
  ALTER TABLE [dbo].[ut_PaymentTerms] ADD [Faellig] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'SkontoTage')
BEGIN
  ALTER TABLE [dbo].[ut_PaymentTerms] ADD [SkontoTage] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'SkontoSatz')
BEGIN
  ALTER TABLE [dbo].[ut_PaymentTerms] ADD [SkontoSatz] float NOT NULL DEFAULT 0
END
GO

-- check lengths of text fields
IF (10 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Code'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_PaymentTerms] ALTER COLUMN [Code] char(10)
END
GO
IF (10 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Code'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Code'))
BEGIN
   ALTER TABLE [dbo].[ut_PaymentTerms] ALTER COLUMN [Code] varchar(10)
END
GO
IF (50 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Descr'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_PaymentTerms] ALTER COLUMN [Descr] char(50)
END
GO
IF (50 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Descr'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_PaymentTerms' and sc.[name] = 'Descr'))
BEGIN
   ALTER TABLE [dbo].[ut_PaymentTerms] ALTER COLUMN [Descr] varchar(50)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'PaymentTerms')
  DROP VIEW [dbo].[PaymentTerms]
GO
CREATE VIEW [dbo].[PaymentTerms] AS
  SELECT [RecId]
 ,[Code]
 ,[Descr]
 ,[Faellig]
 ,[SkontoTage]
 ,[SkontoSatz]
  FROM [dbo].[ut_PaymentTerms]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_PaymentTerms', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_PaymentTerms
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_PaymentTerms() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_PaymentTerms] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Sel_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Sel_PaymentTerms]  @mRI integer
AS
  SELECT
    [Code]
   ,[Descr]
   ,[Faellig]
   ,[SkontoTage]
   ,[SkontoSatz]
  FROM [dbo].[ut_PaymentTerms] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Sel2_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Sel2_PaymentTerms]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[PaymentTerms] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Append_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Append_PaymentTerms]
   @Param_Code char(10)
  ,@Param_Descr varchar(50)
  ,@Param_Faellig integer
  ,@Param_SkontoTage integer
  ,@Param_SkontoSatz float
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_PaymentTerms()
      INSERT INTO [dbo].[ut_PaymentTerms] (RecId
        ,[Code]
        ,[Descr]
        ,[Faellig]
        ,[SkontoTage]
        ,[SkontoSatz]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_Code))
        ,RTRIM(@Param_Descr)
        ,@Param_Faellig
        ,@Param_SkontoTage
        ,@Param_SkontoSatz
          )
      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
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Add_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Add_PaymentTerms]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_PaymentTerms()
  INSERT INTO [dbo].[ut_PaymentTerms] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Upd_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Upd_PaymentTerms] @mRI integer
  ,@Param_Code char(10)
  ,@Param_Descr varchar(50)
  ,@Param_Faellig integer
  ,@Param_SkontoTage integer
  ,@Param_SkontoSatz float
AS
  UPDATE [dbo].[ut_PaymentTerms] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Faellig] = @Param_Faellig
   ,[SkontoTage] = @Param_SkontoTage
   ,[SkontoSatz] = @Param_SkontoSatz
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Upd2_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Upd2_PaymentTerms] @Param_RecId varchar(20)
  ,@Param_Code varchar(10)
  ,@Param_Descr varchar(50)
  ,@Param_Faellig varchar(3)
  ,@Param_SkontoTage varchar(2)
  ,@Param_SkontoSatz varchar(4)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_PaymentTerms] SET
    [Code] = UPPER(RTRIM(@Param_Code))
   ,[Descr] = RTRIM(@Param_Descr)
   ,[Faellig] = dbo.itcfunc_integer(@Param_Faellig)
   ,[SkontoTage] = dbo.itcfunc_integer(@Param_SkontoTage)
   ,[SkontoSatz] = dbo.itcfunc_float(@Param_SkontoSatz)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_PaymentTerms] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Del_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Del_PaymentTerms] @mRI integer
AS
  DELETE FROM [dbo].[ut_PaymentTerms] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_PaymentTerms')
  DROP PROCEDURE [dbo].[up_TrSel_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_TrSel_PaymentTerms]
AS
  SELECT RecId,
    LTRIM(RTRIM([Code]))
   +LTRIM(RTRIM([Descr]))
  AS Data FROM [dbo].[ut_PaymentTerms] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_PaymentTerms')
  DROP PROCEDURE [dbo].[up_Reorg_PaymentTerms]
GO
CREATE PROCEDURE [dbo].[up_Reorg_PaymentTerms] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_PaymentTerms] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_PaymentTerms] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_PaymentTerms] )
  END
GO

-- logical key  ORDER BY Code,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_PaymentTerms' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  DROP INDEX [Ik_Key00] ON [dbo].[ut_PaymentTerms]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_PaymentTerms' and so.[id] = si.[id] and si.[name] = 'Ik_Key00')
  CREATE INDEX [Ik_Key00] ON [dbo].[ut_PaymentTerms] (Code,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_First_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_PaymentTerms_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) ORDER BY Code,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_PaymentTerms_Key00] AS EXECUTE [dbo].[up_First_PaymentTerms_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_Last_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_PaymentTerms_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) ORDER BY Code desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_PaymentTerms_Key00] AS EXECUTE [dbo].[up_Last_PaymentTerms_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_Next_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_PaymentTerms_Key00] @oldRecId integer AS
  DECLARE @oldCode char(10)
  SELECT @oldCode = Code FROM [dbo].[ut_PaymentTerms] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00)
    WHERE (Code = @oldCode) AND (RecId > @oldRecId)
    ORDER BY Code,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00)
    WHERE (Code > @oldCode)
    ORDER BY Code,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_PaymentTerms_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_PaymentTerms_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_Prior_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_PaymentTerms_Key00] @oldRecId integer AS
  DECLARE @oldCode char(10)
  SELECT @oldCode = Code FROM [dbo].[ut_PaymentTerms] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00)
    WHERE (Code = @oldCode) AND (RecId < @oldRecId)
    ORDER BY Code desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00)
    WHERE (Code < @oldCode)
    ORDER BY Code desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_PaymentTerms_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_PaymentTerms_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_Next50_PaymentTerms_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[qp_Next50_PaymentTerms_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_Prior50_PaymentTerms_Key00]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[qp_Prior50_PaymentTerms_Key00]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_FindF_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindF_PaymentTerms_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mCode char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,10)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) WHERE 
      Code >= @mCode AND SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_PaymentTerms_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_PaymentTerms_Key00] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_PaymentTerms_Key00')
  DROP PROCEDURE [dbo].[up_FindL_PaymentTerms_Key00]
GO
CREATE PROCEDURE [dbo].[up_FindL_PaymentTerms_Key00] @xKey char(250), @xLen integer AS
  DECLARE @mCode char(10) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,10)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) WHERE 
      Code = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 10 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key00) WHERE 
      SUBSTRING(Code,1,LEN(@mCode)) = @mCode
      ORDER BY Code desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_PaymentTerms_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_PaymentTerms_Key00] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_PaymentTerms_Key00] @xKey, @xLen')
GO


-- logical key  ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_PaymentTerms' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  DROP INDEX [Ik_Key01] ON [dbo].[ut_PaymentTerms]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_PaymentTerms' and so.[id] = si.[id] and si.[name] = 'Ik_Key01')
  CREATE INDEX [Ik_Key01] ON [dbo].[ut_PaymentTerms] (Faellig,SkontoTage,SkontoSatz,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_First_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_First_PaymentTerms_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_PaymentTerms_Key01] AS EXECUTE [dbo].[up_First_PaymentTerms_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_Last_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_Last_PaymentTerms_Key01] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_PaymentTerms_Key01] AS EXECUTE [dbo].[up_Last_PaymentTerms_Key01]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_Next_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_Next_PaymentTerms_Key01] @oldRecId integer AS
  DECLARE @oldFaellig integer
  DECLARE @oldSkontoTage integer
  DECLARE @oldSkontoSatz float
  SELECT @oldFaellig = Faellig, @oldSkontoTage = SkontoTage, @oldSkontoSatz = SkontoSatz FROM [dbo].[ut_PaymentTerms] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage = @oldSkontoTage) AND (SkontoSatz = @oldSkontoSatz) AND (RecId > @oldRecId)
    ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage = @oldSkontoTage) AND (SkontoSatz > @oldSkontoSatz)
    ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage > @oldSkontoTage)
    ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig > @oldFaellig)
    ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_PaymentTerms_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Next_PaymentTerms_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_Prior_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_Prior_PaymentTerms_Key01] @oldRecId integer AS
  DECLARE @oldFaellig integer
  DECLARE @oldSkontoTage integer
  DECLARE @oldSkontoSatz float
  SELECT @oldFaellig = Faellig, @oldSkontoTage = SkontoTage, @oldSkontoSatz = SkontoSatz FROM [dbo].[ut_PaymentTerms] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage = @oldSkontoTage) AND (SkontoSatz = @oldSkontoSatz) AND (RecId < @oldRecId)
    ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage = @oldSkontoTage) AND (SkontoSatz < @oldSkontoSatz)
    ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig = @oldFaellig) AND (SkontoTage < @oldSkontoTage)
    ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01)
    WHERE (Faellig < @oldFaellig)
    ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_PaymentTerms_Key01] @oldRecId integer AS EXECUTE [dbo].[up_Prior_PaymentTerms_Key01] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_Next50_PaymentTerms_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[qp_Next50_PaymentTerms_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_Prior50_PaymentTerms_Key01]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[qp_Prior50_PaymentTerms_Key01]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_FindF_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindF_PaymentTerms_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mFaellig integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,3)
  DECLARE @mSkontoTage integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,4,2)
  DECLARE @mSkontoSatz float = dbo.itcfunc_KeyToken_float(@xKey,@xLen,6,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig AND 
      SkontoTage = @mSkontoTage AND 
      SkontoSatz = @mSkontoSatz
      ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig AND 
      SkontoTage = @mSkontoTage
      ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig
      ORDER BY Faellig,SkontoTage,SkontoSatz,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)




  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_PaymentTerms_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_PaymentTerms_Key01] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_PaymentTerms_Key01')
  DROP PROCEDURE [dbo].[up_FindL_PaymentTerms_Key01]
GO
CREATE PROCEDURE [dbo].[up_FindL_PaymentTerms_Key01] @xKey char(250), @xLen integer AS
  DECLARE @mFaellig integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,1,3)
  DECLARE @mSkontoTage integer = dbo.itcfunc_KeyToken_integer(@xKey,@xLen,4,2)
  DECLARE @mSkontoSatz float = dbo.itcfunc_KeyToken_float(@xKey,@xLen,6,4)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 9 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig AND 
      SkontoTage = @mSkontoTage AND 
      SkontoSatz = @mSkontoSatz
      ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
    RETURN
  END

  IF @xLen = 5 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig AND 
      SkontoTage = @mSkontoTage
      ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
    RETURN
  END

  IF @xLen = 3 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_PaymentTerms] WITH(INDEX = Ik_Key01) WHERE 
      Faellig = @mFaellig
      ORDER BY Faellig desc,SkontoTage desc,SkontoSatz desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)




  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_PaymentTerms_Key01')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_PaymentTerms_Key01] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_PaymentTerms_Key01] @xKey, @xLen')
GO


