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

-- Diese Funktionen werden schrittweise in die Tabellenscripts verlagert

IF OBJECT_ID (N'itcfunc_NextRecId_KundenLizenzen', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_NextRecId_KundenLizenzen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_KundenLizenzen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_KundenLizenzen]),0) + 1
END
GO

IF OBJECT_ID (N'itcfunc_NextRecId_WebUserRoule', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_NextRecId_WebUserRoule
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_WebUserRoule() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_WebUserRoule]),0) + 1
END
GO

IF OBJECT_ID (N'itcfunc_NextRecId_BelTransaction', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_NextRecId_BelTransaction
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_BelTransaction() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_BelTransaction]),0) + 1
END
GO

IF OBJECT_ID (N'itcfunc_NextRecId_Ausgleichszeilen', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_NextRecId_Ausgleichszeilen
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Ausgleichszeilen() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Ausgleichszeilen]),0) + 1
END
GO

IF OBJECT_ID (N'itcfunc_NextRecId_Zeiterfassung', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_NextRecId_Zeiterfassung
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Zeiterfassung() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Zeiterfassung]),0) + 1
END
GO

-- -------------------------------------------------------------------------------------
-- Einheitennummer aus dem Text ermitteln
IF OBJECT_ID (N'itcfunc_MengenEinheitVonText', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_MengenEinheitVonText
GO
CREATE FUNCTION dbo.itcfunc_MengenEinheitVonText(@mParam_Text varchar(10)) RETURNS integer
AS
BEGIN
  DECLARE @mText varchar(10) = UPPER(@mParam_Text)
  DECLARE @m_PrtNr varchar(4) = (SELECT TOP 1 PrtNr FROM dbo.ut_Mengeneinheiten WHERE Suchen = @mText ORDER BY Suchen)
  IF @m_PrtNr IS NULL
    SET @m_PrtNr = (SELECT TOP 1 PrtNr FROM dbo.ut_Mengeneinheiten WHERE Suchen like @mText + '%' ORDER BY Suchen)
  IF @m_PrtNr IS NULL
    RETURN 1
  RETURN dbo.itcfunc_integer(@m_PrtNr)
END
GO

-- -------------------------------------------------------------------------------------
-- Preiseinheitenwert aus der Nummer
IF OBJECT_ID (N'itcfunc_PreisEinheit', N'FN') IS NOT NULL
    DROP FUNCTION dbo.itcfunc_PreisEinheit
GO
CREATE FUNCTION dbo.itcfunc_PreisEinheit(@Param_Nr integer, @Param_Gebinde float) RETURNS float
AS
BEGIN
  DECLARE @mNr char(2) = STR(@Param_Nr,2)
  DECLARE @mGebinde float = @Param_Gebinde
  IF @mGebinde <= 0.0 SET @mGebinde = 1.0
  RETURN (SELECT Faktor * @mGebinde FROM dbo.ut_Preiseinheiten WHERE Nr = @mNr)
END
GO


