feat: implement set-based invoice payment calculations and enhance email logging configuration
Playwright Tests / test (push) Canceled after 0s

This commit is contained in:
Stefan
2026-09-16 22:31:27 +02:00
parent 944a5bb442
commit 86f6a60860
8 changed files with 268 additions and 12 deletions
@@ -0,0 +1,23 @@
-- =============================================
-- Description: Set-based counterpart of the scalar [fds__fn_InvoicePaymentAmount]:
-- returns the summed (non-storno) banking payments for EVERY invoice in one pass
-- over [fds__fn_bankingtransactions]. Join it instead of calling the scalar
-- function per row — the scalar one declares a table variable, so SQL Server
-- cannot inline it and re-runs the whole banking TVF for each invoice
-- (~0.5 s × N rows; the overdue-sum widget timed out at 90 s with 187 rows).
-- Same semantics as the scalar version with an empty id list: all invoices,
-- no date filter, storno credits NOT included (see *_full for those).
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_InvoicePaymentAmounts]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT [InvId] = bs.[InvId]
,[Amount] = CAST(SUM(ISNULL(bs.[Amount], 0.0)) as numeric(9,2))
FROM [dbo].[fds__fn_bankingtransactions](NULL, NULL, NULL, NULL, DEFAULT) as bs
WHERE bs.[InvId] IS NOT NULL
GROUP BY bs.[InvId]
)
@@ -1,7 +1,9 @@
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- Description: Finalized, not-paid, not-canceled, not-storno'd invoices with a balance > 0.
-- [PaymentAmount] / [OpenAmount] come from one set-based pass over the banking
-- transactions ([fds__fn_InvoicePaymentAmounts]) — use them instead of calling the
-- scalar [fds__fn_InvoicePaymentAmount](Id) per row, which re-runs the whole
-- banking TVF for every invoice and timed out the dashboard widgets.
-- =============================================
CREATE FUNCTION [dbo].[fds__fn_unpaidInvoices]
(
@@ -28,6 +30,10 @@ RETURN
and InvoiceBalance > 0
AND NOT exists (SELECT 0 FROM [dbo].[fds__invoice_details] as d where d.[StornoTo_InvId] = iv.[id])
)
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]) */
SELECT inv.*
,[PaymentAmount] = ISNULL(pay.[Amount], 0.0)
,[OpenAmount] = CAST(inv.[InvoiceBalance] - ISNULL(pay.[Amount], 0.0) as numeric(10,2))
/*[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
)
LEFT JOIN [dbo].[fds__fn_InvoicePaymentAmounts]() as pay ON pay.[InvId] = inv.[Id]
)␍
@@ -2,6 +2,10 @@
returns table
as return
-- [config] is NOT NULL but a logging path may have stored '' (or otherwise non-JSON text).
-- OPENJSON/JSON_QUERY raise on that and took the whole widget down, so the JSON is normalised
-- once (invalid -> '{}') before anything parses it; a WHERE ISJSON() alone is not guaranteed to
-- be evaluated ahead of the CROSS APPLY.
SELECT TOP (@count)
[Betreff] = oj.[subject],
[Empfänger] = (SELECT STRING_AGG(
@@ -9,9 +13,10 @@ SELECT TOP (@count)
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'))),
FROM OPENJSON(JSON_QUERY(j.[cfg], '$.to'))),
[attachments] = (SELECT STRING_AGG([value], ', ') FROM OPENJSON(JSON_QUERY(j.[cfg], '$.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
CROSS APPLY (SELECT [cfg] = IIF(ISJSON(el.[config]) = 1, el.[config], N'{}')) as j
CROSS APPLY OPENJSON(j.[cfg], '$') WITH ( [subject] nvarchar(255) '$.subject' ) as oj
order by [DateSent] desc