From c1936a327ee8dde030a9df8c17d38e879cf5e5dc Mon Sep 17 00:00:00 2001 From: Stefan Date: Wed, 9 Sep 2026 12:10:24 +0200 Subject: [PATCH] Add timezone-conversion and dashboard-widget SQL functions; remove obsolete backup__ copies Add datetime_LocalToUTC / datetime_utcToLocal, and two widget query functions (fds_wdg_MFRSync, fds_wgd_lastEmails). Remove the unreferenced backup__fds__* function files superseded by their current counterparts, and suppress SQL71501 (unresolved cross-database/composite references) project-wide. Co-Authored-By: Claude Sonnet 5 --- Fuchs_Database/FuchsDatabase.sqlproj | 11 ++- .../backup__fds__fn_InvoiceIdByName.sql | 27 ------ .../backup__fds__fn_bankingtransactions.sql | 83 ------------------- .../backup__fds__fn_invoice_customerid.sql | 26 ------ .../backup__fds__fn_unpaidInvoices.sql | 48 ----------- .../backup__fds__getInvoiceTreeIds.sql | 33 -------- .../backup__fds__r_getBalanceThisMonth.sql | 42 ---------- .../dbo/Functions/datetime_LocalToUTC.sql | 26 ++++++ .../dbo/Functions/datetime_utcToLocal.sql | 26 ++++++ .../dbo/Functions/fds_wdg_MFRSync.sql | 33 ++++++++ .../dbo/Functions/fds_wgd_lastEmails.sql | 17 ++++ 11 files changed, 107 insertions(+), 265 deletions(-) delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql delete mode 100644 Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql create mode 100644 Fuchs_Database/dbo/Functions/datetime_LocalToUTC.sql create mode 100644 Fuchs_Database/dbo/Functions/datetime_utcToLocal.sql create mode 100644 Fuchs_Database/dbo/Functions/fds_wdg_MFRSync.sql create mode 100644 Fuchs_Database/dbo/Functions/fds_wgd_lastEmails.sql diff --git a/Fuchs_Database/FuchsDatabase.sqlproj b/Fuchs_Database/FuchsDatabase.sqlproj index cc9be88..7eb264c 100644 --- a/Fuchs_Database/FuchsDatabase.sqlproj +++ b/Fuchs_Database/FuchsDatabase.sqlproj @@ -22,6 +22,7 @@ False True True + 71501 bin\Release\ @@ -215,9 +216,6 @@ - - - @@ -236,7 +234,6 @@ - @@ -246,8 +243,6 @@ - - @@ -404,5 +399,9 @@ + + + + \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql deleted file mode 100644 index c975352..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql +++ /dev/null @@ -1,27 +0,0 @@ --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__fn_InvoiceIdByName] -( - @nme varchar(255) -) -RETURNS varchar(20) -AS -BEGIN - DECLARE @Id varchar(20); - - SET @nme = TRIM((SELECT TOP(1) [value] FROM string_split(@nme, '('))); - - SET @Id = ISNULL( - (SELECT TOP(1) [Id] FROM ( - SELECT TOP(1) [Id], [fds] = 1 FROM [dbo].[fds__invoices] WHERE [InvoiceId] = @nme - UNION - SELECT TOP(1) try_Cast([Id] as varchar(20)), [fds] = 0 FROM [dbo].[mfr__invoices] WHERE [InvoiceId] = @nme - )z ORDER BY [fds] DESC - ),''); - - RETURN @id; - -END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql deleted file mode 100644 index b7bf5d6..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql +++ /dev/null @@ -1,83 +0,0 @@ --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__fn_bankingtransactions] -( - @startdate date = NULL - ,@enddate date = NULL - ,@invoice_startdate date = NULL - ,@invoice_enddate date = NULL - ,@invoice_idlist [dbo].[fds__tt__idlist_vchar] READONLY -) -RETURNS @bankingtransactions TABLE -( - [uid] bigint - ,[AccountIdentification] varchar(50) - ,[ValueDate] date - ,[Amount] numeric(9,2) - ,[AccountNumberOfPayer] varchar(30) - ,[NameOfPayer] nvarchar(60) - ,[SepaRemittanceInformation] varchar(150) - ,[EndToEndReference] varchar(50) - ,[manu] bit - ,[InvId] varchar(15) - ,[done_manually] bit - ,[fds] bit -) -AS -BEGIN - -- Fill the table variable with the rows for your result set - DECLARE @firstever date = '1900-01-01', @tomorrow date = DATEADD(dAy, 1, GETDATE()); - SELECT @invoice_startdate = ISNULL(@invoice_startdate, @firstever), @invoice_enddate = ISNULL(@invoice_enddate, @tomorrow); - DECLARE @idlistempty bit = IIF(EXISTS (SELECT 0 FROM @invoice_idlist) ,0,1); - - with bt as ( - SELECT [uid],[AccountIdentification],[ValueDate],[Amount],AccountNumberOfPayer,NameOfPayer, SepaRemittanceInformation, EndToEndReference - FROM [dbo].[fds__bankingtransactions] as bt_ - where (@startdate is null OR bt_.ValueDate >= @startdate) - AND (@enddate is null or bt_.ValueDate <= @enddate) - ), bs0manu as ( - SELECT bs.[banking_uid], [invid] = spl.[value], [manu] = cast(1 as bit) - FROM [dbo].[fds__bankingtransactions_settings] as bs - CROSS APPLY STRING_SPLIT(bs.assigned_invoice_id, ',') as spl - JOIN bt on bt.[uid] = bs.[banking_uid] - WHERE bs.assigned_invoice_id is not null - ), bs1 as ( - SELECT * FROM bs0manu - UNION - SELECT bs.[banking_uid], [invid] = spl.[value], [manu] = cast(0 as bit) - FROM [dbo].[fds__bankingtransactions_settings] as bs - CROSS APPLY STRING_SPLIT(bs.auto_invoice_id, ',') as spl - JOIN bt on bt.[uid] = bs.[banking_uid] - WHERE bs.auto_invoice_id is not null - and bt.[uid] NOT IN (SELECT [banking_uid] FROM bs0manu) - ), bs as ( - SELECT banking_uid, [invid], manu = CAST(MAX(CAST([manu] as int)) as bit) - from bs1 - WHERE @idlistempty = 1 OR bs1.[invid] in (SELECT [id] FROM @invoice_idlist) - GROUP BY banking_uid, [invid] - ) - INSERT INTO @bankingtransactions - SELECT - bt.[uid] - ,[AccountIdentification] - , bt.[ValueDate] - , [Amount] - ,bt.AccountNumberOfPayer - ,bt.NameOfPayer - ,bt.SepaRemittanceInformation - ,bt.EndToEndReference - ,[manu] - ,[invid] - ,[done_manually] = IIF(ISNULL(bs2.[done_manually],'') <> '', 1,0) - ,[fds] = IIF(mi.id is null, 1, 0) - FROM bt - JOIN bs ON bt.[uid] = bs.[banking_uid] - LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[uid] = bs2.[banking_uid] - LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate - LEFT JOIN [dbo].[mfr__invoices] as mi on try_cast(bs.[invid] as bigint) = mi.[id] AND mi.[DateOfCreation] BETWEEN @invoice_startdate AND @invoice_enddate; - - RETURN -END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql deleted file mode 100644 index 2ab19dc..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql +++ /dev/null @@ -1,26 +0,0 @@ --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__fn_invoice_customerid] -( - @InvId varchar(15) -) -RETURNS bigint -AS -BEGIN - DECLARE @ret bigint; - - SELECT TOP(1) @ret = [customerid] FROM dbo.[fds__invoices] WHERE [id] = @InvId; - - IF @ret is null - SELECT TOP(1) @ret = [customerid] FROM (SELECT TOP(1) [invid] = p.[PartnerId], s.[CustomerId], cy.[supportmail], cy.[Name] - FROM dbo.[mfr__*PartnerSet] as p - JOIN [dbo].[mfr__servicerequests] as s on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices' - JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id - WHERE p.PartnerId = TRY_CAST(@invId as bigint))z; - - RETURN @ret; - -END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql deleted file mode 100644 index 0c5739f..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql +++ /dev/null @@ -1,48 +0,0 @@ --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__fn_unpaidInvoices] -( - -) -RETURNS TABLE -AS -RETURN -( - WITH inv as ( - SELECT [Id] = cast([id] as varchar(15)) - ,[Invoiceid] - ,[DateFinalized] - ,[InvoiceBalance] - ,[DocumentName] - ,[file] - ,[InvoiceTitle] - ,[customerid] - ,[dateSent] - ,[fds] = CAST(1 as bit) - FROM [dbo].[fds__invoices] as iv - WHERE [isfinal] = 1 and CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL([IsPayed],0) END as bit) = 0 - and InvoiceBalance > 0 - UNION - SELECT [Id] = cast([id] as varchar(15)) - ,[Invoiceid] - ,[DateFinalized] = [DateOfCreation] - ,[InvoiceBalance] - ,[DocumentName] - ,[file] = NULL - ,[InvoiceTitle] = '' - ,[customerid] = [dbo].[fds__fn_invoice_customerid](mfri.[id]) - ,[datesent] = NULL - ,[fds] = CAST(0 as bit) - FROM [dbo].[mfr__invoices] as mfri - LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on mfri.[id] = ivi.[invid] - WHERE [invoicestate] in ('eIsSent','eIsOpen') - AND ISNULL(ivi.isPayed,0) = 0 - AND not [invoiceId] like 'preview' - and InvoiceBalance > 0 - ) - SELECT * /*[Rechnung] = [invoiceId], [Name] = [InvoiceTitle] + CHAR(10) + ' (' + (SELECT TOP(1) c.[name] FROM [dbo].[mfr__companies] as c WHERE inv.[customerid] = c.[id]) + ')', [Betrag (net)] = FORMAT([invoicebalance], '#0.00€', 'de'), [Datum] = FORMAT([DateFinalized], 'dd.MM.yy' , 'de'), [Zahlungen] = FORMAT([dbo].[fds__fn_InvoicePaymentAmount]([id]), '#0.00€', 'de'), [order] = ROW_NUMBER() OVER (ORDER BY [DateFinalized], [DateSent]) */ - FROM inv -) \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql b/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql deleted file mode 100644 index 3bce719..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql +++ /dev/null @@ -1,33 +0,0 @@ - --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__getInvoiceTreeIds] -( - @invid varchar(20) -) -RETURNS TABLE -AS -RETURN -( - with mfr_inv as ( - SELECT [type] = 'invoice', [id] = [id] FROM [dbo].[mfr__invoices] as _mfri where _mfri.[id] = TRY_CAST(@invid as bigint) - ), mfr_srq as ( - SELECT DISTINCT [type] = 'servicerequest', [id] = mfrs.[id] FROM mfr_inv - JOIN [dbo].[mfr__*PartnerSet] as ps on Property = 'Invoice:SourceServiceRequest' and ps.EntityId = mfr_inv.[id] - JOIN [dbo].[mfr__servicerequests] as mfrs on ps.[PartnerId] = mfrs.id - ), fds_srq as ( - SELECT distinct [type] = 'servicerequest', [id] = mfrs.[id] - FROM [dbo].[fds__invoices] as _fi - JOIN [dbo].[fds__invoice_servicerequests] as _fs on _fi.Id = _fs.[InvId] and _fs.[mfr__servicerequest] is not null - JOIN [dbo].[mfr__servicerequests] as mfrs on _fs.[mfr__servicerequest] = mfrs.id - WHERE _fi.[Id] = @invid - ) - SELECT * FROM mfr_inv - union - SELECT * FROM mfr_srq - union - SELECT * FROM fds_srq -) \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql b/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql deleted file mode 100644 index 82ec521..0000000 --- a/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql +++ /dev/null @@ -1,42 +0,0 @@ --- ============================================= --- Author: --- Create date: --- Description: --- ============================================= -CREATE FUNCTION [dbo].[backup__fds__r_getBalanceThisMonth]( - @authuser varchar(100) -) -RETURNS varchar(25) -BEGIN - DECLARE @today date = GETDATE(); - DECLARE @tomorrow date = DATEADD(DAY,1,@today); - DECLARE @thismonthfirst date = [dbo].[date_monthfirst](@today); - DECLARE @ret varchar(25) = '' - - IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2 - SET @ret = ''; - ELSE - BEGIN - - WITH inv as ( - SELECT - i.[Dateofcreation] - ,i.[InvoiceBalance] - ,i.[InvoiceBalanceNetto] - FROM [dbo].[mfr__invoices] as i - where --i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and - i.[FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview','') - AND [dbo].[date_monthfirst](i.[Dateofcreation]) = @thismonthfirst - UNION - SELECT - [Dateofcreation] = i.[DateFinalized] - ,i.[InvoiceBalance] - ,[InvoiceBalanceNetto] = i.[InvoiceBalance_net] - FROM [dbo].[fds__invoices] as i - WHERE i.[isFinal] = 1 AND [dbo].[date_monthfirst](i.[DateFinalized]) = @thismonthfirst - ) - SELECT TOP(1) @ret = FORMAT( SUM( ISNULL(inv.[InvoiceBalanceNetto],0) ) * 0.001, '#,0 k€', 'de') FROM inv; - END - - RETURN @ret; -END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/datetime_LocalToUTC.sql b/Fuchs_Database/dbo/Functions/datetime_LocalToUTC.sql new file mode 100644 index 0000000..70574ab --- /dev/null +++ b/Fuchs_Database/dbo/Functions/datetime_LocalToUTC.sql @@ -0,0 +1,26 @@ + + + +-- ============================================= +-- Author: Dr. Stefan Ott +-- Create date: +-- Description: +-- ============================================= +CREATE FUNCTION [dbo].[datetime_LocalToUTC] +( + @datetime datetime + ,@timezone varchar(50) = null +) +RETURNS datetime +AS +BEGIN + DECLARE @Ret datetime; + + IF @datetime is null + SET @RET = null; + ELSE + SET @RET = @datetime AT TIME ZONE ISNULL(@timezone, 'Central European Standard Time') AT TIME ZONE 'UTC' + + RETURN @RET; + +END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/datetime_utcToLocal.sql b/Fuchs_Database/dbo/Functions/datetime_utcToLocal.sql new file mode 100644 index 0000000..8210744 --- /dev/null +++ b/Fuchs_Database/dbo/Functions/datetime_utcToLocal.sql @@ -0,0 +1,26 @@ + + +-- ============================================= +-- Author: Dr. Stefan Ott +-- Create date: +-- Description: +-- ============================================= +CREATE FUNCTION [dbo].[datetime_utcToLocal] +( + @datetime datetime + ,@timezone varchar(50) = null +) +RETURNS datetime +WITH SCHEMABINDING +AS +BEGIN + DECLARE @Ret datetime; + + IF @datetime is null + SET @RET = null; + ELSE + SET @RET = @datetime AT TIME ZONE 'UTC' AT TIME ZONE ISNULL(@timezone, 'Central European Standard Time'); + + RETURN @RET; + +END \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/fds_wdg_MFRSync.sql b/Fuchs_Database/dbo/Functions/fds_wdg_MFRSync.sql new file mode 100644 index 0000000..1f544b2 --- /dev/null +++ b/Fuchs_Database/dbo/Functions/fds_wdg_MFRSync.sql @@ -0,0 +1,33 @@ +CREATE FUNCTION dbo.fds_wdg_MFRSync () +RETURNS TABLE +AS +RETURN +( + WITH LastPerTableAction AS + ( + SELECT + [table], + [action], + [timestamp], + TRY_CAST(JSON_VALUE([info], '$.count') AS INT) AS ItemCount, + ROW_NUMBER() OVER (PARTITION BY [table], [action] ORDER BY [timestamp] DESC) AS rn + FROM [dbo].[fds__log] + WHERE --[action] IN (N'imdu_reset', N'imdu_short', N'update_reset', N'update_short') + [timestamp] > DATEADD(DAY, -3, GETUTCDATE()) + ), + Formatted AS + ( + SELECT + [table], + [action], + CONCAT(FORMAT([dbo].[datetime_utcToLocal]([timestamp], null), 'dd.MM. HH:mm', 'de-DE'), 'Uhr - (', ISNULL(CONVERT(varchar(20), ItemCount), 'n/a'), ' Datensätze)') AS Cell + FROM LastPerTableAction + WHERE rn = 1 + ) + SELECT [Info] = SUBSTRING([table], 6, 100), [imdu_reset], [imdu_short], [update_reset], [update_short] + FROM Formatted + PIVOT + ( + MAX(Cell) FOR [action] IN ([imdu_reset], [imdu_short], [update_reset], [update_short]) + ) AS pvt +); \ No newline at end of file diff --git a/Fuchs_Database/dbo/Functions/fds_wgd_lastEmails.sql b/Fuchs_Database/dbo/Functions/fds_wgd_lastEmails.sql new file mode 100644 index 0000000..2318046 --- /dev/null +++ b/Fuchs_Database/dbo/Functions/fds_wgd_lastEmails.sql @@ -0,0 +1,17 @@ +create function [fds_wgd_lastEmails](@count int = 10) +returns table +as return + +SELECT TOP (@count) + [Betreff] = oj.[subject], + [Empfänger] = (SELECT STRING_AGG( + CASE WHEN CHARINDEX('<', [value]) > 0 + THEN SUBSTRING([value], CHARINDEX('<', [value]) + 1, CHARINDEX('>', [value]) - CHARINDEX('<', [value]) - 1) + ELSE [value] + END, ', ') + FROM OPENJSON(JSON_QUERY(el.[config], '$.to'))), + [attachments] = (SELECT STRING_AGG([value], ', ') FROM OPENJSON(JSON_QUERY(el.[config], '$.attachments'))), + [gesendet] = FORMAT(el.DateSent, 'dd.MM.yy', 'de-DE') + FROM [dbo].[fds__emaillog] as el + CROSS APPLY OPENJSON(el.[config], '$') WITH ( [subject] nvarchar(255) '$.subject' ) as oj + order by [DateSent] desc \ No newline at end of file